diff --git a/cocos2dx/platform/CCFileUtils.h b/cocos2dx/platform/CCFileUtils.h index 274dfc8a1c..2c90a03035 100644 --- a/cocos2dx/platform/CCFileUtils.h +++ b/cocos2dx/platform/CCFileUtils.h @@ -235,6 +235,7 @@ protected: std::vector m_searchResolutionsOrderArray; std::vector m_searchPathArray; + std::string m_strDefaultResRootPath; }; // end of platform group diff --git a/cocos2dx/platform/CCFileUtilsCommon_cpp.h b/cocos2dx/platform/CCFileUtilsCommon_cpp.h index 661f759385..7c9fcb9867 100644 --- a/cocos2dx/platform/CCFileUtilsCommon_cpp.h +++ b/cocos2dx/platform/CCFileUtilsCommon_cpp.h @@ -389,7 +389,20 @@ std::string CCFileUtils::getNewFilename(const char* pszFileName) void CCFileUtils::setSearchResolutionsOrder(const std::vector& searchResolutionsOrder) { - m_searchResolutionsOrderArray = searchResolutionsOrder; + bool bExistDefault = false; + m_searchResolutionsOrderArray.clear(); + for (std::vector::const_iterator iter = searchResolutionsOrder.begin(); iter != searchResolutionsOrder.end(); ++iter) + { + if (!bExistDefault && (*iter) == "") + { + bExistDefault = true; + } + m_searchResolutionsOrderArray.push_back(*iter); + } + if (!bExistDefault) + { + m_searchResolutionsOrderArray.push_back(""); + } } const std::vector& CCFileUtils::getSearchResolutionsOrder() @@ -397,11 +410,6 @@ const std::vector& CCFileUtils::getSearchResolutionsOrder() return m_searchResolutionsOrderArray; } -void CCFileUtils::setSearchPath(const std::vector& searchPaths) -{ - m_searchPathArray = searchPaths; -} - const std::vector& CCFileUtils::getSearchPath() { return m_searchPathArray; diff --git a/cocos2dx/platform/android/CCFileUtils.cpp b/cocos2dx/platform/android/CCFileUtils.cpp index 048463fa79..48a870de5f 100644 --- a/cocos2dx/platform/android/CCFileUtils.cpp +++ b/cocos2dx/platform/android/CCFileUtils.cpp @@ -25,18 +25,17 @@ THE SOFTWARE. #define __CC_PLATFORM_FILEUTILS_CPP__ #include "platform/CCFileUtilsCommon_cpp.h" #include "support/zip_support/ZipUtils.h" +#include "platform/CCCommon.h" +#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h" using namespace std; NS_CC_BEGIN -#include "platform/CCCommon.h" -#include "jni/Java_org_cocos2dx_lib_Cocos2dxHelper.h" - static CCFileUtils* s_pFileUtils = NULL; // record the zip on the resource path static ZipFile *s_pZipFile = NULL; - +// The full path cache, key: relative path, value: full path. static std::map s_fullPathCache; CCFileUtils* CCFileUtils::sharedFileUtils() @@ -53,8 +52,10 @@ CCFileUtils* CCFileUtils::sharedFileUtils() bool CCFileUtils::init() { - m_searchPathArray.push_back("assets/"); + m_strDefaultResRootPath = "assets/"; + m_searchPathArray.push_back(m_strDefaultResRootPath); m_searchResolutionsOrderArray.push_back(""); + return true; } @@ -237,18 +238,45 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) { if (pszResourceDirectory == NULL) return; - m_obDirectory = pszResourceDirectory; - if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') + std::vector searchPaths = this->getSearchPath();; + searchPaths.insert(searchPaths.begin(), pszResourceDirectory); + this->setSearchPath(searchPaths); +} + +void CCFileUtils::setSearchPath(const std::vector& searchPaths) +{ + bool bExistDefaultRootPath = false; + + m_searchPathArray.clear(); + for (std::vector::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter) { - m_obDirectory.append("/"); + std::string strPrefix; + std::string path; + if ((*iter)[0] != '/') + { // Not an absolute path + if (iter->find(m_strDefaultResRootPath) != 0) + { // The path contains no default resource root path, insert the root path. + strPrefix = m_strDefaultResRootPath; + } + } + path = strPrefix+(*iter); + if (path.length() > 0 && path[path.length()-1] != '/') + { + path += "/"; + } + if (!bExistDefaultRootPath && path == m_strDefaultResRootPath) + { + bExistDefaultRootPath = true; + } + m_searchPathArray.push_back(path); } - if (pszResourceDirectory[0] != '/') + + if (!bExistDefaultRootPath) { - m_obDirectory.insert(0, "assets/"); + CCLOG("Default root path doesn't exist, adding it."); + m_searchPathArray.push_back(m_strDefaultResRootPath); } - - m_searchPathArray.insert(m_searchPathArray.begin(), m_obDirectory); } string CCFileUtils::getWriteablePath() diff --git a/cocos2dx/platform/ios/CCFileUtils.mm b/cocos2dx/platform/ios/CCFileUtils.mm index 7d50d3bc3c..d83d9750f4 100644 --- a/cocos2dx/platform/ios/CCFileUtils.mm +++ b/cocos2dx/platform/ios/CCFileUtils.mm @@ -145,6 +145,7 @@ NS_CC_BEGIN static std::map s_fullPathCache; static CCFileUtils* s_pFileUtils = NULL; +static NSFileManager* s_fileManager = [NSFileManager defaultManager]; CCFileUtils* CCFileUtils::sharedFileUtils() { @@ -182,7 +183,20 @@ bool CCFileUtils::init() void CCFileUtils::setSearchResolutionsOrder(const std::vector& searchResolutionsOrder) { - m_searchResolutionsOrderArray = searchResolutionsOrder; + bool bExistDefault = false; + m_searchResolutionsOrderArray.clear(); + for (std::vector::const_iterator iter = searchResolutionsOrder.begin(); iter != searchResolutionsOrder.end(); ++iter) + { + if (!bExistDefault && (*iter) == "") + { + bExistDefault = true; + } + m_searchResolutionsOrderArray.push_back(*iter); + } + if (!bExistDefault) + { + m_searchResolutionsOrderArray.push_back(""); + } } const std::vector& CCFileUtils::getSearchResolutionsOrder() @@ -192,7 +206,20 @@ const std::vector& CCFileUtils::getSearchResolutionsOrder() void CCFileUtils::setSearchPath(const std::vector& searchPaths) { - m_searchPathArray = searchPaths; + bool bExistDefault = false; + m_searchPathArray.clear(); + for (std::vector::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter) + { + if (!bExistDefault && (*iter) == "") + { + bExistDefault = true; + } + m_searchPathArray.push_back(*iter); + } + if (!bExistDefault) + { + m_searchPathArray.push_back(""); + } } const std::vector& CCFileUtils::getSearchPath() @@ -254,13 +281,21 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s path += file_path; path += resourceDirectory; - NSString* fullpath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:file.c_str()] + if (searchPath[0] != '/') + { + NSString* fullpath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:file.c_str()] ofType:nil inDirectory:[NSString stringWithUTF8String:path.c_str()]]; - - - if (fullpath != nil) { - return [fullpath UTF8String]; + if (fullpath != nil) { + return [fullpath UTF8String]; + } + } + else + {// Search path is an absolute path. + std::string fullPath = path + file; + if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:fullPath.c_str()]]) { + return fullPath; + } } // Return empty string when file wasn't found. diff --git a/cocos2dx/platform/linux/CCApplication.cpp b/cocos2dx/platform/linux/CCApplication.cpp index 22e59e9930..54ce509111 100644 --- a/cocos2dx/platform/linux/CCApplication.cpp +++ b/cocos2dx/platform/linux/CCApplication.cpp @@ -9,6 +9,7 @@ #include #include #include "CCDirector.h" +#include "platform/CCFileUtils.h" NS_CC_BEGIN diff --git a/cocos2dx/platform/linux/CCApplication.h b/cocos2dx/platform/linux/CCApplication.h index 81ce0aade5..24f5a7ebea 100644 --- a/cocos2dx/platform/linux/CCApplication.h +++ b/cocos2dx/platform/linux/CCApplication.h @@ -10,6 +10,7 @@ #include "platform/CCCommon.h" #include "platform/CCApplicationProtocol.h" +#include NS_CC_BEGIN class CCRect; @@ -44,13 +45,13 @@ public: * Sets the Resource root path. * @deprecated Please use CCFileUtils::sharedFileUtils()->setSearchPath() instead. */ - CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const char* pszRootResDir); + CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir); /** * Gets the Resource root path. * @deprecated Please use CCFileUtils::sharedFileUtils()->getSearchPath() instead. */ - CC_DEPRECATED_ATTRIBUTE const char* getResourceRootPath(void); + CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void); /** @brief Get target platform diff --git a/cocos2dx/platform/linux/CCFileUtils.cpp b/cocos2dx/platform/linux/CCFileUtils.cpp index dadf2ad893..4073a4eca4 100644 --- a/cocos2dx/platform/linux/CCFileUtils.cpp +++ b/cocos2dx/platform/linux/CCFileUtils.cpp @@ -35,7 +35,18 @@ CCFileUtils* CCFileUtils::sharedFileUtils() bool CCFileUtils::init() { - m_searchPathArray.push_back(""); + // get application path + int length = 0; + char fullpath[256] = {0}; + length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); + fullpath[length] = '\0'; + + std::string resourcePath = fullpath; + resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); + resourcePath += "/../../../Resources/"; + m_strDefaultResRootPath = resourcePath; + CCLOG("DEFAULT RES PATH = %s", m_strDefaultResRootPath.c_str()); + m_searchPathArray.push_back(m_strDefaultResRootPath); m_searchResolutionsOrderArray.push_back(""); return true; @@ -59,13 +70,6 @@ void CCFileUtils::purgeCachedEntries() std::string CCFileUtils::getPathForFilename(const std::string& filename, const std::string& resourceDirectory, const std::string& searchPath) { - std::string ret = CCApplication::sharedApplication()->getResourceRootPath(); - - if (ret[ret.length()-1] != '\\' && ret[ret.length()-1] != '/') - { - ret += "/"; - } - std::string file = filename; std::string file_path = ""; size_t pos = filename.find_last_of("/"); @@ -89,9 +93,8 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s path += "/"; } path += file; - ret += path; - return ret; + return path; } std::string CCFileUtils::fullPathForFilename(const char* pszFileName) @@ -122,6 +125,8 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName) for (std::vector::iterator resOrderIter = m_searchResolutionsOrderArray.begin(); resOrderIter != m_searchResolutionsOrderArray.end(); ++resOrderIter) { + CCLOG("\n\nSEARCHING: %s, %s, %s", newFileName.c_str(), resOrderIter->c_str(), searchPathsIter->c_str()); + fullpath = this->getPathForFilename(newFileName, *resOrderIter, *searchPathsIter); // check if file or path exist @@ -130,6 +135,7 @@ std::string CCFileUtils::fullPathForFilename(const char* pszFileName) { // Adding the full path to cache if the file was found. s_fullPathCache.insert(std::pair(pszFileName, fullpath)); + CCLOG("Returning path: %s", fullpath.c_str()); return fullpath; } } @@ -190,18 +196,52 @@ unsigned char* CCFileUtils::getFileData(const char* pszFileName, const char* psz void CCFileUtils::setResourceDirectory(const char* pszResourceDirectory) { + if (pszResourceDirectory == NULL) return; m_obDirectory = pszResourceDirectory; - if (m_obDirectory.size() > 0 && m_obDirectory[m_obDirectory.size() - 1] != '/') + std::vector searchPaths = this->getSearchPath();; + searchPaths.insert(searchPaths.begin(), pszResourceDirectory); + this->setSearchPath(searchPaths); +} + +void CCFileUtils::setSearchPath(const std::vector& searchPaths) +{ + bool bExistDefaultRootPath = false; + + m_searchPathArray.clear(); + for (std::vector::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter) { - m_obDirectory.append("/"); + std::string strPrefix; + std::string path; + if ((*iter)[0] != '/') + { // Not an absolute path + if (iter->find(m_strDefaultResRootPath) != 0) + { // The path contains no default resource root path, insert the root path. + strPrefix = m_strDefaultResRootPath; + } + } + path = strPrefix+(*iter); + if (path.length() > 0 && path[path.length()-1] != '/') + { + path += "/"; + } + if (!bExistDefaultRootPath && path == m_strDefaultResRootPath) + { + bExistDefaultRootPath = true; + } + m_searchPathArray.push_back(path); + } + + if (!bExistDefaultRootPath) + { + CCLOG("Default root path doesn't exist, adding it."); + m_searchPathArray.push_back(m_strDefaultResRootPath); } - m_searchPathArray.insert(m_searchPathArray.begin(), m_obDirectory); } string CCFileUtils::getWriteablePath() { //return current resource path - return CCApplication::sharedApplication()->getResourceRootPath(); + return m_strDefaultResRootPath; } NS_CC_END diff --git a/cocos2dx/platform/mac/CCFileUtils.mm b/cocos2dx/platform/mac/CCFileUtils.mm index a0b3765698..1a9665561d 100755 --- a/cocos2dx/platform/mac/CCFileUtils.mm +++ b/cocos2dx/platform/mac/CCFileUtils.mm @@ -146,6 +146,7 @@ CCDictionary* ccFileUtils_dictionaryWithContentsOfFileThreadSafe(const char *pFi CCArray* ccFileUtils_arrayWithContentsOfFileThreadSafe(const char* pFileName); static CCFileUtils* s_pFileUtils = NULL; +static NSFileManager* s_fileManager = [NSFileManager defaultManager]; static std::map s_fullPathCache; CCFileUtils* CCFileUtils::sharedFileUtils() @@ -184,7 +185,20 @@ bool CCFileUtils::init() void CCFileUtils::setSearchResolutionsOrder(const std::vector& searchResolutionsOrder) { - m_searchResolutionsOrderArray = searchResolutionsOrder; + bool bExistDefault = false; + m_searchResolutionsOrderArray.clear(); + for (std::vector::const_iterator iter = searchResolutionsOrder.begin(); iter != searchResolutionsOrder.end(); ++iter) + { + if (!bExistDefault && (*iter) == "") + { + bExistDefault = true; + } + m_searchResolutionsOrderArray.push_back(*iter); + } + if (!bExistDefault) + { + m_searchResolutionsOrderArray.push_back(""); + } } const std::vector& CCFileUtils::getSearchResolutionsOrder() @@ -194,7 +208,20 @@ const std::vector& CCFileUtils::getSearchResolutionsOrder() void CCFileUtils::setSearchPath(const std::vector& searchPaths) { - m_searchPathArray = searchPaths; + bool bExistDefault = false; + m_searchPathArray.clear(); + for (std::vector::const_iterator iter = searchPaths.begin(); iter != searchPaths.end(); ++iter) + { + if (!bExistDefault && (*iter) == "") + { + bExistDefault = true; + } + m_searchPathArray.push_back(*iter); + } + if (!bExistDefault) + { + m_searchPathArray.push_back(""); + } } const std::vector& CCFileUtils::getSearchPath() @@ -256,13 +283,21 @@ std::string CCFileUtils::getPathForFilename(const std::string& filename, const s path += file_path; path += resourceDirectory; - NSString* fullpath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:file.c_str()] - ofType:nil - inDirectory:[NSString stringWithUTF8String:path.c_str()]]; - - - if (fullpath != nil) { - return [fullpath UTF8String]; + if (searchPath[0] != '/') + { + NSString* fullpath = [[NSBundle mainBundle] pathForResource:[NSString stringWithUTF8String:file.c_str()] + ofType:nil + inDirectory:[NSString stringWithUTF8String:path.c_str()]]; + if (fullpath != nil) { + return [fullpath UTF8String]; + } + } + else + {// Search path is an absolute path. + std::string fullPath = path + file; + if ([s_fileManager fileExistsAtPath:[NSString stringWithUTF8String:fullPath.c_str()]]) { + return fullPath; + } } // Return empty string when file wasn't found. diff --git a/cocos2dx/proj.linux/Makefile b/cocos2dx/proj.linux/Makefile index 2a9967e048..054a619421 100644 --- a/cocos2dx/proj.linux/Makefile +++ b/cocos2dx/proj.linux/Makefile @@ -7,19 +7,18 @@ VISIBILITY = LIBS = -INCLUDES = -I.. \ +INCLUDES = -I.. \ -I../platform/third_party/linux/libfreetype2 \ -I../cocoa \ -I../include \ -I../kazmath/include \ - -I../platform \ -I../../extensions \ -I../../extensions/CCBReader \ -I../../extensions/GUI/CCControlExtension \ -I../../extensions/GUI/CCControlExtension \ - -I../../external/chipmunk/include/chipmunk \ + -I../../external/chipmunk/include/chipmunk \ -I../../extensions/network \ - -I../platform/linux/ \ + -I../platform/linux \ -I../platform/third_party/linux/libxml2 \ -I../platform/third_party/linux/libpng \ -I../platform/third_party/linux/libjpeg \ @@ -166,10 +165,7 @@ STATICLIBS = $(STATICLIBS_DIR)/libfreetype.a \ $(STATICLIBS_DIR)/libjpeg.a \ $(STATICLIBS_DIR)/libtiff.a -SHAREDLIBS = -SHAREDLIBS += -lglfw -lcurl -lfontconfig -SHAREDLIBS += -Wl,-rpath,../../cocos2dx/platform/third_party/linux/glew-1.7.0/glew-1.7.0/lib -SHAREDLIBS += -L../../cocos2dx/platform/third_party/linux/glew-1.7.0/glew-1.7.0/lib -lGLEW +SHAREDLIBS = -lglfw -lcurl -lfontconfig -lGLEW debug: CCFLAGS += -g3 -O0 debug: CXXFLAGS += -g3 -O0 diff --git a/samples/Cpp/HelloCpp/proj.linux/main.cpp b/samples/Cpp/HelloCpp/proj.linux/main.cpp index 069f414aa5..08245dd052 100644 --- a/samples/Cpp/HelloCpp/proj.linux/main.cpp +++ b/samples/Cpp/HelloCpp/proj.linux/main.cpp @@ -10,26 +10,13 @@ USING_NS_CC; -// 500 is enough? -#define MAXPATHLEN 500 - int main(int argc, char **argv) { - // get application path - int length; - char fullpath[MAXPATHLEN]; - length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); - fullpath[length] = '\0'; - - std::string resourcePath = fullpath; - resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); - resourcePath += "/../../../Resources/"; - // create the application instance AppDelegate app; - CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str()); + CCEGLView* eglView = CCEGLView::sharedOpenGLView(); - eglView->setFrameSize(480, 320); + eglView->setFrameSize(800, 480); return CCApplication::sharedApplication()->run(); } diff --git a/samples/Cpp/TestCpp/Android.mk b/samples/Cpp/TestCpp/Android.mk index 2b1d13100a..bd1213b4dc 100644 --- a/samples/Cpp/TestCpp/Android.mk +++ b/samples/Cpp/TestCpp/Android.mk @@ -90,6 +90,7 @@ Classes/TouchesTest/TouchesTest.cpp \ Classes/TransitionsTest/TransitionsTest.cpp \ Classes/UserDefaultTest/UserDefaultTest.cpp \ Classes/ZwoptexTest/ZwoptexTest.cpp \ +Classes/FileUtilsTest/FileUtilsTest.cpp \ Classes/controller.cpp \ Classes/testBasic.cpp \ Classes/AppDelegate.cpp \ diff --git a/samples/Cpp/TestCpp/Classes/AppDelegate.cpp b/samples/Cpp/TestCpp/Classes/AppDelegate.cpp index cd1f5d8a69..2717727d92 100644 --- a/samples/Cpp/TestCpp/Classes/AppDelegate.cpp +++ b/samples/Cpp/TestCpp/Classes/AppDelegate.cpp @@ -25,11 +25,14 @@ bool AppDelegate::applicationDidFinishLaunching() CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); CCSize designSize = CCSizeMake(480, 320); + CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); if (screenSize.height > 320) { CCSize resourceSize = CCSizeMake(960, 640); - CCFileUtils::sharedFileUtils()->setResourceDirectory("hd"); + std::vector searchPaths; + searchPaths.push_back("hd"); + pFileUtils->setSearchPath(searchPaths); pDirector->setContentScaleFactor(resourceSize.height/designSize.height); } diff --git a/samples/Cpp/TestCpp/Classes/FileUtilsTest/FileUtilsTest.cpp b/samples/Cpp/TestCpp/Classes/FileUtilsTest/FileUtilsTest.cpp new file mode 100644 index 0000000000..17b485d27d --- /dev/null +++ b/samples/Cpp/TestCpp/Classes/FileUtilsTest/FileUtilsTest.cpp @@ -0,0 +1,303 @@ +#include "FileUtilsTest.h" + + +TESTLAYER_CREATE_FUNC(TestResolutionDirectories); +TESTLAYER_CREATE_FUNC(TestSearchPath); +TESTLAYER_CREATE_FUNC(TestFilenameLookup); + +static NEWTESTFUNC createFunctions[] = { + CF(TestResolutionDirectories), + CF(TestSearchPath), + CF(TestFilenameLookup) +}; + +static int sceneIdx=-1; +#define MAX_LAYER (sizeof(createFunctions) / sizeof(createFunctions[0])) + +static CCLayer* nextAction() +{ + sceneIdx++; + sceneIdx = sceneIdx % MAX_LAYER; + + CCLayer* pLayer = (createFunctions[sceneIdx])(); + pLayer->init(); + pLayer->autorelease(); + + return pLayer; +} + +static CCLayer* backAction() +{ + sceneIdx--; + int total = MAX_LAYER; + if( sceneIdx < 0 ) + sceneIdx += total; + + CCLayer* pLayer = (createFunctions[sceneIdx])(); + pLayer->init(); + pLayer->autorelease(); + + return pLayer; +} + +static CCLayer* restartAction() +{ + CCLayer* pLayer = (createFunctions[sceneIdx])(); + pLayer->init(); + pLayer->autorelease(); + + return pLayer; +} + +void FileUtilsTestScene::runThisTest() +{ + CCLayer* pLayer = nextAction(); + addChild(pLayer); + + CCDirector::sharedDirector()->replaceScene(this); +} + +// #pragma mark - FileUtilsDemo + +void FileUtilsDemo::onEnter() +{ + CCLayer::onEnter(); + + CCLabelTTF* label = CCLabelTTF::create(title().c_str(), "Arial", 32); + addChild(label); + label->setPosition(ccp(VisibleRect::center().x, VisibleRect::top().y-50)); + + std::string subTitle = subtitle(); + if(! subTitle.empty()) + { + CCLabelTTF* l = CCLabelTTF::create(subTitle.c_str(), "Thonburi", 16); + addChild(l, 1); + l->setPosition(ccp(VisibleRect::center().x, VisibleRect::top().y-80)); + } + + CCMenuItemImage *item1 = CCMenuItemImage::create("Images/b1.png", "Images/b2.png", this, menu_selector(FileUtilsDemo::backCallback)); + CCMenuItemImage *item2 = CCMenuItemImage::create("Images/r1.png","Images/r2.png", this, menu_selector(FileUtilsDemo::restartCallback) ); + CCMenuItemImage *item3 = CCMenuItemImage::create("Images/f1.png", "Images/f2.png", this, menu_selector(FileUtilsDemo::nextCallback) ); + + CCMenu *menu = CCMenu::create(item1, item2, item3, NULL); + menu->setPosition(CCPointZero); + item1->setPosition(ccp(VisibleRect::center().x - item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2)); + item2->setPosition(ccp(VisibleRect::center().x, VisibleRect::bottom().y+item2->getContentSize().height/2)); + item3->setPosition(ccp(VisibleRect::center().x + item2->getContentSize().width*2, VisibleRect::bottom().y+item2->getContentSize().height/2)); + + addChild(menu, 1); +} + +void FileUtilsDemo::backCallback(CCObject* pSender) +{ + CCScene* pScene = new FileUtilsTestScene(); + CCLayer* pLayer = backAction(); + + pScene->addChild(pLayer); + CCDirector::sharedDirector()->replaceScene(pScene); + pScene->release(); +} + +void FileUtilsDemo::nextCallback(CCObject* pSender) +{ + CCScene* pScene = new FileUtilsTestScene(); + CCLayer* pLayer = nextAction(); + + pScene->addChild(pLayer); + CCDirector::sharedDirector()->replaceScene(pScene); + pScene->release(); +} + +void FileUtilsDemo::restartCallback(CCObject* pSender) +{ + CCScene* pScene = new FileUtilsTestScene(); + CCLayer* pLayer = restartAction(); + + pScene->addChild(pLayer); + CCDirector::sharedDirector()->replaceScene(pScene); + pScene->release(); +} + +string FileUtilsDemo::title() +{ + return "No title"; +} + +string FileUtilsDemo::subtitle() +{ + return ""; +} + +//#pragma mark - TestResolutionDirectories + +void TestResolutionDirectories::onEnter() +{ + FileUtilsDemo::onEnter(); + CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); + + string ret; + + sharedFileUtils->purgeCachedEntries(); + m_defaultSearchPathArray = sharedFileUtils->getSearchPath(); + vector searchPaths = m_defaultSearchPathArray; + searchPaths.insert(searchPaths.begin(), "Misc"); + sharedFileUtils->setSearchPath(searchPaths); + + m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); + vector resolutionsOrder = m_defaultResolutionsOrderArray; + + resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipadhd"); + resolutionsOrder.insert(resolutionsOrder.begin()+1, "resources-ipad"); + resolutionsOrder.insert(resolutionsOrder.begin()+2, "resources-widehd"); + resolutionsOrder.insert(resolutionsOrder.begin()+3, "resources-wide"); + resolutionsOrder.insert(resolutionsOrder.begin()+4, "resources-hd"); + resolutionsOrder.insert(resolutionsOrder.begin()+5, "resources-iphone"); + + sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); + + for( int i=1; i<7; i++) { + CCString *filename = CCString::createWithFormat("test%d.txt", i); + ret = sharedFileUtils->fullPathForFilename(filename->getCString()); + CCLog("%s -> %s", filename->getCString(), ret.c_str()); + } +} + +void TestResolutionDirectories::onExit() +{ + CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); + + // reset search path + sharedFileUtils->setSearchPath(m_defaultSearchPathArray); + sharedFileUtils->setSearchResolutionsOrder(m_defaultResolutionsOrderArray); + FileUtilsDemo::onExit(); +} + +string TestResolutionDirectories::title() +{ + return "FileUtils: resolutions in directories"; +} + +string TestResolutionDirectories::subtitle() +{ + return "See the console"; +} + +//#pragma mark - TestSearchPath + +void TestSearchPath::onEnter() +{ + FileUtilsDemo::onEnter(); + CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); + + string ret; + + sharedFileUtils->purgeCachedEntries(); + m_defaultSearchPathArray = sharedFileUtils->getSearchPath(); + vector searchPaths = m_defaultSearchPathArray; + string writablePath = sharedFileUtils->getWriteablePath(); + string fileName = writablePath+"external.txt"; + char szBuf[100] = "Hello Cocos2d-x!"; + FILE* fp = fopen(fileName.c_str(), "wb"); + if (fp) + { + fwrite(szBuf, 1, strlen(szBuf), fp); + fclose(fp); + CCLog("Writing file to writable path succeed."); + } + + searchPaths.insert(searchPaths.begin(), writablePath); + searchPaths.insert(searchPaths.begin()+1, "Misc/searchpath1"); + searchPaths.insert(searchPaths.begin()+2, "Misc/searchpath2"); + sharedFileUtils->setSearchPath(searchPaths); + + m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder(); + vector resolutionsOrder = m_defaultResolutionsOrderArray; + + resolutionsOrder.insert(resolutionsOrder.begin(), "resources-ipad"); + sharedFileUtils->setSearchResolutionsOrder(resolutionsOrder); + + for( int i=1; i<3; i++) { + CCString *filename = CCString::createWithFormat("file%d.txt", i); + ret = sharedFileUtils->fullPathForFilename(filename->getCString()); + CCLog("%s -> %s", filename->getCString(), ret.c_str()); + } + + // Gets external.txt from writable path + string fullPath = sharedFileUtils->fullPathForFilename("external.txt"); + CCLog("\nexternal file path = %s\n", fullPath.c_str()); + if (fullPath.length() > 0) { + fp = fopen(fullPath.c_str(), "rb"); + if (fp) + { + char szReadBuf[100] = {0}; + fread(szReadBuf, 1, strlen(szBuf), fp); + CCLog("The content of file from writable path: %s", szReadBuf); + fclose(fp); + } + } +} + +void TestSearchPath::onExit() +{ + CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); + + // reset search path + sharedFileUtils->setSearchPath(m_defaultSearchPathArray); + sharedFileUtils->setSearchResolutionsOrder(m_defaultResolutionsOrderArray); + FileUtilsDemo::onExit(); +} + +string TestSearchPath::title() +{ + return "FileUtils: search path"; +} + +string TestSearchPath::subtitle() +{ + return "See the console"; +} + +//#pragma mark - TestFilenameLookup + +void TestFilenameLookup::onEnter() +{ + FileUtilsDemo::onEnter(); + + CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); + + CCDictionary *dict = CCDictionary::create(); + dict->setObject(CCString::create("Images/grossini.png"), "grossini.bmp"); + dict->setObject(CCString::create("Images/grossini.png"), "grossini.xcf"); + + sharedFileUtils->setFilenameLookupDictionary(dict); + + + // Instead of loading carlitos.xcf, it will load grossini.png + CCSprite *sprite = CCSprite::create("grossini.xcf"); + this->addChild(sprite); + + CCSize s = CCDirector::sharedDirector()->getWinSize(); + sprite->setPosition(ccp(s.width/2, s.height/2)); +} + +void TestFilenameLookup::onExit() +{ + + CCFileUtils *sharedFileUtils = CCFileUtils::sharedFileUtils(); + + // reset filename lookup + sharedFileUtils->setFilenameLookupDictionary(CCDictionary::create()); + + FileUtilsDemo::onExit(); +} + +string TestFilenameLookup::title() +{ + return "FileUtils: filename lookup"; +} + +string TestFilenameLookup::subtitle() +{ + return "See the console"; +} + diff --git a/samples/Cpp/TestCpp/Classes/FileUtilsTest/FileUtilsTest.h b/samples/Cpp/TestCpp/Classes/FileUtilsTest/FileUtilsTest.h new file mode 100644 index 0000000000..bd87b83072 --- /dev/null +++ b/samples/Cpp/TestCpp/Classes/FileUtilsTest/FileUtilsTest.h @@ -0,0 +1,59 @@ +#ifndef __FILEUTILSTEST_H__ +#define __FILEUTILSTEST_H__ + +#include "../testBasic.h" +USING_NS_CC; +using namespace std; + +class FileUtilsTestScene : public TestScene +{ +public: + virtual void runThisTest(); +}; + +class FileUtilsDemo : public CCLayer +{ +public: + virtual void onEnter(); + virtual string title(); + virtual string subtitle(); + void backCallback(CCObject* pSender); + void nextCallback(CCObject* pSender); + void restartCallback(CCObject* pSender); +}; + +class TestResolutionDirectories : public FileUtilsDemo +{ +public: + virtual void onEnter(); + virtual void onExit(); + virtual string title(); + virtual string subtitle(); +private: + vector m_defaultSearchPathArray; + vector m_defaultResolutionsOrderArray; +}; + +class TestSearchPath : public FileUtilsDemo +{ +public: + virtual void onEnter(); + virtual void onExit(); + virtual string title(); + virtual string subtitle(); +private: + vector m_defaultSearchPathArray; + vector m_defaultResolutionsOrderArray; +}; + +class TestFilenameLookup : public FileUtilsDemo +{ +public: + virtual void onEnter(); + virtual void onExit(); + virtual string title(); + virtual string subtitle(); +}; + + +#endif /* __FILEUTILSTEST_H__ */ diff --git a/samples/Cpp/TestCpp/Classes/controller.cpp b/samples/Cpp/TestCpp/Classes/controller.cpp index 0cf1aa2174..2b81d88add 100644 --- a/samples/Cpp/TestCpp/Classes/controller.cpp +++ b/samples/Cpp/TestCpp/Classes/controller.cpp @@ -120,6 +120,9 @@ static TestScene* CreateTestScene(int nIdx) pScene = new ClippingNodeTestScene(); break; #endif + case TEST_FILEUTILS: + pScene = new FileUtilsTestScene(); + break; default: break; } diff --git a/samples/Cpp/TestCpp/Classes/tests.h b/samples/Cpp/TestCpp/Classes/tests.h index 3e04587d0c..1148abd605 100644 --- a/samples/Cpp/TestCpp/Classes/tests.h +++ b/samples/Cpp/TestCpp/Classes/tests.h @@ -52,6 +52,7 @@ #include "ClippingNodeTest/ClippingNodeTest.h" #include "ChipmunkTest/ChipmunkTest.h" #endif +#include "FileUtilsTest/FileUtilsTest.h" enum { @@ -107,6 +108,7 @@ enum #if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) TEST_CLIPPINGNODE, #endif + TEST_FILEUTILS, TESTS_COUNT, }; @@ -161,8 +163,9 @@ const std::string g_aTestNames[TESTS_COUNT] = { "ShaderTest", "MutiTouchTest", #if (CC_TARGET_PLATFORM != CC_PLATFORM_MARMALADE) - "ClippingNodeTest" + "ClippingNodeTest", #endif + "FileUtilsTest" }; #endif diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-hd/test4.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-hd/test4.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-hd/test4.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-ipad/test2.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-ipad/test2.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-ipad/test2.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-ipadhd/test1.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-ipadhd/test1.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-ipadhd/test1.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-iphone/test6.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-iphone/test6.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-iphone/test6.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-mac/test2.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-mac/test2.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-mac/test2.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-machd/test1.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-machd/test1.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-machd/test1.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-wide/test5.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-wide/test5.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-wide/test5.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/resources-widehd/test3.txt b/samples/Cpp/TestCpp/Resources/Misc/resources-widehd/test3.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/resources-widehd/test3.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/searchpath1/file1.txt b/samples/Cpp/TestCpp/Resources/Misc/searchpath1/file1.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/searchpath1/file1.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/Resources/Misc/searchpath2/resources-ipad/file2.txt b/samples/Cpp/TestCpp/Resources/Misc/searchpath2/resources-ipad/file2.txt new file mode 100644 index 0000000000..0527e6bd2d --- /dev/null +++ b/samples/Cpp/TestCpp/Resources/Misc/searchpath2/resources-ipad/file2.txt @@ -0,0 +1 @@ +This is a test diff --git a/samples/Cpp/TestCpp/proj.ios/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/Cpp/TestCpp/proj.ios/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id index 4f7cb0a0ad..5516715668 100644 --- a/samples/Cpp/TestCpp/proj.ios/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/Cpp/TestCpp/proj.ios/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -e5a2503bbe8aa034d6b26c86239afd82a4dc8758 \ No newline at end of file +5e326e12903a5d84168a8bc4b8bc6735e1b4c1c8 \ No newline at end of file diff --git a/samples/Cpp/TestCpp/proj.linux/Makefile b/samples/Cpp/TestCpp/proj.linux/Makefile index 5030a0e072..a4db3bd0dc 100644 --- a/samples/Cpp/TestCpp/proj.linux/Makefile +++ b/samples/Cpp/TestCpp/proj.linux/Makefile @@ -121,6 +121,7 @@ OBJECTS = ../Classes/AccelerometerTest/AccelerometerTest.o \ ../Classes/TransitionsTest/TransitionsTest.o \ ../Classes/UserDefaultTest/UserDefaultTest.o \ ../Classes/ZwoptexTest/ZwoptexTest.o \ + ../Classes/FileUtilsTest/FileUtilsTest.o \ ../Classes/controller.o \ ../Classes/testBasic.o \ ../Classes/AppDelegate.o \ diff --git a/samples/Cpp/TestCpp/proj.linux/main.cpp b/samples/Cpp/TestCpp/proj.linux/main.cpp index 71776a9eba..21e3fc5d5c 100644 --- a/samples/Cpp/TestCpp/proj.linux/main.cpp +++ b/samples/Cpp/TestCpp/proj.linux/main.cpp @@ -11,25 +11,11 @@ USING_NS_CC; -// 500 is enough? -#define MAXPATHLEN 500 - int main(int argc, char **argv) -{ - // get application path - int length; - char fullpath[MAXPATHLEN]; - length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); - fullpath[length] = '\0'; - - std::string resourcePath = fullpath; - resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); - resourcePath += "/../../../Resources/"; - +{ // create the application instance AppDelegate app; - CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str()); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); - eglView->setFrameSize(480, 320); + eglView->setFrameSize(800, 480); return CCApplication::sharedApplication()->run(); } diff --git a/samples/Cpp/TestCpp/proj.mac/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id b/samples/Cpp/TestCpp/proj.mac/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id index 9f998e19e4..c4d8937f6d 100644 --- a/samples/Cpp/TestCpp/proj.mac/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id +++ b/samples/Cpp/TestCpp/proj.mac/TestCpp.xcodeproj/project.pbxproj.REMOVED.git-id @@ -1 +1 @@ -3d27ac9829ac45f2433d903b6b4f359a8e20636f \ No newline at end of file +3c6884ef6d8f01816d6bffe1a3bf124b35cf4432 \ No newline at end of file diff --git a/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp index 3501611450..1bd726bcad 100644 --- a/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp +++ b/samples/Javascript/CocosPlayer/Classes/AppDelegate.cpp @@ -67,31 +67,31 @@ bool AppDelegate::applicationDidFinishLaunching() CCSize resourceSize = CCSizeMake(320, 480); CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils(); - std::vector searchResOrder = pFileUtils->getSearchResolutionsOrder(); + std::vector searchResOrder; string res = "xlarge"; // if (screenSize.height > 1024) // { // resourceSize = CCSizeMake(1280, 1920); - // searchResOrder.insert(searchResOrder.begin(), "resources-xlarge"); + // searchResOrder.push_back("resources-xlarge"); // res = "xlarge"; // } // else if (screenSize.height > 960) { resourceSize = CCSizeMake(640, 960); - searchResOrder.insert(searchResOrder.begin(), "resources-large"); + searchResOrder.push_back("resources-large"); res = "large"; } else if (screenSize.height > 480) { resourceSize = CCSizeMake(480, 720); - searchResOrder.insert(searchResOrder.begin(), "resources-medium"); + searchResOrder.push_back("resources-medium"); res = "medium"; } else { resourceSize = CCSizeMake(320, 568); - searchResOrder.insert(searchResOrder.begin(), "resources-small"); + searchResOrder.push_back("resources-small"); res = "small"; } diff --git a/samples/Lua/HelloLua/proj.linux/main.cpp b/samples/Lua/HelloLua/proj.linux/main.cpp index bb6061e8fe..9299d8ff3f 100644 --- a/samples/Lua/HelloLua/proj.linux/main.cpp +++ b/samples/Lua/HelloLua/proj.linux/main.cpp @@ -10,24 +10,10 @@ USING_NS_CC; -// 500 is enough? -#define MAXPATHLEN 500 - int main(int argc, char **argv) -{ - // get application path - int length; - char fullpath[MAXPATHLEN]; - length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); - fullpath[length] = '\0'; - - std::string resourcePath = fullpath; - resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); - resourcePath += "/../../../Resources/"; - +{ // create the application instance AppDelegate app; - CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str()); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setFrameSize(960, 640); return CCApplication::sharedApplication()->run(); diff --git a/samples/Lua/TestLua/proj.linux/main.cpp b/samples/Lua/TestLua/proj.linux/main.cpp index 71776a9eba..c1ce114a4e 100644 --- a/samples/Lua/TestLua/proj.linux/main.cpp +++ b/samples/Lua/TestLua/proj.linux/main.cpp @@ -11,25 +11,11 @@ USING_NS_CC; -// 500 is enough? -#define MAXPATHLEN 500 - int main(int argc, char **argv) -{ - // get application path - int length; - char fullpath[MAXPATHLEN]; - length = readlink("/proc/self/exe", fullpath, sizeof(fullpath)); - fullpath[length] = '\0'; - - std::string resourcePath = fullpath; - resourcePath = resourcePath.substr(0, resourcePath.find_last_of("/")); - resourcePath += "/../../../Resources/"; - +{ // create the application instance AppDelegate app; - CCApplication::sharedApplication()->setResourceRootPath(resourcePath.c_str()); CCEGLView* eglView = CCEGLView::sharedOpenGLView(); - eglView->setFrameSize(480, 320); + eglView->setFrameSize(800, 480); return CCApplication::sharedApplication()->run(); }