鑑於諾基亞(微軟移動這個沒人用的手機)開發者比較少,cocos2dx移植方面更是少的問題,總結一下WP8移植方面的資料,但願對你們有用,本身也看成筆記留念。 c++
一、WP8方面有兩種方式建立項目,HelloCpp和TestCpp就是這樣,XAML方式和純c++方式。最好選擇xaml方式,由於你有可能會c++和c#進行交互。廢話不說,有圖有真相。git
二、說到c++和c#交互,其實叫C++/CX(C++/CX實際上是微軟在Win8開發平臺下,對C++語言的一種擴展),下面就講述其用法,概念能夠去百度。github
a) c++調用c#,好比我想得到該諾基亞的UniqueID,直接上代碼吧,瞭解c#委託的童鞋應該不難理解,if...baidu... c#
**************************************************************************** //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++部分ide
**************************************************************************** //MainPage.xaml.cs //此處爲c#調用,當button按鍵觸發時 m_d3dInterop.OnWP8RedeemResult(WP8RedeemEventType.WP8RedeemSuccess,"110"); **************************************************************************** //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()); } ****************************************************************************
三、一般c#是用的字符串類型是Platform::String^,須要轉換爲std::string。如下的方法能夠任意直接轉化,是否是很方便呢spa
**************************************************************************** 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] = {0}; MultiByteToWideChar(CP_UTF8, 0, szBuf, -1, wszBuf, sizeof(wszBuf)); OutputDebugStringW(wszBuf); OutputDebugStringW(L"\n"); WideCharToMultiByte(CP_ACP, 0, 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() + 1; wchar_t *_Dest = new wchar_t[_Dsize]; wmemset(_Dest, 0, _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 ****************************************************************************
四、我把WP8DataManager所有貼出來,方便在代碼中直接使用,你能夠直接引用到項目的根目錄,如圖。3d
WP8DataManager.h code
#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.cpporm
#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] = {0}; MultiByteToWideChar(CP_UTF8, 0, szBuf, -1, wszBuf, sizeof(wszBuf)); OutputDebugStringW(wszBuf); OutputDebugStringW(L"\n"); WideCharToMultiByte(CP_ACP, 0, 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() + 1; wchar_t *_Dest = new wchar_t[_Dsize]; wmemset(_Dest, 0, _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; }
五、下一篇會講一下常見的錯誤。blog
github地址:https://github.com/eoeuou/wp8-xaml歡迎來噴