鉴于诺基亚(微软移动这个没人用的手机)开发者比较少,cocos2dx移植方面更是少的问题,总结一下WP8移植方面的资料,希望对大家有用,自己也当作笔记留念。

1、WP8方面有两种方式创建项目,HelloCpp和TestCpp就是这样,XAML方式和纯c++方式。最好选择xaml方式,因为你有可能会c++和c#进行交互。废话不说,有图有真相。

2、说到c++和c#交互,其实叫C++/CX(C++/CX其实是微软在Win8开发平台下,对C++语言的一种扩展),下面就讲述其用法,概念可以去百度。

a)  c++调用c#,比如我想获得该诺基亚的UniqueID,直接上代码吧,了解c#委托的童鞋应该不难理解,if...baidu...

****************************************************************************

//WP8DataManager.h
#ifndef __WP8DataManager_H__
#define __WP8DataManager_H__
namespace PhoneDirect3DXamlAppComponent
{
public delegate Platform::String^ GetUniqueIDDelegate(); public ref class WP8DataManager sealed
{
public:
WP8DataManager(void)
{ }
///DeviceInfo
//此方法将在c#中调用
void SetGetUniqueIDDelegate(GetUniqueIDDelegate^ del)
{
m_getUniqueIDDelegate = del;
}
//获得的id,c++直接调用
Platform::String^ GetUniqueID()
{
if(m_getUniqueIDDelegate)
{
return m_getUniqueIDDelegate->Invoke();
}
return "";
}
///DeviceInfo end
private:
property static GetUniqueIDDelegate^ m_getUniqueIDDelegate;
};
}
#endif // __WP8DataManager_H__ **************************************************************************** //MainPage.xaml.cs
namespace PhoneDirect3DXamlAppInterop
{
public partial class MainPage : PhoneApplicationPage
{
// other demo... //datamanager
private WP8DataManager m_dataManager = null; private void DrawingSurface_Loaded(object sender, RoutedEventArgs e)
{
if (m_d3dInterop == null)
{
//demo ......
} if (m_dataManager == null)
{
m_dataManager = new WP8DataManager();
m_dataManager.SetGetUniqueIDDelegate(getUniqueID);
}
} public String getUniqueID()
{
/*try {
byte[] uniqueIDbytes = (byte[])DeviceExtendedProperties.GetValue("DeviceUniqueId");
string uniqueID = System.Convert.ToBase64String(uniqueIDbytes);
}catch(Exception ex){
MessageBox.Show(ex.Message, "Failed", MessageBoxButton.OK);
}*/
return "abc";
}
}
} **************************************************************************** //your cpp demo
PhoneDirect3DXamlAppComponent::WP8DataManager^ manager = ref new PhoneDirect3DXamlAppComponent::WP8DataManager();
Platform::String^ id = manager->GetUniqueID(); ****************************************************************************

b)  c#调用c++,比如自己添加了一个EditBox和一个Button,通过代码将EditBox中的值返回给c++中,详见Demo,我的例子是输入一个兑换码然后返回给c++部分

****************************************************************************

