cocos2d-x lua c++ 相互调用代码中直接调用注册

来源:互联网 发布:linux鼠标切出来 编辑:程序博客网 时间:2024/06/06 15:26

cocos2d-x lua c++ 相互调用代码中直接调用注册

原博客链接http://blog.csdn.net/vpingchangxin/article/details/21382229

我用的是 cocos2d-x 2.2.2 我也是参考 himi博客中的文章 但是他那个我没有跑通 不多废话 下面是我的代码 

如有lua api 等疑问点开链接看himi的文章 http://blog.csdn.net/xiaominghimi/article/details/8816887


AppDelegate.h

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #ifndef __APP_DELEGATE_H__  
  2. #define __APP_DELEGATE_H__  
  3.   
  4. #include "cocos2d.h"  
  5.   
  6. /** 
  7. @brief    The cocos2d Application. 
  8.  
  9. The reason for implement as private inheritance is to hide some interface call by CCDirector. 
  10. */  
  11. class  AppDelegate : private cocos2d::CCApplication  
  12. {  
  13. public:  
  14.     AppDelegate();  
  15.     virtual ~AppDelegate();  
  16.   
  17.     /** 
  18.     @brief    Implement CCDirector and CCScene init code here. 
  19.     @return true    Initialize success, app continue. 
  20.     @return false   Initialize failed, app terminate. 
  21.     */  
  22.     virtual bool applicationDidFinishLaunching();  
  23.   
  24.     /** 
  25.     @brief  The function be called when the application enter background 
  26.     @param  the pointer of the application 
  27.     */  
  28.     virtual void applicationDidEnterBackground();  
  29.   
  30.     /** 
  31.     @brief  The function be called when the application enter foreground 
  32.     @param  the pointer of the application 
  33.     */  
  34.     virtual void applicationWillEnterForeground();  
  35. };  
  36.   
  37. #endif  // __APP_DELEGATE_H__  

AppDelegate.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. #include "cocos2d.h"  
  2. #include "CCEGLView.h"  
  3. #include "AppDelegate.h"  
  4. #include "CCLuaEngine.h"  
  5. #include "SimpleAudioEngine.h"  
  6. #include "Lua_extensions_CCB.h"  
  7. #include "lua_extensions.h" // lua_cjson  
  8. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  9. #include "Lua_web_socket.h"  
  10. #endif  
  11.   
  12.   
  13. #include "Lua_callCppTest.h"  
  14.   
  15. using namespace CocosDenshion;  
  16.   
  17. USING_NS_CC;  
  18.   
  19. AppDelegate::AppDelegate()  
  20. {  
  21. }  
  22.   
  23. AppDelegate::~AppDelegate()  
  24. {  
  25.     SimpleAudioEngine::end();  
  26. }  
  27.   
  28. bool AppDelegate::applicationDidFinishLaunching()  
  29. {  
  30.     // initialize director  
  31.     CCDirector *pDirector = CCDirector::sharedDirector();  
  32.     pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());  
  33.   
  34.     // turn on display FPS  
  35.     pDirector->setDisplayStats(false);  
  36.   
  37.     // set FPS. the default value is 1.0/60 if you don't call this  
  38.     pDirector->setAnimationInterval(1.0 / 60);  
  39. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
  40. //    CCEGLView::sharedOpenGLView()->setDesignResolutionSize(1024, 768, kResolutionShowAll);  
  41.     CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionExactFit);  
  42. #endif  
  43.       
  44.       
  45.     // register lua engine  
  46.     CCLuaEngine* pEngine = CCLuaEngine::defaultEngine();  
  47.     CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);  
  48.       
  49.     CCLuaStack *pStack = pEngine->getLuaStack();  
  50.     lua_State *tolua_s = pStack->getLuaState();  
  51.     tolua_extensions_ccb_open(tolua_s);  
  52.       
  53.     // 添加lua_cjson  
  54.     luaopen_lua_extensions(tolua_s);  
  55.       
  56. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS || CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  57.     pStack = pEngine->getLuaStack();  
  58.     tolua_s = pStack->getLuaState();  
  59.     tolua_web_socket_open(tolua_s);  
  60. #endif  
  61.       
  62. #if (CC_TARGET_PLATFORM == CC_PLATFORM_BLACKBERRY)  
  63.     CCFileUtils::sharedFileUtils()->addSearchPath("script");  
  64. #endif  
  65.       
  66.     ////////test lua call cpp function /////////  
  67.     Lua_callCppTest::sharedHD()->callCppFunction(tolua_s);  
  68.       
  69.   
  70.     std::string path = CCFileUtils::sharedFileUtils()->fullPathForFilename("main.lua");  
  71.     pEngine->executeScriptFile(path.c_str());  
  72.   
  73.       
  74.     ///////////////c++ call lua function////////////////////  
  75.     Lua_callCppTest::sharedHD()->callLuaFunction("testCppTolua");  
  76.   
  77.     return true;  
  78. }  
  79.   
  80. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too  
  81. void AppDelegate::applicationDidEnterBackground()  
  82. {  
  83.     CCDirector::sharedDirector()->stopAnimation();  
  84.   
  85.     SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();  
  86. }  
  87.   
  88. // this function will be called when the app is active again  
  89. void AppDelegate::applicationWillEnterForeground()  
  90. {  
  91.     CCDirector::sharedDirector()->startAnimation();  
  92.   
  93.     SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();  
  94. }  

