Sign in to follow this  
shadymokhtar

Small SDL_Problem

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 image
SDL_Surface *myImage = SDL_LoadBMP("blah.bmp");
//check for errors
if(!myImage)
printf("Error loading file: %s\n", SDL_GetError());



The above code will output a error to "stdout.txt" if it fails to load.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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".

Share this post


Link to post
Share on other sites
Another one of the potential situations could be the following: When you use Visual C++ 2008 (and probably others) and you run through the IDE, the running directory isn't the exe's directory. It actually is the project folder (NOT the solution folder). For example, if I had a solution called Test with a Project called Test and I executed through the ide, the actual running directory would be /Test/Test/ rather then /Test/Debug/ or whichever mode you built in. This is actually a slightly frustrating situation but I am probably sure there is some setting in the IDE to change the running path but I have not looked for it as of yet.


Edit: Removed unnecessary comments about the original post.

Share this post


Link to post
Share on other sites
Quote:
Original post by shadymokhtar
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

Quote:
Original post by shadymokhtar
i'm using visual C++ 2008

Sorry for the confusion, but I didn't realize Visual C++ 2008 causes problems with stdout and stderr. Thread. This is probably why they are not showing up.

You could do this, and get the error reports that way instead, but I think rip-off is probably right about your program looking in the wrong place for the image.

Share this post


Link to post
Share on other sites
Thank u all for helping
i discovered the mistake and it's such a stupid one ..so sorry.
the image i was loading was originally a JPG format so i just renamed it .BMP
and i thought this way will change its format but it didn't, i don't know why although when i renamed it, it had .bmp extention, anyway i fixed the problem now and thanks again

Share this post


Link to post
Share on other sites
Ah, that'd explain it certainly. The file's extension type doesn't guarantee what a file contains, it just helps both programs, and users, understand what the file is. I can rename a .exe file, to a .txt, but that doesn't make it into a text file, and if I tried to open it in Notepad, it'd look like a bunch of gibberish.

However, lucky for you, there is a very very simple and easy way to load .jpg files using SDL. You have to download a sub-library called 'SDL_image', and #include it to your program. You also need to link it, as you linked SDL.

Then all you need to do is replace SDL_LoadBMP with IMG_Load. You can read this tutorial to see how to set it up. In fact, I'd recommend reading a couple of those, as they are well written.

SDL_image adds support for loading .jpg, .png, .gif*, and .tga. (As well as a couple others) It can also load .bmps like SDL_LoadBMP can.


*As long as it's not animated

Share this post


Link to post
Share on other sites
Sign in to follow this