//MainPage.xaml.cs
//此处为c#调用,当button按键触发时
m_d3dInterop.OnWP8RedeemResult(WP8RedeemEventType.WP8RedeemSuccess,""); **************************************************************************** //Direct3DInterop.h
void OnWP8RedeemResult(WP8RedeemEventType type,Platform::String^ code);
//Direct3DInterop.cpp
void Direct3DInterop::OnWP8RedeemResult(WP8RedeemEventType type,Platform::String^ code)
{
std::lock_guard<std::mutex> guard(mMutex);
std::shared_ptr<WP8RedeemEvent> e(new WP8RedeemEvent(type,code));
mInputEvents.push(e);
} **************************************************************************** //WP8DataEvent.h
#ifndef __WP8DATA_EVENT__
#define __WP8DATA_EVENT__ #include <agile.h>
#include "../InputEvent.h" ref class Cocos2dRenderer; namespace PhoneDirect3DXamlAppComponent
{ public enum class WP8RedeemEventType{
WP8RedeemFailed,
WP8RedeemSuccess
}; class WP8RedeemEvent: public InputEvent
{
public:
WP8RedeemEvent(WP8RedeemEventType type,Platform::String^ code);
WP8RedeemEvent(WP8RedeemEventType type); virtual void execute(Cocos2dRenderer ^ renderer); private:
Platform::Agile<Platform::String> m_code;
WP8RedeemEventType m_type;
};
}
#endif // #ifndef __WP8DATA_EVENT__ **************************************************************************** //WP8DataEvent.cpp
#include "WP8DataEvent.h"
#include "../Cocos2dRenderer.h" namespace PhoneDirect3DXamlAppComponent
{ WP8RedeemEvent::WP8RedeemEvent(WP8RedeemEventType type,Platform::String^ code)
:m_type(type),m_code(code)
{ }
WP8RedeemEvent::WP8RedeemEvent(WP8RedeemEventType type)
:m_type(type)
{ }
void WP8RedeemEvent::execute(Cocos2dRenderer ^ renderer)
{
switch(m_type)
{
case WP8RedeemEventType::WP8RedeemSuccess:
renderer->nativeRedeemSuccess(m_code.Get());
break;
case WP8RedeemEventType::WP8RedeemFailed:
renderer->nativeRedeemFailed();
break;
default: break;
}
}
} **************************************************************************** //Cocos2dRenderer.h
void nativeRedeemFailed();
void nativeRedeemSuccess(Platform::String^ code);
//Cocos2dRenderer.cpp
void Cocos2dRenderer::nativeRedeemFailed()
{
//c++ code
CCLog("nativeRedeemFailed");
}
void Cocos2dRenderer::nativeRedeemSuccess(Platform::String^ code)
{
//c++ code
CCLog("nativeRedeemSuccess code=%s",cocos2d::WP8Tran::pstos(code).c_str());
}
****************************************************************************

3、通常c#是用的字符串类型是Platform::String^,需要转换为std::string。以下的方法可以任意直接转化,是不是很方便呢

****************************************************************************

NS_CC_BEGIN

class CC_DLL WP8Tran
{
public:
static std::string tranChina(const char * str); static Platform::String^ tranChinatops(std::string str); static std::wstring stows(std::string s); static Platform::String^ stops(std::string s); static std::string wstos(std::wstring ws); static std::string pstos(Platform::String^ ps);
}; NS_CC_END **************************************************************************** NS_CC_BEGIN #define MAX_LEN (16*1024 + 1) std::string WP8Tran::tranChina(const char * pszFormat)
{
char szBuf[MAX_LEN];
strcpy(szBuf,pszFormat); WCHAR wszBuf[MAX_LEN] = {};
MultiByteToWideChar(CP_UTF8, , szBuf, -, wszBuf, sizeof(wszBuf));
OutputDebugStringW(wszBuf);
OutputDebugStringW(L"\n"); WideCharToMultiByte(CP_ACP, , wszBuf, sizeof(wszBuf), szBuf, sizeof(szBuf), NULL, FALSE); return szBuf;
} Platform::String^ tranChinatops(std::string str)
{
//return ref new Platform::String(cocos2d::WP8Tran::stows(cocos2d::WP8Tran::tranChina(str.c_str())).c_str());
return WP8Tran::stops(WP8Tran::tranChina(str.c_str()));
} std::wstring WP8Tran::stows(std::string str)
{
setlocale(LC_ALL, "chs");
const char* _Source = str.c_str();
size_t _Dsize = str.size() + ;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, , _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
} Platform::String^ WP8Tran::stops(std::string s)
{
return ref new Platform::String(stows(s).c_str());
} std::string WP8Tran::wstos(std::wstring ws)
{
std::string s;
s.assign(ws.begin(), ws.end());
return s;
} std::string WP8Tran::pstos(Platform::String^ ps)
{
return wstos(std::wstring(ps->Data()));
} NS_CC_END ****************************************************************************

4、我把WP8DataManager全部贴出来,方便在代码中直接使用,你可以直接引用到项目的根目录,如图。

WP8DataManager.h

