我能够使用以下类切换全屏/窗口模式:
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private Model grid;
Model tank;
int zoom = 1;
Texture2D thumb;
Vector2 position = new Vector2( 400, 240 );
Vector2 velocity = new Vector2( -1, -1 );
Matrix projection, view;
public Game1()
{
graphics = new GraphicsDeviceManager( this );
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
projection = Matrix.CreatePerspectiveFieldOfView( MathHelper.PiOver4,
GraphicsDevice.Viewport.AspectRatio,
10,
20000 );
view = Matrix.CreateLookAt( new Vector3( 1500, 550, 0 ) * zoom + new Vector3( 0, 150, 0 ),
new Vector3( 0, 150, 0 ),
Vector3.Up );
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch( GraphicsDevice );
// TODO: use this.Content to load your game content here
thumb = Content.Load<Texture2D>( "GameThumbnail" );
grid = Content.Load<Model>( "grid" );
tank = Content.Load<Model>( "tank" );
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
int ToggleDelay = 0;
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update( GameTime gameTime )
{
// Allows the game to exit
if ( GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed )
this.Exit();
var kbState = Keyboard.GetState();
if ( ( kbState.IsKeyDown( Keys.LeftAlt ) || kbState.IsKeyDown( Keys.RightAlt ) ) &&
kbState.IsKeyDown( Keys.Enter ) && ToggleDelay <= 0 )
{
graphics.ToggleFullScreen();
ToggleDelay = 1000;
}
if ( ToggleDelay >= 0 )
{
ToggleDelay -= gameTime.ElapsedGameTime.Milliseconds;
}
// TODO: Add your update logic here
int x, y;
x = (int)position.X;
y = (int)position.Y;
x += (int)velocity.X;
y += (int)velocity.Y;
if ( x > 480 - 64 )
velocity.X = +1;
if ( x < 0 )
velocity.X = -1;
if ( y < 0 )
velocity.Y = +1;
if ( y > 800 - 64 )
velocity.Y = -1;
position.X = x;
position.Y = y;
base.Update( gameTime );
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw( GameTime gameTime )
{
GraphicsDevice.Clear( Color.CornflowerBlue );
Matrix rotation = Matrix.CreateRotationY( gameTime.TotalGameTime.Seconds * 0.1f );
// Set render states.
GraphicsDevice.BlendState = BlendState.Opaque;
GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise;
GraphicsDevice.DepthStencilState = DepthStencilState.Default;
GraphicsDevice.SamplerStates[ 0 ] = SamplerState.LinearWrap;
grid.Draw( rotation, view, projection );
DrawModel( tank, rotation, view, projection );
// TODO: Add your drawing code here
spriteBatch.Begin();
spriteBatch.Draw( thumb, new Rectangle( (int)position.X, (int)position.Y, 64, 64 ), Color.White );
spriteBatch.End();
base.Draw( gameTime );
}
public void DrawModel( Model model, Matrix world, Matrix view, Matrix projection )
{
// Set the world matrix as the root transform of the model.
model.Root.Transform = world;
// Allocate the transform matrix array.
Matrix[] boneTransforms = new Matrix[ model.Bones.Count ];
// Look up combined bone matrices for the entire model.
model.CopyAbsoluteBoneTransformsTo( boneTransforms );
// Draw the model.
foreach ( ModelMesh mesh in model.Meshes )
{
foreach ( BasicEffect effect in mesh.Effects )
{
effect.World = boneTransforms[ mesh.ParentBone.Index ];
effect.View = view;
effect.Projection = projection;
//switch (lightMode)
//{
// case LightingMode.NoLighting:
// effect.LightingEnabled = false;
// break;
// case LightingMode.OneVertexLight:
// effect.EnableDefaultLighting();
// effect.PreferPerPixelLighting = false;
// effect.DirectionalLight1.Enabled = false;
// effect.DirectionalLight2.Enabled = false;
// break;
// case LightingMode.ThreeVertexLights:
//effect.EnableDefaultLighting();
//effect.PreferPerPixelLighting = false;
// break;
// case LightingMode.ThreePixelLights:
// effect.EnableDefaultLighting();
// effect.PreferPerPixelLighting = true;
// break;
//}
effect.SpecularColor = new Vector3( 0.8f, 0.8f, 0.6f );
effect.SpecularPower = 16;
effect.TextureEnabled = true;
}
mesh.Draw();
}
}
}