Gage Timer

Hellow,
I am using Gage Timer for my game loop, here is what i mean :


gameManager.update();
render();
sleep = gt.getTicksPerSecond()/60; //60fps
ticks = gt.getClockTicks();
gt.sleepUntil(sleep+ticks);
ticks = ticks+sleep;

The code shows i have a class “gameManager”. Now if i wanted to create a new Timer (gmt) which is created “gameManager” for its indepedent events, will that timer be affected by the “gt” shown above ??

Basically, will the new timer run in a new Thread independent of gt or not ?
Thx

As far as I understand, the time doesn’t actually use any threads itself. If you create one instance, and call sleepUntil() using two seperate threads they’ll both wait independantly of each other…

I keep wondering whether GAGETimer would be more convinient as a singlton or a bunch of static methods.

Kev

[quote]I keep wondering whether GAGETimer would be more convinient as a singlton or a bunch of static methods.
[/quote]
Nope. You might want to use multiple timers. For example, a new AdvancedTimer could be intialized and used for a “countdown” timer independent of the frame rate timer. (e.g. Mario dies when the timer hit 100 seconds)

Ah, good point :slight_smile:

If the problem is having more timers running…

No need to create other timers for misc events…

I’m using a simple algorithm:

  1. Calculate delta millisec elapsed since the last logical frame.
  2. Subtract this time slice to all “task” pending.
  3. When a task expire you generate an event.
  4. If the task is ciclical you may want to reset the delay for that task

Pack this concept in a Class able to handle more task at the same time and you’ve got a perfect timer for all your game’s event

Hope it helps.

Well yes, you could do that. In fact that’s basically what GAGETimer does internally. It’s just as easy (any probably a bit cleaner) to just use another instance of the timer. All instances are working off the same timing source, but the individual instances are dealing with the naggling details for you. :slight_smile:

[quote]Well yes, you could do that. In fact that’s basically what GAGETimer does internally. It’s just as easy (any probably a bit cleaner) to just use another instance of the timer. All instances are working off the same timing source, but the individual instances are dealing with the naggling details for you. :slight_smile:
[/quote]
Yes it’s a good solution…

But you can generate Events and make entity classes receive them simply implementing a EventListener interface…
In my case tasks have a name string so it’s simple to create em and receive their messages.

For example in CastleTrouble I have used many of this simultaneous tasks:

  • “Hurry up!” (only in beta) after 60 secs it alters the enemies’ speed.
  • Intro delays and events…
  • One cyclic to make a hourGlass run (only in beta)
  • Delay before opening doors
    and many others…

Another great pro of this method is that other “sources” can generate events, my library can generate events:

  • When a sprite change a frame during an animation
  • At the end of an animation (this is really useful to concatenate animations or to use animations as precalculated delays)
  • At the end of a MOD or a MIDI
    and so on…

So having a Task based timer is a great thing expecially if you have (or want) also an Event driven system.