Lua_callCppTest.h

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Lua_callCppTest.h  
  3. //  WestwardJourneyGaiden  
  4. //  
  5. //  Created by vin on 14-3-17.  
  6. //  
  7. //  
  8.   
  9. #ifndef __WestwardJourneyGaiden__Lua_callCppTest__  
  10. #define __WestwardJourneyGaiden__Lua_callCppTest__  
  11.   
  12. #include <iostream>  
  13. #include "cocos2d.h"  
  14. using namespace cocos2d;  
  15. using namespace std;  
  16.   
  17. #include "CCLuaEngine.h"  
  18. extern "C" {  
  19. #include "lua.h"  
  20. #include "lualib.h"  
  21. #include "lauxlib.h"  
  22. };  
  23.   
  24. class Lua_callCppTest {  
  25. public:  
  26.     static Lua_callCppTest* sharedHD();  
  27.     void callCppFunction(const char* luaFileName);  
  28.     static int cppFunction(lua_State* ls);  
  29.     void callCppFunction(lua_State* ls);  
  30.     /* 
  31.      callLuaFunction : 调用lua函数 
  32.       
  33.      luaFileName  = lua文件名 
  34.      functionName = 所要调用Lua中的的函数名 
  35.      */  
  36.     const char* callLuaFunction(const char* functionName);  
  37. private:  
  38.     static bool _isFirst;  
  39.     static Lua_callCppTest* _shared;  
  40.     const char* getFileFullPath(const char* fileName);  
  41.     ~Lua_callCppTest();  
  42. };  
  43. #endif /* defined(__WestwardJourneyGaiden__Lua_callCppTest__) */  

