RUResLoader.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // 资源加载类
  3. //
  4. #ifndef RUResLoader_h
  5. #define RUResLoader_h
  6. #include "cocos2d.h"
  7. #include "RUDefine.h"
  8. NS_RU_BEGIN
  9. enum class kLoaderState {
  10. WAIT = 0, //等待加载
  11. LOADING, //加载中
  12. PAUSE, //暂停加载
  13. LOADED //加载完成
  14. };
  15. #define gResLoader(key) redutils::ResLoader::instance(key)
  16. class ResLoader : public Ref {
  17. public:
  18. static ResLoader* instance(std::string key4Loader);
  19. // 增加各种资源提前加载
  20. void addImgs2Load(const vector<string>& imgs);
  21. void addPlists2Load(const vector<string>& plists);
  22. void addAudios2Load(const vector<string>& audios);
  23. void addShaders2Compile(const vector<vector<string>>& shaders);
  24. void addFuncs2Load(const std::vector<std::function<void(void)>>& funcs);
  25. // 开始同步加载资源
  26. void startLoad();
  27. // 开始异步加载资源
  28. void startLoadAsync(std::function<void(float)> cbUpdateProgress, std::function<void()> cbWhileFinished);
  29. // 加载是否完成
  30. bool isFinished();
  31. // 暂停、重启加载
  32. void pauseLoading();
  33. void resumeLoading();
  34. private:
  35. ResLoader();
  36. ~ResLoader();
  37. void _imgTextureAsyncCallback(cocos2d::Texture2D* texture);
  38. void _plistImageAsyncCallback(cocos2d::Texture2D* texture);
  39. void _loadNextResourceAsyns();
  40. private:
  41. static std::unordered_map<std::string, ResLoader*> _gResLoaders;
  42. std::string _loaderName; //仅起log输出用
  43. std::function<void()> _cbWhileFinihsed = nullptr;
  44. std::function<void(float)> _cbUpdateProgress = nullptr;
  45. // 资源纯图片
  46. vector<string> _vecImgs;
  47. int _idx4Img = 0;
  48. // 资源plist
  49. vector<string> _vecPlists;
  50. int _idx4Plist = 0;
  51. // shader的预编译
  52. vector<vector<string>> _cfgShaders;
  53. int _idx4Shader = 0;
  54. std::string _strFrag;
  55. std::string _strVert;
  56. // 音效文件的加载
  57. vector<string> _vecSounds;
  58. int _idx4Sound = 0;
  59. // 函数加载
  60. std::vector<std::function<void(void)>> _vecFuncs;
  61. int _idx4Func = 0;
  62. kLoaderState _state = kLoaderState::WAIT;
  63. int _progresssTotal = 0;
  64. int _progressNow = 0;
  65. };
  66. NS_RU_END
  67. #endif /* RUResLoader_h */