#ifndef __WP8DataManager_H__
#define __WP8DataManager_H__ #include "cocos2d.h" namespace PhoneDirect3DXamlAppComponent
{
public delegate Platform::String^ GetUniqueIDDelegate(); public delegate void ToastWP8Delegate(Platform::String^ msg); public ref class WP8DataManager sealed
{
public:
WP8DataManager(void)
{ }
///DeviceInfo
void SetGetUniqueIDDelegate(GetUniqueIDDelegate^ del)
{
m_getUniqueIDDelegate = del;
} Platform::String^ GetUniqueID()
{
if(m_getUniqueIDDelegate)
{
return m_getUniqueIDDelegate->Invoke();
}
return "";
} void SetToastWP8Delegate(ToastWP8Delegate^ del)
{
m_toastWP8Delegate = del;
} void ToastWP8(Platform::String^ msg)
{
if(m_toastWP8Delegate)
{
m_toastWP8Delegate->Invoke(msg);
}
}
///DeviceInfo end private:
property static GetUniqueIDDelegate^ m_getUniqueIDDelegate;
property static ToastWP8Delegate^ m_toastWP8Delegate;
};
} NS_CC_BEGIN class CC_DLL WP8Tran
{
public:
static std::string tranChina(const char * str); static Platform::String^ tranChinatops(std::string str); static std::wstring stows(std::string s); static Platform::String^ stops(std::string s); static std::string wstos(std::wstring ws); static std::string pstos(Platform::String^ ps);
}; NS_CC_END class WP8DataHelper
{
public:
WP8DataHelper();
virtual ~WP8DataHelper(); static PhoneDirect3DXamlAppComponent::WP8DataManager^ sharedWP8DataManager();
static void purgeWP8Data();
};
#endif // __WP8DataManager_H__

WP8DataManager.cpp

#include "WP8DataManager.h"

NS_CC_BEGIN

#define MAX_LEN  (16*1024 + 1)

std::string WP8Tran::tranChina(const char * pszFormat)
{
char szBuf[MAX_LEN];
strcpy(szBuf,pszFormat); WCHAR wszBuf[MAX_LEN] = {};
MultiByteToWideChar(CP_UTF8, , szBuf, -, wszBuf, sizeof(wszBuf));
OutputDebugStringW(wszBuf);
OutputDebugStringW(L"\n"); WideCharToMultiByte(CP_ACP, , wszBuf, sizeof(wszBuf), szBuf, sizeof(szBuf), NULL, FALSE); return szBuf;
} Platform::String^ tranChinatops(std::string str)
{
//return ref new Platform::String(cocos2d::WP8Tran::stows(cocos2d::WP8Tran::tranChina(str.c_str())).c_str());
return WP8Tran::stops(WP8Tran::tranChina(str.c_str()));
} std::wstring WP8Tran::stows(std::string str)
{
setlocale(LC_ALL, "chs");
const char* _Source = str.c_str();
size_t _Dsize = str.size() + ;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, , _Dsize);
mbstowcs(_Dest,_Source,_Dsize);
std::wstring result = _Dest;
delete []_Dest;
setlocale(LC_ALL, "C");
return result;
} Platform::String^ WP8Tran::stops(std::string s)
{
return ref new Platform::String(stows(s).c_str());
} std::string WP8Tran::wstos(std::wstring ws)
{
std::string s;
s.assign(ws.begin(), ws.end());
return s;
} std::string WP8Tran::pstos(Platform::String^ ps)
{
return wstos(std::wstring(ps->Data()));
} NS_CC_END static PhoneDirect3DXamlAppComponent::WP8DataManager^ m_wp8dataManager = ref new PhoneDirect3DXamlAppComponent::WP8DataManager(); WP8DataHelper::WP8DataHelper()
{ } WP8DataHelper:: ~WP8DataHelper()
{ } PhoneDirect3DXamlAppComponent::WP8DataManager^ WP8DataHelper::sharedWP8DataManager()
{
return m_wp8dataManager;
} void WP8DataHelper::purgeWP8Data()
{
//delete m_wp8dataManager;
}

5、下一篇会讲一下常见的错误。

github地址:https://github.com/eoeuou/wp8-xaml欢迎来喷

