How to use timing in simple game such as pong

This is a discussion on How to use timing in simple game such as pong within the Game Programming forums, part of the General Programming Boards category; Hey, I'm trying to make my first game in c using openGL: pong (might as wel start with the basics).
...

How to use timing in simple game such as pong

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)

wow, I can't believe I didn't see that. Not including the seconds part was what stopped it.
I also tried it with ftime() and that works too.

There is however something very wrong with this method. One of my 2 PCU cores runs at 100% while the programm is running. I guess this is because I used glutIdleFunc() to run the timer program and it runs it whenever it has the chance. I tried using glutTimerFunc() but that only runs once and I can't put that in a loop.

What is the proper way to accomplish what I am trying to do? Can anyone maybe refer me to a post that explains it?

One thing you could do is to put a call to usleep() or something at the end of the function registered by glutIdleFunc(). That's a bit of a hack though and it would be better to use the glutIdleFunc() mechanism properly. See the section "3.027 Why does glutTimerFunc() only execute my callback once?" here: OpenGL FAQ / 3 GLUT

(That's a really good place to look for glut and OpenGL answers, by the way. I usually just use Google but often end back there again . . . .)

You'd probably find it useful to read some more general web pages as well. I scanned a few and came up with these:

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell