diff --git a/HelloLua/android/jni/helloworld/main.cpp b/HelloLua/android/jni/helloworld/main.cpp index cbdbfe7a69..aeb0bf9d68 100644 --- a/HelloLua/android/jni/helloworld/main.cpp +++ b/HelloLua/android/jni/helloworld/main.cpp @@ -8,8 +8,6 @@ using namespace cocos2d; -#define IMG_PATH "assets" - extern "C" { @@ -23,8 +21,6 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi view->create(480, 320); cocos2d::CCDirector::sharedDirector()->setOpenGLView(view); - CCFileUtils::setRelativePath(IMG_PATH); - AppDelegate *pAppDelegate = new AppDelegate(); cocos2d::CCApplication::sharedApplication().run(); } diff --git a/HelloWorld/AppDelegate.cpp b/HelloWorld/AppDelegate.cpp index fcaa8872e0..75804b9b7e 100644 --- a/HelloWorld/AppDelegate.cpp +++ b/HelloWorld/AppDelegate.cpp @@ -40,6 +40,9 @@ bool AppDelegate::initInstance() // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp // the default setting is to create a fullscreen view // if you want to use auto-scale, please enable view->create(320,480) in main.cpp + // if the resources under '/sdcard" or other writeable path, set it. + // warning: the audio source should in assets/ + // cocos2d::CCFileUtils::setResourcePath("/sdcard"); #endif // CC_PLATFORM_ANDROID diff --git a/HelloWorld/android/jni/helloworld/main.cpp b/HelloWorld/android/jni/helloworld/main.cpp index 623378b14f..e4ceb2f4ea 100644 --- a/HelloWorld/android/jni/helloworld/main.cpp +++ b/HelloWorld/android/jni/helloworld/main.cpp @@ -8,8 +8,6 @@ using namespace cocos2d; -#define IMG_PATH "assets" - extern "C" { @@ -23,8 +21,6 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi // view->create(480, 320); cocos2d::CCDirector::sharedDirector()->setOpenGLView(view); - CCFileUtils::setRelativePath(IMG_PATH); - AppDelegate *pAppDelegate = new AppDelegate(); cocos2d::CCApplication::sharedApplication().run(); } diff --git a/cocos2dx/platform/CCFileUtils.h b/cocos2dx/platform/CCFileUtils.h index c64e473457..d423d72f77 100644 --- a/cocos2dx/platform/CCFileUtils.h +++ b/cocos2dx/platform/CCFileUtils.h @@ -107,12 +107,6 @@ public: */ static void setResource(const char* pszZipFileName); - /////////////////////////////////////////////////// - // interfaces on android - /////////////////////////////////////////////////// - static const char* getResourcePath(void); - static void setRelativePath(const char* pszRelativePath); - /////////////////////////////////////////////////// // interfaces on ios /////////////////////////////////////////////////// diff --git a/cocos2dx/platform/android/CCFileUtils_android.cpp b/cocos2dx/platform/android/CCFileUtils_android.cpp index 04a21d52fc..8621e192ca 100644 --- a/cocos2dx/platform/android/CCFileUtils_android.cpp +++ b/cocos2dx/platform/android/CCFileUtils_android.cpp @@ -29,49 +29,49 @@ NS_CC_BEGIN; #define MAX_PATH 256 +using namespace std; + // record the resource path -static std::string s_strRelativePath = ""; -static std::string s_strResourcePath = ""; +static string s_strResourcePath = ""; -void CCFileUtils::setRelativePath(const char* pszRelativePath) +void CCFileUtils::setResourcePath(const char* pszResourcePath) { - CCAssert(pszRelativePath != NULL, "[FileUtils setRelativePath] -- wrong relative path"); + CCAssert(pszResourcePath != NULL, "[FileUtils setRelativePath] -- wrong relative path"); - if (! pszRelativePath) + if (! pszResourcePath) { return; } - s_strRelativePath = pszRelativePath; - - // if the path is not ended with '/', append it - if (s_strRelativePath.find("/") != (strlen(s_strRelativePath.c_str()) - 1)) + s_strResourcePath = pszResourcePath; + + /* + * If the path is set by user, and not end with "/", append it + */ + if (s_strResourcePath.find(".apk") == string::npos + && s_strResourcePath.find_last_of("/") != s_strResourcePath.length() - 1) { - s_strRelativePath += "/"; + s_strResourcePath += "/"; } } -void CCFileUtils::setResourcePath(const char *pszResourcePath) -{ - CCAssert(pszResourcePath != NULL, "[FileUtils setResourcePath] -- wrong resource path"); - CCAssert(strlen(pszResourcePath) <= MAX_PATH, "[FileUtils setResourcePath] -- resource path too long"); - - s_strResourcePath = pszResourcePath; -} - -const char* CCFileUtils::getResourcePath() -{ - return s_strResourcePath.c_str(); -} - const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) { - return pszRelativePath; + if (s_strResourcePath.find(".apk") != string::npos) + { + return pszRelativePath; + } + else + { + CCString *pRet = new CCString(); + pRet->autorelease(); + pRet->m_sString = s_strResourcePath + pszRelativePath; + return pRet->m_sString.c_str(); + } } const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile) { - //std::string relativeFile = fullPathFromRelativePath(pszRelativeFile); std::string relativeFile = pszRelativeFile; CCString *pRet = new CCString(); pRet->autorelease(); @@ -81,14 +81,38 @@ const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const } unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize) -{ - string fullPath = s_strRelativePath + pszFileName; - unsigned char * pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str(), pSize); +{ + string fullPath = pszFileName; + unsigned char * pData = 0; + + if (s_strResourcePath.find(".apk") != string::npos) + { + // read from apk + fullPath.insert(0, "assets/"); + pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str(), pSize); + } + else + { + do + { + // read rrom other path than user set it + FILE *fp = fopen(pszFileName, pszMode); + CC_BREAK_IF(!fp); + + fseek(fp,0,SEEK_END); + *pSize = ftell(fp); + fseek(fp,0,SEEK_SET); + pData = new unsigned char[*pSize]; + *pSize = fread(pData,sizeof(unsigned char), *pSize,fp); + fclose(fp); + } while (0); + } + if (! pData && getIsPopupNotify()) { std::string title = "Notification"; std::string msg = "Get data from file("; - msg.append(pszFileName).append(") failed!"); + msg.append(fullPath.c_str()).append(") failed!"); CCMessageBox(msg.c_str(), title.c_str()); } return pData; diff --git a/cocos2dx/platform/ios/CCFileUtils_ios.mm b/cocos2dx/platform/ios/CCFileUtils_ios.mm index 6d4f629706..f6bd427e78 100644 --- a/cocos2dx/platform/ios/CCFileUtils_ios.mm +++ b/cocos2dx/platform/ios/CCFileUtils_ios.mm @@ -247,12 +247,7 @@ namespace cocos2d { strcpy(s_pszResourcePath, pszResourcePath); } - - const char* CCFileUtils::getResourcePath() - { - return s_pszResourcePath; - } - + int CCFileUtils::ccLoadFileIntoMemory(const char *filename, unsigned char **out) { CCAssert( out, "ccLoadFileIntoMemory: invalid 'out' parameter"); @@ -347,10 +342,6 @@ namespace cocos2d { { CCAssert(0, "Have not implement!"); } - void CCFileUtils::setRelativePath(const char* pszRelativePath) - { - CCAssert(0, "Have not implement!"); - } // notification support when getFileData from a invalid file static bool s_bPopupNotify = true; diff --git a/cocos2dx/platform/win32/CCFileUtils_win32.cpp b/cocos2dx/platform/win32/CCFileUtils_win32.cpp index 34ba8695e0..98dc525594 100644 --- a/cocos2dx/platform/win32/CCFileUtils_win32.cpp +++ b/cocos2dx/platform/win32/CCFileUtils_win32.cpp @@ -148,18 +148,6 @@ void CCFileUtils::setResource(const char* pszZipFileName) CCAssert(0, "Have not implement!"); } -const char* CCFileUtils::getResourcePath(void) -{ - CCAssert(0, "Have not implement!"); - return NULL; -} - -void CCFileUtils::setRelativePath(const char* pszRelativePath) -{ - CC_UNUSED_PARAM(pszRelativePath); - CCAssert(0, "Have not implement!"); -} - string CCFileUtils::getWriteablePath() { // return the path that the exe file saved in diff --git a/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp b/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp index b891a1254d..6e645a1808 100644 --- a/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp +++ b/cocos2dx/platform/wophone/CCFileUtils_wophone.cpp @@ -247,17 +247,6 @@ void CCFileUtils::setResourcePath(const char *pszResourcePath) CCAssert(0, "Have not implement!"); } -const char* CCFileUtils::getResourcePath(void) -{ - CCAssert(0, "Have not implement!"); - return NULL; -} - -void CCFileUtils::setRelativePath(const char* pszRelativePath) -{ - CCAssert(0, "Have not implement!"); -} - string CCFileUtils::getWriteablePath() { return string(CCApplication::sharedApplication().getAppWritablePath()); diff --git a/tests/test.android/jni/tests/main.cpp b/tests/test.android/jni/tests/main.cpp index 623378b14f..e4ceb2f4ea 100644 --- a/tests/test.android/jni/tests/main.cpp +++ b/tests/test.android/jni/tests/main.cpp @@ -8,8 +8,6 @@ using namespace cocos2d; -#define IMG_PATH "assets" - extern "C" { @@ -23,8 +21,6 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi // view->create(480, 320); cocos2d::CCDirector::sharedDirector()->setOpenGLView(view); - CCFileUtils::setRelativePath(IMG_PATH); - AppDelegate *pAppDelegate = new AppDelegate(); cocos2d::CCApplication::sharedApplication().run(); }