WP8:在Cocos2d-x中使用OpenXLive

一.    Cocos2d-x for Windows Phone

到2013年底,几大手游引擎都陆续支持WP8了,特别是Unity3D和Cocos2d-x。有过游戏开发经验的朋友们应该对这两个引擎不陌生,WP8对Native C++的支持,使得其他平台上用C++编写的游戏移植到WP8平台上变得非常简单。Cocos2d-x在2.2.1版本之前在WP8上只支持纯Direct3D工程模式,目前在Github上已提供了支持XAML的混合托管模式,传送门:https://github.com/MSOpenTech/cocos2d-x/tree/wp8-xaml2

Git下来之后打开cocos2d-wp8.vc2012.sln可以看到工程结构如下:

其中HelloCpp-XAML采用一个XAML与Direct3D组件混合的托管模式,是一个标准的WP8工程模板,编译并运行可见到:

其中的C#项目负责Windows Phone常规控件和页面逻辑,C++项目是一个基于Direct3D的Windows Phone Runtime Component,负责所有的游戏逻辑,两者通过DrawingSurface或DrawingSurfaceBackgroundGrid控件混入Direct3D图像,以非常小的性能代价获取托管代码的优秀特性。DrawingSurface 控件允许使用 Direct3D 来呈现显示在 XAML 控件后方或与其并列显示的图形。

在托管的XAML中,可以发现Direct3DInterop类在XAML引擎和Direct3D之间建立起了一个通信桥梁,作为XAML引擎和Direct3D代码之间的代理。在Direct3DInterop的实现中,CreateContentProvider方法初始化了一个Direct3DContentProvider类的实例,并强制转化为IDrawingSurfaceContentProvider,这个接口并没有任何内容,因为它并不是由代码实现的接口,而是使用WRL(Windows Runtime Library)实现的,将类强制转化为Windows Phone Runtime Component接口,以便由XAML引擎访问。

更多关于托管的XAML和Direct3D引擎的通信,请查看MSDN文档,传送门:http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207012%28v=vs.105%29.aspx

 

二.    演练:在Cocos2d-x中调用OpenXLive

OpenXLive是Windows Phone和Windows 8平台上最大的游戏社交网络平台,对Windows Phone游戏的开发提供了很多非常优秀的辅助功能,比如游戏基础功能(积分榜、成就、公告、在线玩家等)、推广功能(推荐游戏、推广墙)、社交功能(分享、动态、任务、交友)以及支付功能(支付宝、点卡支付、礼品卡)等一些列相关功能。除了OpenXLive之外,一批优秀的第三方库基本都是由C#编写的,如果需要在Direct3D中使用到这些功能,就需要采用拙者在此前提到的一个解决方案,传送门:

《WP8:在WinRT组件(C++)中调用C#类库的解决方案》

以下我们以OpenXLive为例子,介绍如何在Cocos2d-x中进行调用。

1.创建文件并添加引用

首先在cocos2d-x\samples\Cpp\HelloCpp\Classes文件目录下创建一个OpenXLiveHelper目录,并在该目录下添加以下三个文件:ICallback.h、XLiveDelegate.h和XLiveDelegate.cpp,并将他们添加到HelloCppComponent项目中:

2.定义WinRT接口ICallback

接下来在ICallback.h中定义一个WinRT接口,这个接口将在C#项目中具体实现:

1 // A callback interface for C# code to implement.
2 public interface class ICallback
3 {
4 };

3.定义C++托管类XLiveDelegate

接下来定义一个C++托管类,所有Direct3D游戏逻辑通过这个托管类,利用ICallback接口调用具体的C#代码,这里采用单例模式管理这个托管类,在XLiveDelegate.h文件中添加以下代码:

 1 #include "ICallback.h"
 2 
 3 namespace PhoneDirect3DXamlAppComponent
 4 {
 5     namespace OpenXLiveHelper
 6     {
 7         // A native class to pass and store an ICallback instance, calling C# code via the ICallback object.
 8         [Windows::Foundation::Metadata::WebHostHidden]
 9         public ref class XLiveDelegate sealed    
10         {
11         public:
12             // Gets the single XLiveDelegate instance.
13             static XLiveDelegate^ GetInstance();
14             // Set callback into the XLiveDelegate instance.
15             void SetCallback(ICallback^ callback);
16             // Gets or sets the callback.
17             property ICallback^ GlobalCallback;
18 
19         private:
20             XLiveDelegate();
21             static XLiveDelegate^ m_Instance;
22         };
23     }
24 }

4.实现C++托管类XLiveDelegate

接下来实现XLiveDelegate这个C++托管类,在XLiveDelegate.cpp文件中添加以下代码:

 1 #include "pch.h"
 2 #include "XLiveDelegate.h"
 3 
 4 namespace PhoneDirect3DXamlAppComponent
 5 {
 6     namespace OpenXLiveHelper
 7     {
 8         XLiveDelegate::XLiveDelegate()
 9         {
10         }
11 
12         XLiveDelegate^ XLiveDelegate::GetInstance()
13         {
14             if (m_Instance == nullptr)
15             {
16                 m_Instance = ref new XLiveDelegate();
17             }
18             return m_Instance;
19         }
20 
21         void XLiveDelegate::SetCallback(ICallback^ callback)
22         {
23             GlobalCallback = callback;
24         }
25 
26         XLiveDelegate^ XLiveDelegate::m_Instance;
27     }
28 }

5.定义实现ICallback接口的C#类XLiveCallback

在C#项目HelloCpp中,在OpenXLive\OpenXLiveHelper中添加XLiveCallback.cs文件,并添加以下代码:

1 using PhoneDirect3DXamlAppComponent.OpenXLiveHelper;
2 
3 namespace PhoneDirect3DXamlAppInterop
4 {
5     public sealed class XLiveCallback : ICallback
6     {
7     }
8 }

6.根据需求补充ICallback的接口方法,并在XLiveCallback中实现

例如需要在Direct3D游戏逻辑中获取一个成就,则在ICallback.h中定义一个接口方法:

1 // Award an achievement completed event.
2 event Windows::Foundation::EventHandler<CompletedEventArgs^>^ OnAwardComplted;
3 void Achievement_Award(Platform::String^ achievementKey);

在XLiveCallback.cs中实现:

 1 public void Achievement_Award(string achievementKey)
 2 {
 3     Achievement ac = new Achievement(XLiveGameManager.CurrentSession, achievementKey);
 4     ac.AwardCompleted += ac_AwardCompleted;
 5     ac.Award();
 6 }
 7 
 8 void ac_AwardCompleted(object sender, AsyncEventArgs e)
 9 {
10     if (OnAwardComplted != null)
11     {
12         OnAwardComplted(sender, new CompletedEventArgs(e.Result.ReturnValue, e.Result.ErrorCode, e.Result.ErrorMessage));
13     }
14 }
15     

 

观察上述逻辑,可以发现这实现过程其实是一个适配器模式的实现:

其中的ICallback为目标接口(Target),Achievement为需要适配的类(Adaptee),XLiveCallback则为适配器(Adapter),将原有接口转换为目标接口,而XLiveDelegate则为客户端(Client),负责管理和调用目标接口,而不必关心具体的适配接口。

至于其余的OpenXLive接口,拙者认为需要从游戏逻辑中向外POST并且获取返回通知的接口才需要进行封装,而UI跳转或其他GET类型的接口只需要在托管XAML示例中调用即可,附上拙者封装的一些接口。

 ICallback.h源码下载

posted @ 2013-12-26 15:12  zhxilin  阅读(1921)  评论(3编辑  收藏  举报