Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x3.2源码分析(一)类FileUtils--实现把资源放在Resources文件目录下达到多平台的引用大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

当你创建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:\mycoscos2d\test2\cocos2d\cocos\2d中,这就说明这是与具体平台无关的,后面我们会看到已具体平台相关的代码。
代码1:
  1. <spanstyle="font-size:18px;">TMXTiledMap*TMXTiledMap::create(conststd::string&tmxFilE)
  2. {
  3. TMXTiledMap*ret=newTMXTiledMap();
  4. if(ret->initWithTMXFile(tmxFilE))
  5. {
  6. ret->autorelease();
  7. returnret;
  8. }
  9. CC_SAFE_deletE(ret);
  10. returnnullptr;
  11. }</span>
在代码1中,我们可以看到先创建一个TMXTileMap对象,然后初始化,最后加入自动释放池。如果想了解cosco2d-x3.2内存的管理,请继续关注我的博客。在这里我们也完全没有看到关于路径相关的字符串。其中让人觉得,路径设置有可能在TMXTiledMap()::initWithTMXFile()中,于是我们继续转到TMXTiledMap()::initWithTMXFile()定义中。代码2如下。
代码2:
copy
    <spanstyle="font-size:18px;">boolTMXTiledMap::initWithTMXFile( CCassERT(tmxFile.size()>0,"FastTMXTiledMap:tmxfileshouldnotbeempty");
  1. setContentSize(Size::ZERO);
  2. TMXMapInfo*mapInfo=TMXMapInfo::create(tmxFilE);
  3. if(!mapInfo)
  4. returnfalse;
  5. }
  6. CCassERT(!mapInfo->getTilesets().empty(),"FastTMXTiledMap:Mapnotfound.Pleasecheckthefilename.");
  7. buildWithMapInfo(mapInfo);
  8. true;
  9. }</span>
在代码2中,我们也没有发现关于路径字符串的信息。再看看代码1中只调用了此函数,我们由此推断路径字符串设定在此函数或此函数的调用中的概率非常大。在代码2中,我们可以看到两个函数的调用,TMXMapInfo::create()和buildWithMapInfo(),显然,TMXMapInfo::create的函数名让我们觉得路径字符串的设置在其中概率更大,因此我们转到TMXMapInfo::create代码定义中,其代码在CCTMXXMLParser.cpp文件中,如代码3。其目录是E:\mycoscos2d\test2\cocos2d\cocos\2d,这就说明以平台无关。
代码3:
copy
    <spanstyle="font-size:18px;">TMXMapInfo*TMXMapInfo::create( TMXMapInfo*ret=newTMXMapInfo();
  1. }</span>
在代码3中,如同代码1的分析,我们要转到TMXMapInfo::initWithTMXFile()的定义中,如代码4。
代码4:
copy
    boolTMXMapInfo::initWithTMXFile( internalInit(tmxFile,"");
  1. returnparseXMLFile(_TMXFilename.c_str());
  2. </span>
在代码4中,我们还是没有看到路径字符串的设定,如同代码2的分析,我们要转到同一个类中的internalInit()函数中,如代码5。
代码5:
copy @H_273_262@
    <spanstyle="font-size:18px;">voidTMXMapInfo::internalInit(conststd::string&tmxFilename,conststd::string&resourcePath)
  1. if(tmxFilename.size()>0)
  2. _TMXFilename=FileUtils::geTinstance()->fullPathForFilename(tmxFileName);
  3. if(resourcePath.size()>0)
  4. _resources=resourcePath;
  5. ...
  6. }</span>
在代码5中,我们终于看到fullpath的字样了,这就说明路径字符串的设定就在眼前了。于是我们就要转到FileUtils::geTinstance()->fullPathForFilename()函数定义中,其类定义在CCFileUtils.cpp中,如代码6,由于代码有点长,只贴出关键部分。此时我们看看CCFileUtils.cpp的路径,我们会发现路径是E:\mycoscos2d\test2\cocos2d\cocos\platform,到这里我们终于看到platform这个关键字,这就说明已经到了与平台相关的代码中。
代码6:
copy
    <spanstyle="font-size:18px;">std::stringFileUtils::fullPathForFilename(conststd::string&fileName)
  1. std::stringfullpath;
  2. for(autosearchIt=_searchPathArray.cbegin();searchIt!=_searchPathArray.cend();++searchIt)
  3. for(autoresolutionIt=_searchResolutionsOrderArray.cbegin();resolutionIt!=_searchResolutionsOrderArray.cend();++resolutionIt)
  4. fullpath=this->getPathForFilename(newFilename,*resolutionIt,*searchIt);
  5. if(fullpath.length()>0)
  6. //Usingthefilenamepassedinaskey.
  7. _fullPathCache.insert(std::make_pair(filename,fullpath));
  8. returnfullpath;
  9. }</span>
