Jump to content
  • Advertisement
Sign in to follow this  

Help with game loop time control

This topic is 958 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

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) {
		for (int x = 0; x < c.Length; 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;

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


			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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!