棱镜SDK简单介绍

若想让游戏上线,渠道接入步骤是不可缺少的,为了避免一对一接入渠道问题,我选择了棱镜SDK,由于棱镜是游戏与渠道SDK的中间层,为CP厂商屏蔽各个渠道SDK之间的差异,整个接入过程,不会改变各个渠道SDK的功能、特性、參数等,对玩家全然透明。

棱镜平台基本工作原理:http://dev.ljsdk.com/ljdocs/lj_principle.html

棱镜技术接入文档(cocos2d-x)::http://dev.ljsdk.com/ljdocs/lj_tech_integration_cpp.html

棱镜SDK下载:http://dev.ljsdk.com/ljdocs/lj_tech_general.html

client接入流程

1.将Demo项目中Classes/GameProxy.h 拷贝到你的C++project中头文件存放的位置,如Cocos2dx项目放入Classes文件夹, 将Classes/gameproxy.cpp 放入源代码文件夹,如Cocos2dx的Classes文件夹:

2.windows平台加入代码例如以下:

HelloWorldScene.h

  1. #ifndef __HELLOWORLD_SCENE_H__
  2. #define __HELLOWORLD_SCENE_H__
  3.  
  4. #include "cocos2d.h"
  5.  
  6. class HelloWorld : public cocos2d::CCLayer
  7. {
  8. public:
  9. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
  10. virtual bool init();
  11.  
  12. // there's no 'id' in cpp, so we recommend returning the class instance pointer
  13. static cocos2d::CCScene* scene();
  14.  
  15. // a selector callback
  16. void menuCloseCallback(CCObject* pSender);
  17.  
  18. void setUserListener();
  19.  
  20. void loginCallback(CCObject* pSender);
  21.  
  22. void logoutCallback(CCObject* pSender);
  23.  
  24. void chargeCallback(CCObject* pSender);
  25.  
  26. void payCallback(CCObject* pSender);
  27.  
  28. void exitCallback(CCObject* pSender);
  29.  
  30. void showInfo(const char* str);
  31.  
  32. // implement the "static node()" method manually
  33. CREATE_FUNC(HelloWorld);
  34.  
  35. private:
  36. cocos2d::CCLabelTTF* outputLabel;
  37. };
  38.  
  39. #endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

  1. #include "HelloWorldScene.h"
  2. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  3. #include "GameProxy.h"
  4. #endif
  5.  
  6. USING_NS_CC;
  7.  
  8. CCScene* HelloWorld::scene()
  9. {
  10. // 'scene' is an autorelease object
  11. CCScene *scene = CCScene::create();
  12.  
  13. // 'layer' is an autorelease object
  14. HelloWorld *layer = HelloWorld::create();
  15.  
  16. // add layer as a child to scene
  17. scene->addChild(layer);
  18.  
  19. // return the scene
  20. return scene;
  21. }
  22.  
  23. // on "init" you need to initialize your instance
  24. bool HelloWorld::init()
  25. {
  26. //////////////////////////////
  27. // 1. super init first
  28. if ( !CCLayer::init() )
  29. {
  30. return false;
  31. }
  32.  
  33. CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
  34. CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
  35.  
  36. CCLabelTTF* pLabel = CCLabelTTF::create("CoCos2d-x SDKTest", "Arial", 30);
  37.  
  38. // position the label on the center of the screen
  39. pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
  40. origin.y + visibleSize.height - pLabel->getContentSize().height));
  41. // add the label as a child to this layer
  42. this->addChild(pLabel, 1);
  43.  
  44. this->outputLabel = CCLabelTTF::create("Output Here", "Arial", 20);
  45. this->outputLabel->setHorizontalAlignment(kCCTextAlignmentLeft);
  46. // position the label on the center of the screen
  47. this->outputLabel->setAnchorPoint(ccp(0, 0));
  48. this->outputLabel->setPosition(ccp(origin.x + visibleSize.width/2, origin.y));
  49. this->addChild(this->outputLabel, 1);
  50.  
  51. // add "HelloWorld" splash screen"
  52. CCSprite* pSprite = CCSprite::create("icon.png");
  53.  
  54. // position the sprite on the center of the screen
  55. pSprite->setPosition(ccp(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2));
  56.  
  57. // add the sprite as a child to this layer
  58. this->addChild(pSprite, 0);
  59.  
  60. CCMenuItemFont *loginItem = CCMenuItemFont::create(
  61. "login",
  62. this,
  63. menu_selector(HelloWorld::loginCallback));
  64. CCMenuItemFont *logoutItem = CCMenuItemFont::create(
  65. "logout",
  66. this,
  67. menu_selector(HelloWorld::logoutCallback));
  68.  
  69. CCMenuItemFont *chargeItem = CCMenuItemFont::create(
  70. "charge",
  71. this,
  72. menu_selector(HelloWorld::chargeCallback));
  73.  
  74. CCMenuItemFont *payItem = CCMenuItemFont::create(
  75. "pay",
  76. this,
  77. menu_selector(HelloWorld::payCallback));
  78.  
  79. CCMenuItemFont *exitItem = CCMenuItemFont::create(
  80. "exit",
  81. this,
  82. menu_selector(HelloWorld::exitCallback));
  83.  
  84. float borderWidth = 0;
  85. float currentYBorder = origin.y + visibleSize.height;
  86.  
  87. float offset = borderWidth + loginItem->getContentSize().height/2;
  88.  
  89. loginItem->setPosition(ccp(origin.x + loginItem->getContentSize().width/2 + borderWidth,
  90. currentYBorder - offset));
  91.  
  92. currentYBorder -= 2 * offset;
  93. offset = borderWidth + logoutItem->getContentSize().height/2;
  94. logoutItem->setPosition(ccp(origin.x + logoutItem->getContentSize().width/2 + borderWidth ,
  95. currentYBorder - offset));
  96.  
  97. currentYBorder -= 2 * offset;
  98. offset = borderWidth + chargeItem->getContentSize().height/2;
  99. chargeItem->setPosition(ccp(origin.x + chargeItem->getContentSize().width/2 + borderWidth ,
  100. currentYBorder - offset));
  101.  
  102. currentYBorder -= 2 * offset;
  103. offset = borderWidth + payItem->getContentSize().height/2;
  104. payItem->setPosition(ccp(origin.x + payItem->getContentSize().width/2 + borderWidth ,
  105. currentYBorder - offset));
  106.  
  107. currentYBorder -= 2 * offset;
  108. offset = borderWidth + exitItem->getContentSize().height/2;
  109. exitItem->setPosition(ccp(origin.x + exitItem->getContentSize().width/2 + borderWidth ,
  110. currentYBorder - offset));
  111.  
  112. CCMenu* pMenu = CCMenu::create(loginItem,logoutItem,chargeItem,payItem,exitItem,NULL);
  113. pMenu->setPosition(CCPointZero);
  114. this->addChild(pMenu,1,2);
  115.  
  116. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  117. this->showInfo(GameProxy::GetAndroidManifestMeta("XMGAME_PRODUCT_CODE"));
  118. #endif
  119.  
  120. return true;
  121. }
  122.  
  123. void HelloWorld::showInfo(const char *info){
  124.  
  125. this->outputLabel->setString(info);
  126. CCLOG("showinfo %s", info);
  127. }
  128.  
  129. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  130. class XMUserListenerImpl: public XMUserListener {
  131. private:
  132. HelloWorld * parent;
  133. public:
  134. XMUserListenerImpl(HelloWorld* parent) {
  135. this->parent = parent;
  136. }
  137. virtual void onLoginSuccess(XMUser user, const char *customParams) {
  138.  
  139. // 游戏的登陆逻辑须要写在这里
  140.  
  141. // 当游戏调用 GameProxy::Login(params) 时,该函数会被调用
  142. // 当中user对象为渠道登陆系统返回的用户相关參数,包括uid token 渠道号等
  143. // 当中params为调用GameProxy::Login( params)时,传入的params
  144. // 确保该函数被调用后,用户能够正确地进入游戏
  145.  
  146. CCLOG("login success");
  147.  
  148. stringstream is;
  149. is << "uid : " << user.getUserId() << "\ntoken : " << user.getToken() << "\nchannelId : " << user.getChannelId();
  150. parent->showInfo(is.str().c_str());
  151. GameProxy::SetXMRoleData("1", "hi", "10", "1", "1");
  152. };
  153.  
  154. virtual void onLoginFailed(const char *reason,
  155. const char *customParams) {
  156. stringstream is;
  157. is << "login failed reason: " << reason << "\ncustomParams: " << customParams;
  158. parent->showInfo(is.str().c_str());
  159.  
  160. };
  161. virtual void onLogout(const char *customParams) {
  162. // 游戏相关的用户登出注销逻辑须要写在这里
  163.  
  164. // 当渠道的用户中心界面中点击注销,该函数会被调用
  165. // 当游戏调用 GameProxy::Logout(params) 时,该函数也会被调用
  166. // 当中params为用户调用 GameProxy::Logout(params) 时传入的params
  167. // params能够用来传递一些游戏中的上下文,能够是随意字符串
  168. // 确保游戏中不论什么时候,该函数被调用后,游戏能够正确登出
  169.  
  170. parent->showInfo("logout");
  171. };
  172. };
  173.  
  174. class PayCallBackImpl:public PayCallBack{
  175. private:
  176. HelloWorld* parent;
  177. public:
  178. PayCallBackImpl(HelloWorld* parent) {
  179. this->parent = parent;
  180. }
  181. virtual void onPaySuccess(char *successinfo){
  182. stringstream is;
  183. is << "sucess: " <<successinfo;
  184. parent->showInfo(is.str().c_str());
  185. };
  186. virtual void onPayFail(char *failinfo){
  187. stringstream is;
  188. is << "payfailed: " << failinfo;
  189. parent->showInfo(is.str().c_str());
  190. };
  191. };
  192.  
  193. class ExitCallBackImpl:public ExitCallBack{
  194. public:
  195. virtual void onGameExit(){
  196. CCDirector::sharedDirector()->end();
  197. };
  198. virtual void onNo3rd(){
  199.  
  200. CCDirector::sharedDirector()->end();
  201. };
  202. };
  203.  
  204. #endif
  205.  
  206. void HelloWorld::setUserListener(){
  207. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  208. GameProxy::SetUserListener(new XMUserListenerImpl(this));
  209. #endif
  210. }
  211.  
  212. void HelloWorld::loginCallback(CCObject* pSender)
  213. {
  214. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  215. this->showInfo("logining");
  216. HelloWorld::setUserListener();
  217. const char *customParams = "login";
  218. GameProxy::Login(customParams);
  219. #endif
  220. }
  221.  
  222. void HelloWorld::logoutCallback(CCObject* pSender){
  223. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  224. const char *customParams = "logout";
  225. GameProxy::Logout(customParams);
  226. #endif
  227. }
  228.  
  229. void HelloWorld::chargeCallback(CCObject* pSender){
  230. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  231. const char *itemName = "元宝";
  232. int unitPrice = 10;
  233. int defalutNum = 500;
  234. const char *callBackInfo = "this is callback info...";
  235. const char *callbackUrl = "http://test";
  236. GameProxy::Charge(itemName, unitPrice, defalutNum, callBackInfo, callbackUrl,new PayCallBackImpl(this));
  237. #endif
  238. }
  239.  
  240. void HelloWorld::payCallback(CCObject* pSender){
  241. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  242. int amount = 10;
  243. const char *unitName = "天币";
  244. int count = 118;
  245. const char *callBackInfo = "this is callback info";
  246. const char *callbackUrl = "http://test";
  247.  
  248. GameProxy::Pay(amount, unitName, count, callBackInfo, callbackUrl, new PayCallBackImpl(this));
  249. #endif
  250. }
  251.  
  252. void HelloWorld::exitCallback(CCObject* pSender){
  253. #if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
  254. GameProxy::Exit(new ExitCallBackImpl());
  255. #endif
  256. }
  257.  
  258. void HelloWorld::menuCloseCallback(CCObject* pSender)
  259. {
  260. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  261. CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
  262. #else
  263. CCDirector::sharedDirector()->end();
  264. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
  265. exit(0);
  266. #endif
  267. #endif
  268. }

