Reputation: 55
I'm trying to make the game go fullscreen the moment you press the button F, but it doesn't work, I'm thinking because it has to first reload the application for it to take affect, so how do I do that?
Code:
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
if (keyboard.IsKeyDown(Keys.F))
{
WINDOW_WIDTH = 1280;
WINDOW_HEIGHT = 720;
}
base.Update(gameTime);
}
Upvotes: 1
Views: 299
Reputation: 868
I've done some light research (googled 'XNA 4 toggle fullscreen'), and found that there is a ToggleFullScreen-method: http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphicsdevicemanager.togglefullscreen.aspx
I have also applied the fix I mentioned earlier in a comment, to avoid toggling fullscreen every frame
public KeyboardState PreviousKeyboardState = Keyboard.GetState();
public Boolean FullscreenMode = false;
public Vector2 WindowedResolution = new Vector2(800,600);
public Vector2 FullscreenResolution = new Vector2(1280, 720);
public void UpdateDisplayMode(bool fullscreen, Vector2 resolution)
{
graphics.PreferredBackBufferWidth = (int)resolution.X;
graphics.PreferredBackBufferHeight = (int)resolution.Y;
graphics.IsFullScreen = fullscreen;
graphics.ApplyChanges();
}
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
UpdateDisplayMode(FullscreenMode);
}
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
var keyboardState = Keyboard.GetState();
if (keyboardState.IsKeyDown(Keys.F) && PreviousKeyboardState.IsKeyUp(Keys.F))
{
if (FullscreenMode)
UpdateDisplayMode(false, WindowedResolution);
else
UpdateDisplayMode(true, FullscreenResolution);
}
PreviousKeyboardState = keyboardState;
base.Update(gameTime);
}
Upvotes: 1
Reputation: 728
The reason why it is not updating is because when you press the 'F' key you just set your variables to fullscreen size. In order to actually resize the window you have to do the same as in your constructor:
graphics.PreferredBackBufferWidth = WINDOW_WIDTH;
graphics.PreferredBackBufferHeight = WINDOW_HEIGHT;
in addition to that you may also want to set graphics.IsFullScreen = true;
Upvotes: 0