Skip to content

Commit e9ccd08

Browse files
committed
Reworking from removing services
1 parent 3722475 commit e9ccd08

File tree

27 files changed

+284
-213
lines changed

27 files changed

+284
-213
lines changed

articles/tutorials/building_2d_games/07_optimizing_texture_rendering/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Chapter 07: Optimizing Texture Rendering"
33
description: Explore optimization techniques when rendering textures using a texture atlas.
44
---
55

6-
In [Chapter 07](../07_working_with_textures/index.md), you learned how to load and render textures using [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch). While rendering individual textures works well for simple games, it can lead to performance issues as your game grows more complex. In this chapter, we will explore how to optimize texture rendering by reducing texture swaps and creating reusable components for better organization.
6+
In [Chapter 06](../06_working_with_textures/index.md), you learned how to load and render textures using [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch). While rendering individual textures works well for simple games, it can lead to performance issues as your game grows more complex. In this chapter, we will explore how to optimize texture rendering by reducing texture swaps and creating reusable components for better organization.
77

88
In this chapter, you will:
99

@@ -167,7 +167,7 @@ Add this texture atlas to your content project using the MGCB Editor:
167167
168168
First, we'll explore creating the texture atlas and defining the texture regions directly in code. Replace the contents of *Game1.cs* with the following:
169169

170-
[!code-csharp[](./snippets/game1/textureatlas_usage.cs?highlight=13-17,37-53,71-75)]
170+
[!code-csharp[](./snippets/game1/textureatlas_usage.cs?highlight=5,11-15,31-47,67-77)]
171171

172172
The key changes in this implementation are:
173173

@@ -210,7 +210,7 @@ While manual creation works for a few sprites, managing many regions becomes cum
210210
211211
8. Replace the contents of *Game1.cs* with the following code:
212212

213-
[!code-csharp[](./snippets/game1/textureatlas_xml_usage.cs?highlight=37-38)]
213+
[!code-csharp[](./snippets/game1/textureatlas_xml_usage.cs?highlight=31-32)]
214214

215215
The key improvements here is in [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent), where we now:
216216

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
using Microsoft.Xna.Framework;
22
using Microsoft.Xna.Framework.Graphics;
33
using Microsoft.Xna.Framework.Input;
4+
using MonoGameLibrary;
45
using MonoGameLibrary.Graphics;
56

67
namespace DungeonSlime;
78

8-
public class Game1 : Game
9+
public class Game1 : Core
910
{
10-
private GraphicsDeviceManager _graphics;
11-
private SpriteBatch _spriteBatch;
12-
1311
// texture region that defines the slime sprite in the atlas.
1412
private TextureRegion _slime;
1513

1614
// texture region that defines the bat sprite in the atlas.
1715
private TextureRegion _bat;
1816

19-
public Game1()
17+
public Game1() : base("Dungeon Slime", 1280, 720, false)
2018
{
21-
_graphics = new GraphicsDeviceManager(this);
22-
Content.RootDirectory = "Content";
23-
IsMouseVisible = true;
19+
2420
}
2521

2622
protected override void Initialize()
@@ -32,8 +28,6 @@ protected override void Initialize()
3228

3329
protected override void LoadContent()
3430
{
35-
_spriteBatch = new SpriteBatch(GraphicsDevice);
36-
3731
// Load the atlas texture using the content manager
3832
Texture2D atlasTexture = Content.Load<Texture2D>("images/atlas");
3933

@@ -51,32 +45,37 @@ protected override void LoadContent()
5145

5246
// retrieve the bat region from the atlas.
5347
_bat = atlas.GetRegion("bat");
48+
49+
base.LoadContent();
5450
}
5551

5652
protected override void Update(GameTime gameTime)
5753
{
5854
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
5955
Exit();
6056

57+
// TODO: Add your update logic here
58+
6159
base.Update(gameTime);
6260
}
6361

6462
protected override void Draw(GameTime gameTime)
6563
{
64+
// Clear the back buffer.
6665
GraphicsDevice.Clear(Color.CornflowerBlue);
6766

6867
// Begin the sprite batch to prepare for rendering.
69-
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
68+
SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
7069

7170
// Draw the slime texture region.
72-
_slime.Draw(_spriteBatch, Vector2.One, Color.White);
71+
_slime.Draw(SpriteBatch, Vector2.One, Color.White);
7372

7473
// Draw the bat texture region 10px to the right of the slime.
75-
_bat.Draw(_spriteBatch, new Vector2(_slime.Width + 10, 0), Color.White);
74+
_bat.Draw(SpriteBatch, new Vector2(_slime.Width + 10, 0), Color.White);
7675

7776
// Always end the sprite batch when finished.
78-
_spriteBatch.End();
77+
SpriteBatch.End();
7978

8079
base.Draw(gameTime);
8180
}
82-
}
81+
}
Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
using Microsoft.Xna.Framework;
22
using Microsoft.Xna.Framework.Graphics;
33
using Microsoft.Xna.Framework.Input;
4+
using MonoGameLibrary;
45
using MonoGameLibrary.Graphics;
56

67
namespace DungeonSlime;
78

8-
public class Game1 : Game
9+
public class Game1 : Core
910
{
10-
private GraphicsDeviceManager _graphics;
11-
private SpriteBatch _spriteBatch;
12-
1311
// texture region that defines the slime sprite in the atlas.
1412
private TextureRegion _slime;
1513

1614
// texture region that defines the bat sprite in the atlas.
1715
private TextureRegion _bat;
1816

19-
public Game1()
17+
public Game1() : base("Dungeon Slime", 1280, 720, false)
2018
{
21-
_graphics = new GraphicsDeviceManager(this);
22-
Content.RootDirectory = "Content";
23-
IsMouseVisible = true;
19+
2420
}
2521

2622
protected override void Initialize()
@@ -32,8 +28,6 @@ protected override void Initialize()
3228

3329
protected override void LoadContent()
3430
{
35-
_spriteBatch = new SpriteBatch(GraphicsDevice);
36-
3731
// Create the texture atlas from the XML configuration file
3832
TextureAtlas atlas = TextureAtlas.FromFile(Content, "images/atlas-definition.xml");
3933

@@ -42,32 +36,37 @@ protected override void LoadContent()
4236

4337
// retrieve the bat region from the atlas.
4438
_bat = atlas.GetRegion("bat");
39+
40+
base.LoadContent();
4541
}
4642

4743
protected override void Update(GameTime gameTime)
4844
{
4945
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
5046
Exit();
5147

48+
// TODO: Add your update logic here
49+
5250
base.Update(gameTime);
5351
}
5452

5553
protected override void Draw(GameTime gameTime)
5654
{
55+
// Clear the back buffer.
5756
GraphicsDevice.Clear(Color.CornflowerBlue);
5857

5958
// Begin the sprite batch to prepare for rendering.
60-
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
59+
SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
6160

6261
// Draw the slime texture region.
63-
_slime.Draw(_spriteBatch, Vector2.One, Color.White);
62+
_slime.Draw(SpriteBatch, Vector2.One, Color.White);
6463

6564
// Draw the bat texture region 10px to the right of the slime.
66-
_bat.Draw(_spriteBatch, new Vector2(_slime.Width + 10, 0), Color.White);
65+
_bat.Draw(SpriteBatch, new Vector2(_slime.Width + 10, 0), Color.White);
6766

6867
// Always end the sprite batch when finished.
69-
_spriteBatch.End();
68+
SpriteBatch.End();
7069

7170
base.Draw(gameTime);
7271
}
73-
}
72+
}
Loading

articles/tutorials/building_2d_games/08_the_sprite_class/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Chapter 08: The Sprite Class"
33
description: "Explore creating a reusable Sprite class to efficiently sprites and their rendering properties, including position, rotation, scale, and more."
44
---
55

6-
In [Chapter 08](../08_optimizing_texture_rendering/index.md), you learned how to use texture atlases to optimize rendering performance. While this solved the issue of texture swapping, managing individual sprites and their properties becomes increasingly complex as your game grows. Even in our simple example with just a slime and a bat, we would eventually need to track various properties for each sprite:
6+
In [Chapter 07](../07_optimizing_texture_rendering/index.md), you learned how to use texture atlases to optimize rendering performance. While this solved the issue of texture swapping, managing individual sprites and their properties becomes increasingly complex as your game grows. Even in our simple example with just a slime and a bat, we would eventually need to track various properties for each sprite:
77

