Hey, I'm trying to make my first game in c using openGL: pong (might as wel start with the basics).
I have the logic figured out for the bouncing but I can't find a proper method for the timing (making the ball move every x milliseconds). I've tried using time() and difftime but that returns a value in seconds and moving the ball every second makes for a veeery slow game .
Now I've tried gettimeofday() which works for a second or 2 and then it just stops working and the ball stops moving...
This is the relevant part of my code. I've tried finding sollutions online but I'm still relatively new at this so I can't really understand most of the things i find (and it's usually in c++ instead of c)
Globally:
Code:
struct timeval now, before;
In the main to be run once before all of the opengl functions:
Code:
gettimeofday(&before, NULL);
My timing function. If go is 1 the ball can move:
Code:
void myTimer()
{
gettimeofday(&now, NULL);
if(now.tv_usec-before.tv_usec>300)
{
//printf("%lf\n", difftime(now, before));
go=1;
before.tv_usec=now.tv_usec;
glutPostRedisplay();
}
}
When I used time() and difftime before, the ball would not move but the commented printf would still printf the time repeatedly, even if I set the difference to 10000 seconds.
Can anyone give me some hints?