In today’s tutorial, I will show you how to draw a sprite on the MonoGame window.
Use the utility to open the Content.mgcb file.
1 2 3 4 5 | [mythcat@desk MyGame001]$ cd Content/ [mythcat@desk Content]$ ls bin Content.mgcb Fedora_logo.png obj ... [mythcat@desk MyGame001]$ mgcb-editor |
Add the sprite file using the Add Existing Item menu.
I used the Fedora_logo.png file then build with the Build button.
Add the following lines of code to the source code in the Game1.cs file that links to the sprite image and run it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; namespace MyGame001 { public class Game1: Game { private GraphicsDeviceManager _graphics; ResolutionChanger resolution; SpriteBatch spriteBatch; private Texture2D _texture; private Vector2 _position; public Game1() { ProblemReport p = new ProblemReport(); ProblemReport.test(); _graphics = new GraphicsDeviceManager (this); // TODO: Set content by IDisposable Content.RootDirectory = "Content"; this.IsFixedTimeStep = true; resolution = new ResolutionChanger(_graphics, Window); } protected override void Initialize() { // TODO: Add your initialization logic here resolution.set_Width = 640; resolution.set_Height = 480; base.Initialize(); } protected override void LoadContent() { // TODO: load your game content here : sprites ... // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // Use the content manager to load our texture _texture = Content.Load<Texture2D>("Fedora_logo"); // (0, 0) is the top-left corner _position = new Vector2(0, 0); } protected override void Update(GameTime gameTime) { // TODO: This will exit from application when you use ALT+ENTER keys if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) Exit(); // TODO: This will change into FullScreen when you use ALT+ENTER keys if (Keyboard.GetState().IsKeyDown(Keys.LeftAlt) && Keyboard.GetState().IsKeyDown(Keys.Enter)) { resolution.FullScreen = !resolution.FullScreen; } // TODO: This will change game window fullscreen and graphics resolution.Update(false); base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // TODO: This add some sprites to the batcher spriteBatch.Begin(); // TODO: The most basic sprite we can add. spriteBatch.Draw(_texture, _position, Color.White); // TODO: Draws everything added to the batcher, and can only be called after 'Begin' spriteBatch.End(); // TODO: Add source code for drawing base.Draw(gameTime); } } } |
If you running you will see a window with the Fedora icon.
1 2 | [mythcat@desk MyGame001]$ dotnet run Program.cs This change in resolution! |