How To: Write Games for Less Capable Hardware
Demonstrates how to write games for a variety of computer configurations. Specifically, this topic addresses ways to write a game so it will work on high-end hardware, yet continue to give adequate performance when the game is run on a computer that is less capable.
Computers with different graphics cards and CPUs give different levels of performance. Writing game code that prepares for and adapts to slower and less capable hardware will allow games to have a larger audience.
Hardware Specific Degradation
Pixel Shader Support
When possible, take advantage of high-end hardware, but do not rely on it. For example, it is a good idea to create and use shaders to take advantage of higher-end hardware if it will benefit the game. But it is also a good idea to create alternative shaders that give acceptable effects and performance on lower-end hardware.
The following code sample tests the current graphics adapter. If the current graphics adapter does not support at least pixel shader version 2.0, a 1.1 version pixel shader is used instead.
ContentManager content = new ContentManager(Services);
Effect effect;
// Check the graphics device used by the game for the necessary shader support.
GraphicsDeviceCapabilities caps = graphics.GraphicsDevice.GraphicsDeviceCapabilities;
if (caps.MaxPixelShaderProfile < ShaderProfile.PS_2_0)
{
// Select a 1.1 version pixel shader if the computer has a graphics adapter that cannot support 2.0 shaders.
effect = content.Load<Effect>("PixelShader1_1");
}
else
{
// Graphics adapter supports 2.0 version pixel shaders.
effect = content.Load<Effect>("PixelShader2_0");
}
Single vs. Multiple CPUs
If a computer has multiple CPUs, some work can be offloaded to the other CPUs. The following code sample shows how to determine the number of available CPUs in the computer. If the game takes advantage of multiple CPUs, but does not require them, this code sample describes where to place code for single-processor computers and multi-processor computers.
// Scale appropriately for the number of processors on this computer
if (Environment.ProcessorCount == 1)
{
// Perform tasks specific to single processor systems.
}
else
{
// Perform tasks specific to multi-processor systems.
}
Performance-Specific Degradation
You can measure performance levels by the number of times the Update method is called each second. Once the update rate falls below a certain level (the default is 60 times per second) GameTime.IsRunningSlowly will be set to true.
This code sample shows where to place code that should be skipped if the game's performance falls below 60 frames per second.
// Perform the following only if the game is running at 60 frames per second or faster
if (gameTime.IsRunningSlowly == false)
{
// Perform time consuming update or draw here.
}
If 60 frames per second is not ideal for the game, you can change the default by altering TargetElapsedTime. To have IsRunningSlowly become true when the game slows to less than 30 frames per second, include this in the program's initialization code.
// Alert the program when the frame rate falls below 30 frames per second.
TargetElapsedTime = new TimeSpan((1L / 30L) * 10000000L);
To test for a different frame rate, change the 30
of the 30L
to the frames-per-second desired (for example, 45L
for 45 frames per second, or 100L
for 100 frames per second).
See Also
Tasks
How To: Check for Shader Model 2.0 Support
Concepts
Threading (C# Programming Guide)
Using Threading (C# Programming Guide)
Reference
IsRunningSlowly
TargetElapsedTime
System.Environment.ProcessorCount Property
ElapsedGameTime