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

## Recommended Posts

Hello there! New to the forums. :) I've been going through Lazy Foo's SDL tutorials, and have hit a li'l snag in the chapter where he covers colour keys. Here's the link to that. I'm not able to load the background and the foo images. I'm using Visual C++ express edition. I did set up the project properly at the beginning. My code even compiles without any errors. When I execute, I can only see a black window without any images on it. But I am able to 'X' out of it, so that part of the code is working fine. When i tried to debug using watches, I found out that in the loadImage function, loadedImage is always NULL. I've tried to debug this, but since it seems to be a run-time error, I'm having a hard time. I have the images along with the source file and the .dll files are with the executable file. And here's my code:
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

using namespace std;

const int screenWidth = 640;
const int screenHeight = 480;
const int screenBPP = 32;

SDL_Surface* screen = NULL;
SDL_Surface* background = NULL;
SDL_Surface* foo = NULL;

{
SDL_Surface* optimizedImage = NULL;

{
if(optimizedImage != NULL)
{
Uint32 colourKey = SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF);
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, colourKey);
}
}
return optimizedImage;
}

void applyImage(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;

SDL_BlitSurface(source, NULL, destination, &offset);
}

int main(int argc, char* argv[])
{
bool quit = false;
SDL_Event evnt;
if(SDL_Init(SDL_INIT_EVERYTHING)==-1)
return 1;
screen = SDL_SetVideoMode(screenWidth, screenHeight, screenBPP, SDL_SWSURFACE);
SDL_WM_SetCaption("Whatever",NULL);

applyImage(0, 0, background, screen);
applyImage(240, 190, foo, screen);

if(SDL_Flip(screen) == -1)
return 1;
while(quit == false)
{
while(SDL_PollEvent(&evnt))
{
if(evnt.type == SDL_QUIT)
{
quit = true;
}
}
}

SDL_FreeSurface(foo);
SDL_FreeSurface(background);
SDL_Quit();
return 0;
}

Sorry for posting the whole code, I just couldn't guess which part of it I might've gone wrong in. Cheers! :)

##### Share on other sites
Do you actually have a image named "background.png", and a image named "foo.png"? If not, you need to either download them from the site, or else just toss together some test images of your own.

If you do have those images, but they are not being loaded, it usually means it cannot find the images, meaning you have them in a place it doesn't expect. Put the images in the same folder as your .exe file.

[Edit:] Here:

Foo.png
Background.png

(Just right click on the links and Save Target As, save them in the same file as your .exe file)

##### Share on other sites
I have already downloaded both the files from the site. I've copied them to the folder where the executable file resides and also to the folder where the source file resides. The same loadImage function worked fine in the previous chapters of the tutorial(But i hadn't used color key in them). As far as my understanding goes, there's no problem with the loadImage function. But I can't for the life of me figure out where the problem is.

##### Share on other sites
Copying and pasting your exact code works perfectly fine for me. Is there a 'rebuild all' or similar button on your IDE?

Also, do you have your linker settings set up properly? In particular, did you remember to add the linker for SDL_image? (It should toss out a linker error if you didn't)

Here's what I have in my linker settings:

-lmingw32 -lSDLmain -lSDL
-lSDL_image

(I'm using Dev C++, I don't know how to do it in Visual Studio)

##### Share on other sites
Cool! Well atleast you got to see the output of my program. :) I tried rebuilding it, but it didn't help. Also started a new project thinking that i may have configured the last one wrong. That didn't help either. And no, I'm not getting any linker errors either. I'm beginning to wonder if there's some problem with Visual express when it comes to supporting SDL(most probably no, but it helps my frustration :) ). Anyway, i'll just move on to the next tutorial and see how it goes. If it gives any trouble again, i might switch to devcpp too.

Thanks for your help. If you hadn't shown me the output i would've been still cracking my head over it.

##### Share on other sites
Most people don't recommend Dev C++, claiming it's buggy. Having used it for the past 3 years, I know there's some truth to that statement. Once I finish my current project, I intend switching IDEs myself. (To Code::Blocks, since that seems to be what people recommend on these forums)

Just to save you the time of having to download a IDE you probably wont want, I figured I'd let you know.

##### Share on other sites
Have you included the libpng DLLs in the same folder as the executable? SDL_Image can only load a given image format if you supply the appropriate DLLs.

Also, use the error message generated from IMG_GetError() when something goes wrong (like a function returns NULL). This should give you a clue as to the cause. Ideally, you would already have such checks somewhere in your code - before something starts going wrong.

##### Share on other sites
@Servant of the Lord
I already have devcpp actually. I've used it a couple of times. I avoided it since it used to become really slow while using debugging options. I even tried Code::Blocks. That's the IDE i still use for C programming. It works great, but i ran into problems while installing SDL such incessant crashing while trying to edit linker options. So migrated to visual express which seemed to be working fine up until now.

@rip-off
Yes, I've included the .dll files. It would've shown a run-time error otherwise.

And how do i check the error message from IMG_GetError()? I checked in the documentation for SDL_Image and here's what i've come up with:

loadedImage = IMG_Load(filename.c_str());	if(!loadedImage)		printf("Image load %s", IMG_GetError());

But how do I print the error? When i run the code after the above modification, it's still the same. I don't see the above statement.

##### Share on other sites
Quote:
 Original post by dev-oidBut how do I print the error? When i run the code after the above modification, it's still the same. I don't see the above statement.

In SDL, the 'cout' stream that printf writes text to, is rerouted to a file called stdout.txt that appears in the same folder as your .exe file.

##### Share on other sites
Quote:
 Original post by dev-oid@rip-offYes, I've included the .dll files. It would've shown a run-time error otherwise.

If you are thinking of those "You application configuration is incorrect" errors, then no. AFAIK SDL_Image manually loads these DLLs. This means that if you knew in advance you only wanted to load PNG files you could choose not to bother including the jpeg decoding library.

Quote:
 And how do i check the error message from IMG_GetError()? I checked in the documentation for SDL_Image and here's what i've come up with:

IIRC newer versions of SDL no longer redirect output to stdout.txt (etc) by default. You may have to find some other way to show this, examples include manually open a file, grabbing a handle to the console (if one is available) or use a MessageBox().

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 19
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631760
• Total Posts
3002176
×