This is my first post, I don't know exactly where this will lead, but my idea is to share some of my MonoGame and C# knowledge while I'm developing a small game using these pieces of technology. I'm planning to keep these posts as short as possible, but let's see how that goes.
First things first... and I know it was described by many people already, but it will be a practice in writing for me as well as maybe new information for someone who stumble on my post before the post of someone else.
I will skip the what are .Net, C# and MonoGame and how to setup your machine to use them as it depends on your environment, so here we go.
When using the MonoGame Cross-Platform template, you will get a class called Game1. Let's go through it to understand the very basic skeleton of a MonoGame application.
Variables
First you will get these two variables.
private GraphicsDeviceManager _graphics;
private SpriteBatch _spriteBatch;
If I can say very simply, the GraphicsDeviceManager will interact with your GPU (Graphics Card) and the SpriteBatch will be used to render 2D graphics and text.
Constructor
Then we get the constructor where the GraphicsDeviceManager is initialized and the ContentManager get a directory where the content (images, fonts, audio...) will be loaded later. Other things might happen there in the future.
public Game1()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
}
Initialize
After the constructor, the Initialize function is called. Here the base.Initialize() call should not be deleted, it's important for some platform specific initializations. But you can add game specific initializations that need to run once at the start.
protected override void Initialize()
{
base.Initialize();
}
LoadContent
At the end of base.Initialize, the LoadContent method is called, this timing might be important to remember for dependencies of what you want to initialize or load. Here is a good place to load game content, like images, audio, fonts...
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
}
The Game Loop
Now we arrive in what is called the game loop. Basically the whole time your game is running, both the Update and Draw functions are called one after the other. If you don't change anything, MonoGame will try to execute these functions 60 times per second... at least that's the goal.
Update
In the Update function, not much is happening, but later this would be the place to check for inputs, update the position of content in your game, check for collisions... etc.
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
}
Draw
The Draw method is also not doing much for now, but it's clearing the screen so that it is ready to draw something new on the screen.
You should avoid putting code here that updates important things, because, as far as I know, if your game is running too slow, MonoGame will prioritize Update and might not call Draw as often, so it might cause some weird things.
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
base.Draw(gameTime);
}
Conclusion
Now that you know a bit about the skeleton of a MonoGame application, we can draw our first things on the screen in the next post.
Feel free to comment, ask questions, or correct me... we are all here to learn :)
See you
- David
Top comments (0)