XNA游戏开发中横竖屏的设置

移动开发 游戏开发
手机设备里面,会有横竖屏的状态,一般会有3种情况,一个是竖屏,一个是右横屏,一个是左横屏,横屏的设置是通过GraphicsDeviceManager类的SupportedOrientations属性来设置的,GraphicsDeviceManager类在XNA类库介绍中提到的该类型是非常重要的。

手机设备里面,会有横竖屏的状态,一般会有3种情况,一个是竖屏,一个是右横屏,一个是左横屏。XNA游戏开发中,横屏的设置是通过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 SlXnaApp1  
  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.     }  

 

原文链接:http://www.cnblogs.com/linzheng/archive/2012/04/15/2450274.html

【编辑推荐】

  1. 爆料:C#和XNA可以在iOS和Android平台使用
  2. 详解Windows Phone XNA 4.0 3D游戏开发
责任编辑:王晓东 来源: 博客
相关推荐

2012-05-28 15:55:47

XNA 重力感应

2013-05-23 10:51:28

Android开发移动开发横竖屏切换

2010-09-08 11:26:26

Windows PhoXNA 4.0 3D游戏开发

2017-07-25 09:55:10

iOS横竖屏旋转

2010-08-10 09:11:12

Windows PhoNXA

2013-08-21 11:15:54

iOS横竖屏方案

2011-07-29 10:21:03

iPad 横竖屏 切换

2016-09-18 10:51:01

JavascriptHtml5移动应用

2010-01-25 15:23:12

Android横竖屏切

2011-06-08 15:05:43

J2ME

2013-07-29 05:04:19

Cocos2dx横屏竖

2012-05-25 15:20:38

XNA

2012-12-13 13:27:29

Corona SDK

2013-05-20 16:12:23

2013-05-16 15:08:33

2009-07-01 09:27:36

触摸屏驱动WinCE

2010-03-08 19:03:23

Python脚本

2011-08-24 13:56:12

Lua游戏

2013-06-27 13:46:41

游戏开发

2018-03-06 09:33:38

机器学习游戏开发
点赞
收藏

51CTO技术栈公众号