Manager.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include "StdInc.h"
  2. #include <map>
  3. #include "Manager.h"
  4. #include "Animations.h"
  5. #include "Images.h"
  6. #include "FilesHeaders.h"
  7. #include "../../lib/Filesystem/CResourceLoader.h"
  8. #include "../../lib/Filesystem/CFileInfo.h"
  9. namespace Gfx
  10. {
  11. class Resources
  12. {
  13. friend class CManager;
  14. static std::map<std::string, PImage> images;
  15. static std::map<std::string, PAnimation> anims;
  16. };
  17. std::map<std::string, PImage> Resources::images;
  18. std::map<std::string, PAnimation> Resources::anims;
  19. PImage CManager::getImage(const std::string& fname)
  20. {
  21. PImage& img_ptr = Resources::images[fname];
  22. if (img_ptr) return img_ptr;
  23. ResourceID resImageId("DATA/" + fname, EResType::IMAGE);
  24. auto stream = CResourceHandler::get()->load(resImageId);
  25. auto streamSize = stream->getSize();
  26. if (streamSize < H3PCX_HEADER_SIZE) return nullptr;
  27. if (streamSize > 0x10000000) streamSize = 0x10000000;
  28. std::unique_ptr<ui8[]> data(new ui8[(size_t)streamSize]);
  29. auto readSize = stream->read(data.get(), streamSize);
  30. assert(readSize == stream->getSize());
  31. CImage* img_tmp = CImage::makeFromPCX(*(SH3PcxFile*)data.get(), (size_t)readSize);
  32. if (img_tmp == nullptr)
  33. {
  34. CFileInfo info(CResourceHandler::get()->getResourceName(resImageId));
  35. img_tmp = CImage::makeBySDL(data.get(), (size_t)readSize, info.getExtension().c_str()+1);
  36. if (img_tmp == nullptr)
  37. {
  38. tlog1 << "Gfx: Image " << fname << " not loaded!\n";
  39. return nullptr;
  40. }
  41. }
  42. return img_ptr = PImage(img_tmp);
  43. }
  44. PAnimation CManager::getAnimation(const std::string& fname)
  45. {
  46. PAnimation& anim_ptr = Resources::anims[fname];
  47. if (anim_ptr) return anim_ptr;
  48. ResourceID resAnimId("SPRITES/" + fname, EResType::ANIMATION);
  49. auto stream = CResourceHandler::get()->load(resAnimId);
  50. auto streamSize = stream->getSize();
  51. if (streamSize < H3DEF_HEADER_SIZE) return nullptr;
  52. if (streamSize > 0x7FFFFFF0) streamSize = 0x7FFFFFF0;
  53. std::unique_ptr<ui8[]> data(new ui8[(size_t)streamSize]);
  54. auto readSize = stream->read(data.get(), streamSize);
  55. assert(readSize == stream->getSize());
  56. CAnimation* anim_tmp = CAnimation::makeFromDEF(*(SH3DefFile*)data.get(), (size_t)readSize);
  57. if (anim_tmp == nullptr)
  58. {
  59. tlog1 << "Gfx: Animation " << fname << " not loaded!\n";
  60. return nullptr;
  61. }
  62. return anim_ptr = PAnimation(anim_tmp);
  63. }
  64. }