An important element of a game is changing the resolution and switching the game to fullscreen.
However, I would not like to complicate the tutorial so I will show you simply how to do this in MonoGame.
First, create a new C# file and name it ResolutionChanger.cs, and add this source code:
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace MyGame001 { public class ResolutionChanger { private int myWidth; private int myHeight; private bool changeFullScreen; private Rectangle mLastWindowedBounds; private GraphicsDeviceManager _graphics; private GameWindow myWindow; public ResolutionChanger (GraphicsDeviceManager graphics, GameWindow window) { mLastWindowedBounds = window.ClientBounds; myHeight = 1048; myWidth = 840; _graphics = graphics; myWindow = window; } public void Update (bool set_fullscreen) { if (_graphics.GraphicsDevice.PresentationParameters.BackBufferWidth != myWidth || _graphics.GraphicsDevice.PresentationParameters.BackBufferHeight != myHeight || set_fullscreen) { bool setUserResizing = myWindow.AllowUserResizing; if (!_graphics.IsFullScreen && !setUserResizing) { myWindow.AllowUserResizing = true; } mLastWindowedBounds = myWindow.ClientBounds; _graphics.PreferredBackBufferWidth = myWidth; _graphics.PreferredBackBufferHeight = myHeight; _graphics.ApplyChanges(); if (!_graphics.IsFullScreen && !setUserResizing) { myWindow.AllowUserResizing = false; } Console.WriteLine("This change in resolution!"); } if (changeFullScreen != _graphics.IsFullScreen) { _graphics.IsFullScreen = changeFullScreen; if (!_graphics.IsFullScreen) { myWidth = mLastWindowedBounds.Width; myHeight = mLastWindowedBounds.Height; Update(false); } Console.WriteLine("FullScreen is changed!"); } } public int set_Width { get { return myWidth; } set { myWidth = value; } } public int set_Height { get { return myHeight; } set { myHeight = value; } } public bool FullScreen { get { return changeFullScreen; } set { changeFullScreen = value; if (changeFullScreen) { myWidth = _graphics.GraphicsDevice.DisplayMode.Width; myHeight = _graphics.GraphicsDevice.DisplayMode.Height; } } } } } |
This class named ResolutionChanger will help you to change the game resolution and set the fullscreen mode.
Into the Game1.cs file the source code comes with these changes:
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 | 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; 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 ... } protected override void Update(GameTime gameTime) { // 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(); // 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; } // 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: Add source code for drawing base.Draw(gameTime); } } } |
Run it and test with ALT+ENTER keys for fullscreen and ESC key to exit from the game.
The game will start with the default resolution of 640 x 480 pixels.