I am trying to write my first XNA game, and I would like to enable ALT + ENTER functionality to switch between full-screen and windowed modes. My game is extremely simple right now, nothing out of the ordinary. I can make it work in both windowed (default) and full-screen mode (by calling ToggleFullScreen) in my LoadContent function, and everything works fine.
However, when I call ToggleFullScreen in my update function using the same code, my game fails. If I run the window and switch to full screen mode, it seems to freeze, displaying only one frame and not accepting keyboard input (I need CTRL + ALT + DEL). If I started full-screen mode and switched to windowed mode, these are errors in DrawIndexedPrimitive with the message "The current vertex declaration does not include all the elements necessary for the current vertex shader. Normal0 is missing." (which is wrong, my vertex buffer is VertexPositionNormalTexture and contains data, and the status of GraphicsDevice is Normal).
Both of these problems seem to indicate a loss of connection with the device, but I cannot understand why. Every resource I found on the Internet says that turning on full-screen mode is as easy as calling the ToggleFullScreen function without mentioning rebooting devices or buffers. Any ideas?
Edit: here is some code with extraneous left:
protected override void LoadContent()
{
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 600;
graphics.ToggleFullScreen();
basicEffect = new BasicEffect(graphics.GraphicsDevice);
}
protected override void Update(GameTime gameTime)
{
if (k.IsKeyDown(Keys.Enter) && (k.IsKeyDown(Keys.LeftAlt) || k.IsKeyDown(Keys.RightAlt)) && !oldKeys.Contains(Keys.Enter)) {
graphics.ToggleFullScreen();
gameWorld.Refresh();
}
GraphicsBuffers gb = gameWorld.Render(graphics.GraphicsDevice);
graphics.GraphicsDevice.SetVertexBuffer(gb.vb, 0);
graphics.GraphicsDevice.Indices = gb.ib;
}
protected override void Draw(GameTime gameTime)
{
GraphicsBuffers gb = gameWorld.Render(graphics.GraphicsDevice);
foreach (EffectPass effectPass in basicEffect.CurrentTechnique.Passes)
{
effectPass.Apply();
basicEffect.Texture = textures;
graphics.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, gb.vb.VertexCount, 0, gb.ib.IndexCount / 3);
}
}
source
share