I had some input problems which can be partially solved by unlocking game update rate. Now the input updates as fast as possible, but the game also calls the main Draw
method a lot too. I believe there are some calls that may be omitted without causing any noticeable problems, and I'd like to know how I can do it.
I tried using code similar to frame rate counter code, but it just doesn't work. It either doesn't skip frames, or skips them all. Here it is:
double time = 0;
protected override void Draw(GameTime gameTime)
{
double frameRate = 1.0 / 60;
time += gameTime.ElapsedGameTime.TotalSeconds;
if (time > frameRate)
{
time = time % frameRate;
// the rest of the draw call
When the game window is active, only dark purple color is visible I guess that's application's default background color when nothing is being drawn. When the game window is not active, frame rate drops from infinity (though I get about 4000 fps) to around 50 (which is unusual, since the default frame rate is 60, but it's not an issue).
If I set frameRate
value to lower than 1/50 (like 1/10) it becomes obvious that draw calls are skipped and the dark purple background is visible (and resources are being saved), but only when the window is inactive. So the game behaves as expected, but only when it is not in focus, which is not the way it is supposed to be played :)
How do I fix this?
I'd like to extend this to work on heavy logic updates, so they too are not done more than enough times per second. The game should only update input as fast as possible, and I believe, reducing the number of logic update and draw calls frees up resources for input update to use, increasing their frequency.
This is how I ended up doing this:
double interval = 1d / 60; // draw and do heavy updates only 60 frames per second
double time;
protected override void Update(GameTime gameTime)
{
LightUpdate(gameTime);
time += gameTime.ElapsedGameTime.TotalSeconds;
if (time > interval)
time = time % interval; // or while (time > interval) time -= interval;
else
{
SuppressDraw();
base.Update(gameTime);
return;
}
HeavyUpdate(gameTime);
}
Saves frames, light update rate rose from 2k to 110k @100Hz which is a tremendous success!
if
statement in the code you posted? \$\endgroup\$base.Draw()
inside the Draw method, as it doesn't appear necessary in any of my game projects. \$\endgroup\$