Getting the correct framerate.

BlitzMax Forums/OpenGL Module/Getting the correct framerate.

Angus(Posted 2010) [#1]
I'm very new to OpenGL and though I've made some progress, my game's going too fast!

Why is it that this code is doing hundreds of frames a second?

SuperStrict

SetGraphicsDriver GLGraphicsDriver()

Graphics(640,480,32,60,GRAPHICS_BACKBUFFER) '| BGL_FULLSCREEN
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(-1.0,1.0,-1.0,1.0,0.0,1.0)
glEnable(GL_BLEND)
glEnable(GL_TEXTURE_2D)
glEnable(GL_SCISSOR_TEST)
glClear(GL_COLOR_BUFFER_BIT)

Local sms:Int=MilliSecs()
Local frms:Int=0

Repeat

    Flip(-1)
    glClear(GL_COLOR_BUFFER_BIT)
    frms:+1
    
Until KeyDown(KEY_ESCAPE)

Local ems:Int=MilliSecs()
Local tms:Float=ems-sms
Local secs:Float=tms/1000.00
Local fps:Float=Float(frms)/secs

Print
Print "mlllisecs:"+tms+"   seconds:"+secs+"   frames:"+frms+"  FPS:"+fps

EndGraphics
End


Surely it should be doing 60? That's what I request in the graphics command.

I'm running it on an eeePC, so I think it's in intel chip.

Would be grateful for any advice!

Last edited 2010


xlsior(Posted 2010) [#2]
Do you have your videocard drivers set to override the requested framerates?

There's typically a setting in there that tells it to run all video as fast as it can, without waiting for vertical blanks.


Angus(Posted 2010) [#3]
I haven't done that, but this code:

SuperStrict

Graphics(640,480,32,60)


Local sms:Int=MilliSecs()
Local frms:Int=0


Repeat

    Flip(-1)
    Cls
    frms:+1
    
Until KeyDown(KEY_ESCAPE)

Local ems:Int=MilliSecs()
Local tms:Float=ems-sms
Local secs:Float=tms/1000.00
Local fps:Float=Float(frms)/secs

Print
Print "mlllisecs:"+tms+"   seconds:"+secs+"   frames:"+frms+"  FPS:"+fps

EndGraphics
End


Does seem to give 60fps without me making such changes.

I'd certainly prefer if I could determine the framerate (only at 60 of course) through the display without an end-user having to change their settings.


Angus(Posted 2010) [#4]
Actually I now see that setting the working example to use the glmax2d driver also doesn't limit it.

Can I take it that the default 2D driver is D3D then? Oh dear.

Last edited 2010


Angus(Posted 2010) [#5]
OK, so in my main game code it seemed to be occasionally unpredictable. Sometimes doing the correct rate, sometimes not. Then it settled into being predictably wrong. Hunting round the forums I noticed a topic about how alt-tabbing seemed to alter the framerate.

Alt-tabbing my code does indeed occasionally give me the desired framerate. Oh my head.

I'm keen to use openGL and to use a fixed refresh rate of 60. I know I can switch to other systems but using box2D at 60 fps, it just seems natural to lock the refresh rate to one that all monitors will handle without tearing.

Am I going to have to give up on that idea? I feel too dim to start raking through the source.

Last edited 2010


beanage(Posted 2010) [#6]
Considering the time of the last post, I sincerely hope you found a solution by now.

If not, this will work:
SetGraphicsDriver GLMax2dDriver()

Global myRefreshTimer:TTimer = CreateTimer(60) 'Make a refresh timer ticking 60 times per second
Global myGraphics:TGraphics = Graphics(1024, 768)

Repeat
	Cls
	'Custom Drawing Code goes here
	WaitTimer myRefreshTimer 'This will delay execution until the refresh timer ticks
	Flip
Until AppTerminate() Or KeyHit(KEY_ESCAPE)
End



Angus(Posted 2010) [#7]
Thanks, man! I have been playing with different timing plans, but the one you posted is as good as any, really. It's the inconsistency of my problem I can't bear, and I'll never get V-Syncing :(.

Nevermind, I'll lock it to 60 with a different solution and hope that I can make some way of knowing whether my code, running on another machine, is managing to lock to a 60 hertz display. If it is then I'll use the framerate, if not I'll use the timer. Thanks again!

Last edited 2010


Angus(Posted 2010) [#8]
Also, when I say "a different solution" I mean one other than getting Flip to do it. I think your solution really is the best for me.


beanage(Posted 2010) [#9]
Glad to be of help :)