Small SDL_Problem

Started by
12 comments, last by Servant of the Lord 15 years, 11 months ago
hi all, i'm new here and i'm new in SDL i'm using visual C++ 2008 but there is a problem in loading BMP files (not a code error) so do i have to make some settings to include the BMP files in the project or what???. I just pasted theese files in the project folder. need help.. thnx
Advertisement
It might very well be a coding error. It's fairly easy to make mistakes.
Post the code where you load the image, and the code where you draw it. (Copy + paste it between [ source] and [ /source] tags)

How sure are you that it's failing to load, and not just failing to draw?

After you load your image, check to see that it's not spitting out a error, like this:
//load the imageSDL_Surface *myImage = SDL_LoadBMP("blah.bmp");//check for errorsif(!myImage)    printf("Error loading file: %s\n", SDL_GetError());


The above code will output a error to "stdout.txt" if it fails to load.
Have you tried looking at the SDL_GetError() string after the function (SDL_LoadBMP() I assume) fails? It may simply be that your working directory is not what you expect (relative paths are assumed to be rooted in the working directory). If you can use an absolute path and the function works then this is almost certainly the problem.

Note: don't use absolute paths as a solution, even if they work! That would be a brittle one. Consider what might happen when trying to run your program on a different machine, or if you move the files around on your hard drive. This is just a way of diagnosing why the function is failing.
SDL_Surface *bg=NULL;
bg=SDL_LoadBMP("bg.bmp");
if(bg==NULL)
{
printf("Error loading file: %s\n", SDL_GetError());
exit(1);
}
i wrote this in my code, it doesn't get an error, it just exits, and when i remove the if condition, the program works normal but without a background. thats why i'm sure it's not a code mistake
how about adding a pause to it, so you see the message before you quit ?

SDL_Surface *bg=NULL;
bg=SDL_LoadBMP("bg.bmp");
if(bg==NULL)
{
printf("Error loading file: %s\n", SDL_GetError());
system("pause");
exit(1);
}
Quote:Original post by shadymokhtar
i wrote this in my code, it doesn't get an error, it just exits, and when i remove the if condition, the program works normal but without a background. thats why i'm sure it's not a code mistake


From where your .exe is located, find the stdout.txt file, the error message will be displayed there.

If there is no such error, or no such file, post your entire program(assuming it's small) inbetween [ source] and [ /source] tags, and I'll take a look at it, and try to compile it myself. Alternatively, you can use your debugger to 'step through' your program piece by piece, to see where exactly it is failing. I can't tell you how to use the Visual C++ 2008 debugger, though.

Quote:Original post by Molle85
how about adding a pause to it, so you see the message before you quit ?

SDL doesn't work like that, as it's not display the printf() text to a Win32 console, it's writing it to a file. The file continues to exist after the program is ran, so there is no need to pause the program. In fact, calling system("pause") wont pause this kind of program anyway.
i made debug and i found that the mistake is in the SDL_LoadBMP it seems that id doesn't find the file and i'm sure of that, and i didn't file any txt file, and i think i must make some settings in the compiler itself not the code
Did you try using an absolute path? Something like:

Windows: "C:/path/to/file/image.bmp"
Other: "/home/you/path/to/file.bmp"

I highly doubt that changing compiler settings will do anything to solve this problem.
Make sure you typed the filename correctly and that the file is in the same folder as the .exe file. Do not put the image in a subfolder unless you specify it as part of the file name. Make sure the image is a .bmp file. Aside from that, the problem can't be the image location.

What exactly does the program do? Does it compile? If so, does it run and then just quit? If not, what exactly do the errors say? I think, as Servant of the Lord said, you should just post your program. Make sure to put it in [ source] and [/ source] tags, without the space.
Also, remember that some character combinations when using string literals are changed into non-text characters. These begin with a "\", so if you're using Windows-style slashes, you can end up with things such as "\t" or "\n" which get translated into tab or newline when you really wanted "directory\this\name". Check what the file name's string is while debugging.

And as for how to fix this, the character combination "\\" gets turned into a '\' so you can just double up all the backslahses. The alternative is to use '/' instead of '\', which works fine in Windows. Eg: "director/this/name".

This topic is closed to new replies.

Advertisement