#
Testing Our SetupNow that we've created the Scene
class and integrated it into our Game1
class, it's time to test the setup.
#
Add The ContentBefore we can test the setup, we're going to create a few assets to use in our game.
Perform the Following
Create or Download the following assets and add them to your project using the MGCB Editor. If downloading them, you can just right-click > Save As on each link below.
- A SpriteFont that we can use to render text. (Consolas, 12pt)
- A 64 x 64 orange circle image.
- A 64px x 64px green circle image.
You can create these content files yourself, or you can click them below to download them.
Once you have the files, use the MGCB Editor (aka Content Pipeline Tool) to add them to the game content.
note
If you are unfamiliar with adding new content such as a SpriteFont, please see the official MonoGame Documentation
#
Load the Global ContentWe discussed previously how we can use the ContentManager
in the Game1
class to load content that can be used globally in any scene. The SpriteFont we added as content is an excellent example of this, as well be using it in both of the test scenes we create.
Perform the Following
Open the Game1
class file. Locate the LoadContent()
method and change it to the following.
Now, some of you have a sharp eye and you noticed something a little strange here. We're loading the SpriteFont using the ContentManager
, but we're not storing it anywhere. We just load it and move on.
This is because of how a ContentManager
instance works. When you call LoadContent<T>(string)
, it first checks to see if it has already loaded that content before from the disk. If it has not, then it loads from disk and stores a cached version of it. Then later in the code, if we tell the same ContentManager
to load the same piece of content, instead of loading it from disk it will just serve the cached version it previously loaded.
Source Code
Want to see how the ContentManager
caches the assets for your self? That's the great thing about MonoGame, all of the source code is available to view.
The particular bit of code you'd be looking for is https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Content/ContentManager.cs#L246
#
Create the Green Circle SceneNext we're going to create the scene that displays the green circle image.
Perform the Following
Add a new class file to your project called GreenCircleScene.cs then add the following code to the file. We'll go over it after the code.
using statements
GreenCircleScene class
From the code above, you can see that our GreenCircleScene
class is inheriting from the Scene
class
Next there are two instance fields; _font
to hold the font content file that we load in and _greenCircle
to hold the green_circle.png content that we load in.
After that is the constructor. It just takes in the single Game1
parameter and passes it to the base
scene constructor
Following that is the LoadContent()
method. This is where some magic happens. We use _game.Content.Load<T>(string)
to load the font since we know it is stored in our global content manager. Remember, we loaded this previously in the Game1.LoadContent()
method, so when we load it a second time here, it's loading the cached asset and not from the disk. After loading the font, we load our scene specific asset, the green_circle texture using the scene's ContentManager
.
And finally, we have the Draw(SpritBatch)
method that we use to draw the scene to the screen. In short, this is drawing the text "Green Circle Scene" in the top left corner of the screen, then it is drawing the green circle in the center of the screen.
That's it for the Green Circle Scene. Next let's create the Orange Circle Scene
#
Create the Orange Circle SceneThe orange circle scene is pretty much identical to the Green Circle Screen, only it loads the orange_circle.png content and displays the text "Orange Circle Scene" when rendered.
Because of this, we won't discuss the code directly, as we already did that above. Instead I'll just add it here for you to copy and paste.
Perform the Following
Add a new class file to your project called OrangeCircleScene.cs, then add the following code to it.
using statements
OrangeCircleScene class
#
Loading the Starting SceneNow that we have added our two test scene classes, we need to tell the Game1
class which scene to use as the starting scene.
Perform the Following
Open Game1.cs and locate the Initialize()
method. Change the Initialize()
method to the following.
Change the Initialize()
method to the following
By doing this, we're telling Game1
to load and switch to the GreenCircleScene
directly after it finishes its call to base.Initialize()
.
if you run the game now, you should see the following displayed.
Fantastic, the game is now loading our initial scene and rendering it. Next, let's add some code to make it switch between our two scenes.
#
Switching ScenesIn order to switch scenes at the press of a button, we're going to need to do some initial input state management in our game. Since input state is something that is global across the entire game, and not scene specific, it makes more sense for us to do this in our Game1
class.
Perform the Following
Open the Game1.cs class file and add the following to the class
using statement
Properties
These two properties will be used to track the state of keyboard input between the previous and current frames in our game. Next, we need to make sure these have the proper values each frame.
Perform the Following
Scroll down to the Update(GameTime)
method and add the following to the top of the method. Input states should be the first thing updated.
Now that we are managing input states in Game1
, we can do some input checking in the GreenCircleScene.Update(GameTime)
method to see if we should switch scenes. We're going to need to add an override to the Update(GameTime)
method since we did not add one previously. In this update method, we need to tell it that if the space key is pressed, the game should switch to a new OrangeCircleScene
.
Perform the Following
Open the GreenCircleScene.cs class file and add the following code.
using statement
Update(GameTime) Method
Now run the game. You should first be presented with the GreenCircleScene
. When you press the space key, the game will switch to the OrangeCircleScene
. Pretty nifty ya? Unfortunately, we can't switch back to the GreenCircleScene
from the OrangeCircleScene
. Let's fix this.
Perform the Following
Open the OrangeCircleScene.cs class file and add the following code.
using statement
Update(GameTime) Method
If you run the game now, you'll be presented initially with the GreenCircleScene
. Pressing the space key will switch scenes to the OrangeCircleScene
as before, only this time if you press the space key again, it will switch back to the GreenCircleScene
.