# SDL very basic code

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

## Recommended Posts

Hi! everybody.
The code below should create a window with image.bmp as its background image.But when I run it creates a window without the image.I tried everything,my image is in the same folder of my project and main.cpp. The code is exactly same from a youtube tutorial and works fine for many people.Please tell what's wrong in this.

#include<SDL/SDL.h>

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

bool gamerunning = true;
SDL_Event event;

void apply_surface(int x,int y,SDL_Surface *source,SDL_Surface *destination){
SDL_Rect rect;
rect.x=x;
rect.y=y;
SDL_BlitSurface(source,NULL,destination,&rect);
}
int main(int argc,char *argv[]){

SDL_Init(SDL_INIT_VIDEO);
SDL_WM_SetCaption("Apply Images",NULL);

screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);

//Game Loop
while(gamerunning){
if(SDL_PollEvent(&event)){
if(event.type==SDL_QUIT)
gamerunning=false;
}
//Apply surface
apply_surface(0,0,background,screen);

//Update Screen
SDL_Flip(screen);
}

SDL_Quit();
return 0;
}

##### Share on other sites
There are a few things you can try. Easiest is to check if the image has been correctly loaded:
 background = SDL_LoadBMP("image.bmp"); if(!background) { // Write the error somewhere. // In C (requires <stdio.h>) printf("Failed to load image.bmp: %s\n", SDL_GetError()); // In C++ (requires <iostream>) std::cout << "Failed to load image.bmp: " << SDL_GetError() << '\n'; } 
By implementing this and looking at the output, you should know if the file loaded correctly. If not, consider hard coding a full path to the file (remember to correctly escape the path if using windows back slashes). Or download a bitmap file you know is correct. It is unlikely - but your file might be corrupt. It might be a 32 bit bitmap, which has patchy support. It might look like "image.bmp" in Windows Explorer, but unless you've set the option to always show file extensions, the file might actually be called image.bmp.bmp (or even image.bmp.png).

You should add such error checking to all SDL functions, including SDL_Init() and SDL_SetVideoMode(). Even SDL_BlitSurface() can return an error. Better to be aware of these errors when they occur ather than spending time chasing phantom bugs later in your program.

If it is loading correctly, but no displaying, you should note that your rendering logic is tied to your input loop. That is, at least one "event" must occur before the screen will be drawn. That said, I would expect a SDL_VIDEOEXPOSE event to be delivered to your application during the first loop, so this shouldn't be an issue.

##### Share on other sites
@rip off
I have put the error check you have given but still nothing is displayed.The image that I am trying to load is created by me in paint,I have just filled the background with a colour in paint and saved it with the name image.bmp .
I didn't understand the SDL_VIDEOEXPOSE event.

##### Share on other sites
It would help if you post your current code, so we can check if your new error checking/handling is correct.

I have put the error check you have given but still nothing is displayed
[/quote]
Can you verify that the program definitely doesn't enter the error handling code? Rather than, say, the output merely going somewhere you aren't checking. There are a couple of ways of doing this.

• Step through the program in the debugger, and see whether the error branch is executed.
• Output a message in an "else" clause. If you see no output then maybe you have more work to do in your error handling.
• Throw in a platform specific notification, for example a MessageBox() call on Windows.

The image that I am trying to load is created by me in paint,I have just filled the background with a colour in paint and saved it with the name image.bmp
[/quote]
Should work. Another likely problem is that your programs "working directory" isn't where you expect it to be. You said that your image is in the same folder as your source code. Depending on which IDE / toolchain you are using, that will not be where relative paths will be resolved against.

I didn't understand the SDL_VIDEOEXPOSE event.
[/quote]
The event indicates that the video surface has been "exposed". For instance, if the window was hidden and then the user clicks on the entry in the task bar to bring the window back. Or if another window went in front of your program's one. It generally doesn't matter for games, because they often re-draw the entire screen every game loop.

However, if you were making a turn based or other non-active simulation game (e.g. chess, minesweeper), you might only draw the window when something changes. In this case you need to be notified when the stuff you have drawn becomes invalidated due to external events.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 23
• 36
• 16
• 75