displaying bitmap with sdl

Started by
11 comments, last by SKATIN_HARD 18 years, 3 months ago
I made a little hacked up window function to display a bitmap in sdl. The source is:
[sourcecpp]

int InitImages()  
      {
                     if (SDL_Init(SDL_INIT_VIDEO) != 0) {
	printf("Unable to initialize SDL: %s\n", SDL_GetError());
	return 1;
}
        SDL_Surface *screen;

screen = SDL_SetVideoMode(640, 480, 32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if (screen == NULL) {
	printf("Unable to set video mode: %s\n", SDL_GetError());
	return 1;
}         
             // image stuff
                SDL_Surface *image;
               
        SDL_WM_SetCaption( "Starting", "Starting");
        SDL_ShowCursor(SDL_DISABLE);    
        image = SDL_LoadBMP("image/background.bmp");   
        
SDL_Rect src, dest;

src.x = 0;
src.y = 0;
src.w = image->w;
src.h = image->h;
dest.x = 100;
dest.y = 100;
dest.w = image->w;
dest.h = image->h;

SDL_BlitSurface(image, &src, screen, &dest);
        
} 
Now this code compiles and runs. I see the starting window then it goes to my game but the bitmap doesn't get displayed on the screen. Does anybody have a clue why? Thanks again.
Advertisement
Always when you load bitmaps with sdl, check if they are ok.

SDL_Surface* image;image = SDL_LoadBMP("background.bmp");if(image == NULL) {   // image failed to load   cout << "unable to open file: background.bmp" << endl;}


Cheers and good luck! [smile]
you have to call SDL_Flip( screen );

so SDL will update the screen.

you may also need to add in a delayin there if your game would instantly overwrite the image
Hehe forgot that one... quite essential though [smile], always check your bitmaps anyway ;)
I added in the SDL_Flip(); and the screen still comes up blank. The SDL window appears and it says Starting like it should but the bitmap does not get displayed. Dunno why.
Quote:Original post by SKATIN_HARD
I added in the SDL_Flip(); and the screen still comes up blank. The SDL window appears and it says Starting like it should but the bitmap does not get displayed. Dunno why.


post your code then. it should work...
Ok. I have these 2 functions at the top:

int InitImages();
int SDL_Flip(SDL_Surface *screen);

Then the source for them:

[sourcecpp]  int InitImages()         {                     if (SDL_Init(SDL_INIT_VIDEO) != 0) {	printf("Unable to initialize SDL: %s\n", SDL_GetError());	return 1;}        SDL_Surface *screen;screen = SDL_SetVideoMode(640, 480, 32,SDL_HWSURFACE|SDL_DOUBLEBUF);if (screen == NULL) {	printf("Unable to set video mode: %s\n", SDL_GetError());	return 1;}                        SDL_Surface *back;                SDL_Surface *image;                int xpos=0,ypos=0;                                     SDL_WM_SetCaption( "Starting Clone Pong", "Starting Clone Pong");        SDL_ShowCursor(SDL_DISABLE);            image = SDL_LoadBMP("image/background.bmp");                   SDL_Rect src, dest;src.x = 0;src.y = 0;src.w = image->w;src.h = image->h;dest.x = 100;dest.y = 100;dest.w = image->w;dest.h = image->h;SDL_BlitSurface(image, &src, screen, &dest);        } 


Then below in main I have

[sourcecpp] InitImages();             int SDL_Flip(SDL_Surface *screen);                 wait(1); 


I want the first sdl window to come up showing the bitmap....then have a pause then go to the main game window. Ideas and comments welcome. Thanks so much guys.
Ok,
I may be looking at this wrong, but this is what I got. You want the bitmap "image" to show up? Is that right?


Try this for your SDL_Flip.



SDL_Flip(screen);



Again,
I may have just looked at your code wrong. I will look at it again though.



Chad.


EDIT: After looking at your code more, it seems like that is what you need. Just forget the int, and put
SDL_Flip(screen);

Ok I switched that and my code compiled. But it didn't show the bitmap. I just got a blank sdl window with the right caption that said starting but no bitmap was displayed.
I will kindly refer you to look at this thread for a great bunch of references/tutorials on SDL to help you understand what you are after and how you should go about it. [wink] Cone3D's tutorials are outdated, but they have a great beginning one that shows how to go about this, so look there.

This topic is closed to new replies.

Advertisement