timer events

BlitzMax Forums/BlitzMax Programming/timer events

dan_upright(Posted 2006) [#1]
i'm trying to handle my game logic independently of frame rate by hooking timer events but it occasionally jerks as if the ticks are being delayed for a second then a bunch of them process at once

i'm calling PollEvent() in my main loop because it seems to be needed to make the event hook fire - although i thought that using hooks removed the need to use the event queue my hook gets no events unless i poll for them

am i missing something obvious to do with hooking? and if i should be calling pollevent to make events fire, does it make all the queued events fire their hooks or just the first in the queue?


puki(Posted 2006) [#2]
Welcome back.


dan_upright(Posted 2006) [#3]
thanks


tonyg(Posted 2006) [#4]
Have you got any sample code?
I would think you could use waitevent() and still have events fired.


dan_upright(Posted 2006) [#5]
the source is here (run test.bmx and use wasd to move)
http://www.lipteeth.demon.co.uk/downloads/stuff/gamecore.zip

using that source i get stuttering of the movement - changing
PollEvent()

to
While PeekEvent()
  PollEvent()
Wend

lessens the stuttering quite a bit but it's still there and in any case, i fail to see the point in hooks if i have to keep checking the event queue myself, so my guess is i'm missing something (if i remove the pollevent from the loop the program receives no events)


H&K(Posted 2006) [#6]
http://www.blitzbasic.com/codearcs/codearcs.php?code=1721

With this My program just sits in a
While Waitevet()
wend
loop, and I have no jumping


dan_upright(Posted 2006) [#7]
i can't use waitevent because my aim is to have the rendering going on at whatever refresh rate the user has chosen (or as fast as possible) while the game logic updates at a set frequency

although looking at your entry in the code archives i've realised that changing my "repeat .. forever" loop to a "repeat .. until keyhit(whatever)" one removes the need for pollevent (and reduces the stuttering somewhat) - though that's not exactly useful because long term i won't want the main loop to exit on a single keypress


H&K(Posted 2006) [#8]
Well if that is your aim, then think for a moment.

The gamelogic has to sit in a Time frame, that has already had the rendering done. This means that whenever the Gamelogic is called, the screenrender has to wait while it finishes. Hence you will always get a stutter, because no matter what you do, the Gamelogic is taking time from the framelogic.
The framelogic needs to be fast enough so that it has spare time everyframe to do the gameLogic, even if you dont do it every frame.
What needs to be done is that the framlogic needs to know that the gamelogic has just had a pass, and posibly as a result the framelogic needs to delibratly "miss" a frame or two


dan_upright(Posted 2006) [#9]
all that test code is doing is getting key input and moving a word around the screen, so i highly doubt the problem is that it's taking too long to process

if anyone has any example code of using hooks with timers in graphics mode, i'd really appreciate a look to see how it's done


H&K(Posted 2006) [#10]
http://www.blitzbasic.com/Community/posts.php?topic=61428#686821

Halfway down. Jim Ts code.

Edit: From your post below, I see this wanst relevent. Sorry


dan_upright(Posted 2006) [#11]
i can't see anything in that thread about addhook or timers?


dan_upright(Posted 2006) [#12]
well i've been playing about with this and as far as i can tell the problem is just that the timer sometimes seems to stall for a second

i'm running a graphics mode at 75hz and if i update using timer events (also running at 75hz, just for the sake of easy comparison) i get the odd jerk whereas removing the timer and just calling the update function in the main loop runs super smooth, though obviously isn't much of a solution for supporting multiple refresh rates

so unless anyone knows anything about how to fix timer events i'll have to do something else


skidracer(Posted 2006) [#13]
The problem with the timer is it's millisecond accurate so a 75hz timer is going to drift .3333 milliseconds every tick. From my calculations without recalibrating the timer yourself regularly you will get a glitch every 40 frames.

The other problem with graphics card refresh rates is they too have margins of error based on the timing spec of the monitor and the design of the card.

IMHO If you want super smooth sync lock your updates to your video, keep it simple etc.


dan_upright(Posted 2006) [#14]
i still get the same problem with the timer at 100hz, which should be fine by my reckoning, but meh, i'm doing it differently now before i end up spending days messing about with it for no real reason =]

thanks for the help