Graphics.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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-1)],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. std::ifstream ifs(DATA_DIR "/config/cr_bgs.txt");
  295. int id;
  296. std::string name;
  297. while(!ifs.eof())
  298. {
  299. ifs >> id >> name;
  300. tasks += GET_SURFACE(backgrounds[id],name);
  301. name.replace(0,5,"TPCAS");
  302. tasks += GET_SURFACE(backgroundsm[id],name);
  303. }
  304. CThreadHelper th(&tasks,std::max((unsigned int)1,boost::thread::hardware_concurrency()));
  305. th.run();
  306. //handling 32x32px imgs
  307. smi->notFreeImgs = true;
  308. for (size_t i=0; i<smi->ourImages.size(); ++i)
  309. {
  310. smallImgs[i-2] = smi->ourImages[i].bitmap;
  311. }
  312. delete smi;
  313. smi2->notFreeImgs = true;
  314. for (size_t i=0; i<smi2->ourImages.size(); ++i)
  315. {
  316. bigImgs[i-2] = smi2->ourImages[i].bitmap;
  317. }
  318. delete smi2;
  319. }
  320. void Graphics::loadHeroPortraits()
  321. {
  322. std::ifstream of(DATA_DIR "/config/portrety.txt");
  323. int numberOfPortraits;
  324. of>>numberOfPortraits;
  325. for (int j=0; j<numberOfPortraits; j++)
  326. {
  327. int ID;
  328. of>>ID;
  329. std::string path;
  330. of>>path;
  331. portraitSmall.push_back(BitmapHandler::loadBitmap(path));
  332. 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
  333. {
  334. if(path[ff]=='S')
  335. {
  336. path[ff]='L';
  337. break;
  338. }
  339. }
  340. portraitLarge.push_back(BitmapHandler::loadBitmap(path));
  341. SDL_SetColorKey(portraitLarge[portraitLarge.size()-1],SDL_SRCCOLORKEY,SDL_MapRGB(portraitLarge[portraitLarge.size()-1]->format,0,255,255));
  342. }
  343. of.close();
  344. }
  345. void Graphics::loadWallPositions()
  346. {
  347. std::ifstream inp;
  348. inp.open(DATA_DIR "/config/wall_pos.txt", std::ios_base::in|std::ios_base::binary);
  349. if(!inp.is_open())
  350. {
  351. tlog1<<"missing file: config/wall_pos.txt"<<std::endl;
  352. }
  353. else
  354. {
  355. const int MAX_BUF = 2000;
  356. char buf[MAX_BUF+1];
  357. inp.getline(buf, MAX_BUF);
  358. std::string dump;
  359. for(int g=0; g<ARRAY_COUNT(wallPositions); ++g)
  360. {
  361. inp >> dump;
  362. for(int b=0; b<12; ++b)
  363. {
  364. Point pt;
  365. inp >> pt.x;
  366. inp >> pt.y;
  367. wallPositions[g].push_back(pt);
  368. }
  369. }
  370. }
  371. inp.close();
  372. }
  373. void Graphics::loadHeroAnims()
  374. {
  375. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  376. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12), std::make_pair(1,13),
  377. std::make_pair(2,14), std::make_pair(3,15);
  378. for(size_t i=0; i<F_NUMBER * 2; ++i)
  379. {
  380. std::ostringstream nm;
  381. nm << "AH" << std::setw(2) << std::setfill('0') << i << "_.DEF";
  382. loadHeroAnim(nm.str(), rotations, &Graphics::heroAnims);
  383. std::string name = nm.str();
  384. }
  385. loadHeroAnim("AB01_.DEF", rotations, &Graphics::boatAnims);
  386. loadHeroAnim("AB02_.DEF", rotations, &Graphics::boatAnims);
  387. loadHeroAnim("AB03_.DEF", rotations, &Graphics::boatAnims);
  388. }
  389. void Graphics::loadHeroAnim( const std::string &name, const std::vector<std::pair<int,int> > &rotations, std::vector<CDefEssential *> Graphics::*dst )
  390. {
  391. CDefEssential *anim = CDefHandler::giveDefEss(name);
  392. (this->*dst).push_back(anim);
  393. int pom = 0; //how many groups has been rotated
  394. for(int o=7; pom<6; ++o)
  395. {
  396. for(int p=0;p<6;p++)
  397. {
  398. if(anim->ourImages[o].groupNumber == rotations[p].first)
  399. {
  400. for(int e=0; e<8; ++e)
  401. {
  402. Cimage nci;
  403. nci.bitmap = CSDL_Ext::rotate01(anim->ourImages[o+e].bitmap);
  404. nci.groupNumber = rotations[p].second;
  405. nci.imName = std::string();
  406. anim->ourImages.push_back(nci);
  407. if(pom>2) //we need only one frame for groups 13/14/15
  408. break;
  409. }
  410. if(pom<3) //there are eight frames of animtion of groups 6/7/8 so for speed we'll skip them
  411. o+=8;
  412. else //there is only one frame of 1/2/3
  413. o+=1;
  414. ++pom;
  415. if(p==2 && pom<4) //group1 starts at index 1
  416. o = 1;
  417. }
  418. }
  419. }
  420. for(size_t ff=0; ff<anim->ourImages.size(); ++ff)
  421. {
  422. CSDL_Ext::alphaTransform(anim->ourImages[ff].bitmap);
  423. }
  424. }
  425. void Graphics::loadHeroFlags(std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > &pr, bool mode)
  426. {
  427. for(int i=0;i<8;i++)
  428. (this->*pr.first).push_back(CDefHandler::giveDefEss(pr.second[i]));
  429. std::vector<std::pair<int,int> > rotations; //first - group number to be rotated1, second - group number after rotation1
  430. rotations += std::make_pair(6,10), std::make_pair(7,11), std::make_pair(8,12);
  431. for(int q=0; q<8; ++q)
  432. {
  433. std::vector<Cimage> &curImgs = (this->*pr.first)[q]->ourImages;
  434. for(size_t o=0; o<curImgs.size(); ++o)
  435. {
  436. for(size_t p=0; p<rotations.size(); p++)
  437. {
  438. if(curImgs[o].groupNumber==rotations[p].first)
  439. {
  440. for(int e=0; e<8; ++e)
  441. {
  442. Cimage nci;
  443. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  444. nci.groupNumber = rotations[p].second;
  445. nci.imName = std::string();
  446. curImgs.push_back(nci);
  447. }
  448. o+=8;
  449. }
  450. }
  451. }
  452. if (mode)
  453. {
  454. for(size_t o=0; o<curImgs.size(); ++o)
  455. {
  456. if(curImgs[o].groupNumber==1 || curImgs[o].groupNumber==2 || curImgs[o].groupNumber==3)
  457. {
  458. for(int e=0; e<8; ++e)
  459. {
  460. Cimage nci;
  461. nci.bitmap = CSDL_Ext::rotate01(curImgs[o+e].bitmap);
  462. nci.groupNumber = 12 + curImgs[o].groupNumber;
  463. nci.imName = std::string();
  464. curImgs.push_back(nci);
  465. }
  466. o+=8;
  467. }
  468. }
  469. }
  470. for(size_t ff=0; ff<curImgs.size(); ++ff)
  471. {
  472. SDL_SetColorKey(curImgs[ff].bitmap, SDL_SRCCOLORKEY,
  473. SDL_MapRGB(curImgs[ff].bitmap->format, 0, 255, 255)
  474. );
  475. }
  476. }
  477. }
  478. void Graphics::loadHeroFlags()
  479. {
  480. using namespace boost::assign;
  481. timeHandler th;
  482. std::pair<std::vector<CDefEssential *> Graphics::*, std::vector<const char *> > pr[4];
  483. pr[0].first = &Graphics::flags1;
  484. pr[0].second+=("ABF01L.DEF"),("ABF01G.DEF"),("ABF01R.DEF"),("ABF01D.DEF"),("ABF01B.DEF"),
  485. ("ABF01P.DEF"),("ABF01W.DEF"),("ABF01K.DEF");
  486. pr[1].first = &Graphics::flags2;
  487. pr[1].second+=("ABF02L.DEF"),("ABF02G.DEF"),("ABF02R.DEF"),("ABF02D.DEF"),("ABF02B.DEF"),
  488. ("ABF02P.DEF"),("ABF02W.DEF"),("ABF02K.DEF");
  489. pr[2].first = &Graphics::flags3;
  490. pr[2].second+=("ABF03L.DEF"),("ABF03G.DEF"),("ABF03R.DEF"),("ABF03D.DEF"),("ABF03B.DEF"),
  491. ("ABF03P.DEF"),("ABF03W.DEF"),("ABF03K.DEF");
  492. pr[3].first = &Graphics::flags4;
  493. pr[3].second+=("AF00.DEF"),("AF01.DEF"),("AF02.DEF"),("AF03.DEF"),("AF04.DEF"),
  494. ("AF05.DEF"),("AF06.DEF"),("AF07.DEF");
  495. boost::thread_group grupa;
  496. for(int g=3; g>=0; --g)
  497. {
  498. grupa.create_thread(boost::bind(&Graphics::loadHeroFlags,this,boost::ref(pr[g]),true));
  499. }
  500. grupa.join_all();
  501. tlog0 << "Loading and transforming heroes' flags: "<<th.getDif()<<std::endl;
  502. }
  503. SDL_Surface * Graphics::getPic(int ID, bool fort, bool builded)
  504. {
  505. if (ID==-1)
  506. return smallIcons->ourImages[0].bitmap;
  507. else if (ID==-2)
  508. return smallIcons->ourImages[1].bitmap;
  509. else if (ID==-3)
  510. return smallIcons->ourImages[2+F_NUMBER*4].bitmap;
  511. else if (ID>F_NUMBER || ID<-3)
  512. #ifndef __GNUC__
  513. throw new std::exception("Invalid ID");
  514. #else
  515. throw new std::exception();
  516. #endif
  517. else
  518. {
  519. int pom = 3;
  520. if(!fort)
  521. pom+=F_NUMBER*2;
  522. pom += ID*2;
  523. if (!builded)
  524. pom--;
  525. return smallIcons->ourImages[pom].bitmap;
  526. }
  527. }
  528. void Graphics::blueToPlayersAdv(SDL_Surface * sur, int player)
  529. {
  530. // if(player==1) //it is actually blue...
  531. // return;
  532. if(sur->format->BitsPerPixel == 8)
  533. {
  534. SDL_Color *palette = NULL;
  535. if(player < PLAYER_LIMIT && player >= 0)
  536. {
  537. palette = playerColorPalette + 32*player;
  538. }
  539. else if(player == 255 || player == -1)
  540. {
  541. palette = neutralColorPalette;
  542. }
  543. else
  544. {
  545. tlog1 << "Wrong player id in blueToPlayersAdv (" << player << ")!\n";
  546. return;
  547. }
  548. SDL_SetColors(sur, palette, 224, 32);
  549. //for(int i=0; i<32; ++i)
  550. //{
  551. // sur->format->palette->colors[224+i] = palette[i];
  552. //}
  553. }
  554. else if(sur->format->BitsPerPixel == 24) //should never happen in general
  555. {
  556. for(int y=0; y<sur->h; ++y)
  557. {
  558. for(int x=0; x<sur->w; ++x)
  559. {
  560. Uint8* cp = (Uint8*)sur->pixels + y*sur->pitch + x*3;
  561. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  562. {
  563. if(cp[2]>cp[1] && cp[2]>cp[0])
  564. {
  565. std::vector<long long int> sort1;
  566. sort1.push_back(cp[0]);
  567. sort1.push_back(cp[1]);
  568. sort1.push_back(cp[2]);
  569. std::vector< std::pair<long long int, Uint8*> > sort2;
  570. sort2.push_back(std::make_pair(graphics->playerColors[player].r, &(cp[0])));
  571. sort2.push_back(std::make_pair(graphics->playerColors[player].g, &(cp[1])));
  572. sort2.push_back(std::make_pair(graphics->playerColors[player].b, &(cp[2])));
  573. std::sort(sort1.begin(), sort1.end());
  574. if(sort2[0].first>sort2[1].first)
  575. std::swap(sort2[0], sort2[1]);
  576. if(sort2[1].first>sort2[2].first)
  577. std::swap(sort2[1], sort2[2]);
  578. if(sort2[0].first>sort2[1].first)
  579. std::swap(sort2[0], sort2[1]);
  580. for(int hh=0; hh<3; ++hh)
  581. {
  582. (*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2.2;
  583. }
  584. }
  585. }
  586. else
  587. {
  588. if(
  589. (/*(mode==0) && (cp[0]>cp[1]) && (cp[0]>cp[2])) ||
  590. ((mode==1) &&*/ (cp[2]<45) && (cp[0]>80) && (cp[1]<70) && ((cp[0]-cp[1])>40))
  591. )
  592. {
  593. std::vector<long long int> sort1;
  594. sort1.push_back(cp[2]);
  595. sort1.push_back(cp[1]);
  596. sort1.push_back(cp[0]);
  597. std::vector< std::pair<long long int, Uint8*> > sort2;
  598. sort2.push_back(std::make_pair(graphics->playerColors[player].r, &(cp[2])));
  599. sort2.push_back(std::make_pair(graphics->playerColors[player].g, &(cp[1])));
  600. sort2.push_back(std::make_pair(graphics->playerColors[player].b, &(cp[0])));
  601. std::sort(sort1.begin(), sort1.end());
  602. if(sort2[0].first>sort2[1].first)
  603. std::swap(sort2[0], sort2[1]);
  604. if(sort2[1].first>sort2[2].first)
  605. std::swap(sort2[1], sort2[2]);
  606. if(sort2[0].first>sort2[1].first)
  607. std::swap(sort2[0], sort2[1]);
  608. for(int hh=0; hh<3; ++hh)
  609. {
  610. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  611. }
  612. }
  613. }
  614. }
  615. }
  616. }
  617. }
  618. void Graphics::loadTrueType()
  619. {
  620. bool ttfPresent = false;//was TTF initialised or not
  621. for(int i = 0; i < FONTS_NUMBER; i++)
  622. fontsTrueType[i] = NULL;
  623. std::ifstream ff(DATA_DIR "/config/fonts.txt");
  624. while(!ff.eof())
  625. {
  626. int enabl, fntID, fntSize;
  627. std::string fntName;
  628. ff >> enabl;//enabled font or not
  629. if (enabl==-1)
  630. break;//end of data
  631. ff >> fntID;//what font will be replaced
  632. ff >> fntName;//name of truetype font
  633. ff >> fntSize;//size of font
  634. if (enabl)
  635. {
  636. if (!ttfPresent)
  637. {
  638. ttfPresent = true;
  639. TTF_Init();
  640. atexit(TTF_Quit);
  641. };
  642. fntName = DATA_DIR + ( "/Fonts/" + fntName);
  643. fontsTrueType[fntID] = TTF_OpenFont(fntName.c_str(),fntSize);
  644. }
  645. }
  646. ff.close();
  647. ff.clear();
  648. }
  649. Font * Graphics::loadFont( const char * name )
  650. {
  651. int len = 0;
  652. unsigned char * hlp = bitmaph->giveFile(name, FILE_FONT, &len);
  653. if(!hlp || !len)
  654. {
  655. tlog1 << "Error: cannot load font: " << name << std::endl;
  656. return NULL;
  657. }
  658. int magic = *(const int*)hlp;
  659. if(len < 10000 || (magic != 589598 && magic != 589599))
  660. {
  661. tlog1 << "Suspicious font file (length " << len <<", fname " << name << "), logging to suspicious_" << name << ".fnt\n";
  662. std::string suspFName = "suspicious_" + std::string(name) + ".fnt";
  663. std::ofstream o(suspFName.c_str());
  664. o.write((const char*)hlp, len);
  665. }
  666. Font *ret = new Font(hlp);
  667. return ret;
  668. }
  669. void Graphics::loadFonts()
  670. {
  671. static const char *fontnames [] = {"BIGFONT.FNT", "CALLI10R.FNT", "CREDITS.FNT", "HISCORE.FNT", "MEDFONT.FNT",
  672. "SMALFONT.FNT", "TIMES08R.FNT", "TINY.FNT", "VERD10B.FNT"} ;
  673. assert(ARRAY_COUNT(fontnames) == FONTS_NUMBER);
  674. for(int i = 0; i < FONTS_NUMBER; i++)
  675. fonts[i] = loadFont(fontnames[i]);
  676. }
  677. CDefEssential * Graphics::getDef( const CGObjectInstance * obj )
  678. {
  679. return advmapobjGraphics[obj->defInfo->id][obj->defInfo->subid];
  680. }
  681. CDefEssential * Graphics::getDef( const CGDefInfo * info )
  682. {
  683. return advmapobjGraphics[info->id][info->subid];
  684. }
  685. void Graphics::loadErmuToPicture()
  686. {
  687. //loading ERMU to picture
  688. std::ifstream etp(DATA_DIR "/config/ERMU_to_picture.txt");
  689. assert(etp.is_open());
  690. for(int g=0; g<44; ++g)
  691. {
  692. for (int b=0; b<ARRAY_COUNT(ERMUtoPicture); ++b)
  693. {
  694. std::string buf;
  695. etp >> buf;
  696. ERMUtoPicture[b][g] = buf;
  697. }
  698. }
  699. etp.close();
  700. }
  701. Font::Font(unsigned char *Data)
  702. {
  703. data = Data;
  704. int i = 0;
  705. height = data[5];
  706. i = 32;
  707. for(int ci = 0; ci < 256; ci++)
  708. {
  709. chars[ci].unknown1 = readNormalNr(data, i); i+=4;
  710. chars[ci].width = readNormalNr(data, i); i+=4;
  711. chars[ci].unknown2 =readNormalNr(data, i); i+=4;
  712. //if(ci>=30)
  713. // tlog0 << ci << ". (" << (char)ci << "). Width: " << chars[ci].width << " U1/U2:" << chars[ci].unknown1 << "/" << chars[ci].unknown2 << std::endl;
  714. }
  715. for(int ci = 0; ci < 256; ci++)
  716. {
  717. chars[ci].offset = readNormalNr(data, i); i+=4;
  718. chars[ci].pixels = data + 4128 + chars[ci].offset;
  719. }
  720. }
  721. Font::~Font()
  722. {
  723. delete [] data;
  724. }
  725. int Font::getWidth(const char *text ) const
  726. {
  727. int length = std::strlen(text);
  728. int ret = 0;
  729. for(int i = 0; i < length; i++)
  730. {
  731. unsigned char c = text[i];
  732. ret += chars[c].width + chars[c].unknown1 + chars[c].unknown2;
  733. }
  734. return ret;
  735. }
  736. int Font::getCharWidth( char c ) const
  737. {
  738. const Char &C = chars[(unsigned char)c];
  739. return C.width + C.unknown1 + C.unknown2;;
  740. }
  741. /*
  742. void Font::WriteAt(const char *text, SDL_Surface *sur, int x, int y )
  743. {
  744. SDL_Surface *SDL_CreateRGBSurfaceFrom(pixels, w, h, 8, int pitch,
  745. 224, 28, 3, 0);
  746. }
  747. */