在代码6中,我们看到了this->getPathForFilename(),你会不会觉得奇怪其他函数的调用都没有加this,就它加了this,具体原因在后面讲解。在这里,显然我们对this->getPathForFilename()的兴趣最大,于是我们就转到其定义中如代码7,其实从后面讲解中,可以看到代码是先转到与平台一致的FileUtiLSXxx::getPathForFilename()中,然后再由平台FileUtiLSXxx::getPathForFilename 调用FileUtils::getPathForFilename()。平台的getPathForFillname作用是把路径格式转化为符合平台路径的格式。
代码7:
copy
    <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)
  1. std::stringfile=filename;
  2. std::stringfile_path="";
  3. @H_115_450@size_tpos=filename.find_last_of("/");
  4. if(pos!=std::string::npos)
  5. file_path=filename.substr(0,pos+1);
  6. file=filename.substr(pos+1);
  7. //searchPath+file_path+resourceDirectory
  8. std::stringpath=searchPath;
  9. path+=file_path;
  10. path+=resolutionDirectory;
  11. path=getFullPathForDirectoryAndFilename(path,filE);
  12. //CCLOG("getPathForFilename,fullPath=%s",path.c_str());
  13. returnpath;
  14. }</span>
在代码 7中,我们看到这个函数作用是把资源路径和一开始create的文件名相连接。我们转到getFullPathFor-
DirectoryAndFilename()函数定义中,如代码8。
代码8:
copy
    <spanstyle="font-size:18px;">std::stringFileUtils::getFullPathForDirectoryAndFilename(conststd::string&directory,0); BACkground-color:inherit">//getdirectory+filename,safelyadding'/'asnecessary
  1. std::stringret=directory;
  2. if(directory.size()&&directorY[directory.size()-1]!='/'){
  3. ret+='/';
  4. ret+=filename;
  5. //ifthefiledoesn'texist,returnanemptyString
  6. if(!isFileExisTinternal(ret)){
  7. ret="";
  8. returnret;
  9. }</span>
在代码8中,我们看到是字符串的连接,根本没有看到资源路径的获取。于是我们就回到代码7中。
在代码7中,我们看到searchPath变量,从代码注释中可以看到//searchPath + file_path + resourceDirectory,就可以发现searchPath就是我们路径的名称。
在代码7中我们看到也只是字符串的连接,而且searchPath是作为参数传入的。于是我们就回到代码6中。
在代码6中,代码7中的searchPath只是_searchPathArray中的一个迭代器。好,这就说明路径就藏_searchPathArray中,最后我们在CCFileUtils.cpp文件中找到了FileUtils::init(),如代码9。
代码9:
copy @H_315_618@
    boolFileUtils::init()
  1. _searchPathArray.push_BACk(_defaultResRootPath);
  2. _searchResolutionsOrderArray.push_BACk("");
  3. true;
  4. }</span>
在代码9中,我们看到了_searchPathArray.push_BACk(_defaultResRootPath),好的,这就是把路径放进容器中。而又是什么函数调用init()函数?当然是调用代码6中的函数的变量,也我们就回到代码5中this->geTinstance()返回的变量。
于是我们就转到this->geTinstance代码中,此时的目录是E:\mycoscos2d\test2\cocos2d\cocos\platform\win32\CCFileUtilsWin32.cpp,好的,终于转到与平台相关的目录中了。注意我用的VS2012来开发,所以才转到win32这个目录中,如果你是Eclipse来开发,你就转到E:\mycoscos2d\test2\cocos2d\cocos\platform\android\CCFileUtils-Android.cpp这个目录中。如果是IOS,你就转到E:\mycoscos2d\test2\cocos2d\cocos\platform\apple\CCFileUtils-Apple.mm。这是为什么转到相关的平台的CCFileUtiLSXxx.cpp中呢,这是因为在每个与平台相关的头文件中有#if CC_TARGET_PLATFORM == CC_PLATFORM_XXX的条件预处理,也这样说在那个平台就包含那个头文件。例如:在CCFileUtilsWin32.h文件中有代码10。这是很巧妙的技巧!
代码10:
copy
    <spanstyle="font-size:18px;">#include"base/CCPlatformConfig.h"
  1. #ifCC_TARGET_PLATFORM==CC_PLATFORM_WIN32
  2. #include"CCStdC.h"
  3. #include"platform/CCCommon.h"
  4. #include"platform/CCApplicationProtocol.h"
  5. #include<String></span>
