Graphics.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 "../render/CAnimation.h"
  21. #include "../render/IImage.h"
  22. #include "../lib/filesystem/Filesystem.h"
  23. #include "../lib/filesystem/CBinaryReader.h"
  24. #include "../../lib/json/JsonNode.h"
  25. #include "../lib/modding/CModHandler.h"
  26. #include "../lib/modding/ModScope.h"
  27. #include "../lib/VCMI_Lib.h"
  28. #include "../lib/CHeroHandler.h"
  29. #include <SDL_surface.h>
  30. Graphics * graphics = nullptr;
  31. void Graphics::loadPaletteAndColors()
  32. {
  33. auto textFile = CResourceHandler::get()->load(ResourcePath("DATA/PLAYERS.PAL"))->readAll();
  34. std::string pals((char*)textFile.first.get(), textFile.second);
  35. int startPoint = 24; //beginning byte; used to read
  36. for(int i=0; i<8; ++i)
  37. {
  38. for(int j=0; j<32; ++j)
  39. {
  40. ColorRGBA col;
  41. col.r = pals[startPoint++];
  42. col.g = pals[startPoint++];
  43. col.b = pals[startPoint++];
  44. col.a = SDL_ALPHA_OPAQUE;
  45. startPoint++;
  46. playerColorPalette[i][j] = col;
  47. }
  48. }
  49. auto stream = CResourceHandler::get()->load(ResourcePath("config/NEUTRAL.PAL"));
  50. CBinaryReader reader(stream.get());
  51. for(int i=0; i<32; ++i)
  52. {
  53. neutralColorPalette[i].r = reader.readUInt8();
  54. neutralColorPalette[i].g = reader.readUInt8();
  55. neutralColorPalette[i].b = reader.readUInt8();
  56. reader.readUInt8(); // this is "flags" entry, not alpha
  57. neutralColorPalette[i].a = SDL_ALPHA_OPAQUE;
  58. }
  59. //colors initialization
  60. ColorRGBA colors[] = {
  61. {0xff,0, 0, SDL_ALPHA_OPAQUE},
  62. {0x31,0x52,0xff,SDL_ALPHA_OPAQUE},
  63. {0x9c,0x73,0x52,SDL_ALPHA_OPAQUE},
  64. {0x42,0x94,0x29,SDL_ALPHA_OPAQUE},
  65. {0xff,0x84,0, SDL_ALPHA_OPAQUE},
  66. {0x8c,0x29,0xa5,SDL_ALPHA_OPAQUE},
  67. {0x09,0x9c,0xa5,SDL_ALPHA_OPAQUE},
  68. {0xc6,0x7b,0x8c,SDL_ALPHA_OPAQUE}};
  69. for(int i=0;i<8;i++)
  70. {
  71. playerColors[i] = colors[i];
  72. }
  73. //gray
  74. neutralColor.r = 0x84;
  75. neutralColor.g = 0x84;
  76. neutralColor.b = 0x84;
  77. neutralColor.a = SDL_ALPHA_OPAQUE;
  78. }
  79. void Graphics::initializeBattleGraphics()
  80. {
  81. auto allConfigs = VLC->modh->getActiveMods();
  82. allConfigs.insert(allConfigs.begin(), ModScope::scopeBuiltin());
  83. for(auto & mod : allConfigs)
  84. {
  85. if(!CResourceHandler::get(mod)->existsResource(ResourcePath("config/battles_graphics.json")))
  86. continue;
  87. const JsonNode config(JsonPath::builtin("config/battles_graphics.json"), mod);
  88. //initialization of AC->def name mapping
  89. if(!config["ac_mapping"].isNull())
  90. for(const JsonNode &ac : config["ac_mapping"].Vector())
  91. {
  92. int ACid = static_cast<int>(ac["id"].Float());
  93. std::vector< std::string > toAdd;
  94. for(const JsonNode &defname : ac["defnames"].Vector())
  95. {
  96. toAdd.push_back(defname.String());
  97. }
  98. battleACToDef[ACid] = toAdd;
  99. }
  100. }
  101. }
  102. Graphics::Graphics()
  103. {
  104. loadPaletteAndColors();
  105. initializeBattleGraphics();
  106. loadErmuToPicture();
  107. //(!) do not load any CAnimation here
  108. }
  109. void Graphics::setPlayerPalette(SDL_Palette * targetPalette, PlayerColor player)
  110. {
  111. SDL_Color palette[32];
  112. if(player.isValidPlayer())
  113. {
  114. for(int i=0; i<32; ++i)
  115. palette[i] = CSDL_Ext::toSDL(playerColorPalette[player][i]);
  116. }
  117. else
  118. {
  119. for(int i=0; i<32; ++i)
  120. palette[i] = CSDL_Ext::toSDL(neutralColorPalette[i]);
  121. }
  122. SDL_SetPaletteColors(targetPalette, palette, 224, 32);
  123. }
  124. void Graphics::setPlayerFlagColor(SDL_Palette * targetPalette, PlayerColor player)
  125. {
  126. if(player.isValidPlayer())
  127. {
  128. SDL_Color color = CSDL_Ext::toSDL(playerColors[player.getNum()]);
  129. SDL_SetPaletteColors(targetPalette, &color, 5, 1);
  130. }
  131. else
  132. {
  133. SDL_Color color = CSDL_Ext::toSDL(neutralColor);
  134. SDL_SetPaletteColors(targetPalette, &color, 5, 1);
  135. }
  136. }
  137. void Graphics::loadErmuToPicture()
  138. {
  139. //loading ERMU to picture
  140. const JsonNode config(JsonPath::builtin("config/ERMU_to_picture.json"));
  141. int etp_idx = 0;
  142. for(const JsonNode &etp : config["ERMU_to_picture"].Vector()) {
  143. int idx = 0;
  144. for(const JsonNode &n : etp.Vector()) {
  145. ERMUtoPicture[idx][etp_idx] = n.String();
  146. idx ++;
  147. }
  148. assert (idx == std::size(ERMUtoPicture));
  149. etp_idx ++;
  150. }
  151. assert (etp_idx == 44);
  152. }