Graphics.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  1. #include "StdInc.h"
  2. #include "Graphics.h"
  3. #include "CDefHandler.h"
  4. #include "UIFramework/SDL_Extensions.h"
  5. #include <SDL_ttf.h>
  6. #include "../lib/CThreadHelper.h"
  7. #include "CGameInfo.h"
  8. #include "../lib/CLodHandler.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. SDL_Surface * Graphics::drawHeroInfoWin(const InfoAboutHero &curh)
  42. {
  43. char buf[15];
  44. blueToPlayersAdv(hInfo,curh.owner);
  45. SDL_Surface * ret = SDL_DisplayFormat(hInfo);
  46. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format,0,255,255));
  47. printAt(curh.name,75,13,FONT_SMALL,Colors::Cornsilk,ret); //name
  48. blitAt(graphics->portraitLarge[curh.portrait],11,12,ret); //portrait
  49. //army
  50. for (ArmyDescriptor::const_iterator i = curh.army.begin(); i!=curh.army.end();i++)
  51. {
  52. blitAt(graphics->smallImgs[i->second.type->idNumber],slotsPos[(*i).first].first+1,slotsPos[(*i).first].second+1,ret);
  53. if(curh.details)
  54. {
  55. SDL_itoa((*i).second.count,buf,10);
  56. printAtMiddle(buf,slotsPos[(*i).first].first+17,slotsPos[(*i).first].second+41,FONT_TINY,Colors::Cornsilk,ret);
  57. }
  58. else
  59. {
  60. printAtMiddle(VLC->generaltexth->arraytxt[174 + 3*(i->second.count)],slotsPos[(*i).first].first+17,slotsPos[(*i).first].second+41,FONT_TINY,Colors::Cornsilk,ret);
  61. }
  62. }
  63. if(curh.details)
  64. {
  65. for (int i = 0; i < GameConstants::PRIMARY_SKILLS; i++)
  66. {
  67. SDL_itoa(curh.details->primskills[i], buf, 10);
  68. printAtMiddle(buf,84+28*i,70,FONT_SMALL,Colors::Cornsilk,ret);
  69. }
  70. //mana points
  71. SDL_itoa(curh.details->mana,buf,10);
  72. printAtMiddle(buf,167,108,FONT_TINY,Colors::Cornsilk,ret);
  73. blitAt(morale22->ourImages[curh.details->morale+3].bitmap,14,84,ret); //luck
  74. blitAt(luck22->ourImages[curh.details->morale+3].bitmap,14,101,ret); //morale
  75. }
  76. return ret;
  77. }
  78. SDL_Surface * Graphics::drawHeroInfoWin(const CGHeroInstance * curh)
  79. {
  80. InfoAboutHero iah;
  81. iah.initFromHero(curh, true);
  82. return drawHeroInfoWin(iah);
  83. }
  84. SDL_Surface * Graphics::drawTownInfoWin(const CGTownInstance * curh)
  85. {
  86. InfoAboutTown iah;
  87. iah.initFromTown(curh, true);
  88. return drawTownInfoWin(iah);
  89. }
  90. SDL_Surface * Graphics::drawTownInfoWin( const InfoAboutTown & curh )
  91. {
  92. char buf[10];
  93. blueToPlayersAdv(tInfo,curh.owner);
  94. SDL_Surface * ret = SDL_DisplayFormat(tInfo);
  95. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format,0,255,255));
  96. printAt(curh.name,75,12,FONT_SMALL,Colors::Cornsilk,ret); //name
  97. int pom = curh.fortLevel - 1; if(pom<0) pom = 3; //fort pic id
  98. blitAt(forts->ourImages[pom].bitmap,115,42,ret); //fort
  99. for (ArmyDescriptor::const_iterator i=curh.army.begin(); i!=curh.army.end();i++)
  100. {
  101. //if(!i->second.second)
  102. // continue;
  103. blitAt(graphics->smallImgs[(*i).second.type->idNumber],slotsPos[(*i).first].first+1,slotsPos[(*i).first].second+1,ret);
  104. if(curh.details)
  105. {
  106. // Show exact creature amount.
  107. SDL_itoa((*i).second.count,buf,10);
  108. printAtMiddle(buf,slotsPos[(*i).first].first+17,slotsPos[(*i).first].second+41,FONT_TINY,Colors::Cornsilk,ret);
  109. }
  110. else
  111. {
  112. // Show only a rough amount for creature stacks.
  113. // TODO: Deal with case when no information at all about size shold be presented.
  114. std::string roughAmount = curh.obj->getRoughAmount(i->first);
  115. printAtMiddle(roughAmount,slotsPos[(*i).first].first+17,slotsPos[(*i).first].second+41,FONT_TINY,Colors::Cornsilk,ret);
  116. }
  117. }
  118. //blit town icon
  119. if (curh.tType) {
  120. pom = curh.tType->typeID*2;
  121. if (!curh.fortLevel)
  122. pom += GameConstants::F_NUMBER*2;
  123. if(curh.built)
  124. pom++;
  125. blitAt(bigTownPic->ourImages[pom].bitmap,13,13,ret);
  126. }
  127. if(curh.details)
  128. {
  129. //hall level icon
  130. if((pom=curh.details->hallLevel) >= 0)
  131. blitAt(halls->ourImages[pom].bitmap, 77, 42, ret);
  132. if (curh.details->goldIncome >= 0) {
  133. SDL_itoa(curh.details->goldIncome, buf, 10); //gold income
  134. printAtMiddle(buf, 167, 70, FONT_TINY, Colors::Cornsilk, ret);
  135. }
  136. if(curh.details->garrisonedHero) //garrisoned hero icon
  137. blitAt(graphics->heroInGarrison,158,87,ret);
  138. }
  139. return ret;
  140. }
  141. void Graphics::loadPaletteAndColors()
  142. {
  143. std::string pals = bitmaph->getTextFile("PLAYERS.PAL", FILE_OTHER);
  144. playerColorPalette = new SDL_Color[256];
  145. neutralColor = new SDL_Color;
  146. playerColors = new SDL_Color[GameConstants::PLAYER_LIMIT];
  147. int startPoint = 24; //beginning byte; used to read
  148. for(int i=0; i<256; ++i)
  149. {
  150. SDL_Color col;
  151. col.r = pals[startPoint++];
  152. col.g = pals[startPoint++];
  153. col.b = pals[startPoint++];
  154. col.unused = pals[startPoint++];
  155. playerColorPalette[i] = col;
  156. }
  157. neutralColorPalette = new SDL_Color[32];
  158. std::ifstream ncp;
  159. std::string neutralFile = GameConstants::DATA_DIR + "/config/NEUTRAL.PAL";
  160. ncp.open(neutralFile.c_str(), std::ios::binary);
  161. for(int i=0; i<32; ++i)
  162. {
  163. ncp.read((char*)&neutralColorPalette[i].r,1);
  164. ncp.read((char*)&neutralColorPalette[i].g,1);
  165. ncp.read((char*)&neutralColorPalette[i].b,1);
  166. ncp.read((char*)&neutralColorPalette[i].unused,1);
  167. neutralColorPalette[i].unused = !neutralColorPalette[i].unused;
  168. }
  169. //colors initialization
  170. int3 kolory[] = {int3(0xff,0,0),int3(0x31,0x52,0xff),int3(0x9c,0x73,0x52),int3(0x42,0x94,0x29),
  171. int3(0xff,0x84,0x0),int3(0x8c,0x29,0xa5),int3(0x09,0x9c,0xa5),int3(0xc6,0x7b,0x8c)};
  172. for(int i=0;i<8;i++)
  173. {
  174. playerColors[i].r = kolory[i].x;
  175. playerColors[i].g = kolory[i].y;
  176. playerColors[i].b = kolory[i].z;
  177. playerColors[i].unused = 255;
  178. }
  179. neutralColor->r = 0x84; neutralColor->g = 0x84; neutralColor->b = 0x84; neutralColor->unused = 255;//gray
  180. const JsonNode config(GameConstants::DATA_DIR + "/config/town_pictures.json");
  181. BOOST_FOREACH(const JsonNode &p, config["town_pictures"].Vector()) {
  182. townBgs.push_back(p["town_background"].String());
  183. guildBgs.push_back(p["guild_background"].String());
  184. buildingPics.push_back(p["building_picture"].String());
  185. }
  186. }
  187. void Graphics::initializeBattleGraphics()
  188. {
  189. const JsonNode config(GameConstants::DATA_DIR + "/config/battles_graphics.json");
  190. // Reserve enough space for the terrains
  191. int idx = config["backgrounds"].Vector().size();
  192. battleBacks.resize(idx+1); // 1 to idx, 0 is unused
  193. idx = 1;
  194. BOOST_FOREACH(const JsonNode &t, config["backgrounds"].Vector()) {
  195. battleBacks[idx].push_back(t.String());
  196. idx++;
  197. }
  198. //initializing battle hero animation
  199. idx = config["heroes"].Vector().size();
  200. battleHeroes.resize(idx);
  201. idx = 0;
  202. BOOST_FOREACH(const JsonNode &h, config["heroes"].Vector()) {
  203. battleHeroes[idx] = h.String();
  204. idx ++;
  205. }
  206. //initialization of AC->def name mapping
  207. BOOST_FOREACH(const JsonNode &ac, config["ac_mapping"].Vector()) {
  208. int ACid = ac["id"].Float();
  209. std::vector< std::string > toAdd;
  210. BOOST_FOREACH(const JsonNode &defname, ac["defnames"].Vector()) {
  211. toAdd.push_back(defname.String());
  212. }
  213. battleACToDef[ACid] = toAdd;
  214. }
  215. spellEffectsPics = CDefHandler::giveDefEss("SpellInt.def");
  216. }
  217. Graphics::Graphics()
  218. {
  219. slotsPos.push_back(std::pair<int,int>(44,82));
  220. slotsPos.push_back(std::pair<int,int>(80,82));
  221. slotsPos.push_back(std::pair<int,int>(116,82));
  222. slotsPos.push_back(std::pair<int,int>(26,131));
  223. slotsPos.push_back(std::pair<int,int>(62,131));
  224. slotsPos.push_back(std::pair<int,int>(98,131));
  225. slotsPos.push_back(std::pair<int,int>(134,131));
  226. CDefHandler *smi, *smi2;
  227. std::vector<Task> tasks; //preparing list of graphics to load
  228. tasks += boost::bind(&Graphics::loadFonts,this);
  229. tasks += boost::bind(&Graphics::loadTrueType,this);
  230. tasks += boost::bind(&Graphics::loadPaletteAndColors,this);
  231. tasks += boost::bind(&Graphics::loadHeroFlags,this);
  232. tasks += boost::bind(&Graphics::loadHeroPortraits,this);
  233. tasks += boost::bind(&Graphics::initializeBattleGraphics,this);
  234. tasks += boost::bind(&Graphics::loadWallPositions,this);
  235. tasks += boost::bind(&Graphics::loadErmuToPicture,this);
  236. tasks += GET_SURFACE(hInfo,"HEROQVBK.bmp");
  237. tasks += GET_SURFACE(tInfo,"TOWNQVBK.bmp");
  238. tasks += GET_SURFACE(heroInGarrison,"TOWNQKGH.bmp");
  239. tasks += GET_DEF_ESS(artDefs,"ARTIFACT.DEF");
  240. tasks += GET_DEF_ESS(forts,"ITMCLS.DEF");
  241. tasks += GET_DEF_ESS(luck22,"ILCK22.DEF");
  242. tasks += GET_DEF_ESS(luck30,"ILCK30.DEF");
  243. tasks += GET_DEF_ESS(luck42,"ILCK42.DEF");
  244. tasks += GET_DEF_ESS(luck82,"ILCK82.DEF");
  245. tasks += GET_DEF_ESS(morale22,"IMRL22.DEF");
  246. tasks += GET_DEF_ESS(morale30,"IMRL30.DEF");
  247. tasks += GET_DEF_ESS(morale42,"IMRL42.DEF");
  248. tasks += GET_DEF_ESS(morale82,"IMRL82.DEF");
  249. tasks += GET_DEF_ESS(halls,"ITMTLS.DEF");
  250. tasks += GET_DEF_ESS(bigTownPic,"ITPT.DEF");
  251. tasks += GET_DEF_ESS(pskillsb,"PSKILL.DEF");
  252. tasks += GET_DEF_ESS(pskillsm,"PSKIL42.DEF");
  253. tasks += GET_DEF_ESS(pskillst,"PSKIL32.DEF");
  254. tasks += GET_DEF_ESS(resources,"RESOUR82.DEF");
  255. tasks += GET_DEF_ESS(un32,"UN32.DEF");
  256. tasks += GET_DEF_ESS(un44,"UN44.DEF");
  257. tasks += GET_DEF_ESS(smallIcons,"ITPA.DEF");
  258. tasks += GET_DEF_ESS(resources32,"RESOURCE.DEF");
  259. tasks += GET_DEF(smi,"CPRSMALL.DEF");
  260. tasks += GET_DEF(smi2,"TWCRPORT.DEF");
  261. tasks += GET_DEF_ESS(flags,"CREST58.DEF");
  262. tasks += GET_DEF_ESS(abils32,"SECSK32.DEF");
  263. tasks += GET_DEF_ESS(abils44,"SECSKILL.DEF");
  264. tasks += GET_DEF_ESS(abils82,"SECSK82.DEF");
  265. tasks += GET_DEF_ESS(spellscr,"SPELLSCR.DEF");
  266. tasks += GET_DEF_ESS(heroMoveArrows,"ADAG.DEF");
  267. const JsonNode config(GameConstants::DATA_DIR + "/config/creature_backgrounds.json");
  268. BOOST_FOREACH(const JsonNode &b, config["backgrounds"].Vector()) {
  269. const int id = b["id"].Float();
  270. tasks += GET_SURFACE(backgrounds[id], b["bg130"].String());
  271. tasks += GET_SURFACE(backgroundsm[id], b["bg120"].String());
  272. }
  273. CThreadHelper th(&tasks,std::max((ui32)1,boost::thread::hardware_concurrency()));
  274. th.run();
  275. for(size_t y=0; y < heroMoveArrows->ourImages.size(); ++y)
  276. {
  277. CSDL_Ext::alphaTransform(heroMoveArrows->ourImages[y].bitmap);
  278. }
  279. //handling 32x32px imgs
  280. smi->notFreeImgs = true;
  281. for (size_t i=0; i<smi->ourImages.size(); ++i)
  282. {
  283. smallImgs[i-2] = smi->ourImages[i].bitmap;
  284. }
  285. delete smi;
  286. smi2->notFreeImgs = true;
  287. for (size_t i=0; i<smi2->ourImages.size(); ++i)
  288. {
  289. bigImgs[i-2] = smi2->ourImages[i].bitmap;
  290. }
  291. //hack for green color on big infernal troglodite image - Mantis #758
  292. SDL_Color green = {0x30, 0x5c, 0x20, SDL_ALPHA_OPAQUE};
  293. bigImgs[71]->format->palette->colors[7] = green;
  294. delete smi2;
  295. }
  296. void Graphics::loadHeroPortraits()
  297. {
  298. const JsonNode config(GameConstants::DATA_DIR + "/config/portraits.json");
  299. BOOST_FOREACH(const JsonNode &portrait_node, config["hero_portrait"].Vector()) {
  300. std::string filename = portrait_node["filename"].String();
  301. /* Small portrait. */
  302. portraitSmall.push_back(BitmapHandler::loadBitmap(filename));
  303. /* Large portrait. Alter the filename. Size letter is usually
  304. * third one, but there are exceptions and it should fix the
  305. * problem. */
  306. for (int ff=0; ff<filename.size(); ++ff)
  307. {
  308. if (filename[ff]=='S') {
  309. filename[ff]='L';
  310. break;
  311. }
  312. }
  313. portraitLarge.push_back(BitmapHandler::loadBitmap(filename));
  314. SDL_SetColorKey(portraitLarge[portraitLarge.size()-1],SDL_SRCCOLORKEY,SDL_MapRGB(portraitLarge[portraitLarge.size()-1]->format,0,255,255));
  315. }
  316. }
  317. void Graphics::loadWallPositions()
  318. {
  319. const JsonNode config(GameConstants::DATA_DIR + "/config/wall_pos.json");
  320. BOOST_FOREACH(const JsonNode &town, config["towns"].Vector()) {
  321. int townID = town["id"].Float();
  322. BOOST_FOREACH(const JsonNode &coords, town["pos"].Vector()) {
  323. Point pt(coords["x"].Float(), coords["y"].Float());
  324. wallPositions[townID].push_back(pt);
  325. }
  326. assert(wallPositions[townID].size() == 21);
  327. }
  328. }
  329. void Graphics::loadHeroAnims()
  330. {
  331. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  332. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12), std::make_pair(1,13),
  333. std::make_pair(2,14), std::make_pair(3,15);
  334. for(size_t i=0; i<GameConstants::F_NUMBER * 2; ++i)
  335. {
  336. std::ostringstream nm;
  337. nm << "AH" << std::setw(2) << std::setfill('0') << i << "_.DEF";
  338. loadHeroAnim(nm.str(), rotations, &Graphics::heroAnims);
  339. std::string name = nm.str();
  340. }
  341. loadHeroAnim("AB01_.DEF", rotations, &Graphics::boatAnims);
  342. loadHeroAnim("AB02_.DEF", rotations, &Graphics::boatAnims);
  343. loadHeroAnim("AB03_.DEF", rotations, &Graphics::boatAnims);
  344. }
  345. void Graphics::loadHeroAnim( const std::string &name, const std::vector<std::pair<int,int> > &rotations, std::vector<CDefEssential *> Graphics::*dst )
  346. {
  347. CDefEssential *anim = CDefHandler::giveDefEss(name);
  348. (this->*dst).push_back(anim);
  349. int pom = 0; //how many groups has been rotated
  350. for(int o=7; pom<6; ++o)
  351. {
  352. for(int p=0;p<6;p++)
  353. {
  354. if(anim->ourImages[o].groupNumber == rotations[p].first)
  355. {
  356. for(int e=0; e<8; ++e)
  357. {
  358. Cimage nci;
  359. nci.bitmap = CSDL_Ext::rotate01(anim->ourImages[o+e].bitmap);
  360. nci.groupNumber = rotations[p].second;
  361. nci.imName = std::string();
  362. anim->ourImages.push_back(nci);
  363. if(pom>2) //we need only one frame for groups 13/14/15
  364. break;
  365. }
  366. if(pom<3) //there are eight frames of animtion of groups 6/7/8 so for speed we'll skip them
  367. o+=8;
  368. else //there is only one frame of 1/2/3
  369. o+=1;
  370. ++pom;
  371. if(p==2 && pom<4) //group1 starts at index 1
  372. o = 1;
  373. }
  374. }
  375. }
  376. for(size_t ff=0; ff<anim->ourImages.size(); ++ff)
  377. {
  378. CSDL_Ext::alphaTransform(anim->ourImages[ff].bitmap);
  379. }
  380. }
  381. void Graphics::loadHeroFlags(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr, bool mode)
  382. {
  383. for(int i=0;i<8;i++)
  384. (this->*pr.first).push_back(CDefHandler::giveDefEss(pr.second[i]));
  385. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  386. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12);
  387. for(int q=0; q<8; ++q)
  388. {
  389. std::vector<Cimage> &curImgs = (this->*pr.first)[q]->ourImages;
  390. for(size_t o=0; o<curImgs.size(); ++o)
  391. {
  392. for(size_t p=0; p<rotations.size(); p++)
  393. {
  394. if(curImgs[o].groupNumber==rotations[p].first)
  395. {
  396. for(int e=0; e<8; ++e)
  397. {
  398. Cimage nci;
  399. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  400. nci.groupNumber = rotations[p].second;
  401. nci.imName = std::string();
  402. curImgs.push_back(nci);
  403. }
  404. o+=8;
  405. }
  406. }
  407. }
  408. if (mode)
  409. {
  410. for(size_t o=0; o<curImgs.size(); ++o)
  411. {
  412. if(curImgs[o].groupNumber==1 || curImgs[o].groupNumber==2 || curImgs[o].groupNumber==3)
  413. {
  414. for(int e=0; e<8; ++e)
  415. {
  416. Cimage nci;
  417. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  418. nci.groupNumber = 12 + curImgs[o].groupNumber;
  419. nci.imName = std::string();
  420. curImgs.push_back(nci);
  421. }
  422. o+=8;
  423. }
  424. }
  425. }
  426. for(size_t ff=0; ff<curImgs.size(); ++ff)
  427. {
  428. SDL_SetColorKey(curImgs[ff].bitmap, SDL_SRCCOLORKEY,
  429. SDL_MapRGB(curImgs[ff].bitmap->format, 0, 255, 255)
  430. );
  431. }
  432. }
  433. }
  434. void Graphics::loadHeroFlags()
  435. {
  436. using namespace boost::assign;
  437. CStopWatch th;
  438. std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > pr[4];
  439. pr[0].first = &Graphics::flags1;
  440. pr[0].second+=("ABF01L.DEF"),("ABF01G.DEF"),("ABF01R.DEF"),("ABF01D.DEF"),("ABF01B.DEF"),
  441. ("ABF01P.DEF"),("ABF01W.DEF"),("ABF01K.DEF");
  442. pr[1].first = &Graphics::flags2;
  443. pr[1].second+=("ABF02L.DEF"),("ABF02G.DEF"),("ABF02R.DEF"),("ABF02D.DEF"),("ABF02B.DEF"),
  444. ("ABF02P.DEF"),("ABF02W.DEF"),("ABF02K.DEF");
  445. pr[2].first = &Graphics::flags3;
  446. pr[2].second+=("ABF03L.DEF"),("ABF03G.DEF"),("ABF03R.DEF"),("ABF03D.DEF"),("ABF03B.DEF"),
  447. ("ABF03P.DEF"),("ABF03W.DEF"),("ABF03K.DEF");
  448. pr[3].first = &Graphics::flags4;
  449. pr[3].second+=("AF00.DEF"),("AF01.DEF"),("AF02.DEF"),("AF03.DEF"),("AF04.DEF"),
  450. ("AF05.DEF"),("AF06.DEF"),("AF07.DEF");
  451. boost::thread_group grupa;
  452. for(int g=3; g>=0; --g)
  453. {
  454. grupa.create_thread(boost::bind(&Graphics::loadHeroFlags,this,boost::ref(pr[g]),true));
  455. }
  456. grupa.join_all();
  457. tlog0 << "Loading and transforming heroes' flags: "<<th.getDiff()<<std::endl;
  458. }
  459. SDL_Surface * Graphics::getPic(int ID, bool fort, bool builded)
  460. {
  461. if (ID==-1)
  462. return smallIcons->ourImages[0].bitmap;
  463. else if (ID==-2)
  464. return smallIcons->ourImages[1].bitmap;
  465. else if (ID==-3)
  466. return smallIcons->ourImages[2+GameConstants::F_NUMBER*4].bitmap;
  467. else if (ID>GameConstants::F_NUMBER || ID<-3)
  468. #ifndef __GNUC__
  469. throw new std::exception("Invalid ID");
  470. #else
  471. throw new std::exception();
  472. #endif
  473. else
  474. {
  475. int pom = 3;
  476. if(!fort)
  477. pom+=GameConstants::F_NUMBER*2;
  478. pom += ID*2;
  479. if (!builded)
  480. pom--;
  481. return smallIcons->ourImages[pom].bitmap;
  482. }
  483. }
  484. void Graphics::blueToPlayersAdv(SDL_Surface * sur, int player)
  485. {
  486. // if(player==1) //it is actually blue...
  487. // return;
  488. if(sur->format->BitsPerPixel == 8)
  489. {
  490. SDL_Color *palette = NULL;
  491. if(player < GameConstants::PLAYER_LIMIT && player >= 0)
  492. {
  493. palette = playerColorPalette + 32*player;
  494. }
  495. else if(player == 255 || player == -1)
  496. {
  497. palette = neutralColorPalette;
  498. }
  499. else
  500. {
  501. tlog1 << "Wrong player id in blueToPlayersAdv (" << player << ")!\n";
  502. return;
  503. }
  504. SDL_SetColors(sur, palette, 224, 32);
  505. //for(int i=0; i<32; ++i)
  506. //{
  507. // sur->format->palette->colors[224+i] = palette[i];
  508. //}
  509. }
  510. else if(sur->format->BitsPerPixel == 24) //should never happen in general
  511. {
  512. for(int y=0; y<sur->h; ++y)
  513. {
  514. for(int x=0; x<sur->w; ++x)
  515. {
  516. Uint8* cp = (Uint8*)sur->pixels + y*sur->pitch + x*3;
  517. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  518. {
  519. if(cp[2]>cp[1] && cp[2]>cp[0])
  520. {
  521. std::vector<long long int> sort1;
  522. sort1.push_back(cp[0]);
  523. sort1.push_back(cp[1]);
  524. sort1.push_back(cp[2]);
  525. std::vector< std::pair<long long int, Uint8*> > sort2;
  526. sort2.push_back(std::make_pair(graphics->playerColors[player].r, &(cp[0])));
  527. sort2.push_back(std::make_pair(graphics->playerColors[player].g, &(cp[1])));
  528. sort2.push_back(std::make_pair(graphics->playerColors[player].b, &(cp[2])));
  529. std::sort(sort1.begin(), sort1.end());
  530. if(sort2[0].first>sort2[1].first)
  531. std::swap(sort2[0], sort2[1]);
  532. if(sort2[1].first>sort2[2].first)
  533. std::swap(sort2[1], sort2[2]);
  534. if(sort2[0].first>sort2[1].first)
  535. std::swap(sort2[0], sort2[1]);
  536. for(int hh=0; hh<3; ++hh)
  537. {
  538. (*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2.2;
  539. }
  540. }
  541. }
  542. else
  543. {
  544. if(
  545. (/*(mode==0) && (cp[0]>cp[1]) && (cp[0]>cp[2])) ||
  546. ((mode==1) &&*/ (cp[2]<45) && (cp[0]>80) && (cp[1]<70) && ((cp[0]-cp[1])>40))
  547. )
  548. {
  549. std::vector<long long int> sort1;
  550. sort1.push_back(cp[2]);
  551. sort1.push_back(cp[1]);
  552. sort1.push_back(cp[0]);
  553. std::vector< std::pair<long long int, Uint8*> > sort2;
  554. sort2.push_back(std::make_pair(graphics->playerColors[player].r, &(cp[2])));
  555. sort2.push_back(std::make_pair(graphics->playerColors[player].g, &(cp[1])));
  556. sort2.push_back(std::make_pair(graphics->playerColors[player].b, &(cp[0])));
  557. std::sort(sort1.begin(), sort1.end());
  558. if(sort2[0].first>sort2[1].first)
  559. std::swap(sort2[0], sort2[1]);
  560. if(sort2[1].first>sort2[2].first)
  561. std::swap(sort2[1], sort2[2]);
  562. if(sort2[0].first>sort2[1].first)
  563. std::swap(sort2[0], sort2[1]);
  564. for(int hh=0; hh<3; ++hh)
  565. {
  566. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  567. }
  568. }
  569. }
  570. }
  571. }
  572. }
  573. }
  574. void Graphics::loadTrueType()
  575. {
  576. bool ttfPresent = false;//was TTF initialised or not
  577. for(int i = 0; i < FONTS_NUMBER; i++)
  578. fontsTrueType[i] = NULL;
  579. std::string fontsFile = GameConstants::DATA_DIR + "/config/fonts.txt";
  580. std::ifstream ff(fontsFile.c_str());
  581. while(!ff.eof())
  582. {
  583. int enabl, fntID, fntSize;
  584. std::string fntName;
  585. ff >> enabl;//enabled font or not
  586. if (enabl==-1)
  587. break;//end of data
  588. ff >> fntID;//what font will be replaced
  589. ff >> fntName;//name of truetype font
  590. ff >> fntSize;//size of font
  591. if (enabl)
  592. {
  593. if (!ttfPresent)
  594. {
  595. ttfPresent = true;
  596. TTF_Init();
  597. atexit(TTF_Quit);
  598. };
  599. fntName = GameConstants::DATA_DIR + ( "/Fonts/" + fntName);
  600. fontsTrueType[fntID] = TTF_OpenFont(fntName.c_str(),fntSize);
  601. }
  602. }
  603. ff.close();
  604. ff.clear();
  605. }
  606. Font * Graphics::loadFont( const char * name )
  607. {
  608. int len = 0;
  609. ui8 * hlp = bitmaph->giveFile(name, FILE_FONT, &len);
  610. if(!hlp || !len)
  611. {
  612. tlog1 << "Error: cannot load font: " << name << std::endl;
  613. return NULL;
  614. }
  615. int magic = SDL_SwapLE32(*(const Uint32*)hlp);
  616. if(len < 10000 || (magic != 589598 && magic != 589599))
  617. {
  618. tlog1 << "Suspicious font file (length " << len <<", fname " << name << "), logging to suspicious_" << name << ".fnt\n";
  619. std::string suspFName = "suspicious_" + std::string(name) + ".fnt";
  620. std::ofstream o(suspFName.c_str());
  621. o.write((const char*)hlp, len);
  622. }
  623. Font *ret = new Font(hlp);
  624. return ret;
  625. }
  626. void Graphics::loadFonts()
  627. {
  628. static const char *fontnames [] = {"BIGFONT.FNT", "CALLI10R.FNT", "CREDITS.FNT", "HISCORE.FNT", "MEDFONT.FNT",
  629. "SMALFONT.FNT", "TIMES08R.FNT", "TINY.FNT", "VERD10B.FNT"} ;
  630. assert(ARRAY_COUNT(fontnames) == FONTS_NUMBER);
  631. for(int i = 0; i < FONTS_NUMBER; i++)
  632. fonts[i] = loadFont(fontnames[i]);
  633. }
  634. CDefEssential * Graphics::getDef( const CGObjectInstance * obj )
  635. {
  636. return advmapobjGraphics[obj->defInfo->id][obj->defInfo->subid][obj->defInfo->name];
  637. }
  638. CDefEssential * Graphics::getDef( const CGDefInfo * info )
  639. {
  640. return advmapobjGraphics[info->id][info->subid][info->name];
  641. }
  642. void Graphics::loadErmuToPicture()
  643. {
  644. //loading ERMU to picture
  645. const JsonNode config(GameConstants::DATA_DIR + "/config/ERMU_to_picture.json");
  646. int etp_idx = 0;
  647. BOOST_FOREACH(const JsonNode &etp, config["ERMU_to_picture"].Vector()) {
  648. int idx = 0;
  649. BOOST_FOREACH(const JsonNode &n, etp.Vector()) {
  650. ERMUtoPicture[idx][etp_idx] = n.String();
  651. idx ++;
  652. }
  653. assert (idx == ARRAY_COUNT(ERMUtoPicture));
  654. etp_idx ++;
  655. }
  656. assert (etp_idx == 44);
  657. }
  658. Font::Font(ui8 *Data)
  659. {
  660. data = Data;
  661. int i = 0;
  662. height = data[5];
  663. i = 32;
  664. for(int ci = 0; ci < 256; ci++)
  665. {
  666. chars[ci].leftOffset = read_le_u32(data + i); i+=4;
  667. chars[ci].width = read_le_u32(data + i); i+=4;
  668. chars[ci].rightOffset = read_le_u32(data + i); i+=4;
  669. //if(ci>=30)
  670. // tlog0 << ci << ". (" << (char)ci << "). Width: " << chars[ci].width << " U1/U2:" << chars[ci].unknown1 << "/" << chars[ci].unknown2 << std::endl;
  671. }
  672. for(int ci = 0; ci < 256; ci++)
  673. {
  674. int offset = read_le_u32(data + i); i+=4;
  675. chars[ci].pixels = data + 4128 + offset;
  676. }
  677. }
  678. Font::~Font()
  679. {
  680. delete [] data;
  681. }
  682. int Font::getWidth(const char *text ) const
  683. {
  684. int length = std::strlen(text);
  685. int ret = 0;
  686. for(int i = 0; i < length; i++)
  687. {
  688. ui8 c = text[i];
  689. ret += chars[c].width + chars[c].leftOffset + chars[c].rightOffset;
  690. }
  691. return ret;
  692. }
  693. int Font::getCharWidth( char c ) const
  694. {
  695. const Char &C = chars[(ui8)c];
  696. return C.width + C.leftOffset + C.rightOffset;;
  697. }
  698. /*
  699. void Font::WriteAt(const char *text, SDL_Surface *sur, int x, int y )
  700. {
  701. SDL_Surface *SDL_CreateRGBSurfaceFrom(pixels, w, h, 8, int pitch,
  702. 224, 28, 3, 0);
  703. }
  704. */