Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: LoadFromFile() hates me.  (Read 11970 times)

0 Members and 1 Guest are viewing this topic.

Xavura

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
LoadFromFile() hates me.
« on: February 18, 2009, 04:27:12 pm »
I am unable to load an image from file, I think it's because I'm not putting it in the right place but I've tried EVERYWHERE. I've even tried it with absolute paths to the image but that caused all kinds of errors.

I'm using Visual Studio C++ 2008 Express Edition. I've tried putting the .BMPs in "Resource Files", in the Project's directory etc. plus I've tried JPGs and PNGs as well.

The error is "Failed to load image .... Reason: Unable to open file".

I'm not sure if that means unable to OPEN or unable to LOCATE, either way I'm lost.

:?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
LoadFromFile() hates me.
« Reply #1 on: February 18, 2009, 04:37:15 pm »
Hm... What are you expecting with your question? The sf::Image::LoadFromFile() function works fine. Did you put an image file into your project directory itsself (not the release/debug folders, but where the VCPROJ file is located)? And did your path in the code exactly match the file's path?

Perhaps, a little code snippet might help...
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xavura

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
LoadFromFile() hates me.
« Reply #2 on: February 18, 2009, 04:45:13 pm »
Like I said before, I have tried putting it in every possible place... I just tried putting it with the .vcproj file to no avail.

I've had Input, Event, Clock, Window and RenderWindow all working fine it's just this that refuses to work.

... the code is pretty much:

Code: [Select]

#headers go here ...

int main() {
    using namespace sf;
    using namespace std;

    Image grassBitmap;

    if (grassBitmap.LoadFromFile('grass.bmp')) {
        cout << "Loaded bitmap." << endl;
    }
    else {
        cout << "Unable to load bitmap." << endl;
    }

    cin.clear();
    cin.ignore(255, '\n');
    cin.get();

    return 0;
}


Output:

Code: [Select]

Failed to load image "grass.bmp". Reason : Unable to open file
Unable to load bitmap.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
LoadFromFile() hates me.
« Reply #3 on: February 18, 2009, 04:55:24 pm »
Are there really the ' instead of the " characters?
Code: [Select]
if (grassBitmap.LoadFromFile('grass.bmp'))
And grass.bmp is in the project directory?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
LoadFromFile() hates me.
« Reply #4 on: February 18, 2009, 04:56:48 pm »
  • Don't import a namespace inside a function definition.
  • Use double-quotes for strings, not single-quotes (:arrow: "grass.bmp").
  • Double-check paths. Use an absolute path for testing, like C:\grass.bmp, and put the file there.

Xavura

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
LoadFromFile() hates me.
« Reply #5 on: February 18, 2009, 04:57:57 pm »
No, they are actually quotation marks like > " I'm just used to using apostrophes. :lol:

... and if by project directory you mean:

C:\Documents and Settings\Xavura\My Documents\Visual Studio 2008\Projects\SMFLTest\SMFLTest\HERE

... then yes.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
LoadFromFile() hates me.
« Reply #6 on: February 18, 2009, 04:58:47 pm »
Quote from: "Tank"
  • Don't import a namespace inside a function definition.
I don't like to empty namespaces at all, but I think it's better to keep the specifications as local as possible (especially using namespace in headers is evil).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
LoadFromFile() hates me.
« Reply #7 on: February 18, 2009, 05:01:46 pm »
Quote from: "Xavura"
C:\Documents and Settings\Xavura\My Documents\Visual Studio 2008\Projects\SMFLTest\SMFLTest\HERE
You could try the superior directory.

An absolute path (simple as Tank stated) didn't work?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xavura

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
LoadFromFile() hates me.
« Reply #8 on: February 18, 2009, 05:02:49 pm »
Quote from: "Tank"
  • Don't import a namespace inside a function definition.
  • Use double-quotes for strings, not single-quotes (:arrow: "grass.bmp").
  • Double-check paths. Use an absolute path for testing, like C:\grass.bmp, and put the file there.


