Skip to content

Commit 545a000

Browse files
committed
Simplify validation step
1 parent 8a43808 commit 545a000

File tree

1 file changed

+0
-107
lines changed
  • articles/tutorials/building_2d_games/04_game_library

1 file changed

+0
-107
lines changed

articles/tutorials/building_2d_games/04_game_library/index.md

Lines changed: 0 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -112,113 +112,6 @@ When using the *MonoGame Game Library* project template, the generated project c
112112
>
113113
> If you would like more information on this, Simon Jackson has written the article [Going cross-platform with MonoGame](https://darkgenesis.zenithmoon.com/going-cross-platform-with-monogame.html) which covers this in more detail.
114114
115-
## Validating Setup
116-
117-
Before moving on, let's validate that the reference to our game library was setup correctly and can be referenced within the game project. To do this, we're going to add a simple class we can use to measure the frames per second (FPS) that our game is running at. First, create a new class file named *FramesPerSecondCounter.cs* in the *MonoGameLibrary* project. Then add the following to the class file:
118-
119-
```cs
120-
using System;
121-
using Microsoft.Xna.Framework;
122-
123-
namespace MonoGameLibrary;
124-
125-
public class FramesPerSecondCounter
126-
{
127-
// Represents one second of time
128-
private static readonly TimeSpan s_oneSecond = TimeSpan.FromSeconds(1);
129-
130-
// Keeps count of the total number of frames that have elapsed.
131-
private int _counter;
132-
133-
// Tracks the current amount of time that has elapsed.
134-
private TimeSpan _timer;
135-
136-
/// <summary>
137-
/// Gets the calculated total of frames per second.
138-
/// </summary>
139-
public int FramesPerSecond { get; private set; }
140-
141-
/// <summary>
142-
/// Creates a new FramesPerSecondCounter instance.
143-
/// </summary>
144-
public FramesPerSecondCounter()
145-
{
146-
_counter = 0;
147-
_timer = TimeSpan.Zero;
148-
}
149-
150-
/// <summary>
151-
/// Updates this FramesPerSecondCounter.
152-
/// </summary>
153-
/// <remarks>
154-
/// Should only be called once during the main game update.
155-
/// </remarks>
156-
/// <param name="gameTime">Snapshot of the timing values provided by the MonoGame framework.</param>
157-
public void Update(GameTime gameTime)
158-
{
159-
// Increment by the total amount of time elapsed between frames.
160-
_timer += gameTime.ElapsedGameTime;
161-
162-
// If more than a single second has elapsed, set the frames per second
163-
// to the total counted, reset the counter, and decrement the timer
164-
// by one second
165-
if (_timer > s_oneSecond)
166-
{
167-
FramesPerSecond = _counter;
168-
_counter = 0;
169-
_timer -= s_oneSecond;
170-
}
171-
}
172-
173-
/// <summary>
174-
/// Updates the FramesPerSecondCounter during the draw cycle of the game.
175-
/// </summary>
176-
/// <remarks>
177-
/// Should only be called once during the main game draw cycle
178-
/// </remarks>
179-
public void DrawUpdate()
180-
{
181-
// We increment the counter only during draw since each draw completes
182-
// a frame cycle.
183-
_counter++;
184-
}
185-
}
186-
```
187-
188-
Now that we have a class within the game library project to reference, let's ensure it can be referenced. Perform the following
189-
190-
1. Open the *Game1.cs* file in the *MonoGameSnake* project. At the top of the file where the `using` statements are located, locate the `using Microsoft.Xna.Framework.Input` statement and add the following on a new line below it:
191-
192-
```cs
193-
using MonoGameLibrary;
194-
```
195-
196-
2. Locate the `private SpriteBatch _spriteBatch;` member declaration and add the following on a new line below it:
197-
198-
```cs
199-
private FramesPerSecondCounter _fpsCounter = new FramesPerSecondCounter();
200-
```
201-
202-
3. Locate the `Update` method and add the following:
203-
204-
```cs
205-
_fpsCounter.Update(gameTime);
206-
```
207-
208-
4. Finally, locate the `Draw` method and add the following:
209-
210-
```cs
211-
_fpsCounter.DrawUpdate();
212-
Window.Title = "{_fpsCounter.FramesPerSecond}FPS";
213-
```
214-
215-
If you run the game now, you should see the title of the game update to show the game name and the total frames per second it is running at.
216-
217-
<figure><img src="./images/60-fps.png" alt="Figure 4-1: Game window showing the FPS in the title bar."><figcaption><p><em>Figure 4-1: Game window showing the FPS in the title bar.</em></p></figcaption></figure>
218-
219-
> [!TIP]
220-
> If you receive an error that the type or namespace for `MonoGameLibrary` or `FramesPerSecondCounter` could not be found, this means the project reference was not setup correctly. Revisit the [Adding a Reference to the Class Library](#adding-a-reference-to-the-class-library) section above and ensure that the reference is added properly.
221-
222115
## Conclusion
223116
224117
Here is a review of what was accomplished in this chapter:

0 commit comments

Comments
 (0)