Advertisement

SDL: Why does this not work?

Started by January 17, 2005 08:59 PM
14 comments, last by The Rug 20 years, 1 month ago
It isn't very well commented but a few people have found this to be helpful. Hopefully you can expand on the concepts a little but I think it has most of what you are looking for.
Evillive2
Alright, this guy is of the wrong impression of how the code flow. If you want to make a sprite move 1 pixel at a time, but across a full box, you need to program the system to automatically move the sprite after each iteration. For instance, look at this code:
while(1){  keys = SDL_GetKeyState(NULL);  if(keys[SDLK_UP])  {    if ( ( my_sprite.source_y == my_sprite.target_y )    &&   ( my_sprite.source_x == my_sprite.target_x ) )      my_sprite.target_y -= 32;  }   else if (keys[SDLK_DOWN])  {    if ( ( my_sprite.source_y == my_sprite.target_y )    &&   ( my_sprite.source_x == my_sprite.target_x ) )      my_sprite.target_y += 32;  }  if (my_sprite.source_y > my_sprite.target_y)  {    my_sprite.source_y -= 1;  }   else if (my_sprite.source_y < my_sprite.target_y)  {    my_sprite.source_y += 1;  }  blit( my_sprite );}

Its rudimentary, but you should see what I'm pointing at. You have to keep a target destination for the sprite, but draw where the sprite actually is from a different set of variables. Then, on each iteration of the loop, it'll move the sprite accordingly and not allow the player to interrupt the movement.
william bubel
Advertisement
The reason your movement is so fast is because you need to make it time-based.

Your frame rate is so fast that when you hold the key down like that, it registers several "keypresses" per frame. What you need to do is time how long a frame takes, decide how far you want it to move per frame, and increment the position accordingly.

This means that whether your game runs at 20fps or 200fps, the rate of movement is the same. Otherwise if for example it becomes a networked game, the player with the higher framerate has a distinct advantage.

code is something like this:

// these are for calculating FPS and movementfloat currentTime;float lastTime;float frameInterval;int SPEED;  // set this to how many pixels you want your sprite to move per SECOND.  Play with it until you find a value that seems right.// in your keypress event// if (<right movement key is down>)sprite.xpos += SPEED * frameInterval;// Draw frame here:void DrawFrame(void ) {    // calculate frame interval    currentTime = SDL_GetTicks() * 0.001f;    frameInterval = currentTime - lastTime;    lastTime = currentTime;    DrawTiles();    DrawSprite();}


Also, storing your sprite positions as floats makes all this a little easier. Convert to ints immediately before blitting.

Why is
using namespace std;
evil?
Quote:
Original post by andyZER0
Why is *** Source Snippet Removed *** evil?


I have no idea. I'd rather using the STL library nowadays then try to make everything better myself.
Quote:
Original post by andyZER0
Why is *** Source Snippet Removed *** evil?


Its (I think) generally considered bad practice to include the entire std namespace when you are only using a few bits of it. You wouldn't want to include the entire namespace when using just cout for instance, you'd just do:
using std::cout;
the rug - funpowered.com

This topic is closed to new replies.

Advertisement