Cocos2d-x for Windows Phone 用法总结的更多相关文章

  1. 【JavaScript】windows.open用法详解

    windows.open("URL","窗口名称","窗口外观设定");的用法详解 function onNewWindows(redire ...

  2. Java -cp 命令行引用多个jar包的简单写法(Windows、Linux

    1.Windows下用法 在Windows上,可以使用 用法:java your-jar-lib-folder/* your-main-class your-jar-lib-folder为存放一堆ja ...

  3. python jieba包用法总结

    # coding: utf-8 # ###jieba特性介绍 # 支持三种分词模式: # 精确模式,试图将句子最精确地切开,适合文本分析: # 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非 ...

  4. 【194】Windows 上使用 wget

    本文包括两部分,首先就是在 Windows 使用 wget 来下载文件,这样固然很好,然而问题并非这么简单,在 PowerShell 4.0 版本中增加了 Invoke-WebRequest 的别名 ...

  5. 笨办法学Python记录--习题12-14 主要是pydoc用法,raw_input,argv

    20140413 -- 习题12 - 14 1. pydoc在windows的用法,必须进入到python安装目录,执行Python -m pydoc raw_input; 网上给出了一个好玩的,不过 ...

  6. jieba

    # coding: utf-8 # ###jieba特性介绍 # 支持三种分词模式: # 精确模式,试图将句子最精确地切开,适合文本分析: # 全模式,把句子中所有的可以成词的词语都扫描出来, 速度非 ...

  7. 正确使用stl map的erase方法

    先声明:下面的文章是针对windows的用法,因为std::map的erase函数的windows的实现版本是返回一个std::map的迭代器,但是STL标准里面的该函数的返回值确是: map.era ...

  8. VC++6.0环境下调试c语言代码的方法和步骤_附图

    1.C语言程序四步开发步骤 (1)编辑.可以用任何一种编辑软件将在纸上编写好的C语言程序输入计算机,并将C语言源程序文件*.c以纯文本文件形式保存在计算机的磁盘上(不能设置字体.字号等). (2)编译 ...

  9. Cocos2D-x培训课程

    1.1 Cocos2D-x 什么是cocos2d-x cocos2d-x在游戏开发中的运用 cocos2d-x的几个重要版本特点 iOS环境下搭建cocos2d开发环境 windows平台搭建coco ...

随机推荐

  1. 装饰器 and 闭包函数 未完。。。。。

    装饰器是一个返回函数的高阶函数.装饰器=高阶函数+函数嵌套+闭包 装饰器需要遵循的原则:不修改被装饰函数的源代码,不修改被装饰函数的调用方式. 高阶函数 1.函数接收的参数是一个函数名 2.函数的返回 ...

  2. 解决Pytohn安装第三方库出现read timed out 问题

    输入pip --default-timeout=100 install -U Packages 如pip --default-timeout=100 install -U Pillow

  3. js 正则表达式 整数或小数

    非零开头的整数或小数 /^[1-9][0-9]*([.][0-9]+)?$/ 非零开头的整数或两位小数 /^[1-9][0-9]*([.][0-9]{1,2})?$/ /^[1-9][0-9]*([. ...

  4. java抽象类和普通类的区别

    1.抽象类不能被实例化. 2.抽象类可以有构造函数,被继承时子类必须继承父类一个构造方法,抽象方法不能被声明为静态. 3.抽象方法只需申明,而无需实现,抽象类中可以允许普通方法有主体 4.含有抽象方法 ...

  5. 从github上下载一个csv文件

    when u open the raw file(i.e. csv) on github, then point to RAW button, then right click the mouse, ...

  6. 20155306 2016-2017-2 《Java程序设计》第七周学习总结

    20155306 2016-2017-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十三章 时间与日期 三种时间: 格林威治标准时间(GMT)的正午是太阳抵达天空最高点之时, ...

  7. Spring Boot 集成 MyBatis和 SQL Server实践

    概 述 Spring Boot工程集成 MyBatis来实现 MySQL访问的示例我们见过很多,而最近用到了微软的 SQL Server数据库,于是本文则给出一个完整的 Spring Boot + M ...

  8. 浅谈欧几里得算法求最大公约数(GCD)的原理及简单应用

    一.欧几里得算法及其证明 1.定义: 欧几里得算法又称辗转相除法,用于求两数的最大公约数,计算公式为GCD(a,b)=GCD(b,a%b): 2.证明: 设x为两整数a,b(a>=b)的最大公约 ...

  9. hdu 1716 排列2(DFS搜索)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1716 排列2 Time Limit: 1000/1000 MS (Java/Others)    Me ...

  10. Django 基础命令