You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|**Figure 4-2: Using a class library, common modules are shared across projects**|
29
29
30
30
> [!NOTE]
31
31
> A class library is a project type that compiles into a [Dynamic Link Library](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-libraries) (DLL) instead of an executable. It contains reusable code that can be referenced by other projects, making it perfect for sharing common functionality across multiple games.
@@ -130,46 +130,44 @@ When using the *MonoGame Game Library* project template, the generated project c
130
130
131
131
## Creating Our First Library Module
132
132
133
-
Let's validate our class library setup by creating a simple component that counts frames per second (FPS). This will demonstrate that we can successfully create classes in our library and use them in our game.
133
+
Let's create a class for our library called `Core`. This class will extend the MonoGame [**Game**](xref:Microsoft.Xna.Framework.Game) class and provide a starting point for game development with some common functionality built in. Creating this will also let us validate that our class library reference setup was correct.
134
134
135
-
Create a new file called *FramesPerSecondCounter.cs* in the root of the *MonoGameLibrary* project and add the following code:
135
+
Create a new file called *Core.cs* in the *MonoGameLibrary* project and add the following code:
- Tracks how many frames are rendered each second.
142
-
- Updates the FPS calculation based on elapsed time.
143
-
- Provides the current FPS through a property.
141
+
1. It extends the MonoGame [**Game**](xref:Microsoft.Xna.Framework.Game) class, so it inherits all of the base functionality.
142
+
2. It implements a singleton pattern through the `Instance` property, ensure only one core exists.
143
+
3. It provides static access to the graphics device manager and the sprite batch.
144
+
4. It simplifies the game window setup with a constructor that handles common initializations.
144
145
145
-
## Adding the Module To Our Game
146
+
This approach provides a consistent foundation for all our games, handling common setup tasks and providing convenient access to core functionality.
146
147
147
-
Now that we have the `FramesPerSecondCounter` class created, let's add it to our game. Doing this will also help ensure that the project references were setup correctly. Open the *Game1.cs* file and make the following changes:
148
+
> [!NOTE]
149
+
> As this tutorial progress, we'll be coming back to this `Core` class to add more to it.
Now that we have our `Core` class, let's modify our game project to use it. Doing this will also help ensure that the project references were setup correctly.
152
154
153
-
1. The `using MonoGameLibrary` using directive was added so we can use the new `FramesPerSecondCounter` class.
154
-
2. The field `_fpsCounter` was added to track the `FramesPerSecondCounter` instance.
155
-
3. In the constructor, a new instance of the `FramesPerSecondCounter` is created.
156
-
4. In [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), the `Update` method for the frames per second counter is called.
157
-
5. In [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime))
158
-
1. The `UpdateCOunter` method for the frames per second counter is called.
159
-
2. The game window title is updated to display the frames per second value.
155
+
Open the *Game1.cs* file and make the following changes:
160
156
161
-
When you run the game now, you'll see the current FPS displayed in the window title bar. This confirms that:
- The game project can reference and use library classes.
165
-
- The basic structure for creating reusable components works.
159
+
The key changes made here are:
166
160
167
-
||
168
-
| :---: |
169
-
|**Figure 4-3: The game window showing the frames per second in the title bar of the window**|
161
+
1. Adding `using MonoGameLibrary;` directive to reference our library.
162
+
1. Removed the [**GraphicsDeviceManager**](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) and [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) fields, these are now supplied through the `Core` class.
163
+
1. Changed `Game1` class to inherit from `Core` instead of `Game`.
164
+
1. Updated the constructor to call the `Core` base constructor with our game configuration.
170
165
171
-
> [!TIP]
172
-
> While this FPS counter is a simple example, it demonstrates the pattern we'll use throughout the tutorial: create reusable components in the library project, then reference and use them in games. This same approach will work for more complex components like sprite management, input handling, and collision detection.
166
+
Running the game now will show the same window as before, only now it is at a 1280x720 resolution as per the configuration and it is using the `Core` class from our library. This may not seem like a big change visually, but it demonstrates how our library can simplify and standardize game initializations.
167
+
168
+
||
0 commit comments