Graphics.cpp 4.3 KB

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