Managing framerates, time, and such
Okay, so I'm programming my first OpenGL game, and my first game in C for that matter and I'm not exactly a C expert. The game is a super graphically spiffed up version of Tetris. Only problem is the framerates are really inconsistant so the blocks never fall at the same rate. My question is, how do I get the number a milliseconds that have gone by between frames so I can know how much the blocks should have fallen in a given frame? Or is there another method to regulating speed?
Pre-emptive multi-thanking,
-Holmes
Pre-emptive multi-thanking,
-Holmes
In OS 9?
in Carbon, iirc, GetCurrentEventTime(void) will return you a float equal to the number of seconds sinceÖ umÖ startup or something? You'll be subtracting event times from each other to get elapsed time, though, so it doesn't really matter.
However, you can also set up a timer to fire at regular intervals - see the Carbon Event Manager docs for more details.
However, you can also set up a timer to fire at regular intervals - see the Carbon Event Manager docs for more details.
I'm in OSX. The game is Cocoa, though I'm not programming in Cocoa (arg, confusion!). I guess I could change it to carbon extremly easily though since its just standard OpenGL, C, and Glut. I'll try that function you suggested...
If you're using GLUT, glutGet(GLUT_ELAPSED_TIME) will return you the number of milliseconds since your program started. That's probably the most appropriate function to use.
IIRC, it starts counting when glutInit is called, but I could be wrong there. Anyway, as w_reade said, you'll be subtracting the numbers from each other, so it doesn't really matter when it starts
IIRC, it starts counting when glutInit is called, but I could be wrong there. Anyway, as w_reade said, you'll be subtracting the numbers from each other, so it doesn't really matter when it starts
Awesome, thanks.
I've also found a nice <time.h> function called clock(). It just returns the number of clicks (I did printf("%d",CLOCKS_PER_SEC); and figured out there are 100 clicks per second btw) since the program began. Both that and the glut function will suit my needs well. I think I'll use the glut one for better accuracy though.
I've also found a nice <time.h> function called clock(). It just returns the number of clicks (I did printf("%d",CLOCKS_PER_SEC); and figured out there are 100 clicks per second btw) since the program began. Both that and the glut function will suit my needs well. I think I'll use the glut one for better accuracy though.
clock() doesn't do what you want. It returns the number of ticks your process has used, not that have elapsed. That is, if your game is using 33% of the CPU time, three wall-clock seconds will have elapsed by the time clock() says one has.
The number of clocks per second varies across operating systems, too. You should use CLOCKS_PER_SEC rather than 100 in your source code. It was about 60 on Mac OS 9, for example.
There is no ANSI C function which provides high-resolution wall-clock timing. gettimeofday is POSIX, GetCurrentEventTime is Carbon, +[NSDate timeIntervalSinceReferenceDate] is Cocoa.
The number of clocks per second varies across operating systems, too. You should use CLOCKS_PER_SEC rather than 100 in your source code. It was about 60 on Mac OS 9, for example.
There is no ANSI C function which provides high-resolution wall-clock timing. gettimeofday is POSIX, GetCurrentEventTime is Carbon, +[NSDate timeIntervalSinceReferenceDate] is Cocoa.
Thanks again, I used the glut function you told me and it works great. Now it doesn't look like its running dog slow when framerates drop from 600fps to 60fps, smooth all the way around
.
I'm loving OpenGL! My tetris game is almost playable.
-Holmes
.I'm loving OpenGL! My tetris game is almost playable.
-Holmes
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| Managing nested glPolygonOffset usage | TomorrowPlusX | 3 | 3,695 |
Jun 6, 2005 08:36 AM Last Post: TomorrowPlusX |
|