3.android平台:

环境搭建:

将Demo项目中libs文件夹下jar包拷贝到游戏项目的libs文件夹下
将Demo项目中assets文件夹下的文件拷贝到游戏项目的assets文件夹下

Android.mk:

AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.InsetSDK.org"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6.  
  7. <uses-sdk android:minSdkVersion="8"/>
  8. <uses-feature android:glEsVersion="0x00020000" />
  9.  
  10. <application
  11. android:name="com.xinmei365.game.proxy.XMApplication"
  12. android:label="@string/app_name"
  13. android:icon="@drawable/icon">
  14.  
  15. <activity android:name=".InsetSDK"
  16. android:label="@string/app_name"
  17. android:screenOrientation="landscape"
  18. android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
  19. android:configChanges="orientation">
  20. <intent-filter>
  21. <action android:name="android.intent.action.MAIN" />
  22. <category android:name="android.intent.category.LAUNCHER" />
  23. </intent-filter>
  24. </activity>
  25. <meta-data android:name="XMGAME_CHANNEL_CODE" android:value="9f10f46c37214442ba473b5409a05ebf" />
  26. <meta-data android:name="XMGAME_PRODUCT_CODE" android:value="lsjs" />
  27.  
  28. </application>
  29. <supports-screens android:largeScreens="true"
  30. android:smallScreens="true"
  31. android:anyDensity="true"
  32. android:normalScreens="true"/>
  33.  
  34. <uses-permission android:name="android.permission.INTERNET"/>
  35. </manifest>

