RenderHandler.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/JsonUtils.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. std::shared_ptr<CDefFile> RenderHandler::getAnimationFile(const AnimationPath & path)
  18. {
  19. AnimationPath actualPath = boost::starts_with(path.getName(), "SPRITES") ? path : path.addPrefix("SPRITES/");
  20. auto it = animationFiles.find(actualPath);
  21. if (it != animationFiles.end())
  22. return it->second;
  23. if (!CResourceHandler::get()->existsResource(actualPath))
  24. {
  25. animationFiles[actualPath] = nullptr;
  26. return nullptr;
  27. }
  28. auto result = std::make_shared<CDefFile>(actualPath);
  29. animationFiles[actualPath] = result;
  30. return result;
  31. }
  32. void RenderHandler::initFromJson(AnimationLayoutMap & source, const JsonNode & config)
  33. {
  34. std::string basepath;
  35. basepath = config["basepath"].String();
  36. JsonNode base;
  37. base["margins"] = config["margins"];
  38. base["width"] = config["width"];
  39. base["height"] = config["height"];
  40. for(const JsonNode & group : config["sequences"].Vector())
  41. {
  42. size_t groupID = group["group"].Integer();//TODO: string-to-value conversion("moving" -> MOVING)
  43. source[groupID].clear();
  44. for(const JsonNode & frame : group["frames"].Vector())
  45. {
  46. JsonNode toAdd;
  47. JsonUtils::inherit(toAdd, base);
  48. toAdd["file"].String() = basepath + frame.String();
  49. source[groupID].push_back(toAdd);
  50. }
  51. }
  52. for(const JsonNode & node : config["images"].Vector())
  53. {
  54. size_t group = node["group"].Integer();
  55. size_t frame = node["frame"].Integer();
  56. if (source[group].size() <= frame)
  57. source[group].resize(frame+1);
  58. JsonNode toAdd;
  59. JsonUtils::inherit(toAdd, base);
  60. toAdd["file"].String() = basepath + node["file"].String();
  61. source[group][frame] = toAdd;
  62. }
  63. }
  64. const RenderHandler::AnimationLayoutMap & RenderHandler::getAnimationLayout(const AnimationPath & path)
  65. {
  66. auto it = animationLayouts.find(path);
  67. if (it != animationLayouts.end())
  68. return it->second;
  69. AnimationLayoutMap result;
  70. auto defFile = getAnimationFile(path);
  71. if(defFile)
  72. {
  73. const std::map<size_t, size_t> defEntries = defFile->getEntries();
  74. for (auto & defEntry : defEntries)
  75. result[defEntry.first].resize(defEntry.second);
  76. }
  77. auto jsonResource = path.toType<EResType::JSON>();
  78. JsonPath actualJsonPath = boost::starts_with(jsonResource.getName(), "SPRITES") ? jsonResource : jsonResource.addPrefix("SPRITES/");;
  79. auto configList = CResourceHandler::get()->getResourcesWithName(actualJsonPath);
  80. for(auto & loader : configList)
  81. {
  82. auto stream = loader->load(actualJsonPath);
  83. std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
  84. stream->read(textData.get(), stream->getSize());
  85. const JsonNode config(reinterpret_cast<const std::byte*>(textData.get()), stream->getSize(), path.getOriginalName());
  86. initFromJson(result, config);
  87. }
  88. animationLayouts[path] = result;
  89. return animationLayouts[path];
  90. }
  91. //std::shared_ptr<JsonNode> RenderHandler::getJsonFile(const JsonPath & path)
  92. //{
  93. // auto it = jsonFiles.find(path);
  94. //
  95. // if (it != jsonFiles.end())
  96. // return it->second;
  97. //
  98. // auto result = std::make_shared<JsonNode>(path);
  99. //
  100. // jsonFiles[path] = result;
  101. // return result;
  102. //}
  103. std::shared_ptr<IImage> RenderHandler::loadImage(const AnimationPath & path, int frame, int group)
  104. {
  105. AnimationLocator locator{path, frame, group};
  106. auto it = animationFrames.find(locator);
  107. if (it != animationFrames.end())
  108. return it->second->createImageReference();
  109. auto defFile = getAnimationFile(path);
  110. auto result = std::make_shared<SDLImageConst>(defFile.get(), frame, group);
  111. animationFrames[locator] = result;
  112. return result->createImageReference();
  113. }
  114. //std::vector<std::shared_ptr<IImage>> RenderHandler::loadImageGroup(const AnimationPath & path, int group)
  115. //{
  116. // const auto defFile = getAnimationFile(path);
  117. //
  118. // size_t groupSize = defFile->getEntries().at(group);
  119. //
  120. // std::vector<std::shared_ptr<IImage>> result;
  121. // for (size_t i = 0; i < groupSize; ++i)
  122. // loadImage(path, i, group);
  123. //
  124. // return result;
  125. //}
  126. std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path)
  127. {
  128. return loadImage(path, EImageBlitMode::ALPHA);
  129. }
  130. std::shared_ptr<IImage> RenderHandler::loadImage(const ImagePath & path, EImageBlitMode mode)
  131. {
  132. auto it = imageFiles.find(path);
  133. if (it != imageFiles.end())
  134. return it->second->createImageReference();
  135. auto result = std::make_shared<SDLImageConst>(path, mode);
  136. imageFiles[path] = result;
  137. return result->createImageReference();
  138. }
  139. std::shared_ptr<IImage> RenderHandler::createImage(SDL_Surface * source)
  140. {
  141. return std::make_shared<SDLImageConst>(source, EImageBlitMode::ALPHA)->createImageReference();
  142. }
  143. std::shared_ptr<CAnimation> RenderHandler::loadAnimation(const AnimationPath & path)
  144. {
  145. return std::make_shared<CAnimation>(path, getAnimationLayout(path));
  146. }
  147. std::shared_ptr<CAnimation> RenderHandler::createAnimation()
  148. {
  149. return std::make_shared<CAnimation>();
  150. }
  151. //
  152. //void Graphics::addImageListEntry(size_t index, size_t group, const std::string & listName, const std::string & imageName)
  153. //{
  154. // if (!imageName.empty())
  155. // {
  156. // JsonNode entry;
  157. // if (group != 0)
  158. // entry["group"].Integer() = group;
  159. // entry["frame"].Integer() = index;
  160. // entry["file"].String() = imageName;
  161. //
  162. // imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
  163. // }
  164. //}
  165. //
  166. //void Graphics::addImageListEntries(const EntityService * service)
  167. //{
  168. // auto cb = std::bind(&Graphics::addImageListEntry, this, _1, _2, _3, _4);
  169. //
  170. // auto loopCb = [&](const Entity * entity, bool & stop)
  171. // {
  172. // entity->registerIcons(cb);
  173. // };
  174. //
  175. // service->forEachBase(loopCb);
  176. //}
  177. //
  178. //void Graphics::initializeImageLists()
  179. //{
  180. // addImageListEntries(CGI->creatures());
  181. // addImageListEntries(CGI->heroTypes());
  182. // addImageListEntries(CGI->artifacts());
  183. // addImageListEntries(CGI->factions());
  184. // addImageListEntries(CGI->spells());
  185. // addImageListEntries(CGI->skills());
  186. //}
  187. //
  188. //std::shared_ptr<CAnimation> Graphics::getAnimation(const AnimationPath & path)
  189. //{
  190. // if (cachedAnimations.count(path) != 0)
  191. // return cachedAnimations.at(path);
  192. //
  193. // auto newAnimation = GH.renderHandler().loadAnimation(path);
  194. //
  195. // newAnimation->preload();
  196. // cachedAnimations[path] = newAnimation;
  197. // return newAnimation;
  198. //}