Graphics.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Graphics.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 "Graphics.h"
  12. #include <vcmi/Entity.h>
  13. #include <vcmi/ArtifactService.h>
  14. #include <vcmi/CreatureService.h>
  15. #include <vcmi/FactionService.h>
  16. #include <vcmi/HeroTypeService.h>
  17. #include <vcmi/SkillService.h>
  18. #include <vcmi/spells/Service.h>
  19. #include "../renderSDL/SDL_Extensions.h"
  20. #include "../renderSDL/CBitmapFont.h"
  21. #include "../renderSDL/CBitmapHanFont.h"
  22. #include "../renderSDL/CTrueTypeFont.h"
  23. #include "../render/CAnimation.h"
  24. #include "../render/IImage.h"
  25. #include "../render/IRenderHandler.h"
  26. #include "../gui/CGuiHandler.h"
  27. #include "../lib/filesystem/Filesystem.h"
  28. #include "../lib/filesystem/CBinaryReader.h"
  29. #include "../../lib/json/JsonNode.h"
  30. #include "../lib/modding/CModHandler.h"
  31. #include "../lib/modding/ModScope.h"
  32. #include "CGameInfo.h"
  33. #include "../lib/VCMI_Lib.h"
  34. #include "../CCallback.h"
  35. #include "../lib/CGeneralTextHandler.h"
  36. #include "../lib/vcmi_endian.h"
  37. #include "../lib/CStopWatch.h"
  38. #include "../lib/CHeroHandler.h"
  39. #include <SDL_surface.h>
  40. Graphics * graphics = nullptr;
  41. void Graphics::loadPaletteAndColors()
  42. {
  43. auto textFile = CResourceHandler::get()->load(ResourcePath("DATA/PLAYERS.PAL"))->readAll();
  44. std::string pals((char*)textFile.first.get(), textFile.second);
  45. int startPoint = 24; //beginning byte; used to read
  46. for(int i=0; i<8; ++i)
  47. {
  48. for(int j=0; j<32; ++j)
  49. {
  50. ColorRGBA col;
  51. col.r = pals[startPoint++];
  52. col.g = pals[startPoint++];
  53. col.b = pals[startPoint++];
  54. col.a = SDL_ALPHA_OPAQUE;
  55. startPoint++;
  56. playerColorPalette[i][j] = col;
  57. }
  58. }
  59. auto stream = CResourceHandler::get()->load(ResourcePath("config/NEUTRAL.PAL"));
  60. CBinaryReader reader(stream.get());
  61. for(int i=0; i<32; ++i)
  62. {
  63. neutralColorPalette[i].r = reader.readUInt8();
  64. neutralColorPalette[i].g = reader.readUInt8();
  65. neutralColorPalette[i].b = reader.readUInt8();
  66. reader.readUInt8(); // this is "flags" entry, not alpha
  67. neutralColorPalette[i].a = SDL_ALPHA_OPAQUE;
  68. }
  69. //colors initialization
  70. ColorRGBA colors[] = {
  71. {0xff,0, 0, SDL_ALPHA_OPAQUE},
  72. {0x31,0x52,0xff,SDL_ALPHA_OPAQUE},
  73. {0x9c,0x73,0x52,SDL_ALPHA_OPAQUE},
  74. {0x42,0x94,0x29,SDL_ALPHA_OPAQUE},
  75. {0xff,0x84,0, SDL_ALPHA_OPAQUE},
  76. {0x8c,0x29,0xa5,SDL_ALPHA_OPAQUE},
  77. {0x09,0x9c,0xa5,SDL_ALPHA_OPAQUE},
  78. {0xc6,0x7b,0x8c,SDL_ALPHA_OPAQUE}};
  79. for(int i=0;i<8;i++)
  80. {
  81. playerColors[i] = colors[i];
  82. }
  83. //gray
  84. neutralColor.r = 0x84;
  85. neutralColor.g = 0x84;
  86. neutralColor.b = 0x84;
  87. neutralColor.a = SDL_ALPHA_OPAQUE;
  88. }
  89. void Graphics::initializeBattleGraphics()
  90. {
  91. auto allConfigs = VLC->modh->getActiveMods();
  92. allConfigs.insert(allConfigs.begin(), ModScope::scopeBuiltin());
  93. for(auto & mod : allConfigs)
  94. {
  95. if(!CResourceHandler::get(mod)->existsResource(ResourcePath("config/battles_graphics.json")))
  96. continue;
  97. const JsonNode config(JsonPath::builtin("config/battles_graphics.json"), mod);
  98. //initialization of AC->def name mapping
  99. if(!config["ac_mapping"].isNull())
  100. for(const JsonNode &ac : config["ac_mapping"].Vector())
  101. {
  102. int ACid = static_cast<int>(ac["id"].Float());
  103. std::vector< std::string > toAdd;
  104. for(const JsonNode &defname : ac["defnames"].Vector())
  105. {
  106. toAdd.push_back(defname.String());
  107. }
  108. battleACToDef[ACid] = toAdd;
  109. }
  110. }
  111. }
  112. Graphics::Graphics()
  113. {
  114. loadFonts();
  115. loadPaletteAndColors();
  116. initializeBattleGraphics();
  117. loadErmuToPicture();
  118. initializeImageLists();
  119. //(!) do not load any CAnimation here
  120. }
  121. void Graphics::blueToPlayersAdv(SDL_Surface * sur, PlayerColor player)
  122. {
  123. if(sur->format->palette)
  124. {
  125. SDL_Color palette[32];
  126. if(player.isValidPlayer())
  127. {
  128. for(int i=0; i<32; ++i)
  129. palette[i] = CSDL_Ext::toSDL(playerColorPalette[player][i]);
  130. }
  131. else if(player == PlayerColor::NEUTRAL)
  132. {
  133. for(int i=0; i<32; ++i)
  134. palette[i] = CSDL_Ext::toSDL(neutralColorPalette[i]);
  135. }
  136. else
  137. {
  138. logGlobal->error("Wrong player id in blueToPlayersAdv (%s)!", player.toString());
  139. return;
  140. }
  141. //FIXME: not all player colored images have player palette at last 32 indexes
  142. //NOTE: following code is much more correct but still not perfect (bugged with status bar)
  143. CSDL_Ext::setColors(sur, palette, 224, 32);
  144. #if 0
  145. SDL_Color * bluePalette = playerColorPalette + 32;
  146. SDL_Palette * oldPalette = sur->format->palette;
  147. SDL_Palette * newPalette = SDL_AllocPalette(256);
  148. for(size_t destIndex = 0; destIndex < 256; destIndex++)
  149. {
  150. SDL_Color old = oldPalette->colors[destIndex];
  151. bool found = false;
  152. for(size_t srcIndex = 0; srcIndex < 32; srcIndex++)
  153. {
  154. if(old.b == bluePalette[srcIndex].b && old.g == bluePalette[srcIndex].g && old.r == bluePalette[srcIndex].r)
  155. {
  156. found = true;
  157. newPalette->colors[destIndex] = palette[srcIndex];
  158. break;
  159. }
  160. }
  161. if(!found)
  162. newPalette->colors[destIndex] = old;
  163. }
  164. SDL_SetSurfacePalette(sur, newPalette);
  165. SDL_FreePalette(newPalette);
  166. #endif // 0
  167. }
  168. else
  169. {
  170. //TODO: implement. H3 method works only for images with palettes.
  171. // Add some kind of player-colored overlay?
  172. // Or keep palette approach here and replace only colors of specific value(s)
  173. // Or just wait for OpenGL support?
  174. logGlobal->warn("Image must have palette to be player-colored!");
  175. }
  176. }
  177. void Graphics::loadFonts()
  178. {
  179. const JsonNode config(JsonPath::builtin("config/fonts.json"));
  180. const JsonVector & bmpConf = config["bitmap"].Vector();
  181. const JsonNode & ttfConf = config["trueType"];
  182. const JsonNode & hanConf = config["bitmapHan"];
  183. assert(bmpConf.size() == FONTS_NUMBER);
  184. for (size_t i=0; i<FONTS_NUMBER; i++)
  185. {
  186. std::string filename = bmpConf[i].String();
  187. if (!hanConf[filename].isNull())
  188. fonts[i] = std::make_shared<CBitmapHanFont>(hanConf[filename]);
  189. else if (!ttfConf[filename].isNull()) // no ttf override
  190. fonts[i] = std::make_shared<CTrueTypeFont>(ttfConf[filename]);
  191. else
  192. fonts[i] = std::make_shared<CBitmapFont>(filename);
  193. }
  194. }
  195. void Graphics::loadErmuToPicture()
  196. {
  197. //loading ERMU to picture
  198. const JsonNode config(JsonPath::builtin("config/ERMU_to_picture.json"));
  199. int etp_idx = 0;
  200. for(const JsonNode &etp : config["ERMU_to_picture"].Vector()) {
  201. int idx = 0;
  202. for(const JsonNode &n : etp.Vector()) {
  203. ERMUtoPicture[idx][etp_idx] = n.String();
  204. idx ++;
  205. }
  206. assert (idx == std::size(ERMUtoPicture));
  207. etp_idx ++;
  208. }
  209. assert (etp_idx == 44);
  210. }
  211. void Graphics::addImageListEntry(size_t index, size_t group, const std::string & listName, const std::string & imageName)
  212. {
  213. if (!imageName.empty())
  214. {
  215. JsonNode entry;
  216. if (group != 0)
  217. entry["group"].Integer() = group;
  218. entry["frame"].Integer() = index;
  219. entry["file"].String() = imageName;
  220. imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
  221. }
  222. }
  223. void Graphics::addImageListEntries(const EntityService * service)
  224. {
  225. auto cb = std::bind(&Graphics::addImageListEntry, this, _1, _2, _3, _4);
  226. auto loopCb = [&](const Entity * entity, bool & stop)
  227. {
  228. entity->registerIcons(cb);
  229. };
  230. service->forEachBase(loopCb);
  231. }
  232. void Graphics::initializeImageLists()
  233. {
  234. addImageListEntries(CGI->creatures());
  235. addImageListEntries(CGI->heroTypes());
  236. addImageListEntries(CGI->artifacts());
  237. addImageListEntries(CGI->factions());
  238. addImageListEntries(CGI->spells());
  239. addImageListEntries(CGI->skills());
  240. }
  241. std::shared_ptr<CAnimation> Graphics::getAnimation(const AnimationPath & path)
  242. {
  243. if (cachedAnimations.count(path) != 0)
  244. return cachedAnimations.at(path);
  245. auto newAnimation = GH.renderHandler().loadAnimation(path);
  246. newAnimation->preload();
  247. cachedAnimations[path] = newAnimation;
  248. return newAnimation;
  249. }