Graphics.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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/texts/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. //(!) do not load any CAnimation here
  119. }
  120. void Graphics::setPlayerPalette(SDL_Palette * targetPalette, PlayerColor player)
  121. {
  122. SDL_Color palette[32];
  123. if(player.isValidPlayer())
  124. {
  125. for(int i=0; i<32; ++i)
  126. palette[i] = CSDL_Ext::toSDL(playerColorPalette[player][i]);
  127. }
  128. else
  129. {
  130. for(int i=0; i<32; ++i)
  131. palette[i] = CSDL_Ext::toSDL(neutralColorPalette[i]);
  132. }
  133. SDL_SetPaletteColors(targetPalette, palette, 224, 32);
  134. }
  135. void Graphics::setPlayerFlagColor(SDL_Palette * targetPalette, PlayerColor player)
  136. {
  137. if(player.isValidPlayer())
  138. {
  139. SDL_Color color = CSDL_Ext::toSDL(playerColors[player.getNum()]);
  140. SDL_SetPaletteColors(targetPalette, &color, 5, 1);
  141. }
  142. else
  143. {
  144. SDL_Color color = CSDL_Ext::toSDL(neutralColor);
  145. SDL_SetPaletteColors(targetPalette, &color, 5, 1);
  146. }
  147. }
  148. void Graphics::loadFonts()
  149. {
  150. const JsonNode config(JsonPath::builtin("config/fonts.json"));
  151. const JsonVector & bmpConf = config["bitmap"].Vector();
  152. const JsonNode & ttfConf = config["trueType"];
  153. const JsonNode & hanConf = config["bitmapHan"];
  154. assert(bmpConf.size() == FONTS_NUMBER);
  155. for (size_t i=0; i<FONTS_NUMBER; i++)
  156. {
  157. std::string filename = bmpConf[i].String();
  158. if (!hanConf[filename].isNull())
  159. fonts[i] = std::make_shared<CBitmapHanFont>(hanConf[filename]);
  160. else if (!ttfConf[filename].isNull()) // no ttf override
  161. fonts[i] = std::make_shared<CTrueTypeFont>(ttfConf[filename]);
  162. else
  163. fonts[i] = std::make_shared<CBitmapFont>(filename);
  164. }
  165. }
  166. void Graphics::loadErmuToPicture()
  167. {
  168. //loading ERMU to picture
  169. const JsonNode config(JsonPath::builtin("config/ERMU_to_picture.json"));
  170. int etp_idx = 0;
  171. for(const JsonNode &etp : config["ERMU_to_picture"].Vector()) {
  172. int idx = 0;
  173. for(const JsonNode &n : etp.Vector()) {
  174. ERMUtoPicture[idx][etp_idx] = n.String();
  175. idx ++;
  176. }
  177. assert (idx == std::size(ERMUtoPicture));
  178. etp_idx ++;
  179. }
  180. assert (etp_idx == 44);
  181. }