Constant Framerate

General FreeBASIC programming questions.
Post Reply
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Constant Framerate

Post by mambazo »

Not a major issue but if there is a way around it i'd love to know.

A small test game runs the same speed on both Linux and Windows, but is slightly jumpy. the average Framerate is constant but the actual framerate fluctuates visibly.

Does anyone know a method to keep everything running perfectly smooth?
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

Well, in QB, TIMER was limited to 18.2 ticks a second, and SLEEP was done in seconds. However, FB's timer is kickass, and sleep operates in milliseconds. So you can do something like this:

Code: Select all

Dim As Double now
Dim As Double oneFrame
oneFrame = 1/60 '1/60th of a second

Do While gameIsRunning
    now = Timer
    ' Do Stuff

   If Timer < (now + oneFrame) Then Sleep ((now + oneFrame) - Timer) * 1000
Loop
At least I think that works. Completely untested.
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

I'm using a very similar approach allready, please take a look at the code

www.langfordtavern.com/dev/media/file/geoduel.zip

when run, its not too bad, but it isn't really smooth flowing
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

Well, my guess would be that it's running at 23fps. Fine for movies, in all their motion blurring (actual movies are 24fps), but bad for pixel-by-pixel movement in video-games.

It's running plenty smooth over here, in terms of ships moving at a constant rate.
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

True, I'll up it to 60FPS, still have to figure out how to have the FPS effect the actual movement formulas such that if i ever change the FPS, it effectivly runs the same speed (ships move same distance per time).
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

Well, you could always rate speeds in pixels per seconds, and then do the necessary math with dt.
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

wow, thats exactly what i was just doing. you read my mind :D
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

on a side note, how do i display a very small DOUBLE in non exp form? - so i can make better sense of it when its changing 60 times a second?

im timing the actual game code execution, without the compensatory delay code
jofers
Posts: 1525
Joined: May 27, 2005 17:18

Post by jofers »

good ol' sprintf, from crt.bi, should help you :)

Code: Select all

#include crt.bi
dim number as double
dim somestring as string*5
number = 23.3582930485902385#

sprintf strptr(somestring), strptr("%5f"), number
print somestring
sleep
mambazo
Posts: 652
Joined: Jul 17, 2005 13:02
Location: Ireland
Contact:

Post by mambazo »

thanks a bunch :)
Post Reply