Jump to content

  • Log In with Google      Sign In   
  • Create Account


Problem Loading PNGs with SDL_image on Windows


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
14 replies to this topic

#1 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 08 July 2012 - 12:37 PM

I am trying to make a Windows version of a game that uses SDL and SDL_image. The game's image files are PNG files. I am using SDL 1.2.15 and SDL_image 1.2.12. I compiled a release version of my game using Visual C++ 2008 Express on a 32-bit Vista virtual machine. The game builds with no errors. I create a Release version of the game and add the following files to the same directory as the executable file:

The PNG files
SDL.dll
SDL_image.dll
libpng15-15.dll
zlib1.dll

The dll files are the x86 versions from the SDL and SDL_image binary installations.

When I run the game from outside Visual C++, a window opens and the game crashes. Stepping through the code in the debugger, I discovered the following code returns a null pointer:

SDL_RWops* textureFile;

// filename is the argument I pass to the texture loading function.
textureFile = SDL_RWFromFile(filename, "rb");

Adding a call to SDL's GetError functions to get more information about the error provides the error message "Couldn't open ImageFile.png", where ImageFile is one of the PNG files in the game. I figure the files aren't loading due to not having the PNG files or DLLs in the right location. But I thought putting them in the same location as the application was enough to get the image files to load. Are there additional libraries or files I need to place in the application folder to get the PNG files to load? I'm obviously missing something.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com

Sponsor:

#2 Spirrwell   Members   -  Reputation: 258

Like
0Likes
Like

Posted 08 July 2012 - 07:16 PM

Well, assuming you loaded the image as literally "ImageName.png" as opposed to something like "Data/Images/ImageName.png", then it should be in the same directory as the application. DLL files should also be in the same directory as well, it would come up with a DLL error if it couldn't find them. Anyway, as for your dilemma, I'm a bit puzzled as to why you would use SDL_RWFromFile() instead of IMG_Load().

I've had really bad luck in the past getting RW pointers to work properly. I suppose you could try SDL_RWFromFP and see if you still get a NULL pointer. That's about all I can think of for now, perhaps someone else has a better idea.

Edited by Spirrwell, 08 July 2012 - 07:18 PM.


#3 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 08 July 2012 - 07:44 PM

I call IMG_Load_RW after calling SDL_RWFromFile. I tried changing the texturing loading code to call IMG_Load instead of SDL_RWFromFile and IMG_Load_RW, but I still get the same error when I run the game.

Thanks for the help.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com

#4 Spirrwell   Members   -  Reputation: 258

Like
0Likes
Like

Posted 08 July 2012 - 07:55 PM

Oh, I understand calling IMG_Load_RW afterwards, it's just usually simpler to create an SDL_Surface* and load it with IMG_Load(). Also I might've confused you, you wouldn't load the SDL_RWops pointer with IMG_Load(), you'd load an SDL_Surface pointer with it. Either way, does the same error occur with other image files? Also check the name your passing to make sure that it is truly the same name as you png file. I once looked a file I was trying to load five times and realized after I made a forum post about it, that I made a spelling error. X_X.

#5 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 08 July 2012 - 08:10 PM

I understood what you meant about calling IMG_Load. Pass the filename to IMG_Load. The code compiled fine, but IMG_Load returned NULL.

Regarding the file names, I did notice that if I have a file named Tiles.png on Mac OS X, Windows Explorer shows the filename as Tiles. I added .png to the PNG files in Windows Explorer, but the file loading code fails both when the name is Tiles and Tiles.png.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com

#6 Spirrwell   Members   -  Reputation: 258

Like
0Likes
Like

Posted 08 July 2012 - 08:29 PM

The file is still Tiles.png regardless, as that's the full file name. Try opening it as a FILE pointer with fopen to see if your error loading is to do with SDL loading functions or your file name. Have you tried any other images as well?

#7 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 08 July 2012 - 08:56 PM

The FILE pointer is NULL when I call fopen. I have tried to load five different images, and they all return NULL after calling IMG_Load. However, I have a text file in my game, and that file loads. It's just the PNG files that don't load. The text file is in the application directory, just like the PNG files.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com

#8 Spirrwell   Members   -  Reputation: 258

Like
0Likes
Like

Posted 08 July 2012 - 09:52 PM

Well, that's a big clue for those smarter than me. It's clearly not an issue with SDL_image as it would load the file regardless with fopen. It's looking for the file with that file name and it isn't finding it because that file with that name is not there.

When you were talking about Windows Explorer not showing the file extension .png, it's because Windows automatically hides file extensions for known file types, sort of as a failsafe so somebody doesn't mess with it that doesn't know what they are doing. If the image has .blabla.xyz.etc, it may get cut off because of the .png extension. You could remove that by going to Computer->(You may need to press Alt key to bring up the menu)Tools->Folder Options->View-> uncheck "Hide extensions for known file types."->Apply

This way you will guarantee you will see the full name. If you still have the full file name, then someone smarter than me will have to help you. I'm not a debugger by any means, I just know what the code means. I mean you could just make something quick in paint like a white background and a few dots and save it (not neccesarily as a png) as test.png or test.jpg so you know for sure the name of the image.

That's all I can help with, I'm out of ideas from here.

#9 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 09 July 2012 - 10:30 AM

I created a simple PNG and tried loading it, but IMG_Load returned a NULL pointer. I took one of the PNG files I couldn't load and tried to load it in an old texture loading sample I had, and the file loaded. The sample used the same code I originally used: SDL_RWFromFile and IMG_Load_RW. I also used the same DLLs in both programs. I don't know why the old sample loads PNG files but my game doesn't.

Thanks for the help Spirrwell. You got me thinking deeper about the problem. When I discover a solution to this problem, I'll post it here.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com

#10 achild   Crossbones+   -  Reputation: 1592

Like
0Likes
Like

Posted 09 July 2012 - 11:39 AM

Sounds like your current working directory when running the release build isn't that directory your release exe actually resides in. Are you running from windows explorer or are you running without debugger through visual studio (Ctrl+f5 or whatever)?

#11 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 09 July 2012 - 01:04 PM

I run the release build from Windows Explorer. I also debug the debug build from Visual Studio. In both cases I place the PNGs and DLLs in the same directory as the executable. In both cases the PNG files do not load.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com

#12 achild   Crossbones+   -  Reputation: 1592

Like
0Likes
Like

Posted 09 July 2012 - 01:52 PM

The FILE pointer is NULL when I call fopen. I have tried to load five different images, and they all return NULL after calling IMG_Load. However, I have a text file in my game, and that file loads. It's just the PNG files that don't load. The text file is in the application directory, just like the PNG files.

There's only so many things that can go wrong here. Either the working directory is wrong (if you're only passing filename and not a full path), or there is something incorrect in the path being used. Maybe you are using single \ or something.
- What's the name of the file and it's full path on your computer? Does it use any non-english characters?
- What is the working directory when you attempt to open it?
- And what is the contents of the variable "filename" when you pass it to fopen?

Edited by achild, 09 July 2012 - 01:53 PM.


#13 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 09 July 2012 - 03:10 PM

I was able to load the files when running the game inside Visual Studio. Inside the project folder are three folders: Debug, Release, and the name of the project. Inside Visual Studio the working directory was the folder that matched the project name. Moving the PNGs and DLLs into the folder matching the name of the project allowed me to load the PNGs when debugging in Visual Studio.

I moved on to getting the release versions to work. I passed the full path to the PNG files in my code. The program crashed when I launched from Windows Explorer. When I stepped through the code in Visual Studio's debugger, the first two PNG files loaded, but the third one didn't.

I do not have any non-English characters in my path.

When I use fopen, I pass the name of the file. fopen returns a pointer whose address points to NULL when I pass the full path to the file as the filename.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com

#14 achild   Crossbones+   -  Reputation: 1592

Like
0Likes
Like

Posted 11 July 2012 - 07:57 AM

When I use fopen, I pass the name of the file. fopen returns a pointer whose address points to NULL when I pass the full path to the file as the filename.

This is not an uncommon issue. If you would please just tell:
1) What is the actual full path?
2) Please post the code you use when calling with fopen with the full path.

Edited by achild, 11 July 2012 - 07:59 AM.


#15 szymczyk   Members   -  Reputation: 182

Like
0Likes
Like

Posted 11 July 2012 - 12:26 PM

I found the cause of the release version crashing. After creating the OpenGL texture, I freed the memory of the SDL surface where I loaded the image file. Freeing the memory caused the crash.

Thanks for the help, everyone.
Mark Szymczyk
Author of Mac Game Programming and Xcode Tools Sensei
http://www.meandmark.com




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS