SDL draging

Started by
12 comments, last by The Rug 19 years, 6 months ago
well am not able too check your theory at the moment but why would that work? I hope i dont come off as being rude here im just wondering why checking against 0 is any diffrent from a direct relational opperator like == or != which takes one value and compares it with another. should i do a <= >=?
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Advertisement
No offense taken. Basically looking at your code you have X and Y as your determining variable. Lets take the first key check. If you press the left button it sets X to -1. Then going to the check it passes the first one since X is not greater than 0. The next one is true because X is less than 0, but the first thing you do inside is make that check false by setting X to 1, which makes the first check true. If you just kept the X at -1 and used += instead of -+ it would give the opposite result, ie, same as subtracting. I don't have my computer at hand but I am pretty sure that the key check only checks for the initial key press and not holding the key down, I could be wrong. But since your X and Y values only have 2 possible values I thought it would be clearer to read if it was just checking for specific value. Doing it that way lets you compute the value set by the key press without the need to change the value. The more steps there are in a program, the more opportunities there are to create a bug. This is all my personal preference and opinion and should be taken as such. If what you are doing works for you, use it.

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);    




Steven Bradley .:Personal Journal:. .:WEBPLATES:. .:CGP Beginners Group:. "Time is our most precious resource yet it is the resource we most often waste." ~ Dr. R.M. Powell
well its still not working i have tryed multiple configurations and it still dose the same thing i dont know what the problem is i guess ill try one more time and then let this die

*bump*[bawling]
____________________________"This just in, 9 out of 10 americans agree that 1 out of 10 americans will disagree with the other 9"- Colin Mochrie
Ok, so you set the X and Y values according to the keypresses. Thats ok, but why do you then do this?

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;      };


All you need to do, is add the values X and Y to your XYrect to update its position.

Imagine you set Y to -1 because of a key press,
    if(Y < 0)    {        Y = 1;        XYrect.y -=Y;      };


is the same as

XYrect += Y;


For example.


This might not solve your problem, but it will make your code clearer. You could try posting all your code, so we can throw it onto a compiler and see what happens for ourselves.
the rug - funpowered.com

This topic is closed to new replies.

Advertisement