|
Game running much smoother WITHOUT timer |
jeronimo
Member #7,645
August 2006
|
Hey there, However, graphics aren't very smooth, and they appear MUCH smoother when I comment out all the timer stuff. I tried double buffering, page flipping and triple buffering, same story for all techniques. I think the main problem is that, one time, the graphics update shows, say, 5 steps of ball movement, and the other time, it shows just 3 steps. This makes the movement quite jerky. Whereas without timer, every frame update is just one step of game logic, and although there are somme missed frames, I found it impossible to detect them at sight (only noticed this by comparing counters). So, what do you think, is it a Very Bad Thing not to use a timer, and adjust game speed based on the refresh rate of the user (obtainable by get_refresh_rate())? |
George Foot
Member #669
September 2000
|
You do get smoother results if you accurately sync to the refresh rate of the monitor, but it's very hard to do reliably on modern systems. Calling vsync every loop gets you half of the way, but you'll get occasional frame drops when either your code takes too long to execute, or Windows decides to click the hard disk drive for no apparent reason. If you don't care about this then it's OK to just use vsync, though as you say you might want to adjust your game's processing according to what the actual refresh rate is, so it doesn't run faster or slower depending on display settings. I wouldn't trust get_refresh_rate too much - it might be worth timing it yourself. It's worth mentioning that in many games it's quite hard to make the logic work in that way. It does depend on the type of game though. It also makes your game less repeatable - things like recording replays can be more awkward. You can still work around the stuttering from missing the occasional frame if you have access to accurate timing routines. An Allegro timer firing at twice the frame rate might be enough. So long as you've got enough to tell how many frames it's been since you last updated the display. |
jeronimo
Member #7,645
August 2006
|
Thanks. Still I wonder, why does a typical modern game (like Call of Duty 2) run much smoother on my laptop than this *** pong game... Such games also use timers I suppose... What is a typical timer interval for such games? |
Jonatan Hedborg
Member #4,886
July 2004
|
I would say that most modern games (3d games mosly) use some form of delta-timing. Oh, and because the allegro timer is quite inaccurate on windows, i would recomend using queryPerformanceCounter instead This is however not platform independant.
|
George Foot
Member #669
September 2000
|
jeronimo, I didn't mean update your game twice per frame - I just meant that with a timer ticking twice per frame you can tell easily whether you missed a vsync. e.g. you'd normally expect the timer to advance either one or two ticks; if it advances three or four ticks, you need to run your update twice; five or six, three times. You can do something similar if the timer is accurately running at the same rate as the vsync, but doubling the timer frequency like this makes you less dependent on this accuracy. Actually I think you'd want your timer running somewhat under twice per frame, but certainly more than once per frame. It still breaks down if you don't accurately know the vsync rate, but it should be good for compensating for skipping just one or two frames. |
Kris Asick
Member #1,424
July 2001
|
You will want to get used to using timers because without them there's no way to sync your game to everyone's computer. Yes, VSync can work, but the refresh rate of your monitor will not necessarily be the same on someone else's, and you can't just set a timer to the refresh rate because get_refresh_rate() does not work on every system. (For instance, mine, at least, the last time I tried to use it, which would be Allegro v4.0.2) The jerky movement you're experiencing can be helped if you average the change in time between frames. For instance, if you're using a 1 ms timer to time the game to milliseconds, you can keep two timer variables, then every time you draw a new frame you switch which timer to update and make your new timing value the average of the two. For instance:
Then you simply call Update_Frame() at the end of every game frame to draw your double-buffer to the screen and update the timer. You'll also probably need to lock *tref, timer1 and timer2 using the LOCK_VARIABLE() command before you initialize the timer Timer_Routine() as well. Averaging for more than two frames might work better, but probably won't. If you sample too many frames at a time you'll get speed alterations when the framerate changes for longer than a split second. This code is totally untested and written just now, but I believe it should work. --- Kris Asick (Gemini) --- Kris Asick (Gemini) |
Todd Cope
Member #998
November 2000
|
Quote: i would recomend using queryPerformanceCounter instead Ditto. I think Linux has an equivalent high-precision timer as well. Look into these if you like to use a fixed logic rate. Even if the timer and refresh don't match up it still gives the illusion of smoothness if the timer is precise. It's kind of like how they record movies which are 24 FPS and you can play them back on your TV which is 60 FPS and it still looks right. |
Tobias Dammers
Member #2,604
August 2002
|
TV is 60 Hz interlaced. A TV (at least a CRT one) draws even scanlines on one pass, odd ones on the next, giving you a frame rate of 30 Hz. The interlaced approach is used to prevent flicker. Nowadays TV screens display at a higher rate, but that doesn't change the actual frame rate of the TV signal. The linux high-precision timer is gettimeofday(). You don't need a delta-timer per se; fixed-delta usually works well for 2d games, provided you choose a sensible delta. It should be significantly higher than the frame rate, and it should be a multiple of as many refresh rates as possible. 360 fps works well for 60, 72 and 120. 300 works for 50, 60, 75 and 100. --- |
Jonatan Hedborg
Member #4,886
July 2004
|
While 360fps seems like a very nice logic update rate, it leaves only 2.77ms per logic step. That may be too little. If you have some form of AI with pathfinding, a bunch of particles and some fancy sorting perhaps, that will regularly take more than 2.77ms, which will lead to skipped frames, yielding a stuttering gameplay.
|
Tobias Dammers
Member #2,604
August 2002
|
Pathfinding is a b1t$%^ indeed, but there's no need to pathfind every single frame. Sorting is usually not much of an issue because of time coherency (the list from the last frame is very likely to be almost sorted already). --- |
Todd Cope
Member #998
November 2000
|
Quote: TV is 60 Hz interlaced. I am aware . My point is still valid. |
Mr. Big
Member #6,196
September 2005
|
You might find this useful: http://www.gaffer.org/articles/Timestep.html |
jeronimo
Member #7,645
August 2006
|
Thank you all. |
|