add performance counters to SneakGame. Fixes #33.
Also, suppress drawing until Draw() has not been IsRunningSlowly for two frames. This prevents janky behavior that happens during loading (I suspect while texture loading is happening, but I'm not sure?) GitOrigin-RevId: 5df31be3710457c7a8dae38b0b61c5dc50e3c54c
This commit is contained in:
parent
0f9d546398
commit
9e4d863bcf
@ -7,6 +7,9 @@ using System.Collections.Generic;
|
|||||||
|
|
||||||
namespace SemiColinGames {
|
namespace SemiColinGames {
|
||||||
public class SneakGame : Game {
|
public class SneakGame : Game {
|
||||||
|
const int TARGET_FPS = 60;
|
||||||
|
const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
|
||||||
|
|
||||||
readonly GraphicsDeviceManager graphics;
|
readonly GraphicsDeviceManager graphics;
|
||||||
RenderTarget2D renderTarget;
|
RenderTarget2D renderTarget;
|
||||||
|
|
||||||
@ -19,6 +22,12 @@ namespace SemiColinGames {
|
|||||||
readonly History<Input> input = new History<Input>(2);
|
readonly History<Input> input = new History<Input>(2);
|
||||||
|
|
||||||
readonly FpsCounter fpsCounter = new FpsCounter();
|
readonly FpsCounter fpsCounter = new FpsCounter();
|
||||||
|
readonly Timer updateTimer = new Timer(TARGET_FRAME_TIME / 2.0, "UpdateTimer");
|
||||||
|
readonly Timer drawTimer = new Timer(TARGET_FRAME_TIME / 2.0, "DrawTimer");
|
||||||
|
// Draw() needs to be called without IsRunningSlowly this many times before we actually
|
||||||
|
// attempt to draw the scene. This is a workaround for the fact that otherwise the first few
|
||||||
|
// frames can be really slow to draw.
|
||||||
|
int framesToSuppress = 2;
|
||||||
Texture2D grasslandBg1;
|
Texture2D grasslandBg1;
|
||||||
Texture2D grasslandBg2;
|
Texture2D grasslandBg2;
|
||||||
|
|
||||||
@ -32,7 +41,7 @@ namespace SemiColinGames {
|
|||||||
GraphicsProfile = GraphicsProfile.HiDef
|
GraphicsProfile = GraphicsProfile.HiDef
|
||||||
};
|
};
|
||||||
IsFixedTimeStep = true;
|
IsFixedTimeStep = true;
|
||||||
TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60);
|
TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
|
||||||
IsMouseVisible = true;
|
IsMouseVisible = true;
|
||||||
Content.RootDirectory = "Content";
|
Content.RootDirectory = "Content";
|
||||||
}
|
}
|
||||||
@ -69,6 +78,7 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
// Updates the game world.
|
// Updates the game world.
|
||||||
protected override void Update(GameTime gameTime) {
|
protected override void Update(GameTime gameTime) {
|
||||||
|
updateTimer.Start();
|
||||||
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
|
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
|
||||||
|
|
||||||
if (input[0].Exit) {
|
if (input[0].Exit) {
|
||||||
@ -97,10 +107,17 @@ namespace SemiColinGames {
|
|||||||
}
|
}
|
||||||
|
|
||||||
base.Update(gameTime);
|
base.Update(gameTime);
|
||||||
|
updateTimer.Stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Called when the game should draw itself.
|
// Called when the game should draw itself.
|
||||||
protected override void Draw(GameTime gameTime) {
|
protected override void Draw(GameTime gameTime) {
|
||||||
|
drawTimer.Start();
|
||||||
|
|
||||||
|
if (framesToSuppress > 0 && !gameTime.IsRunningSlowly) {
|
||||||
|
framesToSuppress--;
|
||||||
|
}
|
||||||
|
|
||||||
// We need to update the FPS counter in Draw() since Update() might get called more
|
// We need to update the FPS counter in Draw() since Update() might get called more
|
||||||
// frequently, especially when gameTime.IsRunningSlowly.
|
// frequently, especially when gameTime.IsRunningSlowly.
|
||||||
fpsCounter.Update();
|
fpsCounter.Update();
|
||||||
@ -140,20 +157,23 @@ namespace SemiColinGames {
|
|||||||
|
|
||||||
// Draw RenderTarget to screen.
|
// Draw RenderTarget to screen.
|
||||||
GraphicsDevice.SetRenderTarget(null);
|
GraphicsDevice.SetRenderTarget(null);
|
||||||
GraphicsDevice.Clear(Color.Black);
|
GraphicsDevice.Clear(Color.CornflowerBlue);
|
||||||
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
|
if (framesToSuppress == 0) {
|
||||||
SamplerState.PointClamp, DepthStencilState.Default,
|
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
|
||||||
RasterizerState.CullNone);
|
SamplerState.PointClamp, DepthStencilState.Default,
|
||||||
Rectangle drawRect = new Rectangle(
|
RasterizerState.CullNone);
|
||||||
0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
|
Rectangle drawRect = new Rectangle(
|
||||||
spriteBatch.Draw(renderTarget, drawRect, Color.White);
|
0, 0, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
|
||||||
|
spriteBatch.Draw(renderTarget, drawRect, Color.White);
|
||||||
|
|
||||||
// Draw debug toasts.
|
// Draw debug toasts.
|
||||||
Debug.DrawToasts(spriteBatch, font);
|
Debug.DrawToasts(spriteBatch, font);
|
||||||
|
|
||||||
spriteBatch.End();
|
spriteBatch.End();
|
||||||
|
}
|
||||||
|
|
||||||
base.Draw(gameTime);
|
base.Draw(gameTime);
|
||||||
|
drawTimer.Stop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user