Merge pull request #892 from dumganhar/iss1220_getstring

fixed #1220: Using CCString::stringWithContentsOfFile to get string from lua script files.
This commit is contained in:
James Chen 2012-05-03 23:35:32 -07:00
commit dcf7c9e053
9 changed files with 55 additions and 116 deletions

View File

@ -1,23 +1,9 @@
#include "cocos2d.h" #include "cocos2d.h"
#include "AppDelegate.h" #include "AppDelegate.h"
#include "SimpleAudioEngine.h" #include "SimpleAudioEngine.h"
#include "CCScriptSupport.h" #include "CCScriptSupport.h"
#include "CCLuaEngine.h" #include "CCLuaEngine.h"
#define IPAD 0
#if IPAD
#define CC_WIDTH 1024
#define CC_HEIGHT 768
#elif IPHONE_4
#define CC_WIDTH 960
#define CC_HEIGHT 640
#else
#define CC_WIDTH 480
#define CC_HEIGHT 320
#endif
USING_NS_CC; USING_NS_CC;
using namespace CocosDenshion; using namespace CocosDenshion;
@ -55,24 +41,13 @@ bool AppDelegate::applicationDidFinishLaunching()
CCScriptEngineProtocol* pEngine = CCLuaEngine::engine(); CCScriptEngineProtocol* pEngine = CCLuaEngine::engine();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
unsigned long size; CCString* pstrFileContent = CCString::stringWithContentsOfFile("hello.lua");
char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size); if (pstrFileContent)
{
if (pFileContent) pEngine->executeString(pstrFileContent->getCString());
{ }
// copy the file contents and add '\0' at the end, or the lua parser can not parse it #else
char *pCodes = new char[size + 1];
pCodes[size] = '\0';
memcpy(pCodes, pFileContent, size);
delete[] pFileContent;
pEngine->executeString(pCodes);
delete []pCodes;
}
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
std::string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); std::string path = CCFileUtils::fullPathFromRelativePath("hello.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
pEngine->executeScriptFile(path.c_str()); pEngine->executeScriptFile(path.c_str());
@ -85,16 +60,12 @@ bool AppDelegate::applicationDidFinishLaunching()
void AppDelegate::applicationDidEnterBackground() void AppDelegate::applicationDidEnterBackground()
{ {
CCDirector::sharedDirector()->pause(); CCDirector::sharedDirector()->pause();
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
} }
// this function will be called when the app is active again // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() void AppDelegate::applicationWillEnterForeground()
{ {
CCDirector::sharedDirector()->resume(); CCDirector::sharedDirector()->resume();
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
} }

View File

@ -1,6 +1,6 @@
# set params # set params
NDK_ROOT_LOCAL=/cygdrive/d/programe/android/ndk/android-ndk-r7b NDK_ROOT_LOCAL=/cygdrive/e/android/android-ndk-r8
COCOS2DX_ROOT_LOCAL=/cygdrive/e/cocos2d-x COCOS2DX_ROOT_LOCAL=/cygdrive/f/Project/dumganhar/cocos2d-x
buildexternalsfromsource= buildexternalsfromsource=

View File

@ -1,2 +0,0 @@
# Project target.
target=android-10

View File

@ -1,26 +1,26 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY. /* AUTO-GENERATED FILE. DO NOT MODIFY.
* *
* This class was automatically generated by the * This class was automatically generated by the
* aapt tool from the resource data it found. It * aapt tool from the resource data it found. It
* should not be modified by hand. * should not be modified by hand.
*/ */
package org.cocos2dx.hellolua; package org.cocos2dx.hellolua;
public final class R { public final class R {
public static final class attr { public static final class attr {
} }
public static final class drawable { public static final class drawable {
public static final int icon=0x7f020000; public static final int icon=0x7f020000;
} }
public static final class id { public static final class id {
public static final int test_demo_gl_surfaceview=0x7f050001; public static final int test_demo_gl_surfaceview=0x7f050001;
public static final int textField=0x7f050000; public static final int textField=0x7f050000;
} }
public static final class layout { public static final class layout {
public static final int game_demo=0x7f030000; public static final int game_demo=0x7f030000;
} }
public static final class string { public static final class string {
public static final int app_name=0x7f040000; public static final int app_name=0x7f040000;
} }
} }

View File

@ -34,6 +34,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
} }
else else
{ {
ccDrawInit();
ccGLInvalidateStateCache();
CCShaderCache::sharedShaderCache()->reloadDefaultShaders(); CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
CCTextureCache::reloadAllTextures(); CCTextureCache::reloadAllTextures();
CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);

View File

@ -8,4 +8,4 @@
# project structure. # project structure.
# Project target. # Project target.
target=android-8 target=android-10

View File

@ -46,23 +46,12 @@ bool AppDelegate::applicationDidFinishLaunching()
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
unsigned long size; CCString* pstrFileContent = CCString::stringWithContentsOfFile("hello.lua");
char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size); if (pstrFileContent)
if (pFileContent)
{ {
// copy the file contents and add '\0' at the end, or the lua parser can not parse it pEngine->executeString(pstrFileContent->getCString());
char *pCodes = new char[size + 1];
pCodes[size] = '\0';
memcpy(pCodes, pFileContent, size);
delete[] pFileContent;
pEngine->executeString(pCodes);
delete []pCodes;
} }
#endif #else
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
std::string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); std::string path = CCFileUtils::fullPathFromRelativePath("hello.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
pEngine->executeScriptFile(path.c_str()); pEngine->executeScriptFile(path.c_str());

View File

@ -1,23 +1,9 @@
#include "cocos2d.h" #include "cocos2d.h"
#include "AppDelegate.h" #include "AppDelegate.h"
#include "SimpleAudioEngine.h" #include "SimpleAudioEngine.h"
#include "CCScriptSupport.h" #include "CCScriptSupport.h"
#include "CCLuaEngine.h" #include "CCLuaEngine.h"
#define IPAD 0
#if IPAD
#define CC_WIDTH 1024
#define CC_HEIGHT 768
#elif IPHONE_4
#define CC_WIDTH 960
#define CC_HEIGHT 640
#else
#define CC_WIDTH 480
#define CC_HEIGHT 320
#endif
USING_NS_CC; USING_NS_CC;
using namespace std; using namespace std;
using namespace CocosDenshion; using namespace CocosDenshion;
@ -56,24 +42,13 @@ bool AppDelegate::applicationDidFinishLaunching()
CCScriptEngineProtocol* pEngine = CCLuaEngine::engine(); CCScriptEngineProtocol* pEngine = CCLuaEngine::engine();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine); CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
unsigned long size; CCString* pstrFileContent = CCString::stringWithContentsOfFile("hello.lua");
char *pFileContent = (char*)CCFileUtils::getFileData("hello.lua", "r", &size); if (pstrFileContent)
{
if (pFileContent) pEngine->executeString(pstrFileContent->getCString());
{ }
// copy the file contents and add '\0' at the end, or the lua parser can not parse it #else
char *pCodes = new char[size + 1];
pCodes[size] = '\0';
memcpy(pCodes, pFileContent, size);
delete[] pFileContent;
pEngine->executeString(pCodes);
delete []pCodes;
}
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_MARMALADE)
string path = CCFileUtils::fullPathFromRelativePath("hello.lua"); string path = CCFileUtils::fullPathFromRelativePath("hello.lua");
pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str()); pEngine->addSearchPath(path.substr(0, path.find_last_of("/")).c_str());
pEngine->executeScriptFile(path.c_str()); pEngine->executeScriptFile(path.c_str());

View File

@ -26,6 +26,9 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
} }
else else
{ {
ccDrawInit();
ccGLInvalidateStateCache();
CCShaderCache::sharedShaderCache()->reloadDefaultShaders(); CCShaderCache::sharedShaderCache()->reloadDefaultShaders();
CCTextureCache::reloadAllTextures(); CCTextureCache::reloadAllTextures();
CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL); CCNotificationCenter::sharedNotificationCenter()->postNotification(EVNET_COME_TO_FOREGROUND, NULL);