I'm really new to C++ but the tutorials I have been following usually import namespaces inside functions. Is there any reason not to?

Say if function a uses std and function b doesn't, then it makes more sense to import std for function a only rather than globally for the entire file?

I did use " in the actual file, just ' when I posted on the forum.

I'll check the paths and try an absolute path but last time I tried that I got access violations/exceptions.

Thanks for the help so far.

Xavura

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
LoadFromFile() hates me.
« Reply #9 on: February 18, 2009, 05:05:39 pm »
Using an absolute path causes something like this to happen:

Code: [Select]

Unhandled exception at 0x102565af in SMFLTest.exe: 0xC0000005: Access violation reading location 0xcccccccc.


I'll try putting them in other locations but ... as stated twice, I have already tried every possible location.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
LoadFromFile() hates me.
« Reply #10 on: February 18, 2009, 05:09:32 pm »
Quote from: "Xavura"
I'm really new to C++ but the tutorials I have been following usually import namespaces inside functions. Is there any reason not to?

Say if function a uses std and function b doesn't, then it makes more sense to import std for function a only rather than globally for the entire file?
No, as I said, it's good practice to keep the using namespace as local as possible or better to avoid it completely. That's similar to variable declarations: You don't declare something globally if you need it only in one function.

Your problem seems to be anywhere else. Did you use the debugger to find out where the access violation occured (in debug mode)? The adress indicates an uninitialized variable.

If nothing helps, recompile SFML, possibly something went wrong.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Xavura

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
LoadFromFile() hates me.
« Reply #11 on: February 18, 2009, 05:12:28 pm »
Quote from: "Nexus"
No, as I said, it's good practice to keep the using namespace as local as possible or better to avoid it completely. That's similar to variable declarations: You don't declare something globally if you need it only in one function.

Your problem seems to be anywhere else. Did you use the debugger to find out where the access violation occured?

If nothing helps, recompile SFML, possibly something went wrong.

The debugger is pointing to the line with the call to LoadFromFile() (only when using an absolute path).

Recompile? I downloaded the headers/libraries like the tutorial says to. Was I supposed to do something else?

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
LoadFromFile() hates me.
« Reply #12 on: February 18, 2009, 05:15:01 pm »
Nah, importing namespaces is not comparible with defining variables. It's something that happens at compile-time, not run-time.
If you ever want to maximize your compilation times, then yeah, you'll be fine importing in functions, where you need the imports. ;)
Basically, my opinion is to use imports as less as possible. It even makes your code clearer, because you can see where types and other things belong to. Also imagine Laurent wouldn't use CamelCase for classnames. You'd have a perfect nameclash with sf::string and std::string, for example. The only reason why people import is to write less code. But I think that's not really an advantage. C++ invented namespaces, don't throw them away, again! ;)

Tank

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1486
    • View Profile
    • Blog
    • Email
LoadFromFile() hates me.
« Reply #13 on: February 18, 2009, 05:15:58 pm »
Quote from: "Xavura"
The debugger is pointing to the line with the call to LoadFromFile() (only when using an absolute path).

Maybe a dumb question, but are you escaping the \ characters (if used at all)?

Xavura

  • Newbie
  • *
  • Posts: 12
    • View Profile
    • Email
LoadFromFile() hates me.
« Reply #14 on: February 18, 2009, 05:18:45 pm »
Quote from: "Tank"
Nah, importing namespaces is not comparible with defining variables. It's something that happens at compile-time, not run-time.
If you ever want to maximize your compilation times, then yeah, you'll be fine importing in functions, where you need the imports. ;)
Basically, my opinion is to use imports as less as possible. It even makes your code clearer, because you can see where types and other things belong to. Also imagine Laurent wouldn't use CamelCase for classnames. You'd have a perfect nameclash with sf::string and std::string, for example. The only reason why people import is to write less code. But I think that's not really an advantage. C++ invented namespaces, don't throw them away, again! ;)

Thanks for the information, 'tis handy to know and I agree that writing less code isn't that much of an advantage.

Do you have anything to say regarding my problem because you seem to be knowledgeable enough.