SDL controllin sprite moving speed!

I want to know how to control the moving speed of a object. My thought was to have the move function to be executed every x millisecond, but well I would need to use multi-threading wont I? And i keep reading that rendering main screen from other thread is very bad and i shouldnt use so i donno what to do?!
Last edited on
We donno what you're talking about -__-

?!
ToniAz wrote:
We donno what you're talking about
"We"? Did you take a poll?

@Damadger. There are a few ways to call your move function at regular time intervals without multi-threading.

1) If you have a windows callback function responding to windows messages then setup a timer and call your move function when WM_TIMER messages are received.

2) Simple: call move() in the render loop before drawing. The rate may vary from machine to machine though. If you sync to the refresh rate for rendering then your move function would be called at the vsync rate. My monitor is set for a 60hz refresh rate, so on my computer move() would get called 60 times per second.
1) It wont be cross-platform like that would it?!

2) this will be constant speed I mean I cant change Unless I changed the frame rate?!
V-sync is tough to code for, not sure a straight SDL/OpenGl combo can do it. I've actually found that setting v-sync manually for my graphics card has better results than v-sync a game might try to implement anyway.

You don't want to link the speed of your sprite to the framerate, use a clock. Looks like SDL has a timer SDL_GetTicks();.

Basically, your sprite class (or however you're doing it) keeps track of the time the last time it moved. Then when you move it, you give it the current time. The difference between these two is coefficient to the speed of the sprite. It will move at the same speed no matter the framerate. Acutally, it moves the same speed no matter how slow/fast any part of the program is.

My guess, you don't need to worry about multi-thread. By using the timer, you can calculate a framerate (or perhaps just display framerate from you graphics card). Chances are your program will have no trouble displaying 60 fps, maybe it can do 1000+ at this point. If you're doing stress tests and it goes below, well, maybe this is the time to try to multi-thread.

Last edited on
ye i already had this done but it ust dont work right look
class player
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Player
{
public:
	static const int sprite_size = 32;
	static const Uint32 move_speed = 1;
	Player();

	void moveLeft();
	void moveRight();
	void moveUp();
	void moveDown();

	inline bool canMove(){return ((SDL_GetTicks() - last_moved) >= move_speed); } // this check if he can move

	inline void moved(MOVING it){
		last_moved = SDL_GetTicks();//this tracks last move time
		moving_state = it;
	}

	SDL_Rect getSprite(SPRITE sprite_id);

	void replaceSprite(SPRITE new_sprite);
	void onStopMove();
	

private:
	SDL_Surface* player;
	Sint16 x,y;
	MOVING moving_state;
	Uint32 last_moved;
};


code file: one of the methods to move
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Player::moveLeft()
{
	static SPRITE last = SPRITE_MOVE_LEFT2;
	while(key_states[SDLK_LEFT])
	{
		if(canMove())//check if he can move
		{
			SPRITE next = (last == SPRITE_MOVE_LEFT2 ? SPRITE_MOVE_LEFT1:SPRITE_MOVE_LEFT2 );
			Sint16 old_x = x,old_y = y;
			if( (x-1) < 0 )
			{
				g_ui->viewError("You have reached windows end.");
				onStopMove();
				return;
			}
			x -= 1;
			SDL_FillRect(g_ui->getScreen(),&getRect(old_x,old_y,sprite_size,sprite_size),0);
			g_ui->Draw(x,y,player,&getSprite(next));
			g_ui->update();
			last = next;
			moved(MOVING_LEFT); // update last moved time
		}
		SDL_PumpEvents();
	}
	onStopMove();
}


But whatever time i put in move_speed it dont change.
Last edited on
Line 16 of your method should be x -= moveSpeed;, right? Looks like if it can move you move it 1. Could be something wrong with your method of checking key_states. Don't know too much about SDL.
Topic archived. No new replies allowed.