Graphics.cpp 23 KB

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