diff --git a/CC_LUA_ENGINE_README.mdown b/CC_LUA_ENGINE_README.mdown new file mode 100644 index 0000000000..eae1fb7bbb --- /dev/null +++ b/CC_LUA_ENGINE_README.mdown @@ -0,0 +1,191 @@ +# Cocos2d-x \*NEW\* Lua Engine README + +## Main features + +* Support autorelease CCObject object. +* Call Lua function from C++ (local, global, closure), avoid memory leaks. +* Add CCNode:setPosition(x, y), CCNode::getPosition() huge performance boost. +* Remove needless class and functions from tolua++ .pkg files, improved performance. +* CCMenuItem script callback. +* CCNode onEnter/onExit events handler. +* CCLayer touch & multi-touches events handler. +* Build-in Lua modules: cjson, luasocket. + + +## Changes + +* [del] remove old script support from all +* [add] ccConfig.h, define CC_LUA_ENGINE_ENABLED, CC_LUA_ENGINE_DEBUG macro +* [add] CCPlatformMacros.h, define LUALOG macro +* [add] CCObject::m_uLuaID used for Lua VM tracking C++ object +* [add] CCObject::~CCObject() call CCLuaEngine::removeCCObjectByID() +* [add] CCNode add some methods for fast read/write position + const CCPoint& getPositionLua(void); + void getPosition(float* x, float* y); + float getPositionX(void); + float getPositionY(void); + void setPositionX(float x); + void setPositionY(float y); + void setPosition(float x, float y); + void setPositionInPixels(float x, float y); +* [add] CCNode::getChildrenCount() for Lua +* [add] CCNode::registerScriptHandler() enable onEnter/onExit event script callback +* [add] CCNode::unregisterScriptHandler() +* [add] CCMenuItem::registerScriptHandler() +* [add] CCMenuItem::unregisterScriptHandler() +* [add] CCMenuItem::activate() default implements call script callback if exist +* [add] CCMenuItem::m_uScriptHandlerFuncID save Lua function reference +* [add] CCLayer::m_uScriptHandlerFuncID save Lua function reference +* [add] ccTouchBegan(), ccTouchMoved(), ccTouchEnded(), ccTouchCancelled(), + ccTouchesBegan(), ccTouchesMoved(), ccTouchesEnded(), ccTouchesCancelled() + default implements are used to call script callback if exist +* [add] CCLayer::registerScriptTouchHandler() +* [add] CCLayer::unregisterScriptTouchHandler() +* [add] CCTimer::timerWithScriptFuncID() +* [add] CCTimer::initWithScriptFuncID() +* [add] CCTimer::m_uScriptFuncID +* [add] CCScheduler::scheduleScriptFunc() +* [add] CCScheduler::unscheduleScriptEntry() +* [add] CCScheduler::m_pScriptEntries +* [add] add Lua module: cjson, luasocket + +---- + +## How to use + +### CCNode onEnter/onExit events handler + +CCNode:registerScriptHandler() register callback function for onEnter/onExit events。Callback function auto unregister after onExit() called. + + local function createScene() + local scene = CCScene:node() + + + + local function sceneEventHandler(eventType) + if eventType == kCCNodeOnEnter then + if scene.onEnter then scene:onEnter() end + else + if scene.onExit then scene:onExit() end + end + end + + scene:registerScriptHandler(sceneEventHandler) + + return scene + end + + local scene = createScene() + function scene:onEnter() + print("on scene enter") + end + fucntion scene:onExit() + print("on scene exit") + end + + CCDirector:sharedDirector():runWithScene(scene) + + +### Touch and multi-touches events handler + + local function onTouch(eventType, x, y) + -- eventType is CCTOUCHBEGAN, CCTOUCHMOVED, + -- CCTOUCHENDED or CCTOUCHCANCELLED + -- x, y is touch position (use cocos2d-x/OpenGL coordinate) + end + + local function onTouches(eventType, touches) + -- touches is x1, y1, x2, y2, x3, y3, ... + for i = 1, #touches, 2 do + local x, y = touches[i], touches[i + 1] + print(x, y) + end + end + + local layer = CCLayer:node() + + -- enable touch events callback + layer:registerScriptTouchHandler(onTouch, false) -- for single touch + OR + layer:registerScriptTouchHandler(onTouches, true) -- for multi-touches + + -- remove touch events callback + layer:unregisterScriptTouchHandler() + + +### Schedule callback + +scheduler module: + + module("scheduler", package.seeall) + + scheduler = CCScheduler:sharedScheduler() + + function enterFrame(listener, isPaused) + return scheduler:scheduleScriptFunc(listener, 0, isPaused or false) + end + + function schedule(listener, interval, isPaused) + return scheduler:scheduleScriptFunc(listener, interval, isPaused or false) + end + + function unschedule(handle) + scheduler:unscheduleScriptEntry(handle) + end + remove = unschedule + + function performWithDelay(time, listener) + local handle + handle = scheduler:scheduleScriptFunc(function() + scheduler:unscheduleScriptEntry(handle) + listener() + end, time, false) + return handle + end + +use: + + require("scheduler") + + local handle -- save script callback ID + + -- schedule every frame update + local frameCount = 0 + local function onEnterFrame(dt) + -- dt is float number + frameCount = frameCount + 1 + if frameCount >= 60 then + -- unschedule callback + scheduler.unschedule(handle) + end + end + + handle = scheduler.enterFrame(onEnterFrame) + + -- print message after delay + scheduler.performWithDelay(0.5, function() + print("delay 0.5 second") + end) + + +### Create menu + + local function onPlayButtonTap() + print("PLAY NOW") + end + + local button = CCMenuItemSprite:itemFromNormalImage("playButton.png", "playButton.png") + button:setPosition(100, 200) + button:registerScriptHandler(onPlayButtonTap) + + local menu = CCMenu:node() + menu:addChild(button) + scene:addChild(menu) + +---- + + +## TODO + +* When C++ object deleted, remove Lua userdata +* Improvement tolua_isusertype() diff --git a/build-linux.sh b/build-linux.sh index 4a2b57a09f..36ce406135 100755 --- a/build-linux.sh +++ b/build-linux.sh @@ -46,7 +46,7 @@ if [ $# -ne 0 ]; then cd ../../ rm -r lib/linux/Debug/*.so fi - + else cd $p/cocos2dx/proj.linux echo "**********************building cocos2dx**************************" diff --git a/build-wophone.sh b/build-wophone.sh old mode 100644 new mode 100755 diff --git a/create-android-project.sh b/create-android-project.sh index 8c7137bb76..518025f279 100755 --- a/create-android-project.sh +++ b/create-android-project.sh @@ -34,7 +34,7 @@ KERNEL_NAME=`uname -s | grep "CYGWIN*"` if [ $KERNEL_NAME"hi" != "hi" ]; then echo "Error!!!" echo "Don't run in cygwin. You should run corresponding bat." - exit + exit fi # ok, it was run under linux @@ -44,7 +44,7 @@ check_path(){ if [ ! -f create-android-project.sh ];then echo Error!!! echo Please run in cocos2dx root - exit + exit fi } diff --git a/create-qnx-project.sh b/create-qnx-project.sh old mode 100644 new mode 100755 diff --git a/install-templates-xcode.sh b/install-templates-xcode.sh index b202f70cbd..86ff693aa5 100755 --- a/install-templates-xcode.sh +++ b/install-templates-xcode.sh @@ -12,9 +12,9 @@ user_dir= usage(){ cat << EOF usage: $0 [options] - + Install / update templates for ${COCOS2D_VER} - + OPTIONS: -f force overwrite if directories exist -h this help @@ -45,7 +45,7 @@ if [[ ! $user_dir && "$(id -u)" != "0" ]]; then echo "Try running it with 'sudo', or with '-u' to install it only you:" 1>&2 echo " sudo $0" 1>&2 echo "or:" 1>&2 - echo " $0 -u" 1>&2 + echo " $0 -u" 1>&2 exit 1 fi @@ -77,7 +77,7 @@ check_dst_dir(){ exit 1 fi fi - + echo ...creating destination directory: $DST_DIR mkdir -p "$DST_DIR" } @@ -166,54 +166,54 @@ copy_xcode3_project_templates(){ copy_base_files echo done! - + print_template_banner "Installing cocos2d-x iOS + box2d template" - + DST_DIR="$TEMPLATE_DIR""cocos2d-x Box2d Application/" LIBS_DIR="$DST_DIR"libs - + check_dst_dir - + echo ...copying template files copy_files template/xcode3/cocos2d-x_box2d_app/ "$DST_DIR" - + copy_base_files - + echo ...copying Box2D files copy_files Box2D "$LIBS_DIR" - + echo done! - + print_template_banner "Installing cocos2d-x iOS + chipmunk template" - + DST_DIR="$TEMPLATE_DIR""cocos2d-x chipmunk Application/" LIBS_DIR="$DST_DIR"libs - + check_dst_dir - + echo ...copying template files copy_files template/xcode3/cocos2d-x_chipmunk_app/ "$DST_DIR" - + copy_base_files - + echo ...coping chipmunk files copy_files chipmunk "$LIBS_DIR" - + print_template_banner "Installing cocos2d-x iOS + lua template" - + DST_DIR="$TEMPLATE_DIR""cocos2d-x lua Application/" LIBS_DIR="$DST_DIR"libs - + check_dst_dir - + echo ...copying template files copy_files template/xcode3/cocos2d-x_lua_app/ "$DST_DIR" - + copy_base_files - + echo ...coping lua files copy_files lua "$LIBS_DIR" - + echo done! } @@ -223,7 +223,7 @@ copy_xcode3_file_templates(){ else TEMPLATE_DIR="${BASE_TEMPLATE_DIR}/File Templates/${COCOS2D_VER}/" fi - + echo ...copying file templates DST_DIR="$TEMPLATE_DIR" @@ -234,11 +234,11 @@ copy_xcode3_file_templates(){ echo '' mkdir -p "$TEMPLATE_DIR" fi - + # print_template_banner "Installing CCNode file templates..." - + # copy_files "templates/file-templates/CCNode class" "$DST_DIR" - + echo done! } @@ -255,7 +255,7 @@ copy_xcode4_project_templates(){ mkdir -p "$LIBS_DIR" copy_cocos2d_files - + # LIBS_DIR="$DST_DIR""lib_cocoslive.xctemplate/libs/" # mkdir -p "$LIBS_DIR" # copy_cocoslive_files @@ -318,13 +318,13 @@ copy_xcode4_project_templates(){ # Move File Templates to correct position # DST_DIR="$HOME/Library/Developer/Xcode/Templates/File Templates/cocos2d/" # OLD_DIR="$HOME/Library/Developer/Xcode/Templates/cocos2d/" - + # print_template_banner "Installing Xcode 4 CCNode file templates..." # check_dst_dir - + # mv -f "$OLD_DIR""/CCNode class.xctemplate" "$DST_DIR" - + echo done! } @@ -334,22 +334,22 @@ select_template_version(){ echo "3 for xcode3" echo "4 for xcode4" echo "input nothing for all" - + read select - + if [[ "$select" == 3 ]]; then copy_xcode3_project_templates fi - + if [[ "$select" == 4 ]]; then copy_xcode4_project_templates fi - + if [[ "$select""aaaa" == "aaaa" ]]; then copy_xcode3_project_templates copy_xcode4_project_templates fi - + } select_template_version