<span >记录 cocos2dx wp8下的东西</span>
环境是 cocos2dx 3.2 vs2013
1. wp8下的内购
C++ 调用 WP8 API(相当于C++调用C#)
a.进行设置wp8内购相关信息
1.先提交一个beta应用,并且添加测试人员的的 测试帐号,因为只有测试帐号才能进行下载
2. 添加内购项目,记录下每一个item的 ID
3.一般提交后2小时左右就能得到程序的下载地址,使用测试人员帐号下载该程序进行购买
关于 IAP设置和测试的基本过程大致就是这样
b. 修改项目的相关属性
WMAppManifest.xml-->打包,填写相应的产品ID和发布者ID
c. 添加代码进行实现
1.添加 WP8Util.h .cpp
#ifndef __WP8UTIL__#define __WP8UTIL__// c++ to call C# #if CC_TARGET_PLATFORM == CC_PLATFORM_WP8// can not change namespacenamespace 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_WP8namespace 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#方法的调用.
在 MainPage.xaml.cs 文件里面添加:
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. 下载代码
https://github.com/tianjing/lame_wp8b.将libmp3lame,libmpghip,src三个文件夹拷贝到Cocos2dx的proj.wp8-xaml目录下
然后添加 lib3lame和libmpghip这两个项目到解决方案里面
c.让Cocos2dx wp8的 XXXComponent 项目添加libmp3lame,libmpghip这两个项目的引用
d.在CocosDenshion项目属性中->VC++目录->包含目录中添加 "lame.h"文件所在的文件夹
e.修改CocosDenshion下部分代码
MediaStreamer.h 里面添加
// for wp8 add new method#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8 voID Initialize_MP3(_In_ const WCHAR* url);#endif
MediaStreamer.cpp 里面添加实现的方法
#if CC_TARGET_PLATFORM == CC_PLATFORM_WP8voID 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 > </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" />c. 在 MainPage.xaml.cs里添加方法 (见demo)
5. wp8下中文显示
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 上传时出现
总结以上是内存溢出为你收集整理的Cocos2dx 之 WP8........全部内容,希望文章能够帮你解决Cocos2dx 之 WP8........所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)