Graphics.cpp 8.0 KB

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