Activity的Java代码:

  1. public class InsetSDK extends Cocos2dxActivity{
  2.  
  3. protected void onCreate(Bundle savedInstanceState){
  4. super.onCreate(savedInstanceState);
  5.  
  6. GameProxyNativeStub.init(this, GameProxy.getInstance(), new XMGLThreadRunner() {
  7.  
  8. @Override
  9. public void runOnGLThread(Runnable pRunnable) {
  10. InsetSDK.this.runOnGLThread(pRunnable);
  11.  
  12. }});
  13. GameProxy.getInstance().applicationInit(this);
  14. GameProxy.getInstance().onCreate(this);
  15.  
  16. }
  17.  
  18. public Cocos2dxGLSurfaceView onCreateView() {
  19. Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
  20. // InsetSDK should create stencil buffer
  21. glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
  22.  
  23. return glSurfaceView;
  24. }
  25.  
  26. static {
  27. System.loadLibrary("cocos2dcpp");
  28. }
  29.  
  30. public void onStop() {
  31. super.onStop();
  32. GameProxy.getInstance().onStop(this);
  33. }
  34.  
  35. public void onDestroy() {
  36. super.onDestroy();
  37. GameProxy.getInstance().onDestroy(this);
  38. }
  39.  
  40. public void onResume() {
  41. super.onResume();
  42. GameProxy.getInstance().onResume(this);
  43. }
  44. public void onPause() {
  45. super.onPause();
  46. GameProxy.getInstance().onPause(this);
  47. }
  48. public void onRestart() {
  49. super.onRestart();
  50. GameProxy.getInstance().onRestart(this);
  51. }
  52.  
  53. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  54. super.onActivityResult(requestCode, resultCode, data);
  55. GameProxy.getInstance().onActivityResult(this, requestCode, resultCode, data);
  56. }
  57. }

真机执行效果图:

cocos2d-x -- 渠道SDK【棱镜】接入(1)的更多相关文章

  1. 手机游戏渠道SDK接入工具项目分享(一)缘起

    #剧情章节 # 上周刚结束一个外包的项目,开发手机游戏渠道SDK聚合接入工具的,现在有空回顾整理一下这个项目开发过程,因涉嫌商业秘密不会提供项目代码,只谈下开发思路和掉过的坑. 本人多年从事手机互联网 ...

  2. 教你高速高效接入SDK——Unity统一接入渠道SDK(Android篇)

    U8SDK的设计之初,就是为了可以支持各种游戏引擎开发的游戏,而不不过Android的原生平台.眼下一大半的手游,都是採用Unity3D和Cocos2dx开发,那么这里,我们就先来一步步给大家演示,用 ...

  3. U8SDK——游戏接入SDK(只接入抽象框架)

    上一篇文章我们说了整个U8 SDK抽象成的设计,那这篇文章,我们就来验证一下,他是否如我们期待的那样,简单灵活. 正如之前所说,对于每个游戏,只需要接入抽象层,而每个渠道SDK的接入,就是该抽象层的一 ...

  4. cocos2d-x -- 渠道SDK【棱镜】接入(2)

    上一章<cocos2d-x -- 渠道SDK[棱镜]接入(1)>,已经接入好了SDK.如今要准备加入渠道了,以豌豆荚为例. 详细流程: 1.加入渠道:

  5. 手机游戏渠道SDK接入工具项目分享(二)万事开头难

    一般接到任务后程序员们通常都开始着手进行技术调研了,但我这活是项目负责人.还有一大堆事情要先期准备,没人能帮忙. 一.人力配置 考虑的之前已经有一波人搞了大半年,但没有起色,先期也没有太大人力需求,所 ...

  6. 手机游戏渠道SDK接入工具项目分享(三)拨开云雾是个坑

    一直在纠结是先写框架设计还是先写掉过的坑,最后本这娱乐大众的态度先写掉过的坑让大家乐呵下. 项目开发过程中遇问题无数,回顾下8个大坑照成了项目一定程度上延期甚至返工. 项目一开始几个人把现有3家主流的 ...

  7. unity3d如何快速接入渠道SDK之Unity篇

    原文链接: http://bbs.tianya.cn/post-414-53320-1.shtml 首先我们讲一下,为什么要介绍这个插件? 是因为这个插件极大的简化了我对接渠道SDK的工作量,精力和时 ...

  8. Channel SDK (渠道SDK) for Unity

    渠道 英文channel,解释:商品的销售路线,也就是我们开发商(CP)都把游戏开发好了,交付给渠道帮我们运营,帮我们推广,帮我们赚钱. Android和IOS渠道 拿安卓和IOS两大平台来说,它们都 ...

  9. cocos2d 接 android sdk 的一个小坑 关于armbeabi 和 armbeabi-v7a

    cocos2d 接 android sdk 的时候,有些sdk会要求外链到某个工程中,而这个工程的lib文件夹里会包含armbeabi 和 armbeabi-v7a这两个文件夹,如果直接打包会闪退.只 ...

随机推荐

  1. OSPF理论总结

    OSPF学习总结一.OSPF协议的报文类型: 1. Hello 报文:主要用来发现.建立和维护邻居关系. 2. DD报文:数据库的描述报文,主要用来两台路由器的数据库同步. 3. LSR报文:链路状态 ...

  2. 【JAVA学习】“-Xmx1024m -Xms1024m -Xmn512m -Xss256k”——Java执行參数(转)

    年轻代 年老代概念 http://jefferent.iteye.com/blog/1123677 JVM的堆的内存, 是通过以下面两个參数控制的  -Xms 最小堆的大小, 也就是当你的虚拟机启动后 ...

  3. 有关信息ACM/ICPC竞争环境GCC/G++叠插件研究记录的扩展

    0.起因 有时.DFS总是比BFS受人喜爱--毕竟DFS简单粗暴,更,而有些东西BFS不要启动,DFS它似乎是一个可行的选择-- 但是有一个问题,DFS默认直接写入到系统堆栈.系统堆栈和足够浅,此时O ...

  4. Android:主题(Theme)

    1.主题和样式的区别主要区别在 主题不能作用于单个View组建,主题应该对整个应用中的所有Activity起作用或者对指定的Activity起作用. 主题定义的格式应该是改变窗口的外观格式,例如窗口变 ...

  5. 提高SQL执行效率

    原文地址:http://www.cnblogs.com/hlxs/archive/2012/05/07/2487082.html 1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 ...

  6. JS 昵称,手机号,邮箱判断

    <script type="text/javascript"> var leyou = document.getElementById('J-leyou'), _nam ...

  7. JavaScript类数组对象参考

    JavaScript和DOM中有很多类数组对象,它们有以下特点 1.有length属性 2.可以使用[]通过下标访问 3.部分类数组对象使用[]访问成员时不只可以使用下标,还可以使用id或name 4 ...

  8. MongoDB获得短暂的

    大约MongoDB该数据是现在比较少.和大多数英文网站.最上面的经笔者从官方网站翻译.请翻译或误解之处请作证.然后,我们将继续关注MongoDB,和翻译“Developer Zone”和“Admin ...

  9. 王立平--Unity综上所述控制

    GUILayout   Label 创建一个自己主动布局的标签. Box 创建一个自己主动布局的box. Button 创建一个单次button.当用户点击button会马上发生一些事件. Repea ...

  10. 《深入Java虚拟机》笔记:指令集 (转)

    <深入Java虚拟机>笔记:指令集   指令 含义 iconst_m1 把int型常量-1压入栈中 iconst_0 把int型常量压入栈中 fconst_1 把float型常量1压入栈中 ...