Sign in to follow this  

(C#) Timing loop for 2D game

This topic is 4593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 = ~33

public 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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 4593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this