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:
Colin McMillen 2020-02-03 18:17:28 -05:00
parent 0f9d546398
commit 9e4d863bcf

View File

@ -7,6 +7,9 @@ using System.Collections.Generic;
namespace SemiColinGames {
public class SneakGame : Game {
const int TARGET_FPS = 60;
const double TARGET_FRAME_TIME = 1.0 / TARGET_FPS;
readonly GraphicsDeviceManager graphics;
RenderTarget2D renderTarget;
@ -19,6 +22,12 @@ namespace SemiColinGames {
readonly History<Input> input = new History<Input>(2);
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 grasslandBg2;
@ -32,7 +41,7 @@ namespace SemiColinGames {
GraphicsProfile = GraphicsProfile.HiDef
};
IsFixedTimeStep = true;
TargetElapsedTime = TimeSpan.FromSeconds(1.0 / 60);
TargetElapsedTime = TimeSpan.FromSeconds(TARGET_FRAME_TIME);
IsMouseVisible = true;
Content.RootDirectory = "Content";
}
@ -69,6 +78,7 @@ namespace SemiColinGames {
// Updates the game world.
protected override void Update(GameTime gameTime) {
updateTimer.Start();
input.Add(new Input(GamePad.GetState(PlayerIndex.One), Keyboard.GetState()));
if (input[0].Exit) {
@ -97,10 +107,17 @@ namespace SemiColinGames {
}
base.Update(gameTime);
updateTimer.Stop();
}
// Called when the game should draw itself.
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
// frequently, especially when gameTime.IsRunningSlowly.
fpsCounter.Update();
@ -140,7 +157,8 @@ namespace SemiColinGames {
// Draw RenderTarget to screen.
GraphicsDevice.SetRenderTarget(null);
GraphicsDevice.Clear(Color.Black);
GraphicsDevice.Clear(Color.CornflowerBlue);
if (framesToSuppress == 0) {
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
SamplerState.PointClamp, DepthStencilState.Default,
RasterizerState.CullNone);
@ -152,8 +170,10 @@ namespace SemiColinGames {
Debug.DrawToasts(spriteBatch, font);
spriteBatch.End();
}
base.Draw(gameTime);
drawTimer.Stop();
}
}
}