Second-Based FPS Timer (Macro)

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
anonymous1337
Posts: 5494
Joined: Sep 12, 2005 20:06
Location: California

Second-Based FPS Timer (Macro)

Post by anonymous1337 »

D.J.Peters posted this a long while back.
http://www.freebasic.net/forum/viewtopi ... 4741#44741

I've decided to make it a macro (untested). Only updateTimer() once per frame. Get FPS through getFPS. FPS defaults to 60.

Code: Select all

'' MACRO
dim shared as integer __globalFPS = 60
dim shared as integer __frames = 0
dim shared as integer __calcFPS = 60
dim shared as double __curTime = 0
dim shared as double __lastTime = 0
#define setFPS(_fps_) __globalFPS = _fps_
#define getFPS __calcFPS
#macro initTimer()
    __lastTime = timer()
#endmacro
#macro updateTimer()
    __frames += 1
    if( __frames >= __globalFPS ) then
        __curTime = timer()
        __calcFPS = __frames/(__curTime - __lastTime)
        __lastTime = __curTime
        __frames = 0
    end if
#endMacro


'' MAIN
while( len(inkey() ) <> 0 )  '' clear stray input before main loop
wend

initTimer() '' to prevent ridiculously high/low FPS
while( len(inkey() ) = 0 )  '' hit any key to exit
    screenlock()
        cls()
        print "[ANY KEY]=quit FPS:" & Str( getFPS )
    screenunlock()
    updateTimer()
wend
Now you only have to call timer() once per second :)
Post Reply