Graphics.cpp 21 KB

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