当你创建TMXTiledMap* tilemap=TMXTiledMap::create("test1.tmx")或Sprite *sprite=Sprite("HelloWorld.p-ng"),有没有产生这样的疑问--为什么把资源test1.tmx和HelloWorld.png放在项目目录下的Resources文件中即可直接引用而不用标明具体路径,并且可以在多个平台下引用?或许很多人就会这样说:“别人告诉我放在这个文件夹中就可以了,我自己使用确实可行,也没有出错,我就没有多去探究了”。如果你想知道这具体原因,就要阅读下面的分析了。如果你并不关心其原因,你可以关闭这个网页了。
我以TMXTiledMap::Create函数为讲解对象。
首先转到TMXTiledMap::Create的定义中,其定义是在CCFastTMXTiledMap.cpp文件中,代码1如下。其目录是E:mycoscos2dtest2cocos2dcocos2d中,这就说明这是与具体平台无关的,后面我们会看到已具体平台相关的代码。 代码1:
- <spanstyle="font-size:18px;">TMXTiledMap*TMXTiledMap::create(conststd::string&tmxFile)
- {
- TMXTiledMap*ret=newTMXTiledMap();
- if(ret->initWithTMXFile(tmxFile))
- {
- ret->autorelease();
- returnret;
- }
- CC_SAFE_DELETE(ret);
- returnnullptr;
- }</span>
在代码1中,我们可以看到先创建一个TMXTileMap对象,然后初始化,最后加入自动释放池。如果想了解cosco2d-x3.2内存的管理,请继续关注我的博客。在这里我们也完全没有看到关于路径相关的字符串。其中让人觉得,路径设置有可能在TMXTiledMap()::initWithTMXFile()中,于是我们继续转到TMXTiledMap()::initWithTMXFile()定义中。代码2如下。 代码2:
<spanstyle="font-size:18px;">boolTMXTiledMap::initWithTMXFile( CCASSERT(tmxFile.size()>0,"FastTMXTiledMap:tmxfileshouldnotbeempty");
- setContentSize(Size::ZERO);
- TMXMapInfo*mapInfo=TMXMapInfo::create(tmxFile);
- if
(!mapInfo)
- returnfalse;
- }
- CCASSERT(!mapInfo->getTilesets().empty(),"FastTMXTiledMap:Mapnotfound.Pleasecheckthefilename.");
- buildWithMapInfo(mapInfo);
- true;
- }</span>
在代码2中,我们也没有发现关于路径字符串的信息。再看看代码1中只调用了此函数,我们由此推断路径字符串设定在此函数或此函数的调用中的概率非常大。在代码2中,我们可以看到两个函数的调用,TMXMapInfo::create()和buildWithMapInfo(),显然,TMXMapInfo::create的函数名让我们觉得路径字符串的设置在其中概率更大,因此我们转到TMXMapInfo::create代码定义中,其代码在CCTMXXMLParser.cpp文件中,如代码3。其目录是E:mycoscos2dtest2cocos2dcocos2d,这就说明以平台无关。 代码3:
<spanstyle="font-size:18px;">TMXMapInfo*TMXMapInfo::create( TMXMapInfo*ret=newTMXMapInfo();
- }</span>
在代码3中,如同代码1的分析,我们要转到TMXMapInfo::initWithTMXFile()的定义中,如代码4。 代码4:
boolTMXMapInfo::initWithTMXFile( internalInit(tmxFile,"");
- return
parseXMLFile(_TMXFileName.c_str());
- </span>
在代码4中,我们还是没有看到路径字符串的设定,如同代码2的分析,我们要转到同一个类中的internalInit()函数中,如代码5。 代码5:
<spanstyle="font-size:18px;">voidTMXMapInfo::internalInit(conststd::string&tmxFileName,conststd::string&resourcePath)
- if(tmxFileName.size()>0)
- _TMXFileName=FileUtils::getInstance()->fullPathForFilename(tmxFileName);
- if(resourcePath.size()>0)
- _resources=resourcePath;
- ...
- }</span>
在代码5中,我们终于看到fullpath的字样了,这就说明路径字符串的设定就在眼前了。于是我们就要转到FileUtils::getInstance()->fullPathForFilename()函数定义中,其类定义在CCFileUtils.cpp中,如代码6,由于代码有点长,只贴出关键部分。此时我们看看CCFileUtils.cpp的路径,我们会发现路径是E:mycoscos2dtest2cocos2dcocosplatform,到这里我们终于看到platform这个关键字,这就说明已经到了与平台相关的代码中。 代码6:
<spanstyle="font-size:18px;">std::stringFileUtils::fullPathForFilename(conststd::string&filename)
- std::stringfullpath;
- for(autosearchIt=_searchPathArray.cbegin();searchIt!=_searchPathArray.cend();++searchIt)
- for(autoresolutionIt=_searchResolutionsOrderArray.cbegin();resolutionIt!=_searchResolutionsOrderArray.cend();++resolutionIt)
- fullpath=this->getPathForFilename(newFilename,*resolutionIt,*searchIt);
-
- if(fullpath.length()>0)
-
- _fullPathCache.insert(std::make_pair(filename,fullpath));
- returnfullpath;
- }</span>
在代码6中,我们看到了this->getPathForFilename(),你会不会觉得奇怪,其他函数的调用都没有加this,就它加了this,具体原因在后面讲解。在这里,显然我们对this->getPathForFilename()的兴趣最大,于是我们就转到其定义中如代码7,其实从后面讲解中,可以看到代码是先转到与平台一致的FileUtilsxxx::getPathForFilename()中,然后再由平台FileUtilsxxx::getPathForFileName 调用FileUtils::getPathForFilename()。平台的getPathForFillname作用是把路径格式转化为符合平台路径的格式。 代码7:
<spanstyle="font-size:18px;">std::stringFileUtils::getPathForFilename(conststd::string&filename,153); background-color:inherit; font-weight:bold">conststd::string&resolutionDirectory,153); background-color:inherit; font-weight:bold">conststd::string&searchPath)
- std::stringfile=filename;
- std::stringfile_path="";
- size_tpos=filename.find_last_of("/");
- if(pos!=std::string::npos)
- file_path=filename.substr(0,pos+1);
- file=filename.substr(pos+1);
- //searchPath+file_path+resourceDirectory
- std::stringpath=searchPath;
- path+=file_path;
- path+=resolutionDirectory;
- path=getFullPathForDirectoryAndFilename(path,file);
-
- returnpath;
- }</span>
在代码 7中,我们看到这个函数作用是把资源路径和一开始create的文件名相连接。我们转到getFullPathFor- DirectoryAndFilename()函数定义中,如代码8。 代码8:
<spanstyle="font-size:18px;">std::stringFileUtils::getFullPathForDirectoryAndFilename(conststd::string&directory,0); background-color:inherit">//getdirectory+filename,safelyadding'/'asnecessary
- std::stringret=directory;
- if(directory.size()&&directory[directory.size()-1]!='/'){
- ret+='/';
- ret+=filename;
-
- //ifthefiledoesn'texist,returnanemptystring
- if(!isFileExistInternal(ret)){
- ret="";
- returnret;
- }</span>
在代码8中,我们看到是字符串的连接,根本没有看到资源路径的获取。于是我们就回到代码7中。 在代码7中,我们看到searchPath变量,从代码注释中可以看到//searchPath + file_path + resourceDirectory,就可以发现searchPath就是我们路径的名称。 在代码7中我们看到也只是字符串的连接,而且searchPath是作为参数传入的。于是我们就回到代码6中。 在代码6中,代码7中的searchPath只是_searchPathArray中的一个迭代器。好,这就说明路径就藏_searchPathArray中,最后我们在CCFileUtils.cpp文件中找到了FileUtils::init(),如代码9。 代码9:
boolFileUtils::init()
- _searchPathArray.push_back(_defaultResRootPath);
- _searchResolutionsOrderArray.push_back("");
- true;
- }</span>
在代码9中,我们看到了_searchPathArray.push_back(_defaultResRootPath),好的,这就是把路径放进容器中。而又是什么函数调用init()函数?当然是调用代码6中的函数的变量,也我们就回到代码5中this->getInstance()返回的变量。 于是我们就转到this->getInstance代码中,此时的目录是E:mycoscos2dtest2cocos2dcocosplatformwin32CCFileUtilsWin32.cpp,好的,终于转到与平台相关的目录中了。注意我用的VS2012来开发,所以才转到win32这个目录中,如果你是Eclipse来开发,你就转到E:mycoscos2dtest2cocos2dcocosplatformandroidCCFileUtils-Android.cpp这个目录中。如果是IOS,你就转到E:mycoscos2dtest2cocos2dcocosplatformappleCCFileUtils-Apple.mm。这是为什么转到相关的平台的CCFileUtilsxxx.cpp中呢,这是因为在每个与平台相关的头文件中有#if CC_TARGET_PLATFORM == CC_PLATFORM_XXX的条件预处理,也这样说在那个平台就包含那个头文件。例如:在CCFileUtilsWin32.h文件中有代码10。这是很巧妙的技巧! 代码10:
<spanstyle="font-size:18px;">#include"base/CCPlatformConfig.h"
- #ifCC_TARGET_PLATFORM==CC_PLATFORM_WIN32
- #include"CCStdC.h"
- #include"platform/CCCommon.h"
- #include"platform/CCApplicationProtocol.h"
- #include<string></span>
我们来看看this->getInstance()的代码,如代码11。此时的FileUtils* FileUtils::getInstance()是在CCFileUtils-Win32.cpp中的而不是在CCFileUtils.cpp中。这是为了夸平台,s_sharedFileUtils是在FileUtils中定义的。FileUtils-Win32是继承FileUtils的。这是很巧妙的技巧! 代码11:
<spanstyle="font-size:18px;">FileUtils*FileUtils::getInstance()
- if(s_sharedFileUtils==nullptr)
- s_sharedFileUtils=newFileUtilsWin32();
- if(!s_sharedFileUtils->init())
- deletes_sharedFileUtils;
- s_sharedFileUtils=nullptr;
- CCLOG("ERROR:CouldnotinitCCFileUtilsWin32");
- returns_sharedFileUtils;
- }</span>
在代码11中,我们看到s_sharedFileUtils->init(),于是转到定义处,由于此时s_sharedFileUtils是从FileUtilsWin32转换而来的,而且在FileUtils中init()为虚函数,所以init()会转到FileUtilsWin32::init(),而不是FileUtils->init(),这是c++的多态。FileUtilsWin32::init()如代码12。 代码12:
boolFileUtilsWin32::init()
- _checkPath();
- _defaultResRootPath=s_resourcePath;
- returnFileUtils::init();
- }</span>
在代码12中,我们看到_checkPath()函数,那就转到它的定义看看,如代码13。 代码13:
staticvoid_checkPath()
- if(0==s_resourcePath.length())
- WCHARutf16Path[CC_MAX_PATH]={0};
- GetCurrentDirectoryW(sizeof(utf16Path)-1,utf16Path);
- charutf8Path[CC_MAX_PATH]={0};
- intnNum=WideCharToMultiByte(CP_UTF8,utf16Path,-1,utf8Path,153); background-color:inherit; font-weight:bold">sizeof(utf8Path),nullptr,nullptr);
- s_resourcePath=convertPathFormatToUnixStyle(utf8Path);
- s_resourcePath.append("/");
- }</span>
好吧,在这里我们终于看到win32平台获得路径的函数GetCurrentDirectoryW(sizeof(utf16Path)-1,utf16Path),这个函数就是获得资源路径的,例如路径E:mycoscos2dtest2Resources。到此为止,我们终于找到这个设置资源路径的函数了。在Android平台的代码如代码14,每次在用Eclipse导入项目,会先把Resource的资源复制到E:mycoscos-2dtest2proj.androidassets这个路径中,以保持同步。 代码14:
boolFileUtilsAndroid::init()
- _defaultResRootPath="assets/";
- returnFileUtils::init();
- }</span>
回到代码12中,FileUtilsWin32::init()最后还是调用了FileUtils::init(),那我们来看看FileUtils::init()的定义,如代码15。这是很巧妙的机巧! 代码15:
</span>
在代码15中,路径字符串加入了_searchPathArray容器中! 我们现在回到代码6中,this->getPathForFilename(newFilename,*searchIt),为什么加入this?那就要看看代码6的函数调用者代码5,在代码5中有FileUtils::getInstance(),它返回的是由FileUtilsWin32转换而来的,而FileUtils中getPathForFilename为虚函数,根据C++多态,所以会调用FileUtilsWin32::getPathForFilename()。如代码16。 代码16:
<spanstyle="font-size:18px;">std::stringFileUtilsWin32::getPathForFilename( std::stringunixFileName=convertPathFormatToUnixStyle(filename);
- std::stringunixResolutionDirectory=convertPathFormatToUnixStyle(resolutionDirectory);
- std::stringunixSearchPath=convertPathFormatToUnixStyle(searchPath);
- return
FileUtils::getPathForFilename(unixFileName,unixResolutionDirectory,unixSearchPath);
- }</span>
在代码16中,我们看到FileUtilsWin32::getPathForFilename()作用是把路径转换为符合平台的路径格式。 到此为止,我们详细讲解了cocos2d-x3.2如何通过FileUtils类来实现把资源放在Resources文件目录下达到多平台的引用。 最后,我们最后用一张图片作为总结。 如需转载,请标明出处,http://www.52php.cn/article/p-odcwpfdx-sw.html
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|