Lua_callCppTest.cpp

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  Lua_callCppTest.cpp  
  3. //  WestwardJourneyGaiden  
  4. //  
  5. //  Created by vin on 14-3-17.  
  6. //  
  7. //  
  8.   
  9. #include "Lua_callCppTest.h"  
  10. #include "GameHttp.h"  
  11.   
  12. bool Lua_callCppTest::_isFirst;  
  13. Lua_callCppTest* Lua_callCppTest::_shared;  
  14.   
  15. Lua_callCppTest::~Lua_callCppTest(){  
  16.     CC_SAFE_DELETE(_shared);  
  17.     _shared=NULL;  
  18. }  
  19.   
  20. Lua_callCppTest* Lua_callCppTest::sharedHD(){  
  21.     if(!_isFirst){  
  22.         _shared = new Lua_callCppTest();  
  23.     }  
  24.     return _shared;  
  25. }  
  26.   
  27. const char* Lua_callCppTest::getFileFullPath(const char* fileName){  
  28.     return CCFileUtils::sharedFileUtils()->fullPathForFilename(fileName).c_str();  
  29. }  
  30.   
  31. #pragma mark - 废弃不要了  
  32. void Lua_callCppTest::callCppFunction(const char* luaFileName){  
  33.       
  34. //    lua_State*  ls = CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState();  
  35.       
  36.     lua_State* ls = CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState();  
  37.     luaopen_base(ls);  
  38.     luaopen_math(ls);  
  39.     luaopen_string(ls);  
  40. //    luaL_openlibs(ls);  
  41.     /* 
  42.      Lua调用的C++的函数必须是静态的 
  43.      */  
  44.     lua_register(ls, "cppFunction", cppFunction);  
  45.       
  46.     int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName));  
  47.     if(isOpen!=0){  
  48.         CCLOG("Open Lua Error: %i", isOpen);  
  49.         return;  
  50.     }else{  
  51.         CCLog("------sddd");  
  52.     }  
  53. }  
  54.   
  55. void Lua_callCppTest::callCppFunction(lua_State* ls){  
  56.     /* 
  57.      Lua调用的C++的函数必须是静态的 
  58.      */  
  59.     lua_register(ls, "cppFunction", cppFunction);  
  60. }  
  61.   
  62. int Lua_callCppTest::cppFunction(lua_State* ls){  
  63.     int luaNum = (int)lua_tonumber(ls, 1);  
  64.     int luaStr = (int)lua_tostring(ls, 2);  
  65.     CCLOG("Lua调用cpp函数时传来的两个参数: %i  %s",luaNum,luaStr);  
  66.       
  67.     /* 
  68.      返给Lua的值 
  69.      */  
  70.     lua_pushnumber(ls, 321);  
  71.     lua_pushstring(ls, "Himi");  
  72.   
  73. //    GameHttp * request = GameHttp::create();  
  74. //    request->testHttp();  
  75.   
  76.     /* 
  77.      返给Lua值个数 
  78.      */  
  79.     return 2;  
  80. }  
  81.   
  82. const char* Lua_callCppTest::callLuaFunction(const char* functionName){  
  83.     lua_State*  ls = CCLuaEngine::defaultEngine()->getLuaStack()->getLuaState();  
  84. //  
  85. //    int isOpen = luaL_dofile(ls, getFileFullPath(luaFileName));  
  86. //    if(isOpen!=0){  
  87. //        CCLOG("Open Lua Error: %i", isOpen);  
  88. //        return NULL;  
  89. //    }  
  90.       
  91.     lua_getglobal(ls, functionName);  
  92.       
  93.     lua_pushstring(ls, "Himi");  
  94.     lua_pushnumber(ls, 23);  
  95.     lua_pushboolean(ls, true);  
  96.       
  97.     /* 
  98.      lua_call 
  99.      第一个参数:函数的参数个数 
  100.      第二个参数:函数返回值个数 
  101.      */  
  102.     lua_call(ls, 3, 1);  
  103.       
  104.     const char* iResult = lua_tostring(ls, -1);  
  105.     CCLog("cpp print:%s",iResult);  
  106.     return iResult;  
  107. }  

lua代码 main.lua

[javascript] view plaincopy在CODE上查看代码片派生到我的代码片
  1. function __G__TRACKBACK__(msg)  
  2. print("----------------------------------------")  
  3. print("LUA ERROR: " .. tostring(msg) .. "\n")  
  4. print(debug.traceback())  
  5. print("----------------------------------------")  
  6. end  
  7.   
  8. local function main()  
  9.   
  10.      collectgarbage("setpause",500)  
  11.      collectgarbage("setstepmul",5000)  
  12.       
  13.     require "utils.helper" -- 全局变量  
  14.     require "utils.resource" -- 资源图片路径统一配置  
  15.     require "layer/LoginLayer" -- login  
  16.   
  17.     local scene = CCScene:create()  
  18.     local layer = LoginLayer:create()  
  19.     scene:addChild(layer)  
  20.     CCDirector:sharedDirector():runWithScene(scene)  
  21.       
  22.     ------------ test lua cpp ---------  
  23.     local num,str = cppFunction(999,"I'm a lua string")  
  24.     print("从cpp函数中获得两个返回值:",num,str)  
  25.       
  26. end  
  27.   
  28. function testCppTolua(_logStr,_logNum,_logBool)  
  29.     print("Lua 脚本打印从C传来的字符串:",_logStr,_logNum,_logBool)  
  30.     return "call lua function OK"  
  31. end  
  32.   
  33. xpcall(main, __G__TRACKBACK__)  
0 0