SDL draging
ok when i just make a block move around on the screen it drags and leves a trail of were its just been how do i fix this heres my code
tanks [smile]
//there is some intialazation befor this
static void
draw ()
{
SDL_Surface *apple_src;
SDL_Surface *wall_src;
wall_src = SDL_LoadBMP("wall.bmp");
apple_src = SDL_LoadBMP("apple.bmp");
worm->Worm_Blocks.resize(3);
SDL_Rect XYrect;
if(event.key.keysym.sym == SDLK_LEFT)
X--;
if(event.key.keysym.sym == SDLK_RIGHT)
{
X++;
};
XYrect.x =X;
SDL_BlitSurface(block_src, NULL, screen, &XYrect);
SDL_Flip(screen);
}
int
main (int argc, char *argv[])
{
char *msg;
int done;
/* Initialize SDL */
if (SDL_Init (SDL_INIT_VIDEO) < 0)
{
asprintf (&msg, "Couldn't initialize SDL: %s\n", SDL_GetError ());
MessageBox (0, msg, "Error", MB_ICONHAND);
free (msg);
exit (1);
}
atexit (SDL_Quit);
/* Set 640x480 16-bits video mode */
screen = SDL_SetVideoMode (640, 480, 16, SDL_SWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{
asprintf (&msg, "Couldn't set 640x480x16 video mode: %s\n",
SDL_GetError ());
MessageBox (0, msg, "Error", MB_ICONHAND);
free (msg);
exit (2);
}
SDL_WM_SetCaption ("SDL MultiMedia Application", NULL);
done = 0;
while (!done)
{
//SDL_Event event;
/* Check for events */
while (SDL_PollEvent (&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
break;
case SDL_QUIT:
done = 1;
break;
default:
break;
}
}
/* Draw to screen */
draw ();
}
return 0;
};
dev-cpp 4.9.9.0
c++
SDL
You need to clear the surface or draw your paddle on top of a background. It is drawing correctly now. You are just seeing each frame as it was.
yeah it works now that im drawing on somthing but i have another questoin how do i get smooth key press based moving. how i have it now the block just move when the key is press is there a key down method or somthing of that nature?
You can check for key state and change a flag to move until the flag is altered again. Basically, key up and down. Toy around with it, I am sure you'll figure it out. Don't you absolutely love programming!
yeah its totally awsome![smile]
any way one last thing dose SDL have a problem with negative values? im doing somthing like this
those are just code segmants but can rects store negative numbers because when i run my program
when the left key is pressed i go left
when the right key is pressed i go left
when the down key is pressed i go down
when the up key is pressed i go down
dose this have to do with the negative values im using??
any way one last thing dose SDL have a problem with negative values? im doing somthing like this
case SDL_KEYDOWN: if(event.key.keysym.sym == SDLK_LEFT) { X=-1; Y=0; }; if(event.key.keysym.sym == SDLK_RIGHT) { X=1; Y=0; }; if(event.key.keysym.sym == SDLK_DOWN) { X=0; Y=-1; }; if(event.key.keysym.sym == SDLK_UP) { X=0; Y=1; }; break;
if(X > 0) { XYrect.x +=X; }; if(X < 0) { X = 1; XYrect.x -=X; }; if(Y > 0) { XYrect.y +=Y; }; if(Y < 0) { Y = 1; XYrect.y -=Y; }; SDL_BlitSurface(background,NULL,screen,&rect); SDL_BlitSurface(block_src, NULL, screen, &XYrect); SDL_Flip(screen);
those are just code segmants but can rects store negative numbers because when i run my program
when the left key is pressed i go left
when the right key is pressed i go left
when the down key is pressed i go down
when the up key is pressed i go down
dose this have to do with the negative values im using??
Adding a negative number to another number, ie.
X=-1;
XYrect.x += X;
is the same as taking the number away.
Try this instead of your second code block:
Apologies if it doesnt work, its nearly 1am here and Ive been staring at sine waves all day :o|
X=-1;
XYrect.x += X;
is the same as taking the number away.
Try this instead of your second code block:
if(XYrect.x < 0) XYrect.x = 0; if(XYrect.y < 0) XYrect.y = 0; XYrect.x +=X; XYrect.y +=Y; SDL_BlitSurface(background,NULL,screen,&rect); SDL_BlitSurface(block_src, NULL, screen, &XYrect); SDL_Flip(screen);
Apologies if it doesnt work, its nearly 1am here and Ive been staring at sine waves all day :o|
i know there the same thing but nether of them work whenever i try and subtract a .x or .y from a rect it dosnt seem to work
and i dont want to reset the .x and .y values to 0 just increment them accordingly
and i dont want to reset the .x and .y values to 0 just increment them accordingly
I'm at work so this may not work. Try checking:
Sine you already know that X and Y will only hold 2 possible values, try, if(X == -1) and if(X == 1) instead of checking against zero. (Same applies to Y) Then you don't need to change the values inside the if statement making it false again.
Sine you already know that X and Y will only hold 2 possible values, try, if(X == -1) and if(X == 1) instead of checking against zero. (Same applies to Y) Then you don't need to change the values inside the if statement making it false again.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement