SDL Current Working Directory

Started by
10 comments, last by ubersheep 16 years, 5 months ago
Hello all, First time using SDL and embarassingly I'm baffled at trying to compile a simple demo. I'm through the first few hoops of linking and setting up the correct definitions for SDL and SDL_Image, but the problem is that when running, the application cannot seem to find the image files. Whereever I move them to, it doesn't seem to pick them up. Every time the application gets as far as the part where it uses IMG_Load() and fails. I'm trying to compile ballfield-1.0 from this website, using SDL-1.2.12-win32 and SDL_image-devel-1.2.6-VC8. Thanks in advance :)
Advertisement
Using Windows? Linux? Mac OS X?

Have everything linked right? Etc, etc, etc.

FlyingIsFun1217
Windows, but trying to compile an app written in Linux (which should be fine? Maybe?) The app runs correctly (black window), without any errors, but IMG_Load always returns an (undefined/empty) pointer reference, i.e. an SDL_Surface pointer that points to 0x00000000 :(

After this, the program immediately exits.

Which folder should my images be placed in?
The working directory for your application can change depending on whether or not you're running from the IDE and which IDE you're using. When in doubt you can use functions like GetCurrentDirectory() to find out what the directory is, or consult the documentation for your IDE.
Thanks SiCrane, have been looking into using GetCurrentDirectory() to grab the working directory but tiredness is pulling at my concentration right now.

The slow realisation that this is C code and not C++ is slowly setting in, and I've just learned that trying to declare a new variable half way down a function is not a good idea, and also, doesn't work.

I think I'll have another go after work tomorrow, as it's creeping up to 1am here in UK.

Thanks again, your help is much appreciated :) will post back tomorrow.
Hello again :)

Sorry it's taken more than a day, I've been really busy :(

Added
 #include <Windows.h>


and added
 char currentDir[255]; GetCurrentDirectory(255, currentDir);


to the top of main(), which resolves to:
 c:\Documents and Settings\Scott\My Documents\Visual Studio 2005\Projects\ballfield\ballfield


And yes, my name is Scott. :) The images have been moved into this folder to no difference :( after using IMG_LOAD() the variable still contains:
 temp = 0x00000000 {flags=??? format=??? w=??? ...}


Any advice appreciated :) Thanks in advance

Edit: If it's any help, it fails when trying to load "blueball.png". Am currently finding out if that makes any difference.
Add a print statement to print IMG_GetError():

// wherever the image loading call is:SDL_Surface *surface = IMG_Load(...);// add this:if( !surface ){     // if C++     std::cout << IMG_GetError() << std::endl;     // if C     printf("%s",IMG_GetError());     fflush(stdout);}


What does this print to stdout.txt?
Basic quick annoying question, after adding this, where might one find stdout.txt?
Supposedly there's a catch-all in there already, using the following:
		fprintf(stderr, "Could not load balls!\n");


but there is no stderr.txt or stdout.txt generated, and no clear place in VS2005 that it would be displayed. Is there something I'm missing?
Seeing as you are already using windows functions, try:
MessageBox(NULL,IMG_GetError(),"error!",MB_OK);

After the IMG_Load call, inside the if(!surface) block.

It is important to use IMG_GetError() and not a catch all because it might give an additional clue as to why it is failing (for example: SDL_Image can't find the right DLL for the file format).

This topic is closed to new replies.

Advertisement