Advertisement
Guest User

Untitled

a guest
Feb 15th, 2013
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7.  
  8. namespace Fixed_Timed_Step
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             for (int i = 0; i < 10; i++)
  15.             {
  16.                 ThreadStart ts = new ThreadStart(FixedLoop);
  17.                 Thread t = new Thread(ts);
  18.  
  19.                 t.Start();
  20.                 Thread.Sleep(1000);
  21.                 t.Abort();
  22.             }
  23.  
  24.             Console.WriteLine("Loop done.");
  25.             Console.ReadKey();
  26.         }
  27.  
  28.         static void FixedLoop()
  29.         {
  30.             System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  31.             sw.Start();
  32.  
  33.             long t = 0;
  34.             long dt = 1000 / 50;// (long)0.01;
  35.             long accumulator = 0;
  36.             long currentTime = sw.ElapsedMilliseconds;
  37.  
  38.             int i = 1;
  39.  
  40.             while (true)
  41.             {
  42.                 long newTime = sw.ElapsedMilliseconds;
  43.                 long frameTime = newTime - currentTime;
  44.                 currentTime = newTime;
  45.  
  46.                 /*if (frameTime > 0.25)
  47.                     frameTime = (long)0.25; // Max*/
  48.  
  49.                 accumulator += frameTime;
  50.  
  51.                 while (accumulator >= dt)
  52.                 {
  53.                     Console.WriteLine("Update " + i);
  54.                     i++;
  55.                     accumulator -= dt;
  56.                 }
  57.             }
  58.         }
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement