Skip to content

Commit b424461

Browse files
committed
Update chapters for new atlas
1 parent 61c29b3 commit b424461

37 files changed

Lines changed: 249 additions & 233 deletions

File tree

1 Byte
Loading
600 Bytes
Loading

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,17 +183,16 @@ The key changes in this implementation are:
183183
- Created a `TextureAtlas` with the atlas texture.
184184
- Added regions for both the slime and the bat.
185185
- Retrieved the regions using their names.
186-
4. Updated [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) to render both sprites, using the slime's `Width` property to position the bat.
186+
4. Updated [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) to:
187+
- Draw the slime at a scale factor of 4.
188+
- Draw the bat 10 pixels to the right of the bat based on the slime's `Width` property, at a scale of 4
187189

188190
Running the game now shows both sprites in the upper-left corner:
189191

190192
| ![Figure 7-3: The slime and bat texture regions being rendered in the upper-left corner of the game window](./images/slime-and-bat-rendered.png) |
191193
|:------------------------------------------------------------------------------------------------------------------------------------------------:|
192194
| **Figure 7-3: The slime and bat texture regions being rendered in the upper-left corner of the game window** |
193195

194-
> [!NOTE]
195-
> Both the slime and the bat sprites are very small. In fact, both are only 20x20 pixels each. This is not going to be ideal for the game we're building, however do not fret, we will resolve this in the next chapter.
196-
197196
While manual creation works for a few sprites, managing many regions becomes cumbersome. Let's now explore the `TextureAtlas.FromFile` method to load our atlas configuration from XML instead. Perform the following:
198197

199198
1. Create a new file named *atlas-definition.xml* in the *Content/images* directory.

articles/tutorials/building_2d_games/07_optimizing_texture_rendering/snippets/game1/textureatlas_usage.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ protected override void Draw(GameTime gameTime)
6767
// Begin the sprite batch to prepare for rendering.
6868
SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
6969

70-
// Draw the slime texture region.
71-
_slime.Draw(SpriteBatch, Vector2.One, Color.White);
70+
// Draw the slime texture region at a scale of 4.0
71+
_slime.Draw(SpriteBatch, Vector2.Zero, Color.White, 0.0f, Vector2.One, 4.0f, SpriteEffects.None, 0.0f);
7272

73-
// Draw the bat texture region 10px to the right of the slime.
74-
_bat.Draw(SpriteBatch, new Vector2(_slime.Width + 10, 0), Color.White);
73+
// Draw the bat texture region 10px to the right of the slime at a scale of 4.0
74+
_bat.Draw(SpriteBatch, new Vector2(_slime.Width * 4.0f + 10, 0), Color.White, 0.0f, Vector2.One, 4.0f, SpriteEffects.None, 1.0f);
7575

7676
// Always end the sprite batch when finished.
7777
SpriteBatch.End();

articles/tutorials/building_2d_games/07_optimizing_texture_rendering/snippets/game1/textureatlas_xml_usage.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ protected override void Draw(GameTime gameTime)
5858
// Begin the sprite batch to prepare for rendering.
5959
SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
6060

61-
// Draw the slime texture region.
62-
_slime.Draw(SpriteBatch, Vector2.One, Color.White);
61+
// Draw the slime texture region at a scale of 4.0
62+
_slime.Draw(SpriteBatch, Vector2.Zero, Color.White, 0.0f, Vector2.One, 4.0f, SpriteEffects.None, 0.0f);
6363

64-
// Draw the bat texture region 10px to the right of the slime.
65-
_bat.Draw(SpriteBatch, new Vector2(_slime.Width + 10, 0), Color.White);
64+
// Draw the bat texture region 10px to the right of the slime at a scale of 4.0
65+
_bat.Draw(SpriteBatch, new Vector2(_slime.Width * 4.0f + 10, 0), Color.White, 0.0f, Vector2.One, 4.0f, SpriteEffects.None, 1.0f);
6666

