Graphics.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #include "StdInc.h"
  2. #include "Graphics.h"
  3. #include "../lib/Filesystem/CResourceLoader.h"
  4. #include "CDefHandler.h"
  5. #include "UIFramework/SDL_Extensions.h"
  6. #include <SDL_ttf.h>
  7. #include "../lib/CThreadHelper.h"
  8. #include "CGameInfo.h"
  9. #include "../lib/VCMI_Lib.h"
  10. #include "../CCallback.h"
  11. #include "../lib/CTownHandler.h"
  12. #include "../lib/CObjectHandler.h"
  13. #include "../lib/CGeneralTextHandler.h"
  14. #include "../lib/CCreatureHandler.h"
  15. #include "CBitmapHandler.h"
  16. #include "../lib/CObjectHandler.h"
  17. #include "../lib/CDefObjInfoHandler.h"
  18. #include "../lib/CGameState.h"
  19. #include "../lib/JsonNode.h"
  20. #include "../lib/vcmi_endian.h"
  21. #include "../lib/GameConstants.h"
  22. #include "../lib/CStopWatch.h"
  23. using namespace boost::assign;
  24. using namespace CSDL_Ext;
  25. #ifdef min
  26. #undef min
  27. #endif
  28. #ifdef max
  29. #undef max
  30. #endif
  31. /*
  32. * Graphics.cpp, part of VCMI engine
  33. *
  34. * Authors: listed in file AUTHORS in main folder
  35. *
  36. * License: GNU General Public License v2.0 or later
  37. * Full text of license available in license.txt file, in main folder
  38. *
  39. */
  40. Graphics * graphics = NULL;
  41. void Graphics::loadPaletteAndColors()
  42. {
  43. auto textFile = CResourceHandler::get()->loadData(ResourceID("DATA/PLAYERS.PAL"));
  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[GameConstants::PLAYER_LIMIT];
  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.unused = 255;
  56. startPoint++;
  57. playerColorPalette[i] = col;
  58. }
  59. neutralColorPalette = new SDL_Color[32];
  60. std::ifstream ncp;
  61. ncp.open(CResourceHandler::get()->getResourceName(ResourceID("config/NEUTRAL.PAL")), std::ios::binary);
  62. for(int i=0; i<32; ++i)
  63. {
  64. ncp.read((char*)&neutralColorPalette[i].r,1);
  65. ncp.read((char*)&neutralColorPalette[i].g,1);
  66. ncp.read((char*)&neutralColorPalette[i].b,1);
  67. ncp.read((char*)&neutralColorPalette[i].unused,1);
  68. neutralColorPalette[i].unused = !neutralColorPalette[i].unused;
  69. }
  70. //colors initialization
  71. int3 kolory[] = {int3(0xff,0,0),int3(0x31,0x52,0xff),int3(0x9c,0x73,0x52),int3(0x42,0x94,0x29),
  72. int3(0xff,0x84,0x0),int3(0x8c,0x29,0xa5),int3(0x09,0x9c,0xa5),int3(0xc6,0x7b,0x8c)};
  73. for(int i=0;i<8;i++)
  74. {
  75. playerColors[i].r = kolory[i].x;
  76. playerColors[i].g = kolory[i].y;
  77. playerColors[i].b = kolory[i].z;
  78. playerColors[i].unused = 255;
  79. }
  80. neutralColor->r = 0x84; neutralColor->g = 0x84; neutralColor->b = 0x84; neutralColor->unused = 255;//gray
  81. }
  82. void Graphics::initializeBattleGraphics()
  83. {
  84. const JsonNode config(ResourceID("config/battles_graphics.json"));
  85. // Reserve enough space for the terrains
  86. int idx = config["backgrounds"].Vector().size();
  87. battleBacks.resize(idx+1); // 1 to idx, 0 is unused
  88. idx = 1;
  89. BOOST_FOREACH(const JsonNode &t, config["backgrounds"].Vector()) {
  90. battleBacks[idx].push_back(t.String());
  91. idx++;
  92. }
  93. //initializing battle hero animation
  94. idx = config["heroes"].Vector().size();
  95. battleHeroes.resize(idx);
  96. idx = 0;
  97. BOOST_FOREACH(const JsonNode &h, config["heroes"].Vector()) {
  98. battleHeroes[idx] = h.String();
  99. idx ++;
  100. }
  101. //initialization of AC->def name mapping
  102. BOOST_FOREACH(const JsonNode &ac, config["ac_mapping"].Vector()) {
  103. int ACid = ac["id"].Float();
  104. std::vector< std::string > toAdd;
  105. BOOST_FOREACH(const JsonNode &defname, ac["defnames"].Vector()) {
  106. toAdd.push_back(defname.String());
  107. }
  108. battleACToDef[ACid] = toAdd;
  109. }
  110. spellEffectsPics = CDefHandler::giveDefEss("SpellInt.def");
  111. }
  112. Graphics::Graphics()
  113. {
  114. CDefHandler *smi, *smi2;
  115. std::vector<Task> tasks; //preparing list of graphics to load
  116. tasks += boost::bind(&Graphics::loadFonts,this);
  117. tasks += boost::bind(&Graphics::loadTrueType,this);
  118. tasks += boost::bind(&Graphics::loadPaletteAndColors,this);
  119. tasks += boost::bind(&Graphics::loadHeroFlags,this);
  120. tasks += boost::bind(&Graphics::initializeBattleGraphics,this);
  121. tasks += boost::bind(&Graphics::loadErmuToPicture,this);
  122. tasks += GET_DEF_ESS(artDefs,"ARTIFACT.DEF");
  123. tasks += GET_DEF_ESS(resources32,"RESOURCE.DEF");
  124. tasks += GET_DEF(smi,"CPRSMALL.DEF");
  125. tasks += GET_DEF(smi2,"TWCRPORT.DEF");
  126. tasks += GET_DEF_ESS(flags,"CREST58.DEF");
  127. tasks += GET_DEF_ESS(abils82,"SECSK82.DEF");
  128. tasks += GET_DEF_ESS(spellscr,"SPELLSCR.DEF");
  129. tasks += GET_DEF_ESS(heroMoveArrows,"ADAG.DEF");
  130. CThreadHelper th(&tasks,std::max((ui32)1,boost::thread::hardware_concurrency()));
  131. th.run();
  132. for(size_t y=0; y < heroMoveArrows->ourImages.size(); ++y)
  133. {
  134. CSDL_Ext::alphaTransform(heroMoveArrows->ourImages[y].bitmap);
  135. }
  136. //handling 32x32px imgs
  137. smi->notFreeImgs = true;
  138. for (size_t i=0; i<smi->ourImages.size(); ++i)
  139. {
  140. smallImgs[i-2] = smi->ourImages[i].bitmap;
  141. }
  142. delete smi;
  143. smi2->notFreeImgs = true;
  144. for (size_t i=0; i<smi2->ourImages.size(); ++i)
  145. {
  146. bigImgs[i-2] = smi2->ourImages[i].bitmap;
  147. }
  148. //hack for green color on big infernal troglodite image - Mantis #758
  149. SDL_Color green = {0x30, 0x5c, 0x20, SDL_ALPHA_OPAQUE};
  150. bigImgs[71]->format->palette->colors[7] = green;
  151. delete smi2;
  152. }
  153. void Graphics::loadHeroAnims()
  154. {
  155. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  156. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12), std::make_pair(1,13),
  157. std::make_pair(2,14), std::make_pair(3,15);
  158. for(size_t i=0; i<GameConstants::F_NUMBER * 2; ++i)
  159. {
  160. std::ostringstream nm;
  161. nm << "AH" << std::setw(2) << std::setfill('0') << i << "_.DEF";
  162. loadHeroAnim(nm.str(), rotations, &Graphics::heroAnims);
  163. }
  164. loadHeroAnim("AB01_.DEF", rotations, &Graphics::boatAnims);
  165. loadHeroAnim("AB02_.DEF", rotations, &Graphics::boatAnims);
  166. loadHeroAnim("AB03_.DEF", rotations, &Graphics::boatAnims);
  167. }
  168. void Graphics::loadHeroAnim( const std::string &name, const std::vector<std::pair<int,int> > &rotations, std::vector<CDefEssential *> Graphics::*dst )
  169. {
  170. CDefEssential *anim = CDefHandler::giveDefEss(name);
  171. (this->*dst).push_back(anim);
  172. int pom = 0; //how many groups has been rotated
  173. for(int o=7; pom<6; ++o)
  174. {
  175. for(int p=0;p<6;p++)
  176. {
  177. if(anim->ourImages[o].groupNumber == rotations[p].first)
  178. {
  179. for(int e=0; e<8; ++e)
  180. {
  181. Cimage nci;
  182. nci.bitmap = CSDL_Ext::rotate01(anim->ourImages[o+e].bitmap);
  183. nci.groupNumber = rotations[p].second;
  184. nci.imName = std::string();
  185. anim->ourImages.push_back(nci);
  186. if(pom>2) //we need only one frame for groups 13/14/15
  187. break;
  188. }
  189. if(pom<3) //there are eight frames of animtion of groups 6/7/8 so for speed we'll skip them
  190. o+=8;
  191. else //there is only one frame of 1/2/3
  192. o+=1;
  193. ++pom;
  194. if(p==2 && pom<4) //group1 starts at index 1
  195. o = 1;
  196. }
  197. }
  198. }
  199. for(size_t ff=0; ff<anim->ourImages.size(); ++ff)
  200. {
  201. CSDL_Ext::alphaTransform(anim->ourImages[ff].bitmap);
  202. }
  203. }
  204. void Graphics::loadHeroFlags(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr, bool mode)
  205. {
  206. for(int i=0;i<8;i++)
  207. (this->*pr.first).push_back(CDefHandler::giveDefEss(pr.second[i]));
  208. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  209. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12);
  210. for(int q=0; q<8; ++q)
  211. {
  212. std::vector<Cimage> &curImgs = (this->*pr.first)[q]->ourImages;
  213. for(size_t o=0; o<curImgs.size(); ++o)
  214. {
  215. for(size_t p=0; p<rotations.size(); p++)
  216. {
  217. if(curImgs[o].groupNumber==rotations[p].first)
  218. {
  219. for(int e=0; e<8; ++e)
  220. {
  221. Cimage nci;
  222. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  223. nci.groupNumber = rotations[p].second;
  224. nci.imName = std::string();
  225. curImgs.push_back(nci);
  226. }
  227. o+=8;
  228. }
  229. }
  230. }
  231. if (mode)
  232. {
  233. for(size_t o=0; o<curImgs.size(); ++o)
  234. {
  235. if(curImgs[o].groupNumber==1 || curImgs[o].groupNumber==2 || curImgs[o].groupNumber==3)
  236. {
  237. for(int e=0; e<8; ++e)
  238. {
  239. Cimage nci;
  240. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  241. nci.groupNumber = 12 + curImgs[o].groupNumber;
  242. nci.imName = std::string();
  243. curImgs.push_back(nci);
  244. }
  245. o+=8;
  246. }
  247. }
  248. }
  249. for(size_t ff=0; ff<curImgs.size(); ++ff)
  250. {
  251. SDL_SetColorKey(curImgs[ff].bitmap, SDL_SRCCOLORKEY,
  252. SDL_MapRGB(curImgs[ff].bitmap->format, 0, 255, 255)
  253. );
  254. }
  255. }
  256. }
  257. void Graphics::loadHeroFlags()
  258. {
  259. using namespace boost::assign;
  260. CStopWatch th;
  261. std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > pr[4];
  262. pr[0].first = &Graphics::flags1;
  263. pr[0].second+=("ABF01L.DEF"),("ABF01G.DEF"),("ABF01R.DEF"),("ABF01D.DEF"),("ABF01B.DEF"),
  264. ("ABF01P.DEF"),("ABF01W.DEF"),("ABF01K.DEF");
  265. pr[1].first = &Graphics::flags2;
  266. pr[1].second+=("ABF02L.DEF"),("ABF02G.DEF"),("ABF02R.DEF"),("ABF02D.DEF"),("ABF02B.DEF"),
  267. ("ABF02P.DEF"),("ABF02W.DEF"),("ABF02K.DEF");
  268. pr[2].first = &Graphics::flags3;
  269. pr[2].second+=("ABF03L.DEF"),("ABF03G.DEF"),("ABF03R.DEF"),("ABF03D.DEF"),("ABF03B.DEF"),
  270. ("ABF03P.DEF"),("ABF03W.DEF"),("ABF03K.DEF");
  271. pr[3].first = &Graphics::flags4;
  272. pr[3].second+=("AF00.DEF"),("AF01.DEF"),("AF02.DEF"),("AF03.DEF"),("AF04.DEF"),
  273. ("AF05.DEF"),("AF06.DEF"),("AF07.DEF");
  274. boost::thread_group grupa;
  275. for(int g=3; g>=0; --g)
  276. {
  277. grupa.create_thread(boost::bind(&Graphics::loadHeroFlags,this,boost::ref(pr[g]),true));
  278. }
  279. grupa.join_all();
  280. tlog0 << "Loading and transforming heroes' flags: "<<th.getDiff()<<std::endl;
  281. }
  282. void Graphics::blueToPlayersAdv(SDL_Surface * sur, int player)
  283. {
  284. // if(player==1) //it is actually blue...
  285. // return;
  286. if(sur->format->BitsPerPixel == 8)
  287. {
  288. SDL_Color *palette = NULL;
  289. if(player < GameConstants::PLAYER_LIMIT && player >= 0)
  290. {
  291. palette = playerColorPalette + 32*player;
  292. }
  293. else if(player == 255 || player == -1)
  294. {
  295. palette = neutralColorPalette;
  296. }
  297. else
  298. {
  299. tlog1 << "Wrong player id in blueToPlayersAdv (" << player << ")!\n";
  300. return;
  301. }
  302. SDL_SetColors(sur, palette, 224, 32);
  303. //for(int i=0; i<32; ++i)
  304. //{
  305. // sur->format->palette->colors[224+i] = palette[i];
  306. //}
  307. }
  308. else if(sur->format->BitsPerPixel == 24) //should never happen in general
  309. {
  310. for(int y=0; y<sur->h; ++y)
  311. {
  312. for(int x=0; x<sur->w; ++x)
  313. {
  314. Uint8* cp = (Uint8*)sur->pixels + y*sur->pitch + x*3;
  315. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  316. {
  317. if(cp[2]>cp[1] && cp[2]>cp[0])
  318. {
  319. std::vector<long long int> sort1;
  320. sort1.push_back(cp[0]);
  321. sort1.push_back(cp[1]);
  322. sort1.push_back(cp[2]);
  323. std::vector< std::pair<long long int, Uint8*> > sort2;
  324. sort2.push_back(std::make_pair(graphics->playerColors[player].r, &(cp[0])));
  325. sort2.push_back(std::make_pair(graphics->playerColors[player].g, &(cp[1])));
  326. sort2.push_back(std::make_pair(graphics->playerColors[player].b, &(cp[2])));
  327. std::sort(sort1.begin(), sort1.end());
  328. if(sort2[0].first>sort2[1].first)
  329. std::swap(sort2[0], sort2[1]);
  330. if(sort2[1].first>sort2[2].first)
  331. std::swap(sort2[1], sort2[2]);
  332. if(sort2[0].first>sort2[1].first)
  333. std::swap(sort2[0], sort2[1]);
  334. for(int hh=0; hh<3; ++hh)
  335. {
  336. (*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2.2;
  337. }
  338. }
  339. }
  340. else
  341. {
  342. if(
  343. (/*(mode==0) && (cp[0]>cp[1]) && (cp[0]>cp[2])) ||
  344. ((mode==1) &&*/ (cp[2]<45) && (cp[0]>80) && (cp[1]<70) && ((cp[0]-cp[1])>40))
  345. )
  346. {
  347. std::vector<long long int> sort1;
  348. sort1.push_back(cp[2]);
  349. sort1.push_back(cp[1]);
  350. sort1.push_back(cp[0]);
  351. std::vector< std::pair<long long int, Uint8*> > sort2;
  352. sort2.push_back(std::make_pair(graphics->playerColors[player].r, &(cp[2])));
  353. sort2.push_back(std::make_pair(graphics->playerColors[player].g, &(cp[1])));
  354. sort2.push_back(std::make_pair(graphics->playerColors[player].b, &(cp[0])));
  355. std::sort(sort1.begin(), sort1.end());
  356. if(sort2[0].first>sort2[1].first)
  357. std::swap(sort2[0], sort2[1]);
  358. if(sort2[1].first>sort2[2].first)
  359. std::swap(sort2[1], sort2[2]);
  360. if(sort2[0].first>sort2[1].first)
  361. std::swap(sort2[0], sort2[1]);
  362. for(int hh=0; hh<3; ++hh)
  363. {
  364. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }
  371. }
  372. void Graphics::loadTrueType()
  373. {
  374. for(int i = 0; i < FONTS_NUMBER; i++)
  375. fontsTrueType[i] = NULL;
  376. }
  377. Font * Graphics::loadFont( const char * name )
  378. {
  379. ui8 * hlp = CResourceHandler::get()->loadData(
  380. ResourceID(std::string("DATA/") + name, EResType::FONT)).first.release();
  381. if(!hlp)
  382. {
  383. tlog1 << "Error: cannot load font: " << name << std::endl;
  384. return NULL;
  385. }
  386. int magic = SDL_SwapLE32(*(const Uint32*)hlp);
  387. if(magic != 589598 && magic != 589599)
  388. tlog1 << "Suspicious font file, fname " << name << "n";
  389. Font *ret = new Font(hlp);
  390. return ret;
  391. }
  392. void Graphics::loadFonts()
  393. {
  394. static const char *fontnames [] = {"BIGFONT.FNT", "CALLI10R.FNT", "CREDITS.FNT", "HISCORE.FNT", "MEDFONT.FNT",
  395. "SMALFONT.FNT", "TIMES08R.FNT", "TINY.FNT", "VERD10B.FNT"} ;
  396. assert(ARRAY_COUNT(fontnames) == FONTS_NUMBER);
  397. for(int i = 0; i < FONTS_NUMBER; i++)
  398. fonts[i] = loadFont(fontnames[i]);
  399. }
  400. CDefEssential * Graphics::getDef( const CGObjectInstance * obj )
  401. {
  402. return advmapobjGraphics[obj->defInfo->name];
  403. }
  404. CDefEssential * Graphics::getDef( const CGDefInfo * info )
  405. {
  406. return advmapobjGraphics[info->name];
  407. }
  408. void Graphics::loadErmuToPicture()
  409. {
  410. //loading ERMU to picture
  411. const JsonNode config(ResourceID("config/ERMU_to_picture.json"));
  412. int etp_idx = 0;
  413. BOOST_FOREACH(const JsonNode &etp, config["ERMU_to_picture"].Vector()) {
  414. int idx = 0;
  415. BOOST_FOREACH(const JsonNode &n, etp.Vector()) {
  416. ERMUtoPicture[idx][etp_idx] = n.String();
  417. idx ++;
  418. }
  419. assert (idx == ARRAY_COUNT(ERMUtoPicture));
  420. etp_idx ++;
  421. }
  422. assert (etp_idx == 44);
  423. }
  424. Font::Font(ui8 *Data)
  425. {
  426. data = Data;
  427. int i = 0;
  428. height = data[5];
  429. i = 32;
  430. for(int ci = 0; ci < 256; ci++)
  431. {
  432. chars[ci].leftOffset = read_le_u32(data + i); i+=4;
  433. chars[ci].width = read_le_u32(data + i); i+=4;
  434. chars[ci].rightOffset = read_le_u32(data + i); i+=4;
  435. //if(ci>=30)
  436. // tlog0 << ci << ". (" << (char)ci << "). Width: " << chars[ci].width << " U1/U2:" << chars[ci].unknown1 << "/" << chars[ci].unknown2 << std::endl;
  437. }
  438. for(int ci = 0; ci < 256; ci++)
  439. {
  440. int offset = read_le_u32(data + i); i+=4;
  441. chars[ci].pixels = data + 4128 + offset;
  442. }
  443. }
  444. Font::~Font()
  445. {
  446. delete [] data;
  447. }
  448. int Font::getWidth(const char *text ) const
  449. {
  450. int length = std::strlen(text);
  451. int ret = 0;
  452. for(int i = 0; i < length; i++)
  453. {
  454. ui8 c = text[i];
  455. ret += chars[c].width + chars[c].leftOffset + chars[c].rightOffset;
  456. }
  457. return ret;
  458. }
  459. int Font::getCharWidth( char c ) const
  460. {
  461. const Char &C = chars[(ui8)c];
  462. return C.width + C.leftOffset + C.rightOffset;;
  463. }
  464. /*
  465. void Font::WriteAt(const char *text, SDL_Surface *sur, int x, int y )
  466. {
  467. SDL_Surface *SDL_CreateRGBSurfaceFrom(pixels, w, h, 8, int pitch,
  468. 224, 28, 3, 0);
  469. }
  470. */