issue #1683: Fixed compilation errors for linux.

This commit is contained in:
James Chen 2013-01-28 18:32:34 +08:00
parent 632f99b611
commit 733a334ec2
6 changed files with 62 additions and 76 deletions

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

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

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

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