Simple game timer - which method to use?

Started by
2 comments, last by kop0113 11 years, 8 months ago
I have a simple game loop for a pacman clone, and want to add a timer.

After doing some research there are a few options. For example, I have read about a Win32 timer, a WIndows forms timer, and a system threading timer. With the latter the principle appears to be embedding the game loop inside timer events: http://csharptips.wordpress.com/2010/08/24/thread-timer/

This would imply that to count for a number of seconds I could record the number of game loop iterations.

But I wonder, what do people recommend? And what have people used? It's my first look into this subject, so any comments would be useful.

Cheerscool.png
Advertisement
Edit: Oops. My response was for C++, not C#.
In the end I used the thread timer method. It's was very easy indeed.

secondsPassed = 0; // Reset the time
timer = new System.Threading.Timer(new System.Threading.TimerCallback(SecondHasPassed), null, 0, 1000);

private void SecondHasPassed(object obj)
{
secondsPassed++;
}

Problem solved smile.png
In c#, int is not thread safe.

It is actually a Nullable<> struct consisting of an integer and a boolean.

http://stackoverflow...int-thread-safe

So if you do use an integer in this way, perhaps put a mutex lock around it. However, I probably recommend using the Winforms timer (System.Windows.Forms.Timer) or the timer provided with the c# language (System.Timers.Timer)
http://tinyurl.com/shewonyay - Thanks so much for those who voted on my GF's Competition Cosplay Entry for Cosplayzine. She won! I owe you all beers :)

Mutiny - Open-source C++ Unity re-implementation.
Defile of Eden 2 - FreeBSD and OpenBSD binaries of our latest game.

This topic is closed to new replies.

Advertisement