RenderHandler.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * RenderHandler.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "RenderHandler.h"
  12. #include "SDLImage.h"
  13. #include "../render/CAnimation.h"
  14. #include "../render/CDefFile.h"
  15. #include "../../lib/json/JsonNode.h"
  16. std::shared_ptr<CDefFile> RenderHandler::getAnimationFile(const AnimationPath & path)
  17. {
  18. auto it = animationFiles.find(path);
  19. if (it != animationFiles.end())
  20. return it->second;
  21. auto result = std::make_shared<CDefFile>(path);
  22. animationFiles[path] = result;
  23. return result;
  24. }
  25. std::shared_ptr<JsonNode> RenderHandler::getJsonFile(const JsonPath & path)
  26. {
  27. auto it = jsonFiles.find(path);
  28. if (it != jsonFiles.end())
  29. return it->second;
  30. auto result = std::make_shared<JsonNode>(path);
  31. jsonFiles[path] = result;
  32. return result;
  33. }
  34. std::shared_ptr<IImage> RenderHandler::loadImage(const AnimationPath & path, int frame, int group)
  35. {
  36. AnimationLocator locator{path, frame, group};
  37. auto it = animationFrames.find(locator);
  38. if (it != animationFrames.end())
  39. return it->second;
  40. auto defFile = getAnimationFile(path);
  41. auto result = std::make_shared<SDLImage>(defFile.get(), frame, group);
  42. animationFrames[locator] = result;
  43. return result;
  44. }
  45. //std::vector<std::shared_ptr<IImage>> RenderHandler::loadImageGroup(const AnimationPath & path, int group)
  46. //{
  47. // const auto defFile = getAnimationFile(path);
  48. //
  49. // size_t groupSize = defFile->getEntries().at(group);
  50. //
  51. // std::vector<std::shared_ptr<IImage>> result;
  52. // for (size_t i = 0; i < groupSize; ++i)
  53. // loadImage(path, i, group);
  54. //
  55. // return result;
  56. //}
  57. std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path)
  58. {
  59. return loadImage(path, EImageBlitMode::ALPHA);
  60. }
  61. std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path, EImageBlitMode mode)
  62. {
  63. return std::make_shared<SDLImage>(path, mode);
  64. }
  65. std::shared_ptr<IImage> RenderHandler::createImage(SDL_Surface * source)
  66. {
  67. return std::make_shared<SDLImage>(source, EImageBlitMode::ALPHA);
  68. }
  69. std::shared_ptr<CAnimation> RenderHandler::loadAnimation(const AnimationPath & path)
  70. {
  71. return std::make_shared<CAnimation>(path);
  72. }
  73. std::shared_ptr<CAnimation> RenderHandler::createAnimation()
  74. {
  75. return std::make_shared<CAnimation>();
  76. }
  77. //
  78. //void Graphics::addImageListEntry(size_t index, size_t group, const std::string & listName, const std::string & imageName)
  79. //{
  80. // if (!imageName.empty())
  81. // {
  82. // JsonNode entry;
  83. // if (group != 0)
  84. // entry["group"].Integer() = group;
  85. // entry["frame"].Integer() = index;
  86. // entry["file"].String() = imageName;
  87. //
  88. // imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
  89. // }
  90. //}
  91. //
  92. //void Graphics::addImageListEntries(const EntityService * service)
  93. //{
  94. // auto cb = std::bind(&Graphics::addImageListEntry, this, _1, _2, _3, _4);
  95. //
  96. // auto loopCb = [&](const Entity * entity, bool & stop)
  97. // {
  98. // entity->registerIcons(cb);
  99. // };
  100. //
  101. // service->forEachBase(loopCb);
  102. //}
  103. //
  104. //void Graphics::initializeImageLists()
  105. //{
  106. // addImageListEntries(CGI->creatures());
  107. // addImageListEntries(CGI->heroTypes());
  108. // addImageListEntries(CGI->artifacts());
  109. // addImageListEntries(CGI->factions());
  110. // addImageListEntries(CGI->spells());
  111. // addImageListEntries(CGI->skills());
  112. //}
  113. //
  114. //std::shared_ptr<CAnimation> Graphics::getAnimation(const AnimationPath & path)
  115. //{
  116. // if (cachedAnimations.count(path) != 0)
  117. // return cachedAnimations.at(path);
  118. //
  119. // auto newAnimation = GH.renderHandler().loadAnimation(path);
  120. //
  121. // newAnimation->preload();
  122. // cachedAnimations[path] = newAnimation;
  123. // return newAnimation;
  124. //}