View Full Version : Frame limiter
Michael Clease
13-02-2008, 22:57
Ive been trying to find a way to limit the framerate to around 50 - 60 fps and i thought this was a simple cheat.
FrameRate = VAL(TBGL_GETFRAMERATE)
IF FrameRate < 50 THEN TimeDelay -= 1000
IF FrameRate > 60 THEN TimeDelay += 1000
For t = 1 TO TimeDelay : NEXT
What do you think?
Petr Schreiber
13-02-2008, 23:23
Hi Abraxas,
to fix framerate around 50/60, use
tbgl_UseVsync(1) ' -- Before main loop
' -- or maybe better ?
FrameRate = TBGL_GetFrameRate ' -- No need for VAL()
TBGL_UseVSync IIF( FrameRate < 55 , 0, 1 )
The FOR/NEXT approach can be tricky and very CPU dependant.
Thanks,
Petr
Michael Clease
14-02-2008, 01:12
Petr that doesnt work, well if it does its not the way I wanted.
my way self regulates so a slower system should compensate.
Try this
'
' TBGL Script Skeleton
'
Uses "TBGL"
Dim hWnd As Dword
DIM FrameRate AS DWORD
hWnd = TBGL_CreateWindowEx("TBGL script - press ESC to quit", 640, 480, 32, 1)
DIM hFont AS DWORD = TBGL_FontHandle("Courier New", 72)
TBGL_BuildFont hFont
TBGL_ShowWindow
TBGL_GetAsyncKeyState(-1) ' Resets status of all keys
tbgl_UseVsync(1) ' -- Before main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate ' -- No need for VAL()
TBGL_UseVSync IIF( FrameRate < 55 , 0, 1 )
TBGL_RenderMatrix2D
TBGL_ClearFrame
TBGL_Color 255,255,255 ' Setting color this way affects even font
TBGL_PrintFont FrameRate, 100, 100, 0 '
TBGL_DrawFrame
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
TBGL_SetWindowTitle( hWnd, FrameRate)
Wend
TBGL_DestroyWindow
ErosOlmi
14-02-2008, 01:28
Abraxas,
here it seems quite stable.
Sometimes goes up to over 500 for a flash time.
Ciao
Eros
Michael Clease
14-02-2008, 01:39
Eros, so you are getting framerates of around 50-60 fps on that last script?
on my laptop it runs around 200 fps only varying by around +- 2 fps.
if you substite my method for the other one it should hover around 60 fps +- 1 fps.
Michael Hartlef
14-02-2008, 06:10
Why would you want to limit the framerate in the way you do? What is the advantage over delta time movement and VSYNC ON?
Michael Clease
14-02-2008, 10:07
Couple of advantages remember I am only doing 2d work.
Animation - Using a constant for the framerate makes it easier to framecount for the 8,24 fps
level scrolling - should maintain a smoother scroll
there are more but its early and my little brain is still a sleep.
VSYNC ON would be good if you had someway of making your program know that it was a new frame or better still would be to define a scanline to wait until the do all your game routines.
Michael Hartlef
14-02-2008, 13:51
Thanks for the explaination. Do you use delta timing while calculating movement/scrolling? If you don't then once the framerate drops below 50 because of processing time, every movement in your game will slow down also.
Petr Schreiber
14-02-2008, 22:22
Hi all,
TBGL_UseVSync will not work in case driver forces it off, in all other cases it worked well on all PCs I tested :'(
Abraxas, your technique works pretty well, I was tired yesterday, so my reply was not 100% correct.
Anyway... Try to scale any movements, rotations using <quantity>/FrameRate, as Mike suggests.
Higher the framerate, better the feeling.
Making script for fixed framerate is very risky.
Bye,
Petr
ErosOlmi
14-02-2008, 22:33
Next thinBasic preview will have two new functions regarding timers:
HiResTimer_Init: used to initialize internal timer data. To be fired only once at the beginning.
HiResTimer_Get: used to get microseconds elapsed
Mainly they are wrappers of QueryPerformanceCounter and QueryPerformanceFrequency functions but much easier to be used.
Hope they will help you on better measure time.
Ciao
Eros
Petr Schreiber
14-02-2008, 22:40
Hi Eros,
sounds like powerfull addition, thanks!
Petr
ErosOlmi
14-02-2008, 22:46
Here an example:
hirestimer_init '---Init HiRes timer
dim t1, t2 as quad '---Define 2 quads
t1 = hirestimer_get '---Get first
t2 = hirestimer_get '---Get second
msgbox 0, str$(t2-t1) '---Result is the min time we can measure
In my test I get between 6 to 10 microseconds as the min time it can be measured. But, because of the interpretative nature of thinBasic, it will depend on CPU speed. Anyhow it should be much better than TIMER function that measure milliseconds.
Ciao
Eros
Very nice thread with lots of good ideas and implementation, thanks guys for the work!
ErosOlmi
14-02-2008, 22:53
Any better or more clever idea on how measure time, is welcome and I will take into account for development.
Those functions must be Core functions in order to influence script execution at the minimum necessary.
Ciao
Eros
Michael Clease
14-02-2008, 23:58
Ive being reading lots of articles about opengl and timing it a shame windows doesnt support glxwaitsync and variants of it anymore.
the best way to time rendering is to do the following so ive read.
START TIMER
glFLUSH()
Generate scene()
glSWAPBUFFERS()
glFINISH()
Stoptimer
that will give you an idea of how long it takes to render your scene.
Petr Schreiber
15-02-2008, 11:50
Hi Abraxas,
that sounds reasonable.
Only thing I would change is to put glFINISH() before glSWAPBUFFERS() and maybe put out the flush.
Bye,
Petr