Want more features on Pastebin? Sign Up, it's FREE!
Guest

Game loop test

By: a guest on Mar 19th, 2013  |  syntax: C#  |  size: 2.79 KB  |  views: 32  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. using System;
  2. using System.Diagnostics;
  3. using Microsoft.Xna.Framework;
  4. using Microsoft.Xna.Framework.Graphics;
  5. using Microsoft.Xna.Framework.Input;
  6.  
  7. namespace TimingTest
  8. {
  9.     public class Game1 : Microsoft.Xna.Framework.Game
  10.     {
  11.         bool vsync = false;
  12.  
  13.         GraphicsDeviceManager graphics;
  14.         SpriteBatch spriteBatch;
  15.         Texture2D spinner;
  16.  
  17.         double theta = 0;
  18.         double spinSpeed = 0.005f;
  19.  
  20.         float screenWidth = 1280;
  21.         float screenHeight = 768;
  22.  
  23.         const double MILLISECONDS = 1000.0;
  24.         const double TARGET_SPEED = 60.0; //Logic tick rate in hertz
  25.  
  26.         const double dt = MILLISECONDS / TARGET_SPEED;
  27.  
  28.         double currentTime;
  29.         double accumulator = 0.0;
  30.         Stopwatch logicTimer = new Stopwatch();
  31.  
  32.  
  33.         public Game1()
  34.         {
  35.             graphics = new GraphicsDeviceManager(this);
  36.             Content.RootDirectory = "Content";
  37.  
  38.             graphics.PreferredBackBufferWidth = (int)screenWidth;
  39.             graphics.PreferredBackBufferHeight = (int)screenHeight;
  40.             graphics.SynchronizeWithVerticalRetrace = vsync;
  41.  
  42.             logicTimer.Start();
  43.             currentTime = logicTimer.ElapsedMilliseconds;
  44.  
  45.             this.IsFixedTimeStep = false;
  46.             //Setting IsFixedTimeStep to false makes Update and Draw run in lockstep as fast as your
  47.             //system can do go. This allows me to control the update timing using the accumulator loop.
  48.             //Using IsFixedTimeStep=true caps the framerate and introduces some other behaviors I'd like
  49.             //to avoid.
  50.         }
  51.  
  52.         protected override void LoadContent()
  53.         {
  54.             spriteBatch = new SpriteBatch(GraphicsDevice);
  55.             spinner = Content.Load<Texture2D>("Spinner"); //I'm using this image: http://i.imgur.com/5qkVH1d.png
  56.         }
  57.  
  58.         protected override void Update(GameTime gameTime)
  59.         {
  60.             if (Keyboard.GetState().IsKeyDown(Keys.Escape))
  61.                 this.Exit();
  62.  
  63.             double newTime = logicTimer.ElapsedMilliseconds;
  64.             double frameTime = newTime - currentTime;
  65.  
  66.             currentTime = newTime;
  67.             accumulator += frameTime;
  68.  
  69.             while (accumulator >= dt)
  70.             {
  71.                 theta = (theta + spinSpeed * dt) % (Math.PI * 2.0);
  72.                 accumulator -= dt;
  73.             }
  74.         }
  75.  
  76.         protected override void Draw(GameTime gameTime)
  77.         {
  78.             GraphicsDevice.Clear(Color.CornflowerBlue);
  79.  
  80.             spriteBatch.Begin();
  81.             spriteBatch.Draw(spinner, new Vector2(screenWidth / 2, screenHeight / 2), null, Color.White, (float)theta, new Vector2(spinner.Width / 2, spinner.Height / 2), Vector2.One, SpriteEffects.None, 0f);
  82.             spriteBatch.End();
  83.         }
  84.     }
  85. }
clone this paste RAW Paste Data