6767
// Always end the sprite batch when finished.
6868
SpriteBatch.End();
6969

7070
base.Draw(gameTime);
7171
}
72-
}
72+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,13 @@ We can simplify this process by adding an animated spirte creation method to the
166166

167167
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:
168168

169-
[!code-csharp[](./snippets/game1.cs?highlight=11-15,34-38,48-52)]
169+
[!code-csharp[](./snippets/game1.cs?highlight=11-15,34-40,50-54)]
170170

171171
Let's examine the key changes in this implementation:
172172

173173
- The `_slime` and `_bat` members were changed from `Sprite` to `AnimatedSprite`.
174174
- In [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) the `_slime` and `_bat` sprites are now created using the new `TextureAtlas.CreateAnimatedSprite` method.
175-
- In [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), the animations are updated based on the game time using hte `AnimatedSprite.Update` method.
175+
- In [**Update**](xref:Microsoft.Xna.Framework.Game.Update(Microsoft.Xna.Framework.GameTime)), the animations are updated based on the game time using the `AnimatedSprite.Update` method.
176176

177177
Running the game now shows both sprites animating automatically:
178178

articles/tutorials/building_2d_games/09_the_animatedsprite_class/snippets/atlas_definition.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<TextureAtlas>
33
<Texture>images/atlas</Texture>
44
<Regions>
5-
<Region name="slime-1" x="0" y="0" width="80" height="80" />
6-
<Region name="slime-2" x="80" y="0" width="80" height="80" />
7-
<Region name="bat-1" x="0" y="80" width="80" height="80" />
8-
<Region name="bat-2" x="80" y="80" width="80" height="80" />
9-
<Region name="bat-3" x="160" y="80" width="80" height="80" />
5+
<Region name="slime-1" x="0" y="0" width="20" height="20" />
6+
<Region name="slime-2" x="0" y="20" width="20" height="20" />
7+
<Region name="bat-1" x="20" y="0" width="20" height="20" />
8+
<Region name="bat-2" x="20" y="20" width="20" height="20" />
9+
<Region name="bat-3" x="40" y="0" width="20" height="20" />
1010
</Regions>
1111
<Animations>
1212
<Animation name="slime-animation" delay="200">

articles/tutorials/building_2d_games/09_the_animatedsprite_class/snippets/game1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,11 @@ protected override void LoadContent()
3333

3434
// Create the slime animated sprite from the atlas.
3535
_slime = atlas.CreateAnimatedSprite("slime-animation");
36+
_slime.Scale = new Vector2(4.0f, 4.0f);
3637

3738
// Create the bat animated sprite from the atlas.
3839
_bat = atlas.CreateAnimatedSprite("bat-animation");
40+
_bat.Scale = new Vector2(4.0f, 4.0f);
3941

4042
base.LoadContent();
4143
}

articles/tutorials/building_2d_games/10_handling_input/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ For our game, we're going to implement keyboard and gamepad controls based on th
328328

329329
Open *Game1.cs* and update it with the following:
330330

331-
[!code-csharp[](./snippets/game1.cs?highlight=17-21,60-64,69-157,168)]
331+
[!code-csharp[](./snippets/game1.cs?highlight=17-21,62-66,71-159,170)]
332332

333333
The key changes made here are:
334334

articles/tutorials/building_2d_games/10_handling_input/snippets/game1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ protected override void LoadContent()
3939

4040
// Create the slime animated sprite from the atlas.
4141
_slime = atlas.CreateAnimatedSprite("slime-animation");
42+
_slime.Scale = new Vector2(4.0f, 4.0f);
4243

4344
// Create the bat animated sprite from the atlas.
4445
_bat = atlas.CreateAnimatedSprite("bat-animation");
46+
_bat.Scale = new Vector2(4.0f, 4.0f);
4547

4648
base.LoadContent();
4749
}

0 commit comments

Comments
 (0)