88
- Color mask for tinting.
99
- Origin for rotation and scale.
@@ -72,7 +72,7 @@ We can simplify this process by adding a sprite creation method to the `TextureA
7272

7373
Let's adjust our game now to use the `Sprite` class instead of just the texture regions. Replace the contents of *Game1.cs* with the following:
7474

75-
[!code-csharp[](./snippets/game1.cs?highlight=13-17,40-44,62-66)]
75+
[!code-csharp[](./snippets/game1.cs?highlight=11-15,34-38,61-65)]
7676

7777
The key changes in this implementation are:
7878

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
using Microsoft.Xna.Framework;
22
using Microsoft.Xna.Framework.Graphics;
33
using Microsoft.Xna.Framework.Input;
4+
using MonoGameLibrary;
45
using MonoGameLibrary.Graphics;
56

67
namespace DungeonSlime;
78

8-
public class Game1 : Game
9+
public class Game1 : Core
910
{
10-
private GraphicsDeviceManager _graphics;
11-
private SpriteBatch _spriteBatch;
12-
1311
// Defines the slime sprite.
1412
private Sprite _slime;
1513

1614
// Defines the bat sprite.
1715
private Sprite _bat;
1816

19-
public Game1()
17+
public Game1() : base("Dungeon Slime", 1280, 720, false)
2018
{
21-
_graphics = new GraphicsDeviceManager(this);
22-
Content.RootDirectory = "Content";
23-
IsMouseVisible = true;
19+
2420
}
2521

2622
protected override void Initialize()
@@ -32,8 +28,6 @@ protected override void Initialize()
3228

3329
protected override void LoadContent()
3430
{
35-
_spriteBatch = new SpriteBatch(GraphicsDevice);
36-
3731
// Create the texture atlas from the XML configuration file
3832
TextureAtlas atlas = TextureAtlas.FromFile(Content, "images/atlas-definition.xml");
3933

@@ -42,32 +36,37 @@ protected override void LoadContent()
4236

4337
// Create the bat sprite from the atlas.
4438
_bat = atlas.CreateSprite("bat");
39+
40+
base.LoadContent();
4541
}
4642

4743
protected override void Update(GameTime gameTime)
4844
{
4945
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
5046
Exit();
5147

48+
// TODO: Add your update logic here
49+
5250
base.Update(gameTime);
5351
}
5452

5553
protected override void Draw(GameTime gameTime)
5654
{
55+
// Clear the back buffer.
5756
GraphicsDevice.Clear(Color.CornflowerBlue);
5857

5958
// Begin the sprite batch to prepare for rendering.
60-
_spriteBatch.Begin(samplerState: SamplerState.PointClamp);
59+
SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
6160

6261
// Draw the slime sprite.
63-
_slime.Draw(_spriteBatch, Vector2.One);
62+
_slime.Draw(SpriteBatch, Vector2.One);
6463

6564
// Draw the bat sprite 10px to the right of the slime.
66-
_bat.Draw(_spriteBatch, new Vector2(_slime.Width + 10, 0));
65+
_bat.Draw(SpriteBatch, new Vector2(_slime.Width + 10, 0));
6766

6867
// Always end the sprite batch when finished.
69-
_spriteBatch.End();
68+
SpriteBatch.End();
7069

7170
base.Draw(gameTime);
7271
}
73-
}
72+
}

articles/tutorials/building_2d_games/09_the_animatedsprite_class/index.md

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,18 @@ While packing images into a texture atlas and managing them through our `Sprite`
1111
In MonoGame, we can create these animations by cycling through different regions of our texture atlas, with each region representing a single frame of the animation. For example, Figure 10-1 below shows three frames that make up a bat's wing-flapping animation:
1212

1313
| ![Figure 10-1: Animation example of a bat flapping its wings](./images/bat-animation-example.gif) |
14-
|:------------------------------------------------------------------------------------------------:|
14+
| :-----------------------------------------------------------------------------------------------: |
1515
| **Figure 10-1: Animation example of a bat flapping its wings** |
1616

1717
By drawing each frame sequentially over time, we create the illusion that the bat is flapping its wings. The speed at which we switch between frames determines how smooth or rapid the animation appears.
1818

19-
In this chapter, we'll build off of the `Sprite` class we created in [Chapter 09](../09_the_sprite_class/index.md) to create an `AnimatedSprite` class we can use to bring animations to life.
19+
In this chapter, we'll build off of the `Sprite` class we created in [Chapter 08](../08_the_sprite_class/index.md) to create an `AnimatedSprite` class we can use to bring animations to life.
2020

2121
## The Animation Class
2222

2323
Before we can create animated sprites, we need a way to manage animation data. Let's create an `Animation` class to encapsulate this information. In the *Graphics* directory within the *MonoGameLibrary* project, add a new file named *Animation.cs* with this initial structure:
2424

25-
| Animation.cs |
26-
|:-----------------------------------------------------:|
27-
| [!code-csharp[](./snippets/animation.cs#declaration)] |
25+
[!code-csharp[](./snippets/animation.cs#declaration)]
2826

2927
### Animation Properties
3028

@@ -168,7 +166,7 @@ We can simplify this process by adding an animated spirte creation method to the
168166

169167
Let's adjust our game now to use the `AnimatedSprite` class to see our sprites come to life. Replaces the contents of *Game1.cs* with the following:
170168

171-
[!code-csharp[](./snippets/game1.cs?highlight=13-17,40-44,52-56)]
169+
[!code-csharp[](./snippets/game1.cs?highlight=11-15,34-38,48-52)]
172170

173171
Let's examine the key changes in this implementation:
174172

@@ -181,9 +179,9 @@ Running the game now shows both sprites animating automatically:
181179
- The slime bounces between two frames
182180
- The bat's wings flap in a continuous cycle
183181

184-
| ![Figure 10-2: The slime and bat sprite animating](./images/slime-bat-animated.gif) |
185-
|:----------------------------------------------------------------------------------:|
186-
| **Figure 10-2: The slime and bat sprite animating** |
182+
| ![Figure 10-2: The slime and bat sprite animating](./videos/slime-bat-animated.webm) |
183+
| :----------------------------------------------------------------------------------: |
184+
| **Figure 10-2: The slime and bat sprite animating** |
187185

188186
## Conclusion
189187

0 commit comments

Comments
 (0)