设为首页 收藏本站
查看: 4622|回复: 0

Windows Phone 7 编程实践–XNA变身记

[复制链接]

尚未签到

发表于 2015-5-10 09:52:51 | 显示全部楼层 |阅读模式
我们以MSDN上Using a Basic Effect with Texturing介绍的XNA Game 4.0 TexturedQuad_Sample为例,说明如何将Windows上运行的XNA程序快速部署到Windows Phone 7手机和Xbox 360上测试并运行。

DSC0000.jpg
作品目标:Windows Phone 7 开发的实用手册

Windows Phone 7 编程实践

参考资料:
Programming Windows Phone 7
MSDN Library -- Windows Phone Development
UI Design and Interaction Guide for Windows Phone 7 v2.0
Designing Web Sites for Phone Browsers
Develop for Windows Phone & XBOX 360 Code Sample
Windows Phone 7 Application Certification Requirements

在本书的整理过程中,力求从深度和不同的侧面诠释Windows Phone的开发技术。由于个人能力和水平有待提高,很多问题的分析还很肤浅。敬请各位不吝赐教,提出改进建议。


目录
前言   
TexturedQuad程序说明
MSDN上的英文说明
Windows上运行结果
变身Windows Phone 7程序
生成新的Windows Phone 7工程
在Windows Phone 7的模拟器中运行结果
代码究竟发生变化了吗?
Game1.cs
Program.cs
揭秘



前言


XNA是Windows Phone 7在应用和游戏方面的主要开发方式,XNA起源于游戏界大名鼎鼎的DirectX,是微软对于C#版DirectX的修正和扩充版本。
如果您以前就做过XNA在Windows和Xbox 360,那么您很幸运,因为您以前做的XNA的程序将很容易部署到Windows Phone 7的手机上,几乎不用改代码。这是多么不可思议的事,曾有人说明,没有一个程序可以运行在所有的操作系统上。
我们以MSDN上Using a Basic Effect with Texturing介绍的XNA Game 4.0 TexturedQuad_Sample为例,说明如何将Windows上运行的XNA程序快速部署到Windows Phone 7手机和Xbox 360上测试并运行。
特别感谢微软技术专家Bruce.Wang在TechED云计算中国巡演青岛站的精彩讲解,让我知道了XNA的特性。可以写下这篇文章。

TexturedQuad程序说明

MSDN上的英文说明


Using a Basic Effect with Texturing
http://msdn.microsoft.com/en-us/library/bb464051.aspx
Demonstrates how to create and draw a simple quad—two triangles that form a rectangle or square—using DrawUserIndexedPrimitives.
This sample introduces the Quad class, which is used to construct a quad with a list of vertices and indices suitable for drawing with DrawUserIndexedPrimitives. The sample also demonstrates how to use BasicEffect to render the quad and to apply a texture to the primitive. For more information about BasicEffect, see Creating a Basic Effect.
DSC0001.jpg
The code in the topic shows you the technique for creating and drawing a quad. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.
Download TexturedQuad_Sample.zip.

Windows上运行结果


DSC0002.png
这里姑且不谈程序本身实现的功能,下面我们就不手工修改一句代码的情况下,将这个程序移植到Windows Phone 7和Xbox 360上。前提条件是,您的系统中已经为VS2010安装了Windows Phone 7开发所有的环境和开发包,这里就不再累述安装过程。

变身Windows Phone 7程序

生成新的Windows Phone 7工程


在[Solution Explorer]中,选中" TexturedQuadWindows"工程,点击右键,如下图
DSC0003.png
选择[Create Copy of Project for Windows Phone],VS2010会自动为我们建立一个Windows Phone上运行的工程。如下图。

DSC0004.png

如图所示名称为[Windows Phone Copy of TexturedQuadWindows]就是系统为我们生成的,可运行在Windows Phone的工程。下面我们就修改工程的名称为TexturedQuadWindowsPhone,并将其设置为默认启动程序。
DSC0005.png

在Windows Phone 7的模拟器中运行结果


重新编译程序,在Windows Phone 7的模拟器中Debug运行,结果如下:
DSC0006.png

代码究竟发生变化了吗?


代码发生变化了吗?回答是否定的。
仔细瞧瞧Game1.cs和Program.cs文件有什么玄机。Game1.cs


看看Game1.cs中发现在Update方法中有"#if WINDOWS"的字样,原来在windows下程序会处理Keyboard。
///
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
///
///Provides a snapshot of timing values.
protectedoverridevoid Update(GameTime gameTime)
        {
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

#if WINDOWS
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
#endif

// TODO: Add your update logic here

base.Update(gameTime);
        }Program.cs


在Program.cs文件中我们发现有"#if WINDOWS || XBOX"字样。
namespace TexturedQuadWindows
{
#if WINDOWS || XBOX
staticclassProgram
    {
///
/// The main entry point for the application.
///
staticvoid Main(string[] args)
        {
using (Game1 game = newGame1())
            {
                game.Run();
            }
        }
    }
#endif
}
揭秘


其实在TexturedQuadWindows工程和TexturedQuadWindowsPhone中使用的Game1.cs和Program.cs,以及其他程序运行所需的文件都是一致的,但是为什么我们在移植过程中没有遇到任何阻碍呢。原程序在编写时已经考虑到移植的问题了,就是Game1.cs中的
#if WINDOWS
if (Keyboard.GetState().IsKeyDown(Keys.Escape))
this.Exit();
#endif
以及Program.cs中的
#if WINDOWS || XBOX
staticclassProgram
    {
///
/// The main entry point for the application.
///
staticvoid Main(string[] args)
        {
using (Game1 game = newGame1())
            {
                game.Run();
            }
        }
    }
#endif

原程序已经根据Windows、Windows Phone和Xbox 360不同的硬件特点做了区分。因此我们在移植过程中借着东风,一蹴而就。
总结,我们写XNA程序时也应注意这方面的特点,编写好"#if WINDOWS"和"#if WINDOWS || XBOX",方便移植程序,从容变身。
将原程序变身为Xbox 360上运行的程序方法相同,请身临其境去尝试。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-65401-1-1.html 上篇帖子: Windows Phone 7 学习志(开篇 下篇帖子: Windows Phone 7 软件体系结构
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表