Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Runtime.InteropServices;
- using System.Threading;
- namespace Fixed_Timed_Step
- {
- class Program
- {
- static void Main(string[] args)
- {
- for (int i = 0; i < 10; i++)
- {
- ThreadStart ts = new ThreadStart(FixedLoop);
- Thread t = new Thread(ts);
- t.Start();
- Thread.Sleep(1000);
- t.Abort();
- }
- Console.WriteLine("Loop done.");
- Console.ReadKey();
- }
- static void FixedLoop()
- {
- System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
- sw.Start();
- long t = 0;
- long dt = 1000 / 50;// (long)0.01;
- long accumulator = 0;
- long currentTime = sw.ElapsedMilliseconds;
- int i = 1;
- while (true)
- {
- long newTime = sw.ElapsedMilliseconds;
- long frameTime = newTime - currentTime;
- currentTime = newTime;
- /*if (frameTime > 0.25)
- frameTime = (long)0.25; // Max*/
- accumulator += frameTime;
- while (accumulator >= dt)
- {
- Console.WriteLine("Update " + i);
- i++;
- accumulator -= dt;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement