This commit is contained in:
minggo 2013-01-28 19:45:51 +08:00
commit dab38a0254
33 changed files with 602 additions and 129 deletions

View File

@ -235,6 +235,7 @@ protected:
std::vector<std::string> m_searchResolutionsOrderArray;
std::vector<std::string> m_searchPathArray;
std::string m_strDefaultResRootPath;
};
// end of platform group

View File

@ -389,7 +389,20 @@ std::string CCFileUtils::getNewFilename(const char* pszFileName)
void CCFileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
{
m_searchResolutionsOrderArray = searchResolutionsOrder;
bool bExistDefault = false;
m_searchResolutionsOrderArray.clear();
for (std::vector<std::string>::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<std::string>& CCFileUtils::getSearchResolutionsOrder()
@ -397,11 +410,6 @@ const std::vector<std::string>& CCFileUtils::getSearchResolutionsOrder()
return m_searchResolutionsOrderArray;
}
void CCFileUtils::setSearchPath(const std::vector<std::string>& searchPaths)
{
m_searchPathArray = searchPaths;
}
const std::vector<std::string>& CCFileUtils::getSearchPath()
{
return m_searchPathArray;

View File

@ -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<std::string, std::string> 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<std::string> searchPaths = this->getSearchPath();;
searchPaths.insert(searchPaths.begin(), pszResourceDirectory);
this->setSearchPath(searchPaths);
}
void CCFileUtils::setSearchPath(const std::vector<std::string>& searchPaths)
{
bool bExistDefaultRootPath = false;
m_searchPathArray.clear();
for (std::vector<std::string>::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()

View File

@ -145,6 +145,7 @@ NS_CC_BEGIN
static std::map<std::string, std::string> 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<std::string>& searchResolutionsOrder)
{
m_searchResolutionsOrderArray = searchResolutionsOrder;
bool bExistDefault = false;
m_searchResolutionsOrderArray.clear();
for (std::vector<std::string>::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<std::string>& CCFileUtils::getSearchResolutionsOrder()
@ -192,7 +206,20 @@ const std::vector<std::string>& CCFileUtils::getSearchResolutionsOrder()
void CCFileUtils::setSearchPath(const std::vector<std::string>& searchPaths)
{
m_searchPathArray = searchPaths;
bool bExistDefault = false;
m_searchPathArray.clear();
for (std::vector<std::string>::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<std::string>& 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.

View File

@ -9,6 +9,7 @@
#include <sys/time.h>
#include <string>
#include "CCDirector.h"
#include "platform/CCFileUtils.h"
NS_CC_BEGIN

View File

@ -10,6 +10,7 @@
#include "platform/CCCommon.h"
#include "platform/CCApplicationProtocol.h"
#include <string>
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

View File

@ -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<std::string>::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<std::string, std::string>(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<std::string> searchPaths = this->getSearchPath();;
searchPaths.insert(searchPaths.begin(), pszResourceDirectory);
this->setSearchPath(searchPaths);
}
void CCFileUtils::setSearchPath(const std::vector<std::string>& searchPaths)
{
bool bExistDefaultRootPath = false;
m_searchPathArray.clear();
for (std::vector<std::string>::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

View File

@ -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<std::string, std::string> s_fullPathCache;
CCFileUtils* CCFileUtils::sharedFileUtils()
@ -184,7 +185,20 @@ bool CCFileUtils::init()
void CCFileUtils::setSearchResolutionsOrder(const std::vector<std::string>& searchResolutionsOrder)
{
m_searchResolutionsOrderArray = searchResolutionsOrder;
bool bExistDefault = false;
m_searchResolutionsOrderArray.clear();
for (std::vector<std::string>::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<std::string>& CCFileUtils::getSearchResolutionsOrder()
@ -194,7 +208,20 @@ const std::vector<std::string>& CCFileUtils::getSearchResolutionsOrder()
void CCFileUtils::setSearchPath(const std::vector<std::string>& searchPaths)
{
m_searchPathArray = searchPaths;
bool bExistDefault = false;
m_searchPathArray.clear();
for (std::vector<std::string>::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<std::string>& 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.

View File

@ -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

View File

@ -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();
}

View File

@ -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 \

View File

@ -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<std::string> searchPaths;
searchPaths.push_back("hd");
pFileUtils->setSearchPath(searchPaths);
pDirector->setContentScaleFactor(resourceSize.height/designSize.height);
}

View File

@ -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<string> searchPaths = m_defaultSearchPathArray;
searchPaths.insert(searchPaths.begin(), "Misc");
sharedFileUtils->setSearchPath(searchPaths);
m_defaultResolutionsOrderArray = sharedFileUtils->getSearchResolutionsOrder();
vector<string> 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<string> 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<string> 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";
}

View File

@ -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<string> m_defaultSearchPathArray;
vector<string> m_defaultResolutionsOrderArray;
};
class TestSearchPath : public FileUtilsDemo
{
public:
virtual void onEnter();
virtual void onExit();
virtual string title();
virtual string subtitle();
private:
vector<string> m_defaultSearchPathArray;
vector<string> m_defaultResolutionsOrderArray;
};
class TestFilenameLookup : public FileUtilsDemo
{
public:
virtual void onEnter();
virtual void onExit();
virtual string title();
virtual string subtitle();
};
#endif /* __FILEUTILSTEST_H__ */

View File

@ -120,6 +120,9 @@ static TestScene* CreateTestScene(int nIdx)
pScene = new ClippingNodeTestScene();
break;
#endif
case TEST_FILEUTILS:
pScene = new FileUtilsTestScene();
break;
default:
break;
}

View File

@ -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

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -0,0 +1 @@
This is a test

View File

@ -1 +1 @@
e5a2503bbe8aa034d6b26c86239afd82a4dc8758
5e326e12903a5d84168a8bc4b8bc6735e1b4c1c8

View File

@ -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 \

View File

@ -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();
}

View File

@ -1 +1 @@
3d27ac9829ac45f2433d903b6b4f359a8e20636f
3c6884ef6d8f01816d6bffe1a3bf124b35cf4432

View File

@ -67,31 +67,31 @@ bool AppDelegate::applicationDidFinishLaunching()
CCSize resourceSize = CCSizeMake(320, 480);
CCFileUtils* pFileUtils = CCFileUtils::sharedFileUtils();
std::vector<std::string> searchResOrder = pFileUtils->getSearchResolutionsOrder();
std::vector<std::string> 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";
}

View File

@ -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();

View File

@ -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();
}