(C#) Timing loop for 2D game

Started by
4 comments, last by Pipo DeClown 18 years, 11 months ago
Can any one give me a pointer to a C# resource on simple timing loops for 2D games or give me a hand. In my game loop i do several things that dont need to be done a zillion times a second ... i.e updating player positions and the screen. this is the basic contents of my game loop i need to nest away in some kind of timing: PlayerUpdate(ref PlayerPos); TileUpdate(ref ScreenPos); if(LastPlayerPos != PlayerPos) CollideTest(PlayerPos,ScreenPos); Draw(); LastPlayerPos =PlayerPos; Thanks, James.
Advertisement
Look at / use the DX Sample frame-works method of timing and dispatching. Basically all you need to do is check how much time elapsed, and if there isn't anything to do for a particular part because not enough time has gone by skip. Though often games need all the cycles they can get... and the more times something updates the better framerate you get.
Anything posted is personal opinion which does not in anyway reflect or represent my employer. Any code and opinion is expressed “as is” and used at your own risk – it does not constitute a legal relationship of any kind.
1) It's a better practice to use time-based updates instead of limiting the update-time. Why? Because if the update is called slower than you expect, then you get a laggy game. But if you were using time-based updates, the game would only look slower, but wouldn't lag.

2) You could basically use the .Net milliseconds to make your own timer. It's very simple, like this:
class Sample{private int CurrentTick;private int LastUpdate;private int UpdateFreq; // = 1000/30 = ~33public static void Main(){  ...  ...  while() // replace with your own mainloop  {    CurrentTick = System.Environment.TickCount.Milliseconds;    if(CurrentTick > LastUpdate + UpdateFreq)    {      LastUpdate = CurrentTick;      Update();    }  }}};


I hope you understand this relatively easy code.
Great stuff, thanks :)

I just had read different ways of doing time with no explination of why that method was chosen over the others.
With DXTimer, taking the diference between two measurements or adding events to a standard timer, etc.
So thanks for explaining the reasons for using the example code as well.... I'll give it a shot.
ok raninto a problem.....

System.Environment.TickCount does not countain Milliseconds :(
I mistyped, it's only 'System.Environment.TickCount'.

This topic is closed to new replies.

Advertisement