123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- //
- // 资源加载类
- //
- #ifndef RUResLoader_h
- #define RUResLoader_h
- #include "cocos2d.h"
- #include "RUDefine.h"
- NS_RU_BEGIN
- enum class kLoaderState {
- WAIT = 0, //等待加载
- LOADING, //加载中
- PAUSE, //暂停加载
- LOADED //加载完成
- };
- #define gResLoader(key) redutils::ResLoader::instance(key)
- class ResLoader : public Ref {
- public:
- static ResLoader* instance(std::string key4Loader);
-
- // 增加各种资源提前加载
- void addImgs2Load(const vector<string>& imgs);
- void addPlists2Load(const vector<string>& plists);
- void addAudios2Load(const vector<string>& audios);
- void addShaders2Compile(const vector<vector<string>>& shaders);
- void addFuncs2Load(const std::vector<std::function<void(void)>>& funcs);
- // 开始同步加载资源
- void startLoad();
- // 开始异步加载资源
- void startLoadAsync(std::function<void(float)> cbUpdateProgress, std::function<void()> cbWhileFinished);
-
- // 加载是否完成
- bool isFinished();
-
- // 暂停、重启加载
- void pauseLoading();
- void resumeLoading();
-
- private:
- ResLoader();
- ~ResLoader();
- void _imgTextureAsyncCallback(cocos2d::Texture2D* texture);
- void _plistImageAsyncCallback(cocos2d::Texture2D* texture);
- void _loadNextResourceAsyns();
-
- private:
- static std::unordered_map<std::string, ResLoader*> _gResLoaders;
- std::string _loaderName; //仅起log输出用
- std::function<void()> _cbWhileFinihsed = nullptr;
- std::function<void(float)> _cbUpdateProgress = nullptr;
-
- // 资源纯图片
- vector<string> _vecImgs;
- int _idx4Img = 0;
-
- // 资源plist
- vector<string> _vecPlists;
- int _idx4Plist = 0;
- // shader的预编译
- vector<vector<string>> _cfgShaders;
- int _idx4Shader = 0;
- std::string _strFrag;
- std::string _strVert;
-
- // 音效文件的加载
- vector<string> _vecSounds;
- int _idx4Sound = 0;
-
- // 函数加载
- std::vector<std::function<void(void)>> _vecFuncs;
- int _idx4Func = 0;
-
- kLoaderState _state = kLoaderState::WAIT;
-
- int _progresssTotal = 0;
- int _progressNow = 0;
- };
- NS_RU_END
- #endif /* RUResLoader_h */
|