Graphics.cpp 8.2 KB

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