Sign in to follow this  
WilliamLycan

Help with game loop time control

Recommended Posts

Hello, I'm newbie in programming, I', working on some console text games and need help with this: The game loop  runs too fast, how can I put a wait command that lets the cpu free for other tasks within the time span it's waiting? And how can I make my loop run at 60hz? It's just an animation to test the cycles per second. Here's my sample code:

using System;


class Test {
	static void swap(ref char a, ref char b) {
		char temp = a;
		a = b;
		b = temp;
	}

	static void draw(char[] c) {
		Console.Clear();
		for (int x = 0; x < c.Length; x++) {
			Console.Write(c[x]);
		}
	}
	
	static void Main() {
		char[] bar = new char[] {'?', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
		bool done = false;
		int x = 0;
		int y = 1;
		//?
		int time = 0;
		int before;
		int now;
		
		//Hides the cursor
		Console.CursorVisible = false;

		//System.Timers.Timer timer = new System.Timers.Timer();

		System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();

		ConsoleKeyInfo key;

		before = (int)timer.ElapsedMilliseconds;

		while(!done) {

			now = (int)timer.ElapsedMilliseconds;

			time = now - before;
			
			//Process inputs
			if(Console.KeyAvailable) {
				key = Console.ReadKey(true);
					switch (key.Key) {
						case ConsoleKey.Escape:
						done = true;
							break;
				}
			}

			//Process logics
			swap(ref bar[x], ref bar[y]);
			x++;
			y++;
			if(x == bar.Length) {
				x = 0;
			}
			if(y == bar.Length) {
				y = 0;
			}

			//Render
			draw(bar);

			before = now;

			
		}
	}
}
Edited by WilliamLycan

Share this post


Link to post
Share on other sites

Hey WilliamLycan

 

what you need is the delta time between two of your 'render' cycles. This is the time span that determines the time passed between two frames so you will normaly do something like

rotation = rotation * deltaTime;

to avoid framerate based issues.After lloking at your code I might be wrong but I think milliseconds might be too rough to get correct time frames from it. I remember timing issues when I was working on C# game engine prototype when this resulted in 0 always caused by not enougth runtime passed.

 

My Implementation currently looks as this, I call it at the top of every frame when rendering something

cycle_time_elapsed = cycle_time_init;
//update cycle_time_init here by your rpefered timing function

cycle_time_elapsed = cycle_time_init - cycle_time_elapsed;

Share this post


Link to post
Share on other sites

A common method of achieving constant-rate logic updates (independent of rendering speed) is the fixed-timestep gameloop. This is an effect you can also achieve by enabling vsync (though this is generally not recommended).

 

In C#, to get the precise timing you'd want to implement a fixed-timestep you're probably going to have to use System.InteropServices to import some dlls to enable you to check the cpu tick-rate.

Share this post


Link to post
Share on other sites

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