我们来看看this->geTinstance()的代码,如代码11。此时的FileUtils* FileUtils::geTinstance()是在CCFileUtils-Win32.cpp中的而不是在CCFileUtils.cpp中。这是为了夸平台,s_sharedFileUtils是在FileUtils中定义的。FileUtils-Win32是继承FileUtils的。这是很巧妙的技巧!
代码11:
copy
    <spanstyle="font-size:18px;">FileUtils*FileUtils::geTinstance()
  1. if(s_sharedFileUtils==nullptr)
  2. s_sharedFileUtils=newFileUtilsWin32();
  3. if(!s_sharedFileUtils->init())
  4. deletes_sharedFileUtils;
  5. s_sharedFileUtils=nullptr;
  6. CCLOG("ERROR:CouldnoTinitCCFileUtilsWin32");
  7. returns_sharedFileUtils;
  8. }</span>
在代码11中,我们看到s_sharedFileUtils->init(),于是转到定义处,由于此时s_sharedFileUtils是从FileUtilsWin32转换而来的,而且在FileUtils中init()为虚函数,所以init()会转到FileUtilsWin32::init(),而不是FileUtils->init(),这是c++的多态。FileUtilsWin32::init()如代码12。
代码12:
copy
    boolFileUtilsWin32::init()
  1. _checkPath();
  2. _defaultResRootPath=s_resourcePath;
  3. returnFileUtils::init();
  4. }</span>
在代码12中,我们看到_checkPath()函数,那就转到它的定义看看,如代码13。
代码13:
copy
    staticvoid_checkPath()
  1. if(0==s_resourcePath.length())
  2. WCHARutf16Path[CC_MAX_PATH]={0};
  3. GetCurrentDirectoryW(sizeof(utf16Path)-1,utf16Path);
  4. @H_115_450@charutf8Path[CC_MAX_PATH]={0};
  5. intnNum=WideCharToMultiByte(CP_UTF8,utf16Path,-1,utf8Path,153); BACkground-color:inherit; font-weight:bold">sizeof(utf8Path),nullptr,nullptr);
  6. s_resourcePath=convertPathFormatToUnixStyle(utf8Path);
  7. s_resourcePath.append("/");
  8. }</span>
好吧,在这里我们终于看到win32平台获得路径的函数GetCurrentDirectoryW(sizeof(utf16Path)-1,utf16Path),这个函数就是获得资源路径的,例如路径E:\mycoscos2d\test2\resources。到此为止,我们终于找到这个设置资源路径的函数了。在Android平台的代码如代码14,每次在用Eclipse导入项目,会先把resource的资源复制到E:\mycoscos-2d\test2\proj.android\assets这个路径中,以保持同步。
代码14:
copy
    boolFileUtilsAndroid::init()
  1. _defaultResRootPath="assets/";
  2. returnFileUtils::init();
  3. }</span>
回到代码12中,FileUtilsWin32::init()最后还是调用了FileUtils::init(),那我们来看看FileUtils::init()的定义,如代码15。这是很巧妙的机巧!
代码15:
copy
    </span>
在代码15中,路径字符串加入了_searchPathArray容器中!
我们现在回到代码6中,this->getPathForFilename(newFilename,*searchIt),为什么加入this?那就要看看代码6的函数调用者代码5,在代码5中有FileUtils::geTinstance(),它返回的是由FileUtilsWin32转换而来的,而FileUtils中getPathForFilename为虚函数,根据C++多态,所以会调用FileUtilsWin32::getPathForFilename()。如代码16。
代码16:
copy
    <spanstyle="font-size:18px;">std::stringFileUtilsWin32::getPathForFilename( std::stringunixFilename=convertPathFormatToUnixStyle(fileName);
  1. std::stringunixResolutionDirectory=convertPathFormatToUnixStyle(resolutionDirectory);
  2. std::stringunixSearchPath=convertPathFormatToUnixStyle(searchPath);
  3. returnFileUtils::getPathForFilename(unixFilename,unixResolutionDirectory,unixSearchPath);
  4. }</span>
在代码16中,我们看到FileUtilsWin32::getPathForFilename()作用是把路径转换为符合平台的路径格式。
到此为止,我们详细讲解了cocos2d-x3.2如何通过FileUtils类来实现把资源放在resources文件目录下达到多平台的引用。
最后,我们最后用一张图片作为总结。


如需转载,请标明出处,http://www.voidcn.com/article/p-odcwpfdx-sw.html

大佬总结

以上是大佬教程为你收集整理的cocos2d-x3.2源码分析(一)类FileUtils--实现把资源放在Resources文件目录下达到多平台的引用全部内容,希望文章能够帮你解决cocos2d-x3.2源码分析(一)类FileUtils--实现把资源放在Resources文件目录下达到多平台的引用所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:3cocoscocos2ddfileutilsx3.x下达分析多平台实现引用放在文件源码目录资源
猜你在找的Cocos2d-x相关文章
其他相关热搜词更多
phpJavaPython程序员load如何string使用参数jquery开发安装listlinuxiosandroid工具javascriptcap