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

2015-09-30 14:31:17来源:作者:人点击

手机设备里面,会有横竖屏的状态,一般会有3种情况,一个是竖屏,一个是右横屏,一个是左横屏。XNA游戏开发中,横屏的设置是通过GraphicsDeviceManager类的SupportedOrientations属性来设置的,GraphicsDeviceManager类在XNA类库介绍中提到的该类型是非常重要的。它为开发者提供方法来管理目标设备的显卡资源。简单地说就是调用显卡的一个接口,该对象的GraphicsDevice属性代表当前目标设备的显卡。

代码示例:

usingSystem; usingSystem.Collections.Generic; usingSystem.Linq; usingMicrosoft.Xna.Framework; usingMicrosoft.Xna.Framework.Audio; usingMicrosoft.Xna.Framework.Content; usingMicrosoft.Xna.Framework.GamerServices; usingMicrosoft.Xna.Framework.Graphics; usingMicrosoft.Xna.Framework.Input; usingMicrosoft.Xna.Framework.Input.Touch; usingMicrosoft.Xna.Framework.Media; namespaceSlXnaApp1 { ///<summary>///Thisisthemaintypeforyourgame ///</summary>publicclassGame1:Microsoft.Xna.Framework.Game { GraphicsDeviceManagergraphics; SpriteBatchspriteBatch; SpriteFontrotationFont; publicGame1() { graphics=newGraphicsDeviceManager(this); Content.RootDirectory="Content"; //Framerateis30fpsbydefaultforWindowsPhone. TargetElapsedTime=TimeSpan.FromTicks(333333); //添加横屏和竖屏的支持 graphics.SupportedOrientations=DisplayOrientation.Portrait|DisplayOrientation.LandscapeLeft|DisplayOrientation.LandscapeRight; } ///<summary>///Allowsthegametoperformanyinitializationitneedstobeforestartingtorun. ///Thisiswhereitcanqueryforanyrequiredservicesandloadanynon-graphic ///relatedcontent.Callingbase.Initializewillenumeratethroughanycomponents ///andinitializethemaswell. ///</summary>protectedoverridevoidInitialize() { //TODO:Addyourinitializationlogichere base.Initialize(); } ///<summary>///LoadContentwillbecalledoncepergameandistheplacetoload ///allofyourcontent. ///</summary>protectedoverridevoidLoadContent() { //CreateanewSpriteBatch,whichcanbeusedtodrawtextures. spriteBatch=newSpriteBatch(GraphicsDevice); //TODO:usethis.Contenttoloadyourgamecontenthere rotationFont=Content.Load<SpriteFont>("rotationFont"); } ///<summary>///UnloadContentwillbecalledoncepergameandistheplacetounload ///allcontent. ///</summary>protectedoverridevoidUnloadContent() { //TODO:UnloadanynonContentManagercontenthere } ///<summary>///Allowsthegametorunlogicsuchasupdatingtheworld, ///checkingforcollisions,gatheringinput,andplayingaudio. ///</summary>///<paramname="gameTime">Providesasnapshotoftimingvalues.</param>protectedoverridevoidUpdate(GameTimegameTime) { //Allowsthegametoexit if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed) this.Exit(); base.Update(gameTime); } ///<summary>///Thisiscalledwhenthegameshoulddrawitself. ///</summary>///<paramname="gameTime">Providesasnapshotoftimingvalues.</param>protectedoverridevoidDraw(GameTimegameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); //TODO:Addyourdrawingcodehere spriteBatch.Begin(); spriteBatch.DrawString(rotationFont,"你现在手机的摆放状态是:"+Window.CurrentOrientation.ToString()+".",newVector2(50,50),Color.White); spriteBatch.End(); base.Draw(gameTime); } } }

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

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台