# SFML - image.LoadFromFile() not working

This topic is 2824 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey guys,

I hope someone can help with the problem I currently have. I have created a LoadTexture method in my VideoSystem class which looks as follows:

bool VideoSystem::LoadTexture( char* texName, GLuint texHandle ) {    sf::Image image;    // Load the image    if(!image.LoadFromFile( texName ))       return false;    // Set the texHandle as current    glBindTexture(GL_TEXTURE_2D, texHandle);    // Create the texture    glTexImage2D(GL_TEXTURE_2D, 0, 4, image.GetWidth(), image.GetHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.GetPixelsPtr());    // Specify filtering and edge actions    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);    return (TRUE); }

I am trying to pass the following:

videoSystem->LoadTexture("Logo.tga", texture[0]);

But, I get the following error:

Quote:
 Failed to load image "Logo.tga". Reason : Unable to open file

I have the files in the same folder as the project and source but this doesn't seem to work.

Any help would be greatly appreciated.

EDIT

The funtion prototype for LoadFromFile() is:

bool LoadFromFile(const std::string& Filename);

##### Share on other sites
What's the current working directory when you run the code? Is Logo.tga in it?

On Windows you can find out what this is with GetCurrentDirectory(). On UNIX you can use getcwd().

Most IDEs have a way to specify the working directory from which the application should be launched.

Also, when passing around paths,

const char * is better than char *
but
const std::string & is better than const char *
but
some class that handles unicode paths properly is better than const std::string &

Get as far down that list as you can.

##### Share on other sites
What the_edd said.

Oh, and once that function finishes, the sf::Image gets destructed, thus deleting the texture. The pointer you get with image.GetPixelsPtr() becomes invalid at that point.

##### Share on other sites
Quote:
 Original post by mrwonkoOh, and once that function finishes, the sf::Image gets destructed, thus deleting the texture. The pointer you get with image.GetPixelsPtr() becomes invalid at that point.

Does that even matter? glTexImage2D is going to make a copy of the data. You don't have to keep your data around after passing it off to OpenGL unless you need to reuse it.

##### Share on other sites
Quote:
 Original post by the_eddWhat's the current working directory when you run the code? Is Logo.tga in it? On Windows you can find out what this is with GetCurrentDirectory(). On UNIX you can use getcwd().Most IDEs have a way to specify the working directory from which the application should be launched.Also, when passing around paths,const char * is better than char *butconst std::string & is better than const char *butsome class that handles unicode paths properly is better than const std::string &Get as far down that list as you can.

Hi Edd, I currently have the .tga files in the same folder as the source and the VS project - I cant see anywhere else to place them other than the debug folder but that doesn't work. I will however have a go at GetCurrentDirectory() and report back.

I have tried other ways of handling the paths but had stack errors - I'm sure I was doing something obscured.

##### Share on other sites
In general, whenever you ask the OS to look for or open a relative path (one without "X:\" or a UNC prefix on Windows, or one that doesn't start with '/' on UNIX), it will search for that path relative to the current working directory.

Just had a look in Visual C++ (2005 Express edition). You can set the working directory in Project -> Properties -> Configuration Properties -> Debugging.

##### Share on other sites
Quote:
Original post by KulSeran
Quote:
 Original post by mrwonkoOh, and once that function finishes, the sf::Image gets destructed, thus deleting the texture. The pointer you get with image.GetPixelsPtr() becomes invalid at that point.

Does that even matter? glTexImage2D is going to make a copy of the data. You don't have to keep your data around after passing it off to OpenGL unless you need to reuse it.

Is that so? I'm not all that familiar with the OpenGL, sorry. Though I'll change that one day. (Would make sense anyway, textures should be sent to vram so I guess that's what OpenGL does.)

##### Share on other sites
Well, this just keeps getting more, and more interesting!

I took your advice and changed the working directory to the path with the .tga, I also changed the method in which I include the libraries, and used the .dll as per SFML website instructions.

Now, when I run the program I get some really strange artefacts in the console window and a random beeping noise.

I have debugged the program and I can see that the error occurs when I call the SFML function LoadTexture().

Again, any help is greatly appreciated.

##### Share on other sites
Quote:
 Original post by LionMXNow, when I run the program I get some really strange artefacts in the console window and a random beeping noise.

Sounds like binary data is being written to the console. Some specific data is interpreted as a a beep on the internal speaker.

/* Try this for fun :) */#include <stdio.h>int main(void){    int n = 100;    while (n--) putc('\a', stdout);    return 0;}

Why and where that happens is something you'll have to investigate with your debugger, for example.

##### Share on other sites
The beep was coming through my desktop speakers would that matter?

1. 1
2. 2
Rutin
22
3. 3
4. 4
JoeJ
16
5. 5

• 14
• 29
• 9
• 11
• 11
• ### Forum Statistics

• Total Topics
631775
• Total Posts
3002278
×