Graphics.cpp 7.9 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 "../lib/filesystem/Filesystem.h"
  26. #include "../lib/filesystem/CBinaryReader.h"
  27. #include "../lib/modding/CModHandler.h"
  28. #include "../lib/modding/ModScope.h"
  29. #include "CGameInfo.h"
  30. #include "../lib/VCMI_Lib.h"
  31. #include "../CCallback.h"
  32. #include "../lib/CGeneralTextHandler.h"
  33. #include "../lib/JsonNode.h"
  34. #include "../lib/vcmi_endian.h"
  35. #include "../lib/CStopWatch.h"
  36. #include "../lib/CHeroHandler.h"
  37. #include <SDL_surface.h>
  38. Graphics * graphics = nullptr;
  39. void Graphics::loadPaletteAndColors()
  40. {
  41. auto textFile = CResourceHandler::get()->load(ResourceID("DATA/PLAYERS.PAL"))->readAll();
  42. std::string pals((char*)textFile.first.get(), textFile.second);
  43. int startPoint = 24; //beginning byte; used to read
  44. for(int i=0; i<8; ++i)
  45. {
  46. for(int j=0; j<32; ++j)
  47. {
  48. ColorRGBA 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][j] = col;
  55. }
  56. }
  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. ColorRGBA 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(), ModScope::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. void Graphics::blueToPlayersAdv(SDL_Surface * sur, PlayerColor player)
  120. {
  121. if(sur->format->palette)
  122. {
  123. SDL_Color palette[32];
  124. if(player.isValidPlayer())
  125. {
  126. for(int i=0; i<32; ++i)
  127. palette[i] = CSDL_Ext::toSDL(playerColorPalette[player][i]);
  128. }
  129. else if(player == PlayerColor::NEUTRAL)
  130. {
  131. for(int i=0; i<32; ++i)
  132. palette[i] = CSDL_Ext::toSDL(neutralColorPalette[i]);
  133. }
  134. else
  135. {
  136. logGlobal->error("Wrong player id in blueToPlayersAdv (%s)!", player.toString());
  137. return;
  138. }
  139. //FIXME: not all player colored images have player palette at last 32 indexes
  140. //NOTE: following code is much more correct but still not perfect (bugged with status bar)
  141. CSDL_Ext::setColors(sur, palette, 224, 32);
  142. #if 0
  143. SDL_Color * bluePalette = playerColorPalette + 32;
  144. SDL_Palette * oldPalette = sur->format->palette;
  145. SDL_Palette * newPalette = SDL_AllocPalette(256);
  146. for(size_t destIndex = 0; destIndex < 256; destIndex++)
  147. {
  148. SDL_Color old = oldPalette->colors[destIndex];
  149. bool found = false;
  150. for(size_t srcIndex = 0; srcIndex < 32; srcIndex++)
  151. {
  152. if(old.b == bluePalette[srcIndex].b && old.g == bluePalette[srcIndex].g && old.r == bluePalette[srcIndex].r)
  153. {
  154. found = true;
  155. newPalette->colors[destIndex] = palette[srcIndex];
  156. break;
  157. }
  158. }
  159. if(!found)
  160. newPalette->colors[destIndex] = old;
  161. }
  162. SDL_SetSurfacePalette(sur, newPalette);
  163. SDL_FreePalette(newPalette);
  164. #endif // 0
  165. }
  166. else
  167. {
  168. //TODO: implement. H3 method works only for images with palettes.
  169. // Add some kind of player-colored overlay?
  170. // Or keep palette approach here and replace only colors of specific value(s)
  171. // Or just wait for OpenGL support?
  172. logGlobal->warn("Image must have palette to be player-colored!");
  173. }
  174. }
  175. void Graphics::loadFonts()
  176. {
  177. const JsonNode config(ResourceID("config/fonts.json"));
  178. const JsonVector & bmpConf = config["bitmap"].Vector();
  179. const JsonNode & ttfConf = config["trueType"];
  180. const JsonNode & hanConf = config["bitmapHan"];
  181. assert(bmpConf.size() == FONTS_NUMBER);
  182. for (size_t i=0; i<FONTS_NUMBER; i++)
  183. {
  184. std::string filename = bmpConf[i].String();
  185. if (!hanConf[filename].isNull())
  186. fonts[i] = std::make_shared<CBitmapHanFont>(hanConf[filename]);
  187. else if (!ttfConf[filename].isNull()) // no ttf override
  188. fonts[i] = std::make_shared<CTrueTypeFont>(ttfConf[filename]);
  189. else
  190. fonts[i] = std::make_shared<CBitmapFont>(filename);
  191. }
  192. }
  193. void Graphics::loadErmuToPicture()
  194. {
  195. //loading ERMU to picture
  196. const JsonNode config(ResourceID("config/ERMU_to_picture.json"));
  197. int etp_idx = 0;
  198. for(const JsonNode &etp : config["ERMU_to_picture"].Vector()) {
  199. int idx = 0;
  200. for(const JsonNode &n : etp.Vector()) {
  201. ERMUtoPicture[idx][etp_idx] = n.String();
  202. idx ++;
  203. }
  204. assert (idx == std::size(ERMUtoPicture));
  205. etp_idx ++;
  206. }
  207. assert (etp_idx == 44);
  208. }
  209. void Graphics::addImageListEntry(size_t index, size_t group, const std::string & listName, const std::string & imageName)
  210. {
  211. if (!imageName.empty())
  212. {
  213. JsonNode entry;
  214. if (group != 0)
  215. entry["group"].Integer() = group;
  216. entry["frame"].Integer() = index;
  217. entry["file"].String() = imageName;
  218. imageLists["SPRITES/" + listName]["images"].Vector().push_back(entry);
  219. }
  220. }
  221. void Graphics::addImageListEntries(const EntityService * service)
  222. {
  223. auto cb = std::bind(&Graphics::addImageListEntry, this, _1, _2, _3, _4);
  224. auto loopCb = [&](const Entity * entity, bool & stop)
  225. {
  226. entity->registerIcons(cb);
  227. };
  228. service->forEachBase(loopCb);
  229. }
  230. void Graphics::initializeImageLists()
  231. {
  232. addImageListEntries(CGI->creatures());
  233. addImageListEntries(CGI->heroTypes());
  234. addImageListEntries(CGI->artifacts());
  235. addImageListEntries(CGI->factions());
  236. addImageListEntries(CGI->spells());
  237. addImageListEntries(CGI->skills());
  238. }
  239. std::shared_ptr<CAnimation> Graphics::getAnimation(const std::string & path)
  240. {
  241. ResourceID animationPath(path, EResType::ANIMATION);
  242. if (cachedAnimations.count(animationPath.getName()) != 0)
  243. return cachedAnimations.at(animationPath.getName());
  244. auto newAnimation = std::make_shared<CAnimation>(animationPath.getName());
  245. newAnimation->preload();
  246. cachedAnimations[animationPath.getName()] = newAnimation;
  247. return newAnimation;
  248. }