ResourceManager.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "ResourceManager.h"
  2. #include "Resources/Texture.h"
  3. #include "Resources/Material.h"
  4. #include "Resources/ShaderProg.h"
  5. #include "Mesh.h"
  6. #include "Skeleton.h"
  7. #include "SkelAnim.h"
  8. #include "LightRsrc.h"
  9. #include "ParticleEmitterRsrc.h"
  10. #include "Script.h"
  11. #include "Model.h"
  12. #include "Skin.h"
  13. #include "DummyRsrc.h"
  14. #include "Resources/Image.h"
  15. #include "Core/Logger.h"
  16. //==============================================================================
  17. // Constructor =
  18. //==============================================================================
  19. ResourceManager::ResourceManager()
  20. {}
  21. //==============================================================================
  22. // Destructor =
  23. //==============================================================================
  24. ResourceManager::~ResourceManager()
  25. {}
  26. // Because we are bored to write the same
  27. #define SPECIALIZE_TEMPLATE_STUFF(type__, container__) \
  28. template<> \
  29. ResourceManager::Types<type__>::Container& ResourceManager::choseContainer<type__>() \
  30. { \
  31. return container__; \
  32. } \
  33. \
  34. template<> \
  35. void ResourceManager::deallocRsrc<type__>(type__* rsrc) \
  36. { \
  37. delete rsrc; \
  38. }
  39. //SPECIALIZE_TEMPLATE_STUFF(Texture, textures)
  40. SPECIALIZE_TEMPLATE_STUFF(ShaderProg, shaderProgs)
  41. SPECIALIZE_TEMPLATE_STUFF(Material, materials)
  42. SPECIALIZE_TEMPLATE_STUFF(Mesh, meshes)
  43. SPECIALIZE_TEMPLATE_STUFF(Skeleton, skeletons)
  44. SPECIALIZE_TEMPLATE_STUFF(SkelAnim, skelAnims)
  45. SPECIALIZE_TEMPLATE_STUFF(LightRsrc, lightProps)
  46. SPECIALIZE_TEMPLATE_STUFF(ParticleEmitterRsrc, particleEmitterProps)
  47. SPECIALIZE_TEMPLATE_STUFF(Script, scripts)
  48. SPECIALIZE_TEMPLATE_STUFF(Model, models)
  49. SPECIALIZE_TEMPLATE_STUFF(Skin, skins)
  50. SPECIALIZE_TEMPLATE_STUFF(DummyRsrc, dummies)
  51. //==============================================================================
  52. // Texture Specializations =
  53. //==============================================================================
  54. template<>
  55. ResourceManager::Types<Texture>::Container& ResourceManager::choseContainer<Texture>()
  56. {
  57. return textures;
  58. }
  59. template<>
  60. void ResourceManager::deallocRsrc<Texture>(Texture* rsrc)
  61. {
  62. if(rsrc != dummyTex.get() && rsrc != dummyNormTex.get())
  63. {
  64. delete rsrc;
  65. }
  66. }
  67. template<>
  68. void ResourceManager::allocAndLoadRsrc(const char* filename, Texture*& ptr)
  69. {
  70. // Load the dummys
  71. if(dummyTex.get() == NULL)
  72. {
  73. dummyTex.reset(new Texture);
  74. dummyTex->load("engine-rsrc/dummy.png");
  75. }
  76. if(dummyNormTex.get() == NULL)
  77. {
  78. dummyNormTex.reset(new Texture);
  79. dummyNormTex->load("engine-rsrc/dummy.norm.png");
  80. }
  81. // Send a loading request
  82. rsrcAsyncLoadingReqsHandler.sendNewLoadingRequest(filename, &ptr);
  83. // Point to the dummy for now
  84. std::string fname(filename);
  85. size_t found = fname.find("norm.");
  86. if(found != std::string::npos)
  87. {
  88. ptr = dummyNormTex.get();
  89. }
  90. else
  91. {
  92. ptr = dummyTex.get();
  93. }
  94. }