69 lines
2.6 KiB
Markdown
69 lines
2.6 KiB
Markdown
---
|
|
title: "Game"
|
|
date: 2021-01-24T21:15:07-08:00
|
|
weight: 2
|
|
---
|
|
|
|
The `Game` class requires three main parts: initialization, update, and draw routines.
|
|
|
|
To create your Game class, extend the `Game` class:
|
|
|
|
```cs
|
|
namespace MyProject
|
|
{
|
|
public class MyGame : Game
|
|
{
|
|
public MyGame(
|
|
WindowCreateInfo windowCreateInfo,
|
|
PresentMode presentMode
|
|
int timestep,
|
|
bool debugMode
|
|
) : base(windowCreateInfo, presentMode, timestep, debugMode)
|
|
{
|
|
// init code here
|
|
}
|
|
|
|
public override void Update(double dt)
|
|
{
|
|
// update code here
|
|
}
|
|
|
|
public override void Draw(double dt, double alpha)
|
|
{
|
|
// draw code here
|
|
}
|
|
|
|
public override void OnDestroy()
|
|
{
|
|
// cleanup code here
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
### Initialization
|
|
|
|
`WindowCreateInfo` is discussed in the Window section.
|
|
|
|
`FrameLimiterSettings` specifies your game's framerate cap. It can be set to any integer value, or uncapped.
|
|
|
|
`timestep` refers to units of frames-per-second. Passing a timestep of 60 means that your game's logic will always update with an exact timestep of (1/60).
|
|
|
|
`debugMode` does what it sounds like, enables or disabled debug mode. If debug mode is on then the system will yell at you for doing bad graphics things, for release this should obviously be turned off.
|
|
|
|
### Update
|
|
|
|
MoonWorks automatically updates input and audio state before calling your game's Update method. `dt` will be filled in with the timestep. Put your game's update logic here.
|
|
|
|
### Draw
|
|
|
|
Rendering code goes here. I recommend structuring your codebase so that your Game class doesn't just become a big mess of graphics resources.
|
|
|
|
You might be wondering what `alpha` refers to. What if you set your game to update at 60 frames per second but one of your users has, for example, a 144Hz monitor? This will result in ugly stuttering as the game logic and rendering update out-of-step with each other. `alpha` gets filled in with a value between 0 and 1 which refers to a blending factor between the previous game state and current game state. This will allow you to linearly interpolate your position and rotation values between the two states to eliminate this stuttering.
|
|
|
|
For a more in-depth explanation of this behavior, I recommend reading the [Fix Your Timestep](https://www.gafferongames.com/post/fix_your_timestep/) article.
|
|
|
|
### OnDestroy
|
|
|
|
MoonWorks does its best to keep track of audio and graphics resources you have created to clean them up when the program exits. If you have created additional assets that you need to clean up, you can do that here. Otherwise, it's fine to leave this method blank.
|