XNA遊戲:橫豎屏設置

手機設備裏面,會有橫豎屏的狀態,一般會有3種情況,一箇中是豎屏,一個是右橫屏,一個是左橫屏,橫屏的設置是通過GraphicsDeviceManager類的SupportedOrientations屬性來設置的,GraphicsDeviceManager類在XNA類庫介紹中提到的該類型是非常重要的。它爲開發者提供方法來管理目標設備的顯卡資源。簡單地說就是調用顯卡的一個接口,該對象的GraphicsDevice屬性代表當前目標設備的顯卡。

示例:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using Microsoft.Xna.Framework;  
  5. using Microsoft.Xna.Framework.Audio;  
  6. using Microsoft.Xna.Framework.Content;  
  7. using Microsoft.Xna.Framework.GamerServices;  
  8. using Microsoft.Xna.Framework.Graphics;  
  9. using Microsoft.Xna.Framework.Input;  
  10. using Microsoft.Xna.Framework.Input.Touch;  
  11. using Microsoft.Xna.Framework.Media;  
  12.  
  13. namespace RotationSample  
  14. {  
  15.     /// <summary> 
  16.     /// This is the main type for your game  
  17.     /// </summary> 
  18.     public class Game1 : Microsoft.Xna.Framework.Game  
  19.     {  
  20.         GraphicsDeviceManager graphics;  
  21.         SpriteBatch spriteBatch;  
  22.         SpriteFont rotationFont;  
  23.  
  24.         public Game1()  
  25.         {  
  26.             graphics = new GraphicsDeviceManager(this);  
  27.             Content.RootDirectory = "Content";  
  28.  
  29.             // Frame rate is 30 fps by default for Windows Phone.  
  30.             TargetElapsedTime = TimeSpan.FromTicks(333333);  
  31.             //添加橫屏和豎屏的支持  
  32.             graphics.SupportedOrientations = DisplayOrientation.Portrait | DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;  
  33.         }  
  34.  
  35.         /// <summary> 
  36.         /// Allows the game to perform any initialization it needs to before starting to run.  
  37.         /// This is where it can query for any required services and load any non-graphic  
  38.         /// related content.  Calling base.Initialize will enumerate through any components  
  39.         /// and initialize them as well.  
  40.         /// </summary> 
  41.         protected override void Initialize()  
  42.         {  
  43.             // TODO: Add your initialization logic here  
  44.  
  45.             base.Initialize();  
  46.         }  
  47.  
  48.         /// <summary> 
  49.         /// LoadContent will be called once per game and is the place to load  
  50.         /// all of your content.  
  51.         /// </summary> 
  52.         protected override void LoadContent()  
  53.         {  
  54.             // Create a new SpriteBatch, which can be used to draw textures.  
  55.             spriteBatch = new SpriteBatch(GraphicsDevice);  
  56.  
  57.             // TODO: use this.Content to load your game content here  
  58.             rotationFont = Content.Load<SpriteFont>("rotationFont");  
  59.         }  
  60.  
  61.         /// <summary> 
  62.         /// UnloadContent will be called once per game and is the place to unload  
  63.         /// all content.  
  64.         /// </summary> 
  65.         protected override void UnloadContent()  
  66.         {  
  67.             // TODO: Unload any non ContentManager content here  
  68.         }  
  69.  
  70.         /// <summary> 
  71.         /// Allows the game to run logic such as updating the world,  
  72.         /// checking for collisions, gathering input, and playing audio.  
  73.         /// </summary> 
  74.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  75.         protected override void Update(GameTime gameTime)  
  76.         {  
  77.             // Allows the game to exit  
  78.             if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)  
  79.                 this.Exit();  
  80.  
  81.             base.Update(gameTime);  
  82.         }  
  83.  
  84.         /// <summary> 
  85.         /// This is called when the game should draw itself.  
  86.         /// </summary> 
  87.         /// <param name="gameTime">Provides a snapshot of timing values.</param> 
  88.         protected override void Draw(GameTime gameTime)  
  89.         {  
  90.             GraphicsDevice.Clear(Color.CornflowerBlue);  
  91.  
  92.             // TODO: Add your drawing code here  
  93.             spriteBatch.Begin();  
  94.             spriteBatch.DrawString(rotationFont, "你現在手機的擺放狀態是:" + Window.CurrentOrientation.ToString() + ".", new Vector2(50, 50), Color.White);  
  95.             spriteBatch.End();  
  96.  
  97.             base.Draw(gameTime);  
  98.         }  
  99.     }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章