Jump to content
  • Advertisement
Sign in to follow this  
raptorstrike

SDL draging

This topic is 5451 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

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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
yeah its totally awsome![smile]
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??

Share this post


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


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|

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!