PDA

View Full Version : Is there a better way?



endo
04-03-2002, 10:17 AM
I have been using the following glut function to move the paddles in my game. As you can see, I have moved it a little then redraw, move it once more and then redraw again so that I can get a fine movement at a reasonable speed. Is there a better way though?? I know there must be but I just can't see it...



void special( int key, int x, int y )
{
int i;
switch( key )
{
case GLUT_KEY_LEFT:
for( i = 0; i < 2; i++ ) //move and redraw twice to get finer movement
{ //surely not the best method!!!
paddle.movePaddle( -1.0f );
glutPostRedisplay( );
}
break;

case GLUT_KEY_RIGHT:
for( i = 0; i < 2; i++ )
{
paddle.movePaddle( 1.0f );
glutPostRedisplay( );
}
break;

default:
break;
}

glutPostRedisplay( );
}

dorbie
04-03-2002, 11:11 AM
Nope, this is how it's done but you should really be animating everything else at the same time. So, you should process all input, update the paddle, move the ball, kill the bricks etc, then redraw, instead of doing it in the event handler after each key press. I wouldn't be comfortable using the glut event loop to do this but if that's what you want to do, fine. You can take the glut source and crack open the event loop and explicitly have your own game loop. Steve Baker has written about this.

[This message has been edited by dorbie (edited 04-03-2002).]

nexusone
04-03-2002, 11:17 AM
One problem I see, is say that other objects in your scene need to be updated, it will not happen until your paddle loop is done.

this is not complete code but will show you my thoughts on how to handle it, this way all object's will be update all at once. else you have things updating in no well defined order.

Let me know if I need to give more details on the functions.

keyboard_specail_key(int key, int x, int y)
{

switch(key)
{
case GLUT_KEY_LEFT:
paddle_state = 2; Number of moves to make per keypress.
paddle_direction = -1; 0= no movement, -1 = left, 1 = right;
break;
// other key's

}

My_object_control_routine() // I would use glutTimirFunc to call it or glutIdlefunc.
{
// Update paddle
if (paddle_state > 0) // Keep updating paddle until new point is reach.
{
paddle_pos = paddle_pos + X * paddle_direction;// X= is amount of movement per update, paddle_direction makes you amount add or sub from pos..
paddle_state--; decrease our state by 1.
}

// Update ball pos. here.

//
glutPostRedisplay(); // Now we have managed all of our object, redraw screen.
}



Originally posted by endo:
I have been using the following glut function to move the paddles in my game. As you can see, I have moved it a little then redraw, move it once more and then redraw again so that I can get a fine movement at a reasonable speed. Is there a better way though?? I know there must be but I just can't see it...



void special( int key, int x, int y )
{
int i;
switch( key )
{
case GLUT_KEY_LEFT:
for( i = 0; i < 2; i++ ) //move and redraw twice to get finer movement
{ //surely not the best method!!!
paddle.movePaddle( -1.0f );
glutPostRedisplay( );
}
break;

case GLUT_KEY_RIGHT:
for( i = 0; i < 2; i++ )
{
paddle.movePaddle( 1.0f );
glutPostRedisplay( );
}
break;

default:
break;
}

glutPostRedisplay( );
}

endo
04-03-2002, 01:11 PM
Thanks for the advice. I am only using glut as a temporary measure until I understand timing and animation a bit better - this is harder than the vector maths!! I'll have a proper go at changing so be prepared for more questions.

I do have one for nexusone though, why is this bit not a while loop?

if (paddle_state > 0) // Keep updating paddle until new point is reach.
{
paddle_pos = paddle_pos + X * paddle_direction;// X= is amount of movement per update, paddle_direction makes you amount add or sub from pos..
paddle_state--; decrease our state by 1.
}

Furrage
04-03-2002, 01:21 PM
Why not use your Input functions to set/clear flags, and call one update function at the start of your display function? Then you could call your display function with a Timer/TimeOut or put it in your Idle function? This way one display updates everything that needs it and if your frame rate is good it will be just as responsive.

nexusone
04-03-2002, 02:19 PM
To use a while loop would defeat changing from your code, you still have to wait for the loop to end.
My way all object keep a smooth movement and are in sync with each other.

Think about it this way should your ball stop moving while the paddle is moved?

This way you have your smoot paddle movement and ball keep's moving also.

You can also add to this routine a check to see if the paddle has made contact with the ball.


There are a lot of feature to using GLUT, that even after you learn opengl are useful.



Originally posted by endo:
Thanks for the advice. I am only using glut as a temporary measure until I understand timing and animation a bit better - this is harder than the vector maths!! I'll have a proper go at changing so be prepared for more questions.

I do have one for nexusone though, why is this bit not a while loop?

if (paddle_state > 0) // Keep updating paddle until new point is reach.
{
paddle_pos = paddle_pos + X * paddle_direction;// X= is amount of movement per update, paddle_direction makes you amount add or sub from pos..
paddle_state--; decrease our state by 1.
}