fixed #656: remove unneeded methods & android can let user set resource path

This commit is contained in:
minggo 2011-08-04 17:19:58 +08:00
parent eaca1d4a71
commit 76da5f2e84
9 changed files with 57 additions and 80 deletions

View File

@ -8,8 +8,6 @@
using namespace cocos2d; using namespace cocos2d;
#define IMG_PATH "assets"
extern "C" extern "C"
{ {
@ -23,8 +21,6 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
view->create(480, 320); view->create(480, 320);
cocos2d::CCDirector::sharedDirector()->setOpenGLView(view); cocos2d::CCDirector::sharedDirector()->setOpenGLView(view);
CCFileUtils::setRelativePath(IMG_PATH);
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
cocos2d::CCApplication::sharedApplication().run(); cocos2d::CCApplication::sharedApplication().run();
} }

View File

@ -40,6 +40,9 @@ bool AppDelegate::initInstance()
// OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp // OpenGLView initialized in HelloWorld/android/jni/helloworld/main.cpp
// the default setting is to create a fullscreen view // 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 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 #endif // CC_PLATFORM_ANDROID

View File

@ -8,8 +8,6 @@
using namespace cocos2d; using namespace cocos2d;
#define IMG_PATH "assets"
extern "C" extern "C"
{ {
@ -23,8 +21,6 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
// view->create(480, 320); // view->create(480, 320);
cocos2d::CCDirector::sharedDirector()->setOpenGLView(view); cocos2d::CCDirector::sharedDirector()->setOpenGLView(view);
CCFileUtils::setRelativePath(IMG_PATH);
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
cocos2d::CCApplication::sharedApplication().run(); cocos2d::CCApplication::sharedApplication().run();
} }

View File

@ -107,12 +107,6 @@ public:
*/ */
static void setResource(const char* pszZipFileName); static void setResource(const char* pszZipFileName);
///////////////////////////////////////////////////
// interfaces on android
///////////////////////////////////////////////////
static const char* getResourcePath(void);
static void setRelativePath(const char* pszRelativePath);
/////////////////////////////////////////////////// ///////////////////////////////////////////////////
// interfaces on ios // interfaces on ios
/////////////////////////////////////////////////// ///////////////////////////////////////////////////

View File

@ -29,49 +29,49 @@ NS_CC_BEGIN;
#define MAX_PATH 256 #define MAX_PATH 256
using namespace std;
// record the resource path // record the resource path
static std::string s_strRelativePath = ""; static string s_strResourcePath = "";
static std::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; return;
} }
s_strRelativePath = pszRelativePath; s_strResourcePath = pszResourcePath;
// if the path is not ended with '/', append it /*
if (s_strRelativePath.find("/") != (strlen(s_strRelativePath.c_str()) - 1)) * 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) 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) const char *CCFileUtils::fullPathFromRelativeFile(const char *pszFilename, const char *pszRelativeFile)
{ {
//std::string relativeFile = fullPathFromRelativePath(pszRelativeFile);
std::string relativeFile = pszRelativeFile; std::string relativeFile = pszRelativeFile;
CCString *pRet = new CCString(); CCString *pRet = new CCString();
pRet->autorelease(); 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) unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* pszMode, unsigned long * pSize)
{ {
string fullPath = s_strRelativePath + pszFileName; string fullPath = pszFileName;
unsigned char * pData = CCFileUtils::getFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str(), pSize); 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()) if (! pData && getIsPopupNotify())
{ {
std::string title = "Notification"; std::string title = "Notification";
std::string msg = "Get data from file("; 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()); CCMessageBox(msg.c_str(), title.c_str());
} }
return pData; return pData;

View File

@ -247,12 +247,7 @@ namespace cocos2d {
strcpy(s_pszResourcePath, pszResourcePath); strcpy(s_pszResourcePath, pszResourcePath);
} }
const char* CCFileUtils::getResourcePath()
{
return s_pszResourcePath;
}
int CCFileUtils::ccLoadFileIntoMemory(const char *filename, unsigned char **out) int CCFileUtils::ccLoadFileIntoMemory(const char *filename, unsigned char **out)
{ {
CCAssert( out, "ccLoadFileIntoMemory: invalid 'out' parameter"); CCAssert( out, "ccLoadFileIntoMemory: invalid 'out' parameter");
@ -347,10 +342,6 @@ namespace cocos2d {
{ {
CCAssert(0, "Have not implement!"); CCAssert(0, "Have not implement!");
} }
void CCFileUtils::setRelativePath(const char* pszRelativePath)
{
CCAssert(0, "Have not implement!");
}
// notification support when getFileData from a invalid file // notification support when getFileData from a invalid file
static bool s_bPopupNotify = true; static bool s_bPopupNotify = true;

View File

@ -148,18 +148,6 @@ void CCFileUtils::setResource(const char* pszZipFileName)
CCAssert(0, "Have not implement!"); 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() string CCFileUtils::getWriteablePath()
{ {
// return the path that the exe file saved in // return the path that the exe file saved in

View File

@ -247,17 +247,6 @@ void CCFileUtils::setResourcePath(const char *pszResourcePath)
CCAssert(0, "Have not implement!"); 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() string CCFileUtils::getWriteablePath()
{ {
return string(CCApplication::sharedApplication().getAppWritablePath()); return string(CCApplication::sharedApplication().getAppWritablePath());

View File

@ -8,8 +8,6 @@
using namespace cocos2d; using namespace cocos2d;
#define IMG_PATH "assets"
extern "C" extern "C"
{ {
@ -23,8 +21,6 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
// view->create(480, 320); // view->create(480, 320);
cocos2d::CCDirector::sharedDirector()->setOpenGLView(view); cocos2d::CCDirector::sharedDirector()->setOpenGLView(view);
CCFileUtils::setRelativePath(IMG_PATH);
AppDelegate *pAppDelegate = new AppDelegate(); AppDelegate *pAppDelegate = new AppDelegate();
cocos2d::CCApplication::sharedApplication().run(); cocos2d::CCApplication::sharedApplication().run();
} }