Must glutTimerFunc be called in main? Can i use it like this?

As the question asks, must glutTimerFunc be called in main? basically, i am making a tank game, and im making a movement function. When a key is pressed, the tank will move so many pixels across the screen (Left or right, this is a 2d game).
I want the movement to be a smooth transition, which is why im trying to incoroprate this function in the first place.

Now, i want this only to be triggered when a key is pressed, as i said. Can glutTimerFunc be called by another function, which is inevitably called by glutDisplayFunc?

Im not so sure my question of implementation is clear, but i think it is clear what i want to do.

Can i do this, or is there a better way to implement what i want to do? Thanks.

The timer function callback is just an ordinary function, there is nothing special with it so you can call it as much as you want, but I am not sure that’s what you want to use it for.

Just setup the timer and when the key is pressed set a variable to indicate this, and clear the variable when you let go of the key, in the timer function only update when the variable is set.

Mikael

I read that this function does not terminate somewhere, though. Is this true?

No, you do not have to call or setup glutTimerFunc before main.

There are two basic ways to handle your movement, I guess in the future you want to have the tank fight something???

So the best solution would be to use the glutTimerFuncion for event and animation loop.

Once the game starts or in a initilize game routine start the glut timer.

glutTimerFunc( 100, My_timer_func, 0);

void My_timer_func( int te )
{

if (current_location != destination_location)
{
update_tank() // we only update when there is a new destination
}

update_enemy_tank(); // would have some logic to make it move or target the players tank

glutPostRedisplay(); //Update screen with new position data.
glutTimerFunc( 100, My_timer_func, 0);// restart the timer
}

In you keyboard routine, just add the corrent new position in which you want your tank to go in destination_location variable.
Then each update add a small movement until the current_location is equal to the destination.

To stop the timer just do this:

if ( game_over ) glutTimerFunc( 100, My_timer_func, 0);

Only when game_over variable is zero will the loop stop. So at start of game, set game_over to 1, then at the end switch to zero to stop it.

But you may want to keep it running to some animation or other things you may want to keep going when the game is idle.

In the timer loop could be something like this:

if (!game_over) Game_over_screen(); would only display at end of game

[This message has been edited by nexusone (edited 03-11-2004).]