From c5fa5d75f81a4b8db811f7b0a58ee6cd5434711e Mon Sep 17 00:00:00 2001 From: YuLei Liao Date: Fri, 3 Feb 2012 15:16:33 +0800 Subject: [PATCH] * merge upstream/master to master --- CocosDenshion/proj.qnx/.cproject | 11 +- CocosDenshion/qnx/SimpleAudioEngine.cpp | 424 +++++++++--------- HelloLua/Resource/hello.lua | 336 +++++++------- .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 10 +- .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 14 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../Resource/fonts/arial.ttf.REMOVED.git-id | 1 + .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 10 +- .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 14 +- .../project.pbxproj.REMOVED.git-id | 2 +- README.mdown | 122 +---- .../Templates/1033/Classes/AppDelegate.cpp | 33 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../project.pbxproj.REMOVED.git-id | 2 +- .../cocos2d-x_lua_app/Classes/AppDelegate.cpp | 62 +-- .../cocos2d-x_lua_app/Classes/AppDelegate.h | 4 - .../project.pbxproj.REMOVED.git-id | 2 +- .../Classes/AppDelegate.cpp | 58 ++- .../Classes/AppDelegate.h | 4 - .../Resources/hello.lua | 334 +++++++------- .../Res/fonts/Courier New.ttf.REMOVED.git-id | 1 + tests/Res/fonts/Thonburi.ttf.REMOVED.git-id | 1 + .../Res/fonts/ThonburiBold.ttf.REMOVED.git-id | 1 + tests/Res/fonts/arial.ttf.REMOVED.git-id | 1 + tests/Res/fonts/tahoma.ttf.REMOVED.git-id | 1 + .../src/org/cocos2dx/lib/Cocos2dxBitmap.java | 10 +- .../cocos2dx/lib/Cocos2dxGLSurfaceView.java | 14 +- .../project.pbxproj.REMOVED.git-id | 2 +- tests/test.qnx/.cproject | 25 +- .../CocosDenshionTest/CocosDenshionTest.cpp | 2 + .../tests/UserDefaultTest/UserDefaultTest.cpp | 4 + 32 files changed, 713 insertions(+), 798 deletions(-) create mode 100644 HelloWorld/Resource/fonts/arial.ttf.REMOVED.git-id create mode 100644 tests/Res/fonts/Courier New.ttf.REMOVED.git-id create mode 100644 tests/Res/fonts/Thonburi.ttf.REMOVED.git-id create mode 100644 tests/Res/fonts/ThonburiBold.ttf.REMOVED.git-id create mode 100644 tests/Res/fonts/arial.ttf.REMOVED.git-id create mode 100644 tests/Res/fonts/tahoma.ttf.REMOVED.git-id diff --git a/CocosDenshion/proj.qnx/.cproject b/CocosDenshion/proj.qnx/.cproject index 96c58e32a8..dd86568d6f 100644 --- a/CocosDenshion/proj.qnx/.cproject +++ b/CocosDenshion/proj.qnx/.cproject @@ -47,6 +47,7 @@ + @@ -118,6 +119,7 @@ + @@ -183,6 +185,7 @@ + @@ -251,6 +254,7 @@ + @@ -317,6 +321,7 @@ + @@ -383,6 +388,7 @@ + @@ -450,6 +456,7 @@ + @@ -511,6 +518,8 @@ - + + + diff --git a/CocosDenshion/qnx/SimpleAudioEngine.cpp b/CocosDenshion/qnx/SimpleAudioEngine.cpp index 1e1b4e428b..35b8cd2bdb 100644 --- a/CocosDenshion/qnx/SimpleAudioEngine.cpp +++ b/CocosDenshion/qnx/SimpleAudioEngine.cpp @@ -30,10 +30,8 @@ THE SOFTWARE. #include #include #include -#include #include - -//#include "CCFileUtils.h" +#include #include "SimpleAudioEngine.h" @@ -56,115 +54,68 @@ namespace CocosDenshion PAUSED, } playStatus; - static int s_audioOid; + static float s_volume = 1.0f; + static float s_effectVolume = 1.0f; + static bool s_isBackgroundInitialized = false; + static std::string s_currentBackgroundStr; - static float s_volume = 1.0f; - static float s_effectVolume = 1.0f; - static bool s_isBackgroundInitialized = false; - static bool s_hasMMRError = false; - static playStatus s_playStatus = STOPPED; - - static string s_currentBackgroundStr; - static mmr_connection_t *s_mmrConnection = 0; - static mmr_context_t *s_mmrContext = 0; - static strm_dict_t *s_repeatDictionary = 0; - static strm_dict_t *s_volumeDictionary = 0; + ALuint s_backgroundBuffer; + ALuint s_backgroundSource; static SimpleAudioEngine *s_engine = 0; - static void printALError(int err) + static int checkALError(const char *funcName) { - switch (err) + int err = alGetError(); + + if (err != AL_NO_ERROR) { - case AL_NO_ERROR: - fprintf(stderr, "AL_NO_ERROR"); - break; + switch (err) + { + case AL_INVALID_NAME: + fprintf(stderr, "AL_INVALID_NAME in %s\n", funcName); + break; - case AL_INVALID_NAME: - fprintf(stderr, "AL_INVALID_NAME"); - break; + case AL_INVALID_ENUM: + fprintf(stderr, "AL_INVALID_ENUM in %s\n", funcName); + break; - case AL_INVALID_ENUM: - fprintf(stderr, "AL_INVALID_ENUM"); - break; + case AL_INVALID_VALUE: + fprintf(stderr, "AL_INVALID_VALUE in %s\n", funcName); + break; - case AL_INVALID_VALUE: - fprintf(stderr, "AL_INVALID_VALUE"); - break; + case AL_INVALID_OPERATION: + fprintf(stderr, "AL_INVALID_OPERATION in %s\n", funcName); + break; - case AL_INVALID_OPERATION: - fprintf(stderr, "AL_INVALID_OPERATION"); - break; + case AL_OUT_OF_MEMORY: + fprintf(stderr, "AL_OUT_OF_MEMORY in %s\n", funcName); + break; + } + } - case AL_OUT_OF_MEMORY: - fprintf(stderr, "AL_OUT_OF_MEMORY"); - break; - }; + return err; } - static void mmrerror(mmr_context_t *ctxt, const char *msg) - { - const mmr_error_info_t *err = mmr_error_info( ctxt ); - unsigned errcode = (err) ? err->error_code : -1; - const char *name; - - fprintf(stderr, "%s: error %d \n", msg, errcode); - s_hasMMRError = true; - } - static void stopBackground(bool bReleaseData) { - s_playStatus = STOPPED; - - if (s_mmrContext) - mmr_stop(s_mmrContext); + alSourceStop(s_backgroundSource); if (bReleaseData) { - if (s_mmrContext) - { - mmr_input_detach(s_mmrContext); - mmr_context_destroy(s_mmrContext); - } - - if (s_mmrConnection) - mmr_disconnect(s_mmrConnection); - - if (s_repeatDictionary) - strm_dict_destroy(s_repeatDictionary); - - if (s_volumeDictionary) - strm_dict_destroy(s_volumeDictionary); - - s_mmrContext = 0; - s_mmrConnection = 0; - s_repeatDictionary = 0; - s_volumeDictionary = 0; - s_hasMMRError = false; s_currentBackgroundStr = ""; s_isBackgroundInitialized = false; + + alDeleteBuffers(1, &s_backgroundBuffer); + checkALError("stopBackground"); + alDeleteSources(1, &s_backgroundSource); + checkALError("stopBackground"); } } static void setBackgroundVolume(float volume) { - if (!s_isBackgroundInitialized) - { - return; - } - char volume_str[128]; - - // set it up the background volume - strm_dict_t *dictionary = strm_dict_new(); - - sprintf(volume_str, "%d", (int)(volume * 100) ); - s_volumeDictionary = strm_dict_set(dictionary, "volume", volume_str); - - if (mmr_output_parameters(s_mmrContext, s_audioOid, s_volumeDictionary) != 0) - { - mmrerror(s_mmrContext, "output parameters"); - return; - } + alSourcef(s_backgroundSource, AL_GAIN, volume); } SimpleAudioEngine::SimpleAudioEngine() @@ -187,23 +138,22 @@ namespace CocosDenshion void SimpleAudioEngine::end() { + checkALError("end"); + // clear all the sounds EffectsMap::const_iterator end = s_effects.end(); for (EffectsMap::iterator it = s_effects.begin(); it != end; it++) { alSourceStop(it->second->source); - + checkALError("end"); alDeleteBuffers(1, &it->second->buffer); + checkALError("end"); alDeleteSources(1, &it->second->source); + checkALError("end"); delete it->second; } s_effects.clear(); - if (s_isBackgroundInitialized) - { - s_isBackgroundInitialized = false; - } - // and the background too stopBackground(true); } @@ -213,134 +163,170 @@ namespace CocosDenshion } // - // background audio (using mmrenderer) + // OGG support + // + static bool isOGGFile(const char *pszFilePath) + { + FILE *file; + OggVorbis_File ogg_file; + int result; + + file = fopen(pszFilePath, "rb"); + result = ov_test(file, &ogg_file, 0, 0); + ov_clear(&ogg_file); + + return (result == 0); + } + + static ALuint createBufferFromOGG(const char *pszFilePath) + { + ALuint buffer; + OggVorbis_File ogg_file; + vorbis_info* info; + ALenum format; + int result; + int section; + int err; + unsigned int size = 0; + + if (ov_fopen(pszFilePath, &ogg_file) < 0) + { + ov_clear(&ogg_file); + fprintf(stderr, "Could not open OGG file %s\n", pszFilePath); + return -1; + } + + info = ov_info(&ogg_file, -1); + + if (info->channels == 1) + format = AL_FORMAT_MONO16; + else + format = AL_FORMAT_STEREO16; + + // size = #samples * #channels * 2 (for 16 bit) + unsigned int data_size = ov_pcm_total(&ogg_file, -1) * info->channels * 2; + char* data = new char[data_size]; + + while (size < data_size) + { + result = ov_read(&ogg_file, data + size, data_size - size, 0, 2, 1, §ion); + if (result > 0) + { + size += result; + } + else if (result < 0) + { + delete [] data; + fprintf(stderr, "OGG file problem %s\n", pszFilePath); + return -1; + } + else + { + break; + } + } + + if (size == 0) + { + delete [] data; + fprintf(stderr, "Unable to read OGG data\n"); + return -1; + } + + // clear al errors + checkALError("createBufferFromOGG"); + + // Load audio data into a buffer. + alGenBuffers(1, &buffer); + + if (checkALError("createBufferFromOGG") != AL_NO_ERROR) + { + fprintf(stderr, "Couldn't generate a buffer for OGG file\n"); + delete [] data; + return buffer; + } + + alBufferData(buffer, format, data, data_size, info->rate); + checkALError("createBufferFromOGG"); + + delete [] data; + ov_clear(&ogg_file); + + return buffer; + } + + + // + // background audio // void SimpleAudioEngine::preloadBackgroundMusic(const char* pszFilePath) { - if (!s_isBackgroundInitialized) - { - const char *mmrname = NULL; - const char *ctxtname = "mmrplayer"; - char cwd[PATH_MAX]; - mode_t mode = S_IRUSR | S_IXUSR; + if (!s_isBackgroundInitialized || s_currentBackgroundStr != pszFilePath) + { + string path = pszFilePath; - getcwd(cwd, PATH_MAX); - string path = "file://"; - path += cwd; - path += "/"; - path += pszFilePath; + if (isOGGFile(path.data())) + { + s_backgroundBuffer = createBufferFromOGG(path.data()); + } + else + { + s_backgroundBuffer = alutCreateBufferFromFile(path.data()); + } - s_mmrConnection = mmr_connect(mmrname); - if (!s_mmrConnection) - { - perror("mmr_connect"); - s_hasMMRError = true; - return; - } + checkALError("preloadBackgroundMusic"); - s_mmrContext = mmr_context_create(s_mmrConnection, ctxtname, 0, mode); - if (!s_mmrContext) - { - perror(ctxtname); - s_hasMMRError = true; - return; - } + if (s_backgroundBuffer == AL_NONE) + { + fprintf(stderr, "Error loading file: '%s'\n", path.data()); + alDeleteBuffers(1, &s_backgroundBuffer); + return; + } - if ((s_audioOid = mmr_output_attach(s_mmrContext, "audio:default", "audio")) < 0) - { - mmrerror(s_mmrContext, "audio:default"); - return; - } + alGenSources(1, &s_backgroundSource); + checkALError("preloadBackgroundMusic"); - if (mmr_input_attach(s_mmrContext, path.data(), "autolist") < 0) - { - fprintf(stderr, "unable to load %s\n", path.data()); - mmrerror(s_mmrContext, path.data()); - return; - } + alSourcei(s_backgroundSource, AL_BUFFER, s_backgroundBuffer); + checkALError("preloadBackgroundMusic"); - s_currentBackgroundStr = pszFilePath; - s_isBackgroundInitialized = true; - setBackgroundVolume(s_volume); - } + s_currentBackgroundStr = pszFilePath; + } + + s_currentBackgroundStr = pszFilePath; + s_isBackgroundInitialized = true; } void SimpleAudioEngine::playBackgroundMusic(const char* pszFilePath, bool bLoop) { - if (0 != strcmp(s_currentBackgroundStr.c_str(), pszFilePath)) - { - stopBackgroundMusic(true); - } - else - { - if (s_playStatus == PAUSED) - resumeBackgroundMusic(); - else - rewindBackgroundMusic(); - } - if (!s_isBackgroundInitialized) preloadBackgroundMusic(pszFilePath); - if (bLoop) - { - // set it up to loop - strm_dict_t *dictionary = strm_dict_new(); - s_repeatDictionary = strm_dict_set(dictionary, "repeat", "all"); - - if (mmr_input_parameters(s_mmrContext, s_repeatDictionary) != 0) - { - mmrerror(s_mmrContext, "input parameters (loop)"); - return; - } - } - - if (s_hasMMRError || !s_mmrContext) - return; - - if (mmr_play(s_mmrContext) < 0) - { - mmrerror(s_mmrContext, "mmr_play"); - s_hasMMRError = true; - } - - if (!s_hasMMRError) - s_playStatus = PLAYING; + alSourcei(s_backgroundSource, AL_LOOPING, bLoop ? AL_TRUE : AL_FALSE); + alSourcePlay(s_backgroundSource); + checkALError("playBackgroundMusic"); } void SimpleAudioEngine::stopBackgroundMusic(bool bReleaseData) { - // if we were paused then we need to resume first so that we can play - if (s_playStatus == PAUSED) - resumeBackgroundMusic(); - stopBackground(bReleaseData); } void SimpleAudioEngine::pauseBackgroundMusic() { - if (s_mmrContext && mmr_speed_set(s_mmrContext, 0) < 0) - { - mmrerror(s_mmrContext, "pause"); - } - s_playStatus = PAUSED; + alSourcePause(s_backgroundSource); + checkALError("pauseBackgroundMusic"); } void SimpleAudioEngine::resumeBackgroundMusic() { - if (s_mmrContext && mmr_speed_set(s_mmrContext, 1000) < 0) - { - mmrerror(s_mmrContext, "resume"); - } - s_playStatus = PLAYING; + alSourcePlay(s_backgroundSource); + checkALError("resumeBackgroundMusic"); } void SimpleAudioEngine::rewindBackgroundMusic() { - if (s_mmrContext && mmr_seek(s_mmrContext, "1:0") < 0) - { - mmrerror(s_mmrContext, "rewind"); - } + alSourceRewind(s_backgroundSource); + checkALError("rewindBackgroundMusic"); } bool SimpleAudioEngine::willPlayBackgroundMusic() @@ -350,7 +336,10 @@ namespace CocosDenshion bool SimpleAudioEngine::isBackgroundMusicPlaying() { - return (s_playStatus == PLAYING) && s_isBackgroundInitialized; + ALint play_status; + alGetSourcei(s_backgroundSource, AL_SOURCE_STATE, &play_status); + + return (play_status == AL_PLAYING); } float SimpleAudioEngine::getBackgroundMusicVolume() @@ -407,9 +396,11 @@ namespace CocosDenshion } } + checkALError("playEffect"); iter->second->isLooped = bLoop; alSourcei(iter->second->source, AL_LOOPING, iter->second->isLooped ? AL_TRUE : AL_FALSE); alSourcePlay(iter->second->source); + checkALError("playEffect"); return iter->second->source; } @@ -417,6 +408,7 @@ namespace CocosDenshion void SimpleAudioEngine::stopEffect(unsigned int nSoundId) { alSourceStop(nSoundId); + checkALError("stopEffect"); } void SimpleAudioEngine::preloadEffect(const char* pszFilePath) @@ -429,10 +421,19 @@ namespace CocosDenshion ALuint buffer; ALuint source; soundData *data = new soundData; + string path = pszFilePath; - string path = pszFilePath; + checkALError("preloadEffect"); - buffer = alutCreateBufferFromFile(path.data()); + if (isOGGFile(path.data())) + { + buffer = createBufferFromOGG(path.data()); + } + else + { + buffer = alutCreateBufferFromFile(path.data()); + checkALError("preloadEffect"); + } if (buffer == AL_NONE) { @@ -442,7 +443,15 @@ namespace CocosDenshion } alGenSources(1, &source); + + if (checkALError("preloadEffect") != AL_NO_ERROR) + { + alDeleteBuffers(1, &buffer); + return; + } + alSourcei(source, AL_BUFFER, buffer); + checkALError("preloadEffect"); data->isLooped = false; data->buffer = buffer; @@ -458,14 +467,17 @@ namespace CocosDenshion if (iter != s_effects.end()) { - alSourceStop(iter->second->source); - alDeleteSources(1, &iter->second->source); - alDeleteBuffers(1, &iter->second->buffer); - delete iter->second; + checkALError("unloadEffect"); - int err = alGetError(); - if (err != AL_NO_ERROR) - printALError(err); + alSourceStop(iter->second->source); + checkALError("unloadEffect"); + + alDeleteSources(1, &iter->second->source); + checkALError("unloadEffect"); + + alDeleteBuffers(1, &iter->second->buffer); + checkALError("unloadEffect"); + delete iter->second; s_effects.erase(iter); } @@ -474,6 +486,7 @@ namespace CocosDenshion void SimpleAudioEngine::pauseEffect(unsigned int nSoundId) { alSourcePause(nSoundId); + checkALError("pauseEffect"); } void SimpleAudioEngine::pauseAllEffects() @@ -483,15 +496,14 @@ namespace CocosDenshion if (iter != s_effects.end()) { alSourcePause(iter->second->source); - int err = alGetError(); - if (err != AL_NO_ERROR) - printALError(err); + checkALError("pauseAllEffects"); } } void SimpleAudioEngine::resumeEffect(unsigned int nSoundId) { alSourcePlay(nSoundId); + checkALError("resumeEffect"); } void SimpleAudioEngine::resumeAllEffects() @@ -500,10 +512,9 @@ namespace CocosDenshion if (iter != s_effects.end()) { + checkALError("resumeAllEffects"); alSourcePlay(iter->second->source); - int err = alGetError(); - if (err != AL_NO_ERROR) - printALError(err); + checkALError("resumeAllEffects"); } } @@ -513,10 +524,9 @@ namespace CocosDenshion if (iter != s_effects.end()) { + checkALError("stopAllEffects"); alSourceStop(iter->second->source); - int err = alGetError(); - if (err != AL_NO_ERROR) - printALError(err); + checkALError("stopAllEffects"); } } diff --git a/HelloLua/Resource/hello.lua b/HelloLua/Resource/hello.lua index 7569a2ac0a..2c48dafa90 100644 --- a/HelloLua/Resource/hello.lua +++ b/HelloLua/Resource/hello.lua @@ -1,166 +1,180 @@ -require "hello2" - -cocos2d.CCLuaLog("result is " .. myadd(3, 5)) - --- create scene & layer -layerFarm = cocos2d.CCLayer:node() -layerFarm:setIsTouchEnabled(true) - -layerMenu = cocos2d.CCLayer:node() - -sceneGame = cocos2d.CCScene:node() -sceneGame:addChild(layerFarm) -sceneGame:addChild(layerMenu) - -winSize = cocos2d.CCDirector:sharedDirector():getWinSize() - --- add in farm background -spriteFarm = cocos2d.CCSprite:spriteWithFile("farm.jpg") -spriteFarm:setPosition(cocos2d.CCPoint(winSize.width/2 + 80, winSize.height/2)) -layerFarm:addChild(spriteFarm) - --- touch handers -pointBegin = nil - -function btnTouchMove(e) - cocos2d.CCLuaLog("btnTouchMove") - if pointBegin ~= nil then - local v = e[1] - local pointMove = v:locationInView(v:view()) - pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove) - local positionCurrent = layerFarm:getPosition() - layerFarm:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y)) - pointBegin = pointMove - end -end - -function btnTouchBegin(e) - cocos2d.CCScheduler:sharedScheduler():unscheduleScriptFunc("tick") - cocos2d.CCLuaLog("btnTouchBegin") - for k,v in ipairs(e) do - pointBegin = v:locationInView(v:view()) - pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin) - end -end - -function btnTouchEnd(e) - cocos2d.CCLuaLog("btnTouchEnd") - touchStart = nil -end - --- regiester touch handlers -layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin") -layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove") -layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd") - - --- add land sprite -for i=0,3,1 do - for j=0,1,1 do - spriteLand = cocos2d.CCSprite:spriteWithFile("land.png") - layerFarm:addChild(spriteLand) - spriteLand:setPosition(cocos2d.CCPoint(200+j*180 - i%2*90, 10+i*95/2)) - end -end - --- add crop - -for i=0,3,1 do - for j=0,1,1 do - - textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png") - frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95)) - spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop); - - layerFarm:addChild(spriteCrop) - - spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2)) - - end -end - --- add the moving dog - -FrameWidth = 105 -FrameHeight = 95 - -textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog.png") -frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(0, 0, FrameWidth, FrameHeight)) -frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(FrameWidth*1, 0, FrameWidth, FrameHeight)) - -spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0) -spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3)) -layerFarm:addChild(spriteDog) - -animation = cocos2d.CCAnimation:animation() - -animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2) -animFrames:addObject(frame0) -animFrames:addObject(frame1) - -animation = cocos2d.CCAnimation:animationWithFrames(animFrames,0.5) - -animate = cocos2d.CCAnimate:actionWithAnimation(animation, false); -spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate)) - - --- add a popup menu - -function menuCallbackClosePopup() --- stop test sound effect -CocosDenshion.SimpleAudioEngine:sharedEngine():stopEffect(effectID) -menuPopup:setIsVisible(false) -end - -menuPopupItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") -menuPopupItem:setPosition( cocos2d.CCPoint(0, 0) ) -menuPopupItem:registerScriptHandler("menuCallbackClosePopup") -menuPopup = cocos2d.CCMenu:menuWithItem(menuPopupItem) -menuPopup:setPosition( cocos2d.CCPoint(winSize.width/2, winSize.height/2) ) -menuPopup:setIsVisible(false) -layerMenu:addChild(menuPopup) - --- add the left-bottom "tools" menu to invoke menuPopup - -function menuCallbackOpenPopup() --- loop test sound effect --- NOTE: effectID is global, so it can be used to stop -effectID = CocosDenshion.SimpleAudioEngine:sharedEngine():playEffect("effect1.wav") -menuPopup:setIsVisible(true) -end - -menuToolsItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png") -menuToolsItem:setPosition( cocos2d.CCPoint(0, 0) ) -menuToolsItem:registerScriptHandler("menuCallbackOpenPopup") -menuTools = cocos2d.CCMenu:menuWithItem(menuToolsItem) -menuTools:setPosition( cocos2d.CCPoint(30, 40) ) -layerMenu:addChild(menuTools) - - -function tick() - - local point = spriteDog:getPosition(); - - if point.x > winSize.width then - point.x = 0 - spriteDog:setPosition(point) - else - point.x = point.x + 1 - spriteDog:setPosition(point) - end - -end -- avoid memory leak -collectgarbage( "setpause", 100) -collectgarbage( "setstepmul", 5000) +collectgarbage("setpause", 100) +collectgarbage("setstepmul", 5000) + +local cclog = function(...) + print(string.format(...)) +end + +require "hello2" +cclog("result is " .. myadd(3, 5)) + +--------------- + +local winSize = CCDirector:sharedDirector():getWinSize() + +-- add the moving dog +local function creatDog() + local frameWidth = 105 + local frameHeight = 95 + + -- create dog animate + local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png") + local rect = CCRectMake(0, 0, frameWidth, frameHeight) + local frame0 = CCSpriteFrame:frameWithTexture(textureDog, rect) + rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight) + local frame1 = CCSpriteFrame:frameWithTexture(textureDog, rect) + + local spriteDog = CCSprite:spriteWithSpriteFrame(frame0) + spriteDog.isPaused = false + spriteDog:setPosition(0, winSize.height / 4 * 3) + + local animFrames = CCMutableArray_CCSpriteFrame__:new(2) + animFrames:addObject(frame0) + animFrames:addObject(frame1) + + local animation = CCAnimation:animationWithFrames(animFrames, 0.5) + local animate = CCAnimate:actionWithAnimation(animation, false); + spriteDog:runAction(CCRepeatForever:actionWithAction(animate)) + + -- moving dog at every frame + local function tick() + if spriteDog.isPaused then return end + local x, y = spriteDog:getPosition() + if x > winSize.width then + x = 0 + else + x = x + 1 + end + spriteDog:setPositionX(x) + end + + CCScheduler:sharedScheduler():scheduleScriptFunc(tick, 0, false) + + return spriteDog +end + +-- create farm +local function createLayerFram() + local layerFarm = CCLayer:node() + + -- add in farm background + local bg = CCSprite:spriteWithFile("farm.jpg") + bg:setPosition(winSize.width / 2 + 80, winSize.height / 2) + layerFarm:addChild(bg) + + -- add land sprite + for i = 0, 3 do + for j = 0, 1 do + local spriteLand = CCSprite:spriteWithFile("land.png") + spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2) + layerFarm:addChild(spriteLand) + end + end + + -- add crop + local textureCrop = CCTextureCache:sharedTextureCache():addImage("crop.png") + local frameCrop = CCSpriteFrame:frameWithTexture(textureCrop, CCRectMake(0, 0, 105, 95)) + for i = 0, 3 do + for j = 0, 1 do + local spriteCrop = CCSprite:spriteWithSpriteFrame(frameCrop); + spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2) + layerFarm:addChild(spriteCrop) + end + end + + -- add moving dog + local spriteDog = creatDog() + layerFarm:addChild(spriteDog) + + -- handing touch events + local touchBeginPoint = nil + + local function onTouchBegan(x, y) + cclog("onTouchBegan: %0.2f, %0.2f", x, y) + touchBeginPoint = {x = x, y = y} + spriteDog.isPaused = true + -- CCTOUCHBEGAN event must return true + return true + end + + local function onTouchMoved(x, y) + cclog("onTouchMoved: %0.2f, %0.2f", x, y) + if touchBeginPoint then + local cx, cy = layerFarm:getPosition() + layerFarm:setPosition(cx + x - touchBeginPoint.x, + cy + y - touchBeginPoint.y) + touchBeginPoint = {x = x, y = y} + end + end + + local function onTouchEnded(x, y) + cclog("onTouchEnded") + touchBeginPoint = nil + spriteDog.isPaused = false + end + + local function onTouch(eventType, x, y) + if eventType == CCTOUCHBEGAN then + return onTouchBegan(x, y) + elseif eventType == CCTOUCHMOVED then + return onTouchMoved(x, y) + else + return onTouchEnded(x, y) + end + end + + layerFarm:registerScriptTouchHandler(onTouch) + + return layerFarm +end -cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false) +-- create menu +local function createLayerMenu() + local layerMenu = CCLayer:node() --- play background music -CocosDenshion.SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true); --- preload effect -CocosDenshion.SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav"); --- run -cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame) + local menuPopup, menuTools, effectID + + local function menuCallbackClosePopup() + -- stop test sound effect + SimpleAudioEngine:sharedEngine():stopEffect(effectID) + menuPopup:setIsVisible(false) + end + + local function menuCallbackOpenPopup() + -- loop test sound effect + effectID = SimpleAudioEngine:sharedEngine():playEffect("effect1.wav") + menuPopup:setIsVisible(true) + end + + -- add a popup menu + local menuPopupItem = CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") + menuPopupItem:setPosition(0, 0) + menuPopupItem:registerScriptHandler(menuCallbackClosePopup) + menuPopup = CCMenu:menuWithItem(menuPopupItem) + menuPopup:setPosition(winSize.width / 2, winSize.height / 2) + menuPopup:setIsVisible(false) + layerMenu:addChild(menuPopup) + + -- add the left-bottom "tools" menu to invoke menuPopup + local menuToolsItem = CCMenuItemImage:itemFromNormalImage("menu1.png", "menu1.png") + menuToolsItem:setPosition(0, 0) + menuToolsItem:registerScriptHandler(menuCallbackOpenPopup) + menuTools = CCMenu:menuWithItem(menuToolsItem) + menuTools:setPosition(30, 40) + layerMenu:addChild(menuTools) + + return layerMenu +end + +-- play background music, preload effect +SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true); +SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav"); + +-- run +local sceneGame = CCScene:node() +sceneGame:addChild(createLayerFram()) +sceneGame:addChild(createLayerMenu()) +CCDirector:sharedDirector():runWithScene(sceneGame) diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 62e2904f78..4a8755068f 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -74,8 +74,8 @@ public class Cocos2dxBitmap{ // Draw string FontMetricsInt fm = paint.getFontMetricsInt(); int x = 0; - int y = height == 0 ?(-fm.ascent): - (-fm.ascent + (height - textProperty.totalHeight)/2); + int y = height == 0 ?(-fm.top): + (-fm.top + (height - textProperty.totalHeight)/2); String[] lines = textProperty.lines; for (String line : lines){ x = computeX(paint, line, textProperty.maxWidth, alignment); @@ -132,7 +132,7 @@ public class Cocos2dxBitmap{ private static TextProperty computeTextProperty(String content, Paint paint, int maxWidth, int maxHeight){ FontMetricsInt fm = paint.getFontMetricsInt(); - int h = (int)Math.ceil(fm.descent - fm.ascent); + int h = (int)Math.ceil(fm.bottom - fm.top); int maxContentWidth = 0; String[] lines = splitString(content, maxHeight, maxWidth, paint); @@ -165,7 +165,7 @@ public class Cocos2dxBitmap{ String[] lines = content.split("\\n"); String[] ret = null; FontMetricsInt fm = paint.getFontMetricsInt(); - int heightPerLine = (int)Math.ceil(fm.descent - fm.ascent); + int heightPerLine = (int)Math.ceil(fm.bottom - fm.top); int maxLines = maxHeight / heightPerLine; if (maxWidth != 0){ @@ -236,7 +236,7 @@ public class Cocos2dxBitmap{ if (tempWidth >= width){ int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" "); - if (lastIndexOfSpace != -1){ + if (lastIndexOfSpace != -1 && lastIndexOfSpace > start){ /** * Should wrap the word */ diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index 0c199ad60f..0be943dd6d 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -282,9 +282,10 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: - final int idPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; - final float xPointerDown = event.getX(idPointerDown); - final float yPointerDown = event.getY(idPointerDown); + final int indexPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + final int idPointerDown = event.getPointerId(indexPointerDown); + final float xPointerDown = event.getX(indexPointerDown); + final float yPointerDown = event.getY(indexPointerDown); queueEvent(new Runnable() { @Override @@ -318,9 +319,10 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { break; case MotionEvent.ACTION_POINTER_UP: - final int idPointerUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; - final float xPointerUp = event.getX(idPointerUp); - final float yPointerUp = event.getY(idPointerUp); + final int indexPointUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + final int idPointerUp = event.getPointerId(indexPointUp); + final float xPointerUp = event.getX(indexPointUp); + final float yPointerUp = event.getY(indexPointUp); queueEvent(new Runnable() { @Override diff --git a/HelloLua/ios/HelloLua.xcodeproj/project.pbxproj.REMOVED.git-id b/HelloLua/ios/HelloLua.xcodeproj/project.pbxproj.REMOVED.git-id index 769dab970d..4f39f2ee05 100644 --- a/HelloLua/ios/HelloLua.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/HelloLua/ios/HelloLua.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -d1f0ba1c97add6d33ba2628f3a35ad1ed676c4f5 \ No newline at end of file +0a874bd51db1bbbd82d2c0ed77650a0ac0f323f6 \ No newline at end of file diff --git a/HelloWorld/Resource/fonts/arial.ttf.REMOVED.git-id b/HelloWorld/Resource/fonts/arial.ttf.REMOVED.git-id new file mode 100644 index 0000000000..0e6ab72e71 --- /dev/null +++ b/HelloWorld/Resource/fonts/arial.ttf.REMOVED.git-id @@ -0,0 +1 @@ +abc899cd55086105f439df7a6d01db84168aebb5 \ No newline at end of file diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 62e2904f78..4a8755068f 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -74,8 +74,8 @@ public class Cocos2dxBitmap{ // Draw string FontMetricsInt fm = paint.getFontMetricsInt(); int x = 0; - int y = height == 0 ?(-fm.ascent): - (-fm.ascent + (height - textProperty.totalHeight)/2); + int y = height == 0 ?(-fm.top): + (-fm.top + (height - textProperty.totalHeight)/2); String[] lines = textProperty.lines; for (String line : lines){ x = computeX(paint, line, textProperty.maxWidth, alignment); @@ -132,7 +132,7 @@ public class Cocos2dxBitmap{ private static TextProperty computeTextProperty(String content, Paint paint, int maxWidth, int maxHeight){ FontMetricsInt fm = paint.getFontMetricsInt(); - int h = (int)Math.ceil(fm.descent - fm.ascent); + int h = (int)Math.ceil(fm.bottom - fm.top); int maxContentWidth = 0; String[] lines = splitString(content, maxHeight, maxWidth, paint); @@ -165,7 +165,7 @@ public class Cocos2dxBitmap{ String[] lines = content.split("\\n"); String[] ret = null; FontMetricsInt fm = paint.getFontMetricsInt(); - int heightPerLine = (int)Math.ceil(fm.descent - fm.ascent); + int heightPerLine = (int)Math.ceil(fm.bottom - fm.top); int maxLines = maxHeight / heightPerLine; if (maxWidth != 0){ @@ -236,7 +236,7 @@ public class Cocos2dxBitmap{ if (tempWidth >= width){ int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" "); - if (lastIndexOfSpace != -1){ + if (lastIndexOfSpace != -1 && lastIndexOfSpace > start){ /** * Should wrap the word */ diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index 0c199ad60f..0be943dd6d 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -282,9 +282,10 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: - final int idPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; - final float xPointerDown = event.getX(idPointerDown); - final float yPointerDown = event.getY(idPointerDown); + final int indexPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + final int idPointerDown = event.getPointerId(indexPointerDown); + final float xPointerDown = event.getX(indexPointerDown); + final float yPointerDown = event.getY(indexPointerDown); queueEvent(new Runnable() { @Override @@ -318,9 +319,10 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { break; case MotionEvent.ACTION_POINTER_UP: - final int idPointerUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; - final float xPointerUp = event.getX(idPointerUp); - final float yPointerUp = event.getY(idPointerUp); + final int indexPointUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + final int idPointerUp = event.getPointerId(indexPointUp); + final float xPointerUp = event.getX(indexPointUp); + final float yPointerUp = event.getY(indexPointUp); queueEvent(new Runnable() { @Override diff --git a/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id b/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id index f8e381e18b..caa2a23e07 100644 --- a/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/HelloWorld/ios/HelloWorld.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -0bf2e5e584ed953f8de4c1594175403a1d14abd0 \ No newline at end of file +27ae070112e44d08eaf37af409452a684dcb4185 \ No newline at end of file diff --git a/README.mdown b/README.mdown index 2bd046d32e..961cb5ae5d 100644 --- a/README.mdown +++ b/README.mdown @@ -2,7 +2,7 @@ cocos2d-x multiplatform ================== [cocos2d-x][1] is a multi-platform 2D game engine in C++, based on [cocos2d-iphone][2] and licensed under MIT. -Now this engine has been ported to WOPhone, iOS 4.1, Android 2.1 and higher, WindowsXP & Windows7. +Now this engine has been ported to WOPhone, iOS 4.1, Android 2.1 and higher, WindowsXP & Windows7. Multi Platform ------------- @@ -16,14 +16,14 @@ Multi Platform Document ------------------ * Website: [www.cocos2d-x.org][7] - * [Online API References][8] - + * [Online API References][8] + Contact us ------------------ * Forum: [http://forum.cocos2d-x.org][9] * Twitter: [http://www.twitter.com/cocos2dx][10] * Sina mini-blog: [http://t.sina.com.cn/cocos2dx][11] - + [1]: http://www.cocos2d-x.org "cocos2d-x" [2]: http://www.cocos2d-iphone.org "cocos2d for iPhone" [3]: http://www.cocos2d-x.org/wiki/cocos2d-x/Cocos2d-iphone-cpp "cocos2d-iphone-cpp" @@ -35,117 +35,3 @@ Contact us [9]: http://forum.cocos2d-x.org "http://forum.cocos2d-x.org" [10]: http://www.twitter.com/cocos2dx "http://www.twitter.com/cocos2dx" [11]: http://t.sina.com.cn/cocos2dx "http://t.sina.com.cn/cocos2dx" - - - -Lua Support FIX Readme ------------------- - * CCScheduler support local function - * Support autorelease C++ object - * When C++ object deleted, set Lua object userdata to nil - * Add CCNode:setPosition(x, y), CCNode::getPosition(), huge performance boost - * Remove needless class and functions from tolua++ .pkg files, improved performance - * CCMenuItem support script function - * CCScene script callback - * CCLayer touch & multi-touches events handler - -**How to use:** - - -- use CCSchedule::scheduleScriptFunc() - - local scheduler = CCScheduler:sharedScheduler() - - local handle -- save script callback handle - - local frameCount = 0 - local function onEnterFrame(dt) - print("onEnterFrame:", frameCount, dt) - frameCount = frameCount + 1 - if frameCount >= 60 then - -- remove script callback - scheduler:unscheduleScriptFunc(handle) - end - end - - handle = scheduler:scheduleScriptFunc(onEnterFrame, 1.0 / 60, false) - -- - - - - -- use CCMenuItem:registerScriptHandler() - - local function onMenuItemTap(menuItemTag) - print(menuItemTag) -- output "1" - end - - local menuItem = CCMenuItemImage:itemFromNormalImage("button.png", "button.png") - menuItem:registerScriptHandler(onMenuItemTap) - - local menu = CCMenu:node() - menu:addChild(menuItem) - menuItem:setTag(1) -- menuItemTag - - scene:addChild(menu) - -- - - - - -- use CCScene script callback - - local function newScene() - local scene = CCScene:node() - - local function sceneEventHandler(isOnEnter) - if isOnEnter then - if scene.onEnter then scene:onEnter() end - else - if scene.onExit then scene:onExit() end - scene:unregisterScriptEventsHandler() - end - end - - scene:registerScriptEventsHandler(sceneEventHandler) - return scene - end - - local myScene = newScene("MyScene") - function myScene:onEnter() - print("MyScene:onEnter()") - end - - function myScene:onExit() - print("MyScene:onExit()") - end - - CCDirector:sharedDirector():runWithScene(myScene) - -- - - - - -- use CCLayer touch event handler - - local layer = CCLayer:node() - local function onTouch(event, x, y) - -- event: CCTOUCHBEGAN, CCTOUCHMOVED, CCTOUCHENDED, CCTOUCHCANCELLED - print(event, x, y) - if event == CCTOUCHBEGAN then return true end - end - - layer:registerScriptTouchHandler(onTouch, false) - -- - - - - -- use CCLayer multi-touches event handler - - local layer = CCLayer:node() - local function onTouches(event, points) - -- event: CCTOUCHBEGAN, CCTOUCHMOVED, CCTOUCHENDED, CCTOUCHCANCELLED - print(event) - - for i = 1, #points, 2 do - print(string.format("x: 0.2f, y: 0.2f", points[i], points[i + 1])) - end - end - - layer:registerScriptTouchHandler(onTouches, true) - -**TODO:** - - * When C++ object deleted, remove Lua object userdata diff --git a/template/msvc/CCAppWiz.win32/Templates/1033/Classes/AppDelegate.cpp b/template/msvc/CCAppWiz.win32/Templates/1033/Classes/AppDelegate.cpp index 1f01de9ef0..4db06999fa 100644 --- a/template/msvc/CCAppWiz.win32/Templates/1033/Classes/AppDelegate.cpp +++ b/template/msvc/CCAppWiz.win32/Templates/1033/Classes/AppDelegate.cpp @@ -9,6 +9,12 @@ using namespace CocosDenshion; [! if !CC_USE_LUA] #include "HelloWorldScene.h" +[! else] + +#if CC_LUA_ENGINE_ENABLED +#include "CCLuaEngine.h" +#endif + [! endif] #include "CCEGLView.h" @@ -16,11 +22,7 @@ using namespace CocosDenshion; USING_NS_CC; AppDelegate::AppDelegate() -[! if CC_USE_LUA] -:m_pLuaEngine(NULL) -[! endif] { - } AppDelegate::~AppDelegate() @@ -29,16 +31,16 @@ AppDelegate::~AppDelegate() SimpleAudioEngine::end(); [! endif] [! if CC_USE_LUA] - - CCScriptEngineManager::sharedScriptEngineManager()->removeScriptEngine(); - CC_SAFE_DELETE(m_pLuaEngine); +#if CC_LUA_ENGINE_ENABLED + CCLuaEngine::purgeSharedEngine(); +#endif [! endif] } bool AppDelegate::initInstance() { bool bRet = false; - do + do { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) @@ -55,7 +57,7 @@ bool AppDelegate::initInstance() // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. #endif // CC_PLATFORM_IOS - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp @@ -74,7 +76,7 @@ bool AppDelegate::initInstance() CCEGLView* pMainWnd = new CCEGLView(this); CC_BREAK_IF(! pMainWnd || ! pMainWnd->Create(320,480, WM_WINDOW_ROTATE_MODE_CW)); -#ifndef _TRANZDA_VM_ +#ifndef _TRANZDA_VM_ // on wophone emulator, we copy resources files to Work7/NEWPLUS/TDA_DATA/Data/ folder instead of zip file cocos2d::CCFileUtils::setResource("HelloWorld.zip"); #endif @@ -131,9 +133,8 @@ bool AppDelegate::applicationDidFinishLaunching() pDirector->setAnimationInterval(1.0 / 60); [! if CC_USE_LUA] - // register lua engine - m_pLuaEngine = new LuaEngine; - CCScriptEngineManager::sharedScriptEngineManager()->setScriptEngine(m_pLuaEngine); + // init lua engine + CCLuaEngine* pEngine = CCLuaEngine::sharedEngine(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) unsigned long size; @@ -147,15 +148,15 @@ bool AppDelegate::applicationDidFinishLaunching() memcpy(pCodes, pFileContent, size); delete[] pFileContent; - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeString(pCodes); + pEngine->executeString(pCodes); delete []pCodes; } #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile(path.c_str()); -#endif + pEngine->executeScriptFile(path.c_str()); +#endif [! else] // create a scene. it's an autorelease object CCScene *pScene = HelloWorld::scene(); diff --git a/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index 6755749f14..d077fbb0d6 100644 --- a/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -84d1a5cd21bd26c8e2f08241d504b59fe40defc9 \ No newline at end of file +60686646874bc76241984a6df8beeaa3673f934e \ No newline at end of file diff --git a/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index 6a0d4f8640..7a983c0df8 100644 --- a/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_box2d_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -1a854b530b8894d2453fe8777d2d83b8bc68ab91 \ No newline at end of file +b8d0b27908d823a2687767793e04c64fe1541def \ No newline at end of file diff --git a/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index 6772dc8a1f..2e99e2b364 100644 --- a/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_chipmunk_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -866c5fd880bbb121951818d47d13a4ee914279ad \ No newline at end of file +4df124f68856ca5915635b8392bb8aa85c79259f \ No newline at end of file diff --git a/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.cpp b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.cpp index f1c6afbc33..b0d16d518b 100644 --- a/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.cpp +++ b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.cpp @@ -1,24 +1,27 @@ -#include "AppDelegate.h" +#include "AppDelegate.h" #include "cocos2d.h" +#include "SimpleAudioEngine.h" +#include "CCLuaEngine.h" USING_NS_CC; +using namespace CocosDenshion; AppDelegate::AppDelegate() -:m_pLuaEngine(NULL) { } AppDelegate::~AppDelegate() { - CCScriptEngineManager::sharedScriptEngineManager()->removeScriptEngine(); - CC_SAFE_DELETE(m_pLuaEngine); + // end simple audio engine here, or it may crashed on win32 + SimpleAudioEngine::sharedEngine()->end(); + CCLuaEngine::purgeSharedEngine(); } bool AppDelegate::initInstance() { bool bRet = false; - do + do { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) @@ -29,13 +32,13 @@ bool AppDelegate::initInstance() || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320)); #endif // CC_PLATFORM_WIN32 - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. #endif // CC_PLATFORM_IOS - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp @@ -51,7 +54,7 @@ bool AppDelegate::initInstance() CCEGLView* pMainWnd = new CCEGLView(this); CC_BREAK_IF(! pMainWnd || ! pMainWnd->Create(320,480, WM_WINDOW_ROTATE_MODE_CW)); -#ifndef _TRANZDA_VM_ +#ifndef _TRANZDA_VM_ // on wophone emulator, we copy resources files to Work7/NEWPLUS/TDA_DATA/Data/ folder instead of zip file cocos2d::CCFileUtils::setResource("HelloWorld.zip"); #endif @@ -84,9 +87,8 @@ bool AppDelegate::applicationDidFinishLaunching() // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); - // register lua engine - m_pLuaEngine = new LuaEngine; - CCScriptEngineManager::sharedScriptEngineManager()->setScriptEngine(m_pLuaEngine); + // init lua engine + CCLuaEngine* pEngine = CCLuaEngine::sharedEngine(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) unsigned long size; @@ -100,42 +102,16 @@ bool AppDelegate::applicationDidFinishLaunching() memcpy(pCodes, pFileContent, size); delete[] pFileContent; - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeString(pCodes); + pEngine->executeString(pCodes); delete []pCodes; } #endif -#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) - // CCLuaScriptModule::sharedLuaScriptModule()->executeScriptFile("./../../HelloLua/Resource/hello.lua"); - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile("./../../HelloLua/Resource/hello.lua"); - - /* - * Another way to run lua script. - * Load the file into memory and run it. - * - unsigned long size; - char *pFileContent = (char*)CCFileUtils::getFileData("./../../HelloLua/Resource/hello.lua", "r", &size); - if (pFileContent) - { - // copy the file contents and add '\0' at the end, or the lua parser can not parse it - char *pTmp = new char[size + 1]; - pTmp[size] = '\0'; - memcpy(pTmp, pFileContent, size); - delete[] pFileContent; - - string code(pTmp); - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->excuteScriptFile(code); - delete []pTmp; - } - */ - -#endif - -#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile(path.c_str()); -#endif + pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); + pEngine->executeScriptFile(path.c_str()); +#endif return true; } @@ -153,7 +129,7 @@ void AppDelegate::applicationDidEnterBackground() void AppDelegate::applicationWillEnterForeground() { CCDirector::sharedDirector()->resume(); - + // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); } diff --git a/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.h b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.h index bb948380cb..6a7b537389 100644 --- a/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.h +++ b/template/xcode3/cocos2d-x_lua_app/Classes/AppDelegate.h @@ -2,7 +2,6 @@ #define _APP_DELEGATE_H_ #include "CCApplication.h" -#include "LuaEngine.h" /** @brief The cocos2d Application. @@ -38,9 +37,6 @@ public: @param the pointer of the application */ virtual void applicationWillEnterForeground(); - -private: - LuaEngine* m_pLuaEngine; }; #endif // _APP_DELEGATE_H_ diff --git a/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id b/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id index 9b2da8e892..25298a6bb6 100644 --- a/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/template/xcode3/cocos2d-x_lua_app/___PROJECTNAME___.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -6b542917dfd2180ff6c55bca6cd7b30c64c4a300 \ No newline at end of file +23cd0723f3707e7fc8c77d718bf3fc0b81b105f6 \ No newline at end of file diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp index dead3fc8a3..e64f5e2c14 100644 --- a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp +++ b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.cpp @@ -1,28 +1,27 @@ -#include "AppDelegate.h" +#include "AppDelegate.h" #include "cocos2d.h" #include "SimpleAudioEngine.h" +#include "CCLuaEngine.h" USING_NS_CC; using namespace CocosDenshion; AppDelegate::AppDelegate() -:m_pLuaEngine(NULL) { } AppDelegate::~AppDelegate() { - // end simple audio engine here, or it may crashed on win32 - SimpleAudioEngine::sharedEngine()->end(); - CCScriptEngineManager::sharedScriptEngineManager()->removeScriptEngine(); - CC_SAFE_DELETE(m_pLuaEngine); + // end simple audio engine here, or it may crashed on win32 + SimpleAudioEngine::sharedEngine()->end(); + CCLuaEngine::purgeSharedEngine(); } bool AppDelegate::initInstance() { bool bRet = false; - do + do { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) @@ -33,13 +32,13 @@ bool AppDelegate::initInstance() || ! pMainWnd->Create(TEXT("cocos2d: Hello World"), 480, 320)); #endif // CC_PLATFORM_WIN32 - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) // OpenGLView initialized in testsAppDelegate.mm on ios platform, nothing need to do here. #endif // CC_PLATFORM_IOS - + #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp @@ -55,7 +54,7 @@ bool AppDelegate::initInstance() CCEGLView* pMainWnd = new CCEGLView(this); CC_BREAK_IF(! pMainWnd || ! pMainWnd->Create(320,480, WM_WINDOW_ROTATE_MODE_CW)); -#ifndef _TRANZDA_VM_ +#ifndef _TRANZDA_VM_ // on wophone emulator, we copy resources files to Work7/NEWPLUS/TDA_DATA/Data/ folder instead of zip file cocos2d::CCFileUtils::setResource("HelloWorld.zip"); #endif @@ -88,32 +87,31 @@ bool AppDelegate::applicationDidFinishLaunching() // set FPS. the default value is 1.0/60 if you don't call this pDirector->setAnimationInterval(1.0 / 60); - // register lua engine - m_pLuaEngine = new LuaEngine; - CCScriptEngineManager::sharedScriptEngineManager()->setScriptEngine(m_pLuaEngine); + // init lua engine + CCLuaEngine* pEngine = CCLuaEngine::sharedEngine(); #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) - unsigned long size; - char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size); + unsigned long size; + char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size); - if (pFileContent) - { - // copy the file contents and add '\0' at the end, or the lua parser can not parse it - char *pCodes = new char[size + 1]; - pCodes[size] = '\0'; - memcpy(pCodes, pFileContent, size); - delete[] pFileContent; + if (pFileContent) + { + // copy the file contents and add '\0' at the end, or the lua parser can not parse it + char *pCodes = new char[size + 1]; + pCodes[size] = '\0'; + memcpy(pCodes, pFileContent, size); + delete[] pFileContent; - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeString(pCodes); - delete []pCodes; - } + pEngine->executeString(pCodes); + delete []pCodes; + } #endif #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) - string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); - CCScriptEngineManager::sharedScriptEngineManager()->getScriptEngine()->executeScriptFile(path.c_str()); -#endif + string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); + pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); + pEngine->executeScriptFile(path.c_str()); +#endif return true; } @@ -131,7 +129,7 @@ void AppDelegate::applicationDidEnterBackground() void AppDelegate::applicationWillEnterForeground() { CCDirector::sharedDirector()->resume(); - + // if you use SimpleAudioEngine, it must resume here // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic(); } diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h index bb948380cb..6a7b537389 100644 --- a/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h +++ b/template/xcode4/cocos2dx_lua.xctemplate/Classes/AppDelegate.h @@ -2,7 +2,6 @@ #define _APP_DELEGATE_H_ #include "CCApplication.h" -#include "LuaEngine.h" /** @brief The cocos2d Application. @@ -38,9 +37,6 @@ public: @param the pointer of the application */ virtual void applicationWillEnterForeground(); - -private: - LuaEngine* m_pLuaEngine; }; #endif // _APP_DELEGATE_H_ diff --git a/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua b/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua index a3c1b38fc6..2c48dafa90 100644 --- a/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua +++ b/template/xcode4/cocos2dx_lua.xctemplate/Resources/hello.lua @@ -1,164 +1,180 @@ -require "hello2" - -cocos2d.CCLuaLog("result is " .. myadd(3, 5)) - --- create scene & layer -layerFarm = cocos2d.CCLayer:node() -layerFarm:setIsTouchEnabled(true) - -layerMenu = cocos2d.CCLayer:node() - -sceneGame = cocos2d.CCScene:node() -sceneGame:addChild(layerFarm) -sceneGame:addChild(layerMenu) - -winSize = cocos2d.CCDirector:sharedDirector():getWinSize() - --- add in farm background -spriteFarm = cocos2d.CCSprite:spriteWithFile("farm.jpg") -spriteFarm:setPosition(cocos2d.CCPoint(winSize.width/2 + 80, winSize.height/2)) -layerFarm:addChild(spriteFarm) - --- touch handers -pointBegin = nil - -function btnTouchMove(e) - cocos2d.CCLuaLog("btnTouchMove") - if pointBegin ~= nil then - local v = e[1] - local pointMove = v:locationInView(v:view()) - pointMove = cocos2d.CCDirector:sharedDirector():convertToGL(pointMove) - local positionCurrent = layerFarm:getPosition() - layerFarm:setPosition(cocos2d.CCPoint(positionCurrent.x + pointMove.x - pointBegin.x, positionCurrent.y + pointMove.y - pointBegin.y)) - pointBegin = pointMove - end -end - -function btnTouchBegin(e) - cocos2d.CCScheduler:sharedScheduler():unscheduleScriptFunc("tick") - cocos2d.CCLuaLog("btnTouchBegin") - for k,v in ipairs(e) do - pointBegin = v:locationInView(v:view()) - pointBegin = cocos2d.CCDirector:sharedDirector():convertToGL(pointBegin) - end -end - -function btnTouchEnd(e) - cocos2d.CCLuaLog("btnTouchEnd") - touchStart = nil -end - --- regiester touch handlers -layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHBEGAN, "btnTouchBegin") -layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHMOVED, "btnTouchMove") -layerFarm.__CCTouchDelegate__:registerScriptTouchHandler(cocos2d.CCTOUCHENDED, "btnTouchEnd") - - --- add land sprite -for i=0,3,1 do - for j=0,1,1 do - spriteLand = cocos2d.CCSprite:spriteWithFile("land.png") - layerFarm:addChild(spriteLand) - spriteLand:setPosition(cocos2d.CCPoint(200+j*180 - i%2*90, 10+i*95/2)) - end -end - --- add crop - -for i=0,3,1 do - for j=0,1,1 do - - textureCrop = cocos2d.CCTextureCache:sharedTextureCache():addImage("crop.png") - frameCrop = cocos2d.CCSpriteFrame:frameWithTexture(textureCrop, cocos2d.CCRectMake(0, 0, 105, 95)) - spriteCrop = cocos2d.CCSprite:spriteWithSpriteFrame(frameCrop); - - layerFarm:addChild(spriteCrop) - - spriteCrop:setPosition(cocos2d.CCPoint(10+200+j*180 - i%2*90, 30+10+i*95/2)) - - end -end - --- add the moving dog - -FrameWidth = 105 -FrameHeight = 95 - -textureDog = cocos2d.CCTextureCache:sharedTextureCache():addImage("dog.png") -frame0 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(0, 0, FrameWidth, FrameHeight)) -frame1 = cocos2d.CCSpriteFrame:frameWithTexture(textureDog, cocos2d.CCRectMake(FrameWidth*1, 0, FrameWidth, FrameHeight)) - -spriteDog = cocos2d.CCSprite:spriteWithSpriteFrame(frame0) -spriteDog:setPosition(cocos2d.CCPoint(0, winSize.height/4*3)) -layerFarm:addChild(spriteDog) - -animFrames = cocos2d.CCMutableArray_CCSpriteFrame__:new(2) -animFrames:addObject(frame0) -animFrames:addObject(frame1) - -animation = cocos2d.CCAnimation:animationWithFrames(animFrames, 0.5) - -animate = cocos2d.CCAnimate:actionWithAnimation(animation, false); -spriteDog:runAction(cocos2d.CCRepeatForever:actionWithAction(animate)) - - --- add a popup menu - -function menuCallbackClosePopup() --- stop test sound effect -CocosDenshion.SimpleAudioEngine:sharedEngine():stopEffect(effectID) -menuPopup:setIsVisible(false) -end - -menuPopupItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") -menuPopupItem:setPosition( cocos2d.CCPoint(0, 0) ) -menuPopupItem:registerScriptHandler("menuCallbackClosePopup") -menuPopup = cocos2d.CCMenu:menuWithItem(menuPopupItem) -menuPopup:setPosition( cocos2d.CCPoint(winSize.width/2, winSize.height/2) ) -menuPopup:setIsVisible(false) -layerMenu:addChild(menuPopup) - --- add the left-bottom "tools" menu to invoke menuPopup - -function menuCallbackOpenPopup() --- loop test sound effect --- NOTE: effectID is global, so it can be used to stop -effectID = CocosDenshion.SimpleAudioEngine:sharedEngine():playEffect("effect1.wav") -menuPopup:setIsVisible(true) -end - -menuToolsItem = cocos2d.CCMenuItemImage:itemFromNormalImage("menu1.png","menu1.png") -menuToolsItem:setPosition( cocos2d.CCPoint(0, 0) ) -menuToolsItem:registerScriptHandler("menuCallbackOpenPopup") -menuTools = cocos2d.CCMenu:menuWithItem(menuToolsItem) -menuTools:setPosition( cocos2d.CCPoint(30, 40) ) -layerMenu:addChild(menuTools) - - -function tick() - - local point = spriteDog:getPosition(); - - if point.x > winSize.width then - point.x = 0 - spriteDog:setPosition(point) - else - point.x = point.x + 1 - spriteDog:setPosition(point) - end - -end -- avoid memory leak -collectgarbage( "setpause", 100) -collectgarbage( "setstepmul", 5000) +collectgarbage("setpause", 100) +collectgarbage("setstepmul", 5000) + +local cclog = function(...) + print(string.format(...)) +end + +require "hello2" +cclog("result is " .. myadd(3, 5)) + +--------------- + +local winSize = CCDirector:sharedDirector():getWinSize() + +-- add the moving dog +local function creatDog() + local frameWidth = 105 + local frameHeight = 95 + + -- create dog animate + local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png") + local rect = CCRectMake(0, 0, frameWidth, frameHeight) + local frame0 = CCSpriteFrame:frameWithTexture(textureDog, rect) + rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight) + local frame1 = CCSpriteFrame:frameWithTexture(textureDog, rect) + + local spriteDog = CCSprite:spriteWithSpriteFrame(frame0) + spriteDog.isPaused = false + spriteDog:setPosition(0, winSize.height / 4 * 3) + + local animFrames = CCMutableArray_CCSpriteFrame__:new(2) + animFrames:addObject(frame0) + animFrames:addObject(frame1) + + local animation = CCAnimation:animationWithFrames(animFrames, 0.5) + local animate = CCAnimate:actionWithAnimation(animation, false); + spriteDog:runAction(CCRepeatForever:actionWithAction(animate)) + + -- moving dog at every frame + local function tick() + if spriteDog.isPaused then return end + local x, y = spriteDog:getPosition() + if x > winSize.width then + x = 0 + else + x = x + 1 + end + spriteDog:setPositionX(x) + end + + CCScheduler:sharedScheduler():scheduleScriptFunc(tick, 0, false) + + return spriteDog +end + +-- create farm +local function createLayerFram() + local layerFarm = CCLayer:node() + + -- add in farm background + local bg = CCSprite:spriteWithFile("farm.jpg") + bg:setPosition(winSize.width / 2 + 80, winSize.height / 2) + layerFarm:addChild(bg) + + -- add land sprite + for i = 0, 3 do + for j = 0, 1 do + local spriteLand = CCSprite:spriteWithFile("land.png") + spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2) + layerFarm:addChild(spriteLand) + end + end + + -- add crop + local textureCrop = CCTextureCache:sharedTextureCache():addImage("crop.png") + local frameCrop = CCSpriteFrame:frameWithTexture(textureCrop, CCRectMake(0, 0, 105, 95)) + for i = 0, 3 do + for j = 0, 1 do + local spriteCrop = CCSprite:spriteWithSpriteFrame(frameCrop); + spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2) + layerFarm:addChild(spriteCrop) + end + end + + -- add moving dog + local spriteDog = creatDog() + layerFarm:addChild(spriteDog) + + -- handing touch events + local touchBeginPoint = nil + + local function onTouchBegan(x, y) + cclog("onTouchBegan: %0.2f, %0.2f", x, y) + touchBeginPoint = {x = x, y = y} + spriteDog.isPaused = true + -- CCTOUCHBEGAN event must return true + return true + end + + local function onTouchMoved(x, y) + cclog("onTouchMoved: %0.2f, %0.2f", x, y) + if touchBeginPoint then + local cx, cy = layerFarm:getPosition() + layerFarm:setPosition(cx + x - touchBeginPoint.x, + cy + y - touchBeginPoint.y) + touchBeginPoint = {x = x, y = y} + end + end + + local function onTouchEnded(x, y) + cclog("onTouchEnded") + touchBeginPoint = nil + spriteDog.isPaused = false + end + + local function onTouch(eventType, x, y) + if eventType == CCTOUCHBEGAN then + return onTouchBegan(x, y) + elseif eventType == CCTOUCHMOVED then + return onTouchMoved(x, y) + else + return onTouchEnded(x, y) + end + end + + layerFarm:registerScriptTouchHandler(onTouch) + + return layerFarm +end -cocos2d.CCScheduler:sharedScheduler():scheduleScriptFunc("tick", 0.01, false) +-- create menu +local function createLayerMenu() + local layerMenu = CCLayer:node() --- play background music -CocosDenshion.SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true); --- preload effect -CocosDenshion.SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav"); --- run -cocos2d.CCDirector:sharedDirector():runWithScene(sceneGame) + local menuPopup, menuTools, effectID + + local function menuCallbackClosePopup() + -- stop test sound effect + SimpleAudioEngine:sharedEngine():stopEffect(effectID) + menuPopup:setIsVisible(false) + end + + local function menuCallbackOpenPopup() + -- loop test sound effect + effectID = SimpleAudioEngine:sharedEngine():playEffect("effect1.wav") + menuPopup:setIsVisible(true) + end + + -- add a popup menu + local menuPopupItem = CCMenuItemImage:itemFromNormalImage("menu2.png", "menu2.png") + menuPopupItem:setPosition(0, 0) + menuPopupItem:registerScriptHandler(menuCallbackClosePopup) + menuPopup = CCMenu:menuWithItem(menuPopupItem) + menuPopup:setPosition(winSize.width / 2, winSize.height / 2) + menuPopup:setIsVisible(false) + layerMenu:addChild(menuPopup) + + -- add the left-bottom "tools" menu to invoke menuPopup + local menuToolsItem = CCMenuItemImage:itemFromNormalImage("menu1.png", "menu1.png") + menuToolsItem:setPosition(0, 0) + menuToolsItem:registerScriptHandler(menuCallbackOpenPopup) + menuTools = CCMenu:menuWithItem(menuToolsItem) + menuTools:setPosition(30, 40) + layerMenu:addChild(menuTools) + + return layerMenu +end + +-- play background music, preload effect +SimpleAudioEngine:sharedEngine():playBackgroundMusic("background.mp3", true); +SimpleAudioEngine:sharedEngine():preloadEffect("effect1.wav"); + +-- run +local sceneGame = CCScene:node() +sceneGame:addChild(createLayerFram()) +sceneGame:addChild(createLayerMenu()) +CCDirector:sharedDirector():runWithScene(sceneGame) diff --git a/tests/Res/fonts/Courier New.ttf.REMOVED.git-id b/tests/Res/fonts/Courier New.ttf.REMOVED.git-id new file mode 100644 index 0000000000..6354ea7a26 --- /dev/null +++ b/tests/Res/fonts/Courier New.ttf.REMOVED.git-id @@ -0,0 +1 @@ +ebb3361a7a53d63ab60931a48bedc3ec8ccfe206 \ No newline at end of file diff --git a/tests/Res/fonts/Thonburi.ttf.REMOVED.git-id b/tests/Res/fonts/Thonburi.ttf.REMOVED.git-id new file mode 100644 index 0000000000..58b5744ea7 --- /dev/null +++ b/tests/Res/fonts/Thonburi.ttf.REMOVED.git-id @@ -0,0 +1 @@ +ab579da81872bb45c6e102dc2dbf6ee9e8082f75 \ No newline at end of file diff --git a/tests/Res/fonts/ThonburiBold.ttf.REMOVED.git-id b/tests/Res/fonts/ThonburiBold.ttf.REMOVED.git-id new file mode 100644 index 0000000000..db62b49902 --- /dev/null +++ b/tests/Res/fonts/ThonburiBold.ttf.REMOVED.git-id @@ -0,0 +1 @@ +cce195a8ee10fc4fd7b5acf048ab3e04b0750685 \ No newline at end of file diff --git a/tests/Res/fonts/arial.ttf.REMOVED.git-id b/tests/Res/fonts/arial.ttf.REMOVED.git-id new file mode 100644 index 0000000000..0e6ab72e71 --- /dev/null +++ b/tests/Res/fonts/arial.ttf.REMOVED.git-id @@ -0,0 +1 @@ +abc899cd55086105f439df7a6d01db84168aebb5 \ No newline at end of file diff --git a/tests/Res/fonts/tahoma.ttf.REMOVED.git-id b/tests/Res/fonts/tahoma.ttf.REMOVED.git-id new file mode 100644 index 0000000000..93ceec1614 --- /dev/null +++ b/tests/Res/fonts/tahoma.ttf.REMOVED.git-id @@ -0,0 +1 @@ +3f8554bba4214706aafc933d057de89351197eb4 \ No newline at end of file diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java index 62e2904f78..4a8755068f 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxBitmap.java @@ -74,8 +74,8 @@ public class Cocos2dxBitmap{ // Draw string FontMetricsInt fm = paint.getFontMetricsInt(); int x = 0; - int y = height == 0 ?(-fm.ascent): - (-fm.ascent + (height - textProperty.totalHeight)/2); + int y = height == 0 ?(-fm.top): + (-fm.top + (height - textProperty.totalHeight)/2); String[] lines = textProperty.lines; for (String line : lines){ x = computeX(paint, line, textProperty.maxWidth, alignment); @@ -132,7 +132,7 @@ public class Cocos2dxBitmap{ private static TextProperty computeTextProperty(String content, Paint paint, int maxWidth, int maxHeight){ FontMetricsInt fm = paint.getFontMetricsInt(); - int h = (int)Math.ceil(fm.descent - fm.ascent); + int h = (int)Math.ceil(fm.bottom - fm.top); int maxContentWidth = 0; String[] lines = splitString(content, maxHeight, maxWidth, paint); @@ -165,7 +165,7 @@ public class Cocos2dxBitmap{ String[] lines = content.split("\\n"); String[] ret = null; FontMetricsInt fm = paint.getFontMetricsInt(); - int heightPerLine = (int)Math.ceil(fm.descent - fm.ascent); + int heightPerLine = (int)Math.ceil(fm.bottom - fm.top); int maxLines = maxHeight / heightPerLine; if (maxWidth != 0){ @@ -236,7 +236,7 @@ public class Cocos2dxBitmap{ if (tempWidth >= width){ int lastIndexOfSpace = content.substring(0, i).lastIndexOf(" "); - if (lastIndexOfSpace != -1){ + if (lastIndexOfSpace != -1 && lastIndexOfSpace > start){ /** * Should wrap the word */ diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index 0c199ad60f..0be943dd6d 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -282,9 +282,10 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_POINTER_DOWN: - final int idPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; - final float xPointerDown = event.getX(idPointerDown); - final float yPointerDown = event.getY(idPointerDown); + final int indexPointerDown = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + final int idPointerDown = event.getPointerId(indexPointerDown); + final float xPointerDown = event.getX(indexPointerDown); + final float yPointerDown = event.getY(indexPointerDown); queueEvent(new Runnable() { @Override @@ -318,9 +319,10 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { break; case MotionEvent.ACTION_POINTER_UP: - final int idPointerUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; - final float xPointerUp = event.getX(idPointerUp); - final float yPointerUp = event.getY(idPointerUp); + final int indexPointUp = event.getAction() >> MotionEvent.ACTION_POINTER_ID_SHIFT; + final int idPointerUp = event.getPointerId(indexPointUp); + final float xPointerUp = event.getX(indexPointUp); + final float yPointerUp = event.getY(indexPointUp); queueEvent(new Runnable() { @Override diff --git a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id index d5959d4c16..9a953f2f58 100644 --- a/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/tests/test.ios/test.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -da69ff183d2f96f95b8b1f8aec196130ba76a193 \ No newline at end of file +6f3595799e6b2dd55a7666a9a191c04093559b7f \ No newline at end of file diff --git a/tests/test.qnx/.cproject b/tests/test.qnx/.cproject index 9fa68fbc3d..b09b9be9b3 100644 --- a/tests/test.qnx/.cproject +++ b/tests/test.qnx/.cproject @@ -53,14 +53,13 @@ - - + - + + + diff --git a/tests/tests/CocosDenshionTest/CocosDenshionTest.cpp b/tests/tests/CocosDenshionTest/CocosDenshionTest.cpp index 79bb0bf939..6d82ad6c4f 100644 --- a/tests/tests/CocosDenshionTest/CocosDenshionTest.cpp +++ b/tests/tests/CocosDenshionTest/CocosDenshionTest.cpp @@ -13,6 +13,8 @@ #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) #define MUSIC_FILE "music.mid" +#elif (CC_TARGET_PLATFORM == CC_PLATFORM_QNX) + #define MUSIC_FILE "background.ogg" #else #define MUSIC_FILE "background.mp3" #endif // CC_PLATFORM_WIN32 diff --git a/tests/tests/UserDefaultTest/UserDefaultTest.cpp b/tests/tests/UserDefaultTest/UserDefaultTest.cpp index ca5d966d0d..4d6583b421 100644 --- a/tests/tests/UserDefaultTest/UserDefaultTest.cpp +++ b/tests/tests/UserDefaultTest/UserDefaultTest.cpp @@ -50,6 +50,8 @@ void UserDefaultTest::doTest() { CCLOG("bool is false"); } + + //CCUserDefault::sharedUserDefault()->flush(); CCLOG("********************** after change value ***********************"); @@ -61,6 +63,8 @@ void UserDefaultTest::doTest() CCUserDefault::sharedUserDefault()->setDoubleForKey("double", 2.6); CCUserDefault::sharedUserDefault()->setBoolForKey("bool", false); + CCUserDefault::sharedUserDefault()->flush(); + // print value ret = CCUserDefault::sharedUserDefault()->getStringForKey("string");