<span style="font-family: Arial,Helvetica,sans-serif; background-color: rgb(255,255,255);">记录 cocos2dx wp8下的东西</span>
环境是 cocos2dx 3.2 vs2013
1. wp8下的内购
a.进行设置wp8内购相关信息
1.先提交一个beta应用,并且添加测试人员的的 测试帐号,因为只有测试帐号才能进行下载
2. 添加内购项目,记录下每一个item的 id
3.一般提交后2小时左右就能得到程序的下载地址,使用测试人员帐号下载该程序进行购买
关于 IAP设置和测试的基本过程大致就是这样
WMAppManifest.xml-->打包,填写相应的产品ID和发布者ID
1.添加 WP8Util.h .cpp
#ifndef __WP8UTIL__ #define __WP8UTIL__ // c++ to call C# #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 // can not change namespace namespace PhoneDirect3DXamlAppComponent{ [Windows::Foundation::Metadata::WebHostHidden] public interface class IWP8Callback{ virtual void Exec(Platform::String ^Command,Platform::String ^Param); }; public ref class WP8Util sealed{ public: static void SetWP8UtilCallback(IWP8Callback^ callback); static void buy(); static void rank(); static void submit(); }; } #endif #endif // !__WP8UTIL__
#include "WP8Util.h" #include "cocos2d.h" using namespace cocos2d; #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 namespace PhoneDirect3DXamlAppComponent{ static IWP8Callback^ gWp8Callback = nullptr; void WP8Util::buy(){ if (gWp8Callback != nullptr) { log("WP8Util::buy clicked......"); } } void WP8Util::rank(){ if (gWp8Callback != nullptr) { } } void WP8Util::submit(){ if (gWp8Callback != nullptr) { } } void WP8Util::SetWP8UtilCallback(IWP8Callback^ callback){ gWp8Callback = callback; log("------>>>>>>>>>>>>>>>>>>>>>Regist IWP8Callback"); } } #endif
主要流程就是在 WP8 C# 端进行实现 IWP8Callback,设置 IWP8Callback.
在IWP8Callback的方法里面根据相应的命令去完成相应C#方法的调用.
1. using PhoneDirect3DXamlAppComponent;
2. 添加:
/// IWP8Callback public class WP8CallbackImpl : IWP8Callback { public WP8CallbackImpl() { WP8Util.SetWP8UtilCallback(this); } public void Exec(String Command,String Param) { //Execute some C# code,if you call UI stuff you will need to call this too /*Deployment.Current.Dispatcher.BeginInvoke(() => { });*/ Debug.WriteLine("C# method---" + Command + "-----" + Param); } }
即可,来源于:http://discuss.cocos2d-x.org/t/wp8-c-call-c-function-with-param/11302/2
2.wp8 下播放 mp3
默认 wp8 不支持 .mp3文件,为了和 ios/Android 统一,使其支持.mp3文件格式的播放
a. 下载代码
b.将libmp3lame,libmpghip,src三个文件夹拷贝到Cocos2dx的proj.wp8-xaml目录下
然后添加 lib3lame和libmpghip这两个项目到解决方案里面
c.让Cocos2dx wp8的 XXXComponent 项目添加libmp3lame,libmpghip这两个项目的引用
d.在CocosDenshion项目属性中->VC++目录->包含目录中添加 "lame.h"文件所在的文件夹
MediaStreamer.h 里面添加
// for wp8 add new method #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 void Initialize_MP3(_In_ const WCHAR* url); #endif
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 void MediaStreamer::Initialize_MP3(_In_ const WCHAR* url){ #if 1 WCHAR filePath[MAX_PATH] = { 0 }; if ((wcslen(url) > 1 && url[1] == ':')) { // path start with "x:",is absolute path wcscat_s(filePath,url); } else if(wcslen(url) > 0 && (L'/' == url[0] || L'\\' == url[0])) { // path start with '/' or '\',is absolute path without driver name wcscat_s(filePath,m_locationPath->Data()); // remove '/' or '\\' wcscat_s(filePath,(const WCHAR*)url[1]); } else { wcscat_s(filePath,m_locationPath->Data()); wcscat_s(filePath,url); } hip_t hip = hip_decode_init(); if (!hip) { printf("create mp3 decoder Failed."); return; } mp3data_struct mp3str;//mp3 encoding info std::vector<short*> mp3Buffer;// mp3 data stream std::vector<int> mp3BufferSize; int samples; int mp3_bytes; int write_bytes = 0; const int BUF_SIZE = 512; const int INBUF_SIZE = 4096; const int MP3BUF_SIZE = (int)(1.25 * BUF_SIZE) + 7200; short pcm_l[INBUF_SIZE]; short pcm_r[INBUF_SIZE]; unsigned char mp3_buf[MP3BUF_SIZE]; FILE * MP3File; std::wstring wstr = std::wstring(filePath); std::string str_filePath = std::string(wstr.begin(),wstr.end()); auto error = fopen_s(&MP3File,str_filePath.c_str(),"rb"); mp3data_struct mp3Header; while ((mp3_bytes = fread(mp3_buf,1,210,MP3File)) > 0) { samples = hip_decode_headers(hip,mp3_buf,pcm_l,pcm_r,&mp3Header); if (samples > 0) { short *tt = new short[samples*sizeof(short)]; memcpy((void*)tt,(const void*)pcm_l,samples*sizeof(short)); mp3Buffer.push_back(tt); write_bytes += samples*sizeof(short); mp3BufferSize.push_back(samples*sizeof(short)); } } byte* _mp3Buffer = new byte[write_bytes]; byte* temp = _mp3Buffer; int size = mp3BufferSize.size(); for (int i = 0; i < size; i++) { memcpy(temp,mp3Buffer[i],mp3BufferSize[i]); delete mp3Buffer[i]; temp += mp3BufferSize[i]; } mp3Buffer.clear(); hip_decode_exit(hip); m_data.resize(write_bytes); for (int i = 0; i < write_bytes; i++) { m_data[i] = _mp3Buffer[i]; } fclose(MP3File); m_waveFormat.wFormatTag = WAVE_FORMAT_PCM; // m_waveFormat.nChannels = 1; // m_waveFormat.nSamplesPerSec = (DWORD)mp3Header.samplerate;// m_waveFormat.wBitsPerSample = 16; m_waveFormat.nBlockAlign = m_waveFormat.nChannels * m_waveFormat.wBitsPerSample / 8.0; m_waveFormat.nAvgBytesPerSec = m_waveFormat.nSamplesPerSec * m_waveFormat.nBlockAlign; // m_waveFormat.cbSize = 0; delete[] _mp3Buffer; #endif } #endif
修改 Audio.cpp的 PreloadSoundEffect方法.
将
<span style="white-space:pre"> </span>mediaStreamer.Initialize(CCUtf8ToUnicode(pszFilePath).c_str());
替换成:
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 // add .mp3 decode in wp8 std::string path(pszFilePath); std::string::size_type pos = path.find(".mp3"); if (pos != path.npos) { mediaStreamer.Initialize_MP3(CCUtf8ToUnicode(pszFilePath).c_str()); } else { mediaStreamer.Initialize(CCUtf8ToUnicode(pszFilePath).c_str()); } #else mediaStreamer.Initialize(CCUtf8ToUnicode(pszFilePath).c_str()); #endif
即可,内容来自于:http://blogs.msdn.com/b/windows__windows_game_dev_faq_/archive/2014/12/10/mp3-cocos2dx-windows-phone8.aspx
3.wp8 下使用sqlite3
a.在 VS2013-->工具-->扩展和更新 里面下载使用于 windows phone 8.0的 sqlite
b.在项目里面XXXComponent -->属性 通用属性-->引用-->添加新引用-->扩展里面,选择 winodws phone sqlite
c.在项目里面XXXComponent -->属性-->链接器-->输入-->附加依赖项目,加上
C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v8.0\ExtensionSDKs\sqlite.WP80\3.8.9\DesignTime\Retail\ARM\sqlite3.lib
c.基本就可以正常使用sqlite了
4. wp8下添加广告
a.c# wp8 工程下 add reference -> google admob
b. 到 MainPage.xaml 下添加 AdControl
<GoogleAds:AdView AdUnitID="ca-app-pub-9565466250630588/6418716150" Format="Banner" ReceivedAd="onAdReceived" FailedToReceiveAd="onFailedToReceived" x:Name="adView" VerticalAlignment="Bottom" />
6. wp8跳转到内购后,点击取消返回到游戏时,游戏 Crash,在LabelTextFormatter.cpp中
LabelTextFormatter::createStringSprites方法里面
const auto& kernings = theLabel->_horizontalKernings;前面加上:
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 theLabel->computeHorizontalKernings(theLabel->_currentUTF16String); #endif
7. wp8 上传时出现