鉴于诺基亚(微软移动这个没人用的手机)开发者比较少,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. Go_18: Golang 中三种读取文件发放性能对比

    Golang 中读取文件大概有三种方法,分别为: 1. 通过原生态 io 包中的 read 方法进行读取 2. 通过 io/ioutil 包提供的 read 方法进行读取 3. 通过 bufio 包提 ...

  2. LINUX 线程

    1.使用进程技术的优势(1)CPU时分复用,单核心CPU可以实现宏观上的并行(2)实现多任务系统需求(多任务的需求是客观的)2.进程技术的劣势(1)进程间切换开销大(2)进程间通信麻烦而且效率低3.解 ...

  3. c# 的一些基本操作或属性

    http下载文件,不保存到服务器,直接使用浏览器下载 /// <summary> /// 根据url下载文件 /// </summary> /// <param name ...

  4. Android 使用GPS获取到经纬度后 无法在Android8.0上使用Geocoder类获取位置信息

    由于我的应用在获取到经纬度后在Android8.0不能使用如下代码获取位置信息.只好使用百度地图 WEB服务API 通过调接口的方式获取位置信息. Geocoder geocoder = new Ge ...

  5. Hive性能优化--map数和reduce数

    转自http://superlxw1234.iteye.com/blog/1582880 一.    控制hive任务中的map数:  1.    通常情况下,作业会通过input的目录产生一个或者多 ...

  6. JavaScript实现单向链表

    JavaScript 本身提供了十分好用的数据类型,以满足大家的日常使用.单靠 Array  和 Object 也的确足够应付日常的绝大部分需求,这也导致了很多前端er对数据结构这一块不是十分的了解. ...

  7. 是否使用TDD(测试驱动开发)进行UI开发

    问题 StackOverflow上有一则是否使用TDD(测试驱动开发)进行UI开发 的提问. _JacobE_问: 对于是否使用TDD进行开发UI这件事,我想了很久,但难以决定.我想听听你们的意见. ...

  8. 【leetcode 简单】 第五十八题 计数质数

    统计所有小于非负整数 n 的质数的数量. 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 . class Solution: def cou ...

  9. 【译】SSH隧道:本地和远程端口转发

    本文是:SSH Tunnel - Local and Remote Port Forwarding Explained With Examples 的译文 有两种方法可以创建SSH隧道,本地和远程端口 ...

  10. 二次开发中cad字体的总结

    目前手头一个项目,关于制图统一平台的,特别研究了CAD中的字体,总结出来,给需要的朋友,希望少走弯路.1.cad2008中,netload之后,输入注册的命令,提示未知命令解决:将引用中CAD两个dl ...