Cocos2d-x for Windows Phone 用法總結


鑒於諾基亞(微軟移動這個沒人用的手機)開發者比較少,cocos2dx移植方面更是少的問題,總結一下WP8移植方面的資料,希望對大家有用,自己也當作筆記留念。 

1、WP8方面有兩種方式創建項目,HelloCpp和TestCpp就是這樣,XAML方式和純c++方式。最好選擇xaml方式,因為你有可能會c++和c#進行交互。廢話不說,有圖有真相。

 

2、說到c++c#交互,其實叫C++/CXC++/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();

****************************************************************************
View Code

 

    b)  c#調用c++,比如自己添加了一個EditBox和一個Button,通過代碼將EditBox中的值返回給c++中,詳見Demo,我的例子是輸入一個兌換碼然后返回給c++部分

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

//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());
}
****************************************************************************
View Code

 

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] = {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

****************************************************************************
View Code

 

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__
View Code

    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] = {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;
}
View Code


5、下一篇會講一下常見的錯誤。

 

         

github地址:https://github.com/eoeuou/wp8-xaml歡迎來噴

 

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM