title: constant framerate Gameloop using System.Diagnostics.Stopwatch and delta time in C# from tutorial
body: <p>I picked up a tutorial on how to do a game loop where the framerate is always the same no matter how big the rendering space gets or the speed of your CPU.  It involves using a time interval, but I've poured over this a hundred times and I can not see why my version does not work, so I don't know if it's mine or if they got it wrong.</p>
  
  <p>the original tutorial can be found here
  <a href="http://www.dreamincode.net/forums/topic/140540-creating-games-with-c%23-part-2/" rel="nofollow">http://www.dreamincode.net/forums/topic/140540-creating-games-with-c%23-part-2/</a></p>
  
  <pre><code>namespace TileGame
  {
      public partial class GameForm : Form
      {
          HiResTimer gameTime = new HiResTimer();
  
          long startTime;
          long interval = (long)TimeSpan.FromSeconds(1 / 30).TotalMilliseconds;
  
          Graphics g;
          Graphics imageGraphics;
  
          Image backBuffer;
  
          int clientWidth;
          int clientHeight;
  
          Rectangle image = new Rectangle(0, 0, 40, 50);
          Point direction = new Point(1, 2);
  
          public GameForm()
          {
              InitializeComponent();
              this.DoubleBuffered = true;
              this.MaximizeBox = false;
              this.FormBorderStyle = FormBorderStyle.FixedSingle;
              this.ClientSize = new Size(320, 240);
  
              clientHeight = this.ClientRectangle.Height;
              clientWidth = this.ClientRectangle.Width;
  
              backBuffer = (Image)new Bitmap(clientWidth, clientHeight);
              g = this.CreateGraphics();
              imageGraphics = Graphics.FromImage(backBuffer);
      }
  
      public void GameLoop()
      {
              gameTime.Start();
              while (this.Created)
          {
                  startTime = gameTime.ElapsedMiliseconds;
                  GameLogic();
                  Render();
                  Application.DoEvents();
                  while (gameTime.ElapsedMiliseconds - startTime &lt; interval) ;
              }
          }
  
          private void GameLogic()
          {
              image.X += direction.X;
              image.Y += direction.Y;
  
              if (image.X &lt; 0)
              {
                  image.X = 0;
                  direction.X *= -1;
              }
  
              if (image.Y &lt; 0)
              {
                  image.Y = 0;
                  direction.Y *= -1;
              }
  
              if (image.X + image.Width &gt; clientWidth)
              {
                  image.X = clientWidth - image.Width;
                  direction.X *= -1;
              }
  
              if (image.Y + image.Height &gt; clientHeight)
              {
                  image.Y = clientHeight - image.Height;
                  direction.Y *= -1;
              }
  
          }
  
          private void Render()
          {
              imageGraphics.FillRectangle(new SolidBrush(Color.Black),
                                      this.ClientRectangle);
              imageGraphics.FillRectangle(new SolidBrush(Color.Blue), image);
  
              this.BackgroundImage = backBuffer;
              this.Invalidate();
  
          }
      }
  }
  </code></pre>