Sign in to follow this  

displaying bitmap with sdl

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Thanks for the links Drew. I've been to most of those sites, even just today. I just dunno why the image won't show up on the screen. Everything else works fine it just doesn't show the bitmap even though it knows where it is located.

EDIT: After going through and making a lot of code adjustments I finally got it to work. Thanks to all who posted with helpful tips.

[Edited by - SKATIN_HARD on January 12, 2006 12:22:54 AM]

Share this post


Link to post
Share on other sites
Well I added in the SDL_Flip(screen) and then I looked over at Cone3D tutorial number 2 and went from that. I needed up changing around the code a good bit and ended up adding 2 more functions.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this