Graphics.cpp 19 KB

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