Skip to content

Commit 16fa3ea

Browse files
committed
Update images
1 parent 9f1a486 commit 16fa3ea

34 files changed

+95
-78
lines changed
322 KB
Loading
682 KB
Loading
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.Xna.Framework;
2+
using Microsoft.Xna.Framework.Graphics;
3+
using Microsoft.Xna.Framework.Input;
4+
5+
namespace MonoGameSnake;
6+
7+
public class Game1 : Game
8+
{
9+
private GraphicsDeviceManager _graphics;
10+
private SpriteBatch _spriteBatch;
11+
private Texture2D _logo;
12+
13+
public Game1()
14+
{
15+
_graphics = new GraphicsDeviceManager(this);
16+
Content.RootDirectory = "Content";
17+
IsMouseVisible = true;
18+
}
19+
20+
protected override void Initialize()
21+
{
22+
// TODO: Add your initialization logic here
23+
24+
base.Initialize();
25+
}
26+
27+
protected override void LoadContent()
28+
{
29+
_spriteBatch = new SpriteBatch(GraphicsDevice);
30+
31+
// TODO: use this.Content to load your game content here
32+
_logo = Content.Load<Texture2D>("images/logo");
33+
}
34+
35+
protected override void Update(GameTime gameTime)
36+
{
37+
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
38+
Exit();
39+
40+
// TODO: Add your update logic here
41+
42+
base.Update(gameTime);
43+
}
44+
45+
protected override void Draw(GameTime gameTime)
46+
{
47+
GraphicsDevice.Clear(Color.CornflowerBlue);
48+
49+
// TODO: Add your drawing code here
50+
_spriteBatch.Begin();
51+
_spriteBatch.Draw(_logo, Vector2.Zero, Color.White);
52+
_spriteBatch.End();
53+
54+
base.Draw(gameTime);
55+
}
56+
}
506 KB
Loading
311 KB
Loading
403 KB
Loading
349 KB
Loading
342 KB
Loading

articles/tutorials/building_2d_games/04_content_pipeline/index.md

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -157,31 +157,20 @@ The [**Game**](xref:Microsoft.Xna.Framework.Game) class provides the [**Content*
157157
1. `T` Type Reference: The content type we are loading.
158158
2. `assetName` Parameter: A string path that matches the content path of the asset to load. As mentioned in the [Understanding Content Paths](#understanding-content-paths) section, the content path is relative to the [**ContentManager.RootDirectory**](xref:Microsoft.Xna.Framework.Content.ContentManager.RootDirectory), minus the extension. For instance, we added our image to the *images* folder in the content project, the content path for it will be `"images/logo"`.
159159

160-
Let's update the game now to load the image file using the [**ContentManager**](xref:Microsoft.Xna.Framework.Content.ContentManager). First, open the *Game1.cs* file in your project and add
160+
Let's update the game now to load the image file using the [**ContentManager**](xref:Microsoft.Xna.Framework.Content.ContentManager). First, open the *Game1.cs* file in your project and replace the contents with the following:
161161

162-
```cs
163-
private Texture2D _logo;
164-
```
165-
166-
beneath where the [**GraphicsDeviceManager**](xref:Microsoft.Xna.Framework.GraphicsDeviceManager) and [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) instance member variables are declared. This adds a new [**Texture2D**](xref:Microsoft.Xna.Framework.Graphics.Texture2D) instance called `_logo`. [**Texture2D**](xref:Microsoft.Xna.Framework.Graphics.Texture2D) is the type used to store a reference to 2D image data in MonoGame.
167-
168-
Next, locate the [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent) method and add the following after `_spriteBatch` is instantiated:
169-
170-
```cs
171-
_logo = Content.Load<Texture2D>("images/logo");
172-
```
173-
174-
If you run the game now, the image will be loaded as a texture, but all we'll see is the empty cornflower blue game window. This is because we are only loading it and not telling the game to draw it.
162+
[!code-csharp[](./Game1.cs?highlight=11,32,50-52)]
175163

176-
In the next chapter, we'll go more into detail on how to work with textures, for now, locate the [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)) method in the *Game1.cs* file and add the following after the [**GraphicsDevice.Clear**](xref:Microsoft.Xna.Framework.Graphics.GraphicsDevice.Clear(Microsoft.Xna.Framework.Color)) method call is made:
164+
The key changes we made here are
177165

178-
```cs
179-
_spriteBatch.Begin();
180-
_spriteBatch.Draw(_logo, Vector2.Zero, Color.White);
181-
_spriteBatch.End();
182-
```
166+
- The `_logo` member was added to store a reference to the logo texture once we load it.
167+
- In [**LoadContent**](xref:Microsoft.Xna.Framework.Game.LoadContent), the logo texture is loaded using [**ContentManager.Load<T>**](xref:Microsoft.Xna.Framework.Content.ContentManager.Load``1(System.String)).
168+
- In [**Draw**](xref:Microsoft.Xna.Framework.Game.Draw(Microsoft.Xna.Framework.GameTime)), the logo is rendered using the [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch).
169+
170+
> [!NOTE]
171+
> We'll go more into detail about the [**SpriteBatch**](xref:Microsoft.Xna.Framework.Graphics.SpriteBatch) in the next chapter.
183172
184-
Run the game and see the logo appear in the window's upper-left corner:
173+
Running the game now will show the MonoGame logo displayed in the upper-left corner of the game window.
185174

186175
<figure><img src="./images/logo-drawn.png" alt="Figure 4-7: The MonoGame logo drawn to the game window."><figcaption><p><strong>Figure 4-7: The MonoGame logo drawn to the game window.</strong></p></figcaption></figure>
187176

308 KB
Loading

0 commit comments

Comments
 (0)