SDL Bitmap Loading Issue?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <SDL/SDL.h>

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = NULL;
screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF); 

//Draw the Player
SDL_Surface *image = NULL;
SDL_Surface *temp = NULL;
image = SDL_LoadBMP("C:/sprite_index/player.bmp");
image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
SDL_Rect dest;
dest.x = 100;
dest.y = 100;
dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image, NULL, screen, &dest);
SDL_UpdateRects(screen, 1, &dest);
SDL_Delay(2500);
SDL_FreeSurface(image);

return 0;
}


When I run the application it displays a black Windows application for a split second then closes. Is it a compatibility issue since I am running Windows 7 and it is a 16 bit application? If not can someone help me point out the problem?
I dont know SDL but you probably just need to put it in a loop.
I'm not sure if that's it, but I will try it. The SDL_Delay() function keeps the image on the screen for a given amount of milliseconds, so that's why I assumed I did not need a loop to keep it on.

Edit: Even with the loop added the application still closes. I have even tried the system("PAUSE") function after the SDL_UpdateRects() function. Still did not work. :/.
Last edited on
Maybe SDL_Flip?
I had that on for a little while before and it still didn't work. I will try and put it back on.

Edit: Still did not work :/.
Last edited on
I think you need a source rect and destination rect. See http://www.aaroncox.net/tutorials/2dtutorials/sdl_sprite.pdf
I also had that in too at one point. I look at numerous tutorials trying to find a fix. Some had the source rect, and some didn't.

Edit: Added it once again. Still doesn't work. Here is my current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <SDL/SDL.h>

int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = NULL;
screen = SDL_SetVideoMode(640, 480, 16, SDL_DOUBLEBUF); 

//Draw the Player
SDL_Surface *image = NULL;
SDL_Surface *temp = NULL;
image = SDL_LoadBMP("C:/sprite_index/player.bmp");
image = SDL_DisplayFormat(temp);
SDL_FreeSurface(temp);
SDL_Rect src;
src.x = 0;
src.y = 0;
src.w = image->w;
src.h = image->h;
SDL_Rect dest;
dest.x = 100;
dest.y = 100;
dest.w = image->w;
dest.h = image->h;
SDL_BlitSurface(image, NULL, screen, &dest);
SDL_Flip(screen);
SDL_Delay(2500);
SDL_FreeSurface(image);

return 0;
}


Could it be a problem with the bitmap itself?
Last edited on
Try passing source to blit_surface
Change line 12 to
temp = SDL_LoadBMP("C:/sprite_index/player.bmp");
I will try both of those. Thank you.

Edit: It worked. Thank you :D.
Last edited on
Topic archived. No new replies allowed.