轉載自:http://i.kimiazhu.info/?p=271
Title: Cocos2d-x項目移植到WP8小記
Tags: Android, adb, WP, Visual Studio
-
Cocos2d-x v2.2已經支持了Windows一系列的系統(cocos2d-x 0.13那個除外啦),包括RT,x86以及WP8
-
創建工程用create_project.py命令,工程會建立在cocos2d-x目錄下的projects子目錄中。
-
移植之前如果你的源文件中有中文,在vs 2012中編譯可能會出現許多莫名其妙的錯誤,諸如類型未定義、變量未定義 warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss 之類的問題,但是在編輯器中是正常的。
解決方法,選中有問題的文件,ctrl-A全選所有文本,File->Advanced Saved Options…對話框中選擇 Unicode – Codepage 1200,或者 Unicode (UTF-8 with signature) – Codepage 65001 保存,建議優先嚐試後者。
-
做完第三步之後,可能會發現中文無法識別,我不清楚有多少人會遇到這個問題,但是在將Eclipse(Linux上的開發環境,非Windows)和XCode上做好的代碼移植到Windows+Visual Studio的時候,我發現我的代碼中(甚至是註釋中)的中文字符會導致無法編譯,錯誤信息很奇怪的,比如告訴你哪個位置少了分號,某個變量缺少類型之類的根本不存在的錯誤。
這個絕對是編碼問題,和代碼邏輯無關。爲了解決這個問題,文件中儘量少出現中文符號(如果從頭開始就是在VS上寫代碼不知道是不是會有這樣的問題,移植的代碼,問題會更多)。同時也爲了國際化,我們可以把遊戲中需要出現的文案抽取出來,可以參考Android的方式來做國際化,將中文放到配置文件上(配置文件編碼確保和CCLabelBMFont所使用的fnt文件的編碼相同,一般使用Unicode (UTF-8 without signature) – Codepage 65001 編碼)。這樣配置文件不參與編譯,就可以避免編譯期間中文編碼擾亂編譯過程這個問題。(這個問題上卡了我兩天 ~_~ )
-
引入其他工程,請注意引入工程依賴之後,你還需要將需要include的頭文件配置到工程屬性中,在這裏:右鍵點擊你的工程名->Properties->Configuration Properties->C/C++->Addtional Include Directories 中加入響應的頭文件所在的位置。例如對於聲音依賴工程CocosDenshion的頭文件在這裏 $(ProjectDir)..\..\..\CocosDenshion\include ;而對於extionsion則在這裏: $(ProjectDir)..\..\..\extensions
-
關於導入Classes目錄中的子目錄,目前沒有發現能用的好辦法,一個一個建立Filter,然後往Filter中添加sources吧.
-
錯誤信息: *error C2440: ‘type cast’ : cannot convert from ‘void (__cdecl XXXX::* )(void)’ to ‘cocos2d::SEL_MenuHandler’*
解決方法,可能是函數原型不對,看看菜單的回調函數原型是不是這樣的(檢查一下是不是你少了參數?):
C++ Code:
void HelloWorld::doClick1(CCObject* pSender);
其他類似的情形也一樣,你可以留意一下這幾個函數的原型,它們定義在 CCObject.h 中:
C++ Code:
typedef void (CCObject::*SEL_SCHEDULE)(float); typedef void (CCObject::*SEL_CallFunc)(); typedef void (CCObject::*SEL_CallFuncN)(CCNode*); typedef void (CCObject::*SEL_CallFuncND)(CCNode*, void*); typedef void (CCObject::*SEL_CallFuncO)(CCObject*); typedef void (CCObject::*SEL_MenuHandler)(CCObject*); typedef void (CCObject::*SEL_EventHandler)(CCEvent*); typedef int (CCObject::*SEL_Compare)(CCObject*);
-
提示資源文件找不到?選中問題資源,點屬性,然後把 Content 屬性設置爲true即可。
-
沒有聲音?建議使用wav資源。。體積那是相對其他格式要大一點的。。
-
最棘手的問題:cocos2d-x 2.2中的CCScrollView和CCTableView存在bug,導致整個屏幕圖像會變形。論壇上已有用戶反饋,我自己也有回帖,但估計WP開發者相對弱勢,這個問題並沒有得到解決。折騰了幾天之後得到了解決方法,修改CCScrollView.cpp替換一下函數:
C++ Code:
/** * clip this view so that outside of the visible bounds can be hidden. */ void CCScrollView::beforeDraw() { if (m_bClippingToBounds) { m_bScissorRestored = false; CCRect frame = getViewRect(); if (CCEGLView::sharedOpenGLView()->isScissorEnabled()) { m_bScissorRestored = true; m_tParentScissorRect = CCEGLView::sharedOpenGLView()->getScissorRect(); //set the intersection of m_tParentScissorRect and frame as the new scissor rect if (frame.intersectsRect(m_tParentScissorRect)) { float x = MAX(frame.origin.x, m_tParentScissorRect.origin.x); float y = MAX(frame.origin.y, m_tParentScissorRect.origin.y); float xx = MIN(frame.origin.x+frame.size.width, m_tParentScissorRect.origin.x+m_tParentScissorRect.size.width); float yy = MIN(frame.origin.y+frame.size.height, m_tParentScissorRect.origin.y+m_tParentScissorRect.size.height); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) glScissor((GLint)x, (GLint)y, (GLsizei)xx-x, (GLsizei)yy-y); #else CCEGLView::sharedOpenGLView()->setScissorInPoints(x, y, xx-x, yy-y); #endif } } else { glEnable(GL_SCISSOR_TEST); #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) glScissor((GLint)frame.origin.x, (GLint)frame.origin.y, (GLsizei)(frame.size.width), (GLsizei)(frame.size.height)); //glDisable(GL_SCISSOR_TEST); #else CCEGLView::sharedOpenGLView()->setScissorInPoints(frame.origin.x, frame.origin.y, frame.size.width, frame.size.height); #endif } } }
實際使用我新建了名爲HTScrollView和HTTableView兩個類,代碼引用到這兩個新建的類上,避免直接更改框架代碼。當然上面代碼使用了宏預編譯,直接修改框架代碼也不會影響到Android和iOS。(我只發現原cocos2d-x-2.2框架在WP8下有此bug,至於WinRT下是否也有此問題並未測試。)CCScrollView.cpp做修改以後,CCTableView的問題也會得到解決。
-
關於CCLOG日誌無輸出的問題:
在Android中,要在Android.mk文件中添加以下編譯選項: APP_CPPFLAGS := -DCOCOS2D_DEBUG=1
在VC++中的編譯器不是gcc,而應該在Visual Studio設置: Your Project -> Settings -> Configuration Properties -> C/C++ -> Command Line -> Additional Options 輸入框中添加定義: /D “COCOS2D_DEBUG=1″,重新編譯即可。當你決定發佈Release版本的時候,請把這個去掉。
-
關於WP8返回物理按鍵的響應,另文總結。
-
提交市場的時候出現錯誤:
1028: The native API api-ms-win-core-debug-l1-1-1.dll:OutputDebugStringA() isn’t allowed in assembly libcocos2d.dll. Update it and then try again. 1028: The native API api-ms-win-core-synch-l1-2-0.dll:CreateMutexExA() isn’t allowed in assembly libcocos2d.dll. Update it and then try again. 1028: The native API api-ms-win-core-synch-l1-2-0.dll:CreateEventExA() isn’t allowed in assembly libGLESv2_phone.dll. Update it and then try again.
記得提交到MarketPlace一定要打成Release包,這裏是兩個問題:
1) 在libcocos2d工程中,將platform/winrt/CCCommon.cpp中的 CCLuaLog(const char *pszMsg) 方法的 OutputDebugStringA(“\n”); 註釋掉或者加入預編譯使得Release版本不出現。
2) 有人說可以使用c++11中的mutex替代,但我直接註釋掉了。兩處地方:
- 修改文件: cocos2dx/platform/winrt/CCWinRTUtils.h 中的 inline void pthread_mutex_init(pthread_mutex_t* m, void* attributes) 方法註釋掉內容。
- 修改文件:cocos2dx/platform/third_party/winrt/angleproject/src/third_party/winrt/
ThreadEmulation/ThreadEmulation.cpp 中的 _Use_decl_annotations_ VOID WINAPI Sleep(DWORD dwMilliseconds) 方法,註釋掉內容。
-
Release版本無聲音,在 CocosDenshion/wp8/Audio.cpp 中註釋此行:
m_soundEffects[sound].m_soundEffectSourceVoice->SetVolume(m_soundEffctVolume);
如圖:
-
關於內購:
WP8完美支持C++,所以內購其實很簡單。
-
對於Consumable產品,可以用IsConsumable來加以判斷,當然你設置產品屬性的時候可能已經知道類型,這裏可以可以免於判斷了。重要的是使用IsActive判斷用戶是否計費成功。記得給用戶發放道具之後,要調用以下方法來向服務器彙報,否則你將無法繼續下次計費。
C++ Code:
CurrentApp::ReportProductFulfillment(productId);
-
對於Durable類型產品,使用IsActive判斷計費成功後,直接給用戶反饋即可,無需向服務器彙報。調用 ReportProductFulfillment(productId) 方法是會拋出異常的。切記。
-
-
鏈接錯誤,提示找不到extension或者CocosDenshion的庫:
Error 204 error LNK2001: unresolved external symbol "public: __cdecl cocos2d::extension::CCBReader::CCBReader(class cocos2d::extension::CCNodeLoaderLibrary *,class cocos2d::extension::CCBMemberVariableAssigner *,class cocos2d::extension::CCBSelectorResolver *,class cocos2d::extension::CCNodeLoaderListener *)" (??0CCBReader@extension@cocos2d@@QAA@PAVCCNodeLoaderLibrary@12@PAVCCBMemberVariableAssigner@12@PAVCCBSelectorResolver@12@PAVCCNodeLoaderListener@12@@Z)
嘗試解決方案->屬性中指定遊戲的依賴,添加libExtension和CocosDenshion兩個項目,另外還需要在遊戲項目的屬性->框架和引用頁面裏添加上對libExtension和CocosDenshion兩個項目引用的依賴
-
分辨率適配。