#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
SDL_Surface *background;
SDL_Surface *cardA;
SDL_Surface *card2;
SDL_Surface *card3;
SDL_Surface *card4;
SDL_Surface *card5;
SDL_Surface *card6;
SDL_Surface *card7;
SDL_Surface *card8;
SDL_Surface *card9;
SDL_Surface *cardJ;
SDL_Surface *cardQ;
SDL_Surface *cardK;
SDL_Surface *screen;
int xpos=0,ypos=0;
int cxpos=0, cypos=0;
int InitImages()
{
background = SDL_LoadBMP("background.bmp");
cardA = SDL_LoadBMP ("cardA.bmp");
card2 = SDL_LoadBMP ("card2.bmp");
card4 = SDL_LoadBMP ("card4.bmp");
card5 = SDL_LoadBMP ("card5.bmp");
card6 = SDL_LoadBMP ("card6.bmp");
card7 = SDL_LoadBMP ("card7.bmp");
card8 = SDL_LoadBMP ("card8.bmp");
card9 = SDL_LoadBMP ("card9.bmp");
cardJ = SDL_LoadBMP ("cardJ.bmp");
cardQ = SDL_LoadBMP ("cardQ.bmp");
cardK = SDL_LoadBMP ("cardK.bmp");
return 0;
}
int SDL_BlitSurface(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);
void DrawIMG(SDL_Surface *img, int x, int y)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_BlitSurface(img, NULL, screen, &dest);
}
void DrawIMG(SDL_Surface *img, int x, int y,
int w, int h, int x2, int y2)
{
SDL_Rect dest;
dest.x = x;
dest.y = y;
SDL_Rect dest2;
dest2.x = x2;
dest2.y = y2;
dest2.w = w;
dest2.h = h;
SDL_BlitSurface(img, &dest2, screen, &dest);
}
void DrawBG()
{
DrawIMG(background, 0, 0);
}
void DrawScene()
{
DrawIMG(background, xpos-2, ypos-2, 800,600, xpos-2, ypos-2);
DrawIMG(card2,cxpos,cypos,30,40,cxpos,cypos-10);
}
int main(int argc, char *argv[])
{
Uint8* keys;
if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf("Unable to init SDL: %s\n", SDL_GetError());
exit(1);
}
atexit(SDL_Quit);
screen=SDL_SetVideoMode(800,600,32,SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( screen == NULL )
{
printf("Unable to set 800x600 video: %s\n", SDL_GetError());
exit(1);
}
InitImages();
int done=0;
while(done == 0)
{
SDL_Event event;
while ( SDL_PollEvent(&event) )
{
if ( event.type == SDL_QUIT ) { done = 1; }
if ( event.type == SDL_KEYDOWN )
{
if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
}
}
DrawBG();
keys = SDL_GetKeyState(NULL);
if ( keys[SDLK_UP] ) {
cxpos++;
}
DrawScene();
}
return 0;
}
Disappearing Squares
I am having a problem. When a put a picture of a card onto the screen, without changing the xpos, ypos, it is fine, but when I add 6 to the xpos, half of the picture disappears! Same thing with the ypos. What do I do???
This is the same problem for the people that read SDL Problem, but I thought I needed to repost, because no more people were coming and answering. Anyway, this code places a "card" onto the screen and when you press the up key once, the card's xpos is increased by one. Please help me!!!
DrawBG();keys = SDL_GetKeyState(NULL);if ( keys[SDLK_UP] ) { cxpos++;}DrawScene();
Shouldn't this part of your code be running in a loop? It looks like your program stops after you draw the scene once. oops, I see the loop now.
[Edited by - Kelly G on November 18, 2004 5:31:14 AM]
at the end of your rendering loop you need to have SDL_Flip() :
in your initialization code, you are creating a double buffered screen, which means SDL will render to the backbuffer, then swap it with the front buffer. since you never call SDL_SwapBufferes(), you're essentially drawing to a non-existant screen, as it is never being switched out to replace the front one.
even if that doesn't solve the problem, with a double-buffered screen it's something you always need to have anyway.
cheers.
<edit :: changed SDL_SwaphBuffers() to SDL_Flip().
[Edited by - stormrunner on November 18, 2004 2:38:27 AM]
while ( SDL_PollEvent(&event) ){ if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } DrawBG(); keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { cxpos++; } DrawScene(); SDL_Flip();}
in your initialization code, you are creating a double buffered screen, which means SDL will render to the backbuffer, then swap it with the front buffer. since you never call SDL_SwapBufferes(), you're essentially drawing to a non-existant screen, as it is never being switched out to replace the front one.
even if that doesn't solve the problem, with a double-buffered screen it's something you always need to have anyway.
cheers.
<edit :: changed SDL_SwaphBuffers() to SDL_Flip().
[Edited by - stormrunner on November 18, 2004 2:38:27 AM]
- stormrunner
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement