CMessage.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. #include "../stdafx.h"
  2. #include "CMessage.h"
  3. #include "SDL_ttf.h"
  4. #include "../hch/CDefHandler.h"
  5. #include "CGameInfo.h"
  6. #include "SDL_Extensions.h"
  7. #include "../hch/CLodHandler.h"
  8. #include <boost/algorithm/string.hpp>
  9. #include <boost/algorithm/string/replace.hpp>
  10. #include "../hch/CDefHandler.h"
  11. #include "CGameInfo.h"
  12. #include "SDL_Extensions.h"
  13. #include <sstream>
  14. #include "../hch/CGeneralTextHandler.h"
  15. #include "Graphics.h"
  16. #include "GUIClasses.h"
  17. #include "AdventureMapButton.h"
  18. #include "CConfigHandler.h"
  19. /*
  20. * CMessage.cpp, part of VCMI engine
  21. *
  22. * Authors: listed in file AUTHORS in main folder
  23. *
  24. * License: GNU General Public License v2.0 or later
  25. * Full text of license available in license.txt file, in main folder
  26. *
  27. */
  28. SDL_Color tytulowy = {229, 215, 123, 0},
  29. tlo = {66, 44, 24, 0},
  30. zwykly = {255, 255, 255, 0};
  31. extern SDL_Surface * screen;
  32. extern TTF_Font * TNRB16, *TNR, *GEOR13;
  33. using namespace NMessage;
  34. const int COMPONENT_TO_SUBTITLE = 5;
  35. const int BETWEEN_COMPS_ROWS = 10;
  36. const int BEFORE_COMPONENTS = 30;
  37. const int SIDE_MARGIN = 30;
  38. template <typename T, typename U> std::pair<T,U> max(const std::pair<T,U> &x, const std::pair<T,U> &y)
  39. {
  40. std::pair<T,U> ret;
  41. ret.first = std::max(x.first,y.first);
  42. ret.second = std::max(x.second,y.second);
  43. return ret;
  44. }
  45. namespace NMessage
  46. {
  47. CDefHandler * ok, *cancel;
  48. std::vector<std::vector<SDL_Surface*> > piecesOfBox; //in colors of all players
  49. SDL_Surface * background = NULL;
  50. }
  51. void CMessage::init()
  52. {
  53. {
  54. for (int i=0;i<PLAYER_LIMIT;i++)
  55. {
  56. CDefHandler * bluePieces = CDefHandler::giveDef("DIALGBOX.DEF");
  57. std::vector<SDL_Surface *> n;
  58. piecesOfBox.push_back(n);
  59. if (i==1)
  60. {
  61. for (size_t j=0;j<bluePieces->ourImages.size();++j)
  62. {
  63. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  64. }
  65. }
  66. for (size_t j=0;j<bluePieces->ourImages.size();++j)
  67. {
  68. graphics->blueToPlayersAdv(bluePieces->ourImages[j].bitmap,i);
  69. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  70. }
  71. }
  72. NMessage::background = BitmapHandler::loadBitmap("DIBOXBCK.BMP");
  73. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  74. }
  75. ok = CDefHandler::giveDef("IOKAY.DEF");
  76. cancel = CDefHandler::giveDef("ICANCEL.DEF");
  77. }
  78. void CMessage::dispose()
  79. {
  80. for (int i=0;i<PLAYER_LIMIT;i++)
  81. {
  82. for (size_t j=0; j<piecesOfBox[i].size(); ++j)
  83. {
  84. SDL_FreeSurface(piecesOfBox[i][j]);
  85. }
  86. }
  87. SDL_FreeSurface(background);
  88. delete ok;
  89. delete cancel;
  90. }
  91. SDL_Surface * CMessage::drawBox1(int w, int h, int playerColor) //draws box for window
  92. {
  93. //prepare surface
  94. SDL_Surface * ret = SDL_CreateRGBSurface(screen->flags, w, h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
  95. for (int i=0; i<h; i+=background->h)//background
  96. {
  97. for (int j=0; j<w; j+=background->w-1)
  98. {
  99. SDL_BlitSurface(background,&genRect(background->h,background->w-1,1,0),ret,&genRect(h,w,j,i)); //FIXME taking address of temporary
  100. }
  101. }
  102. drawBorder(playerColor, ret, w, h);
  103. return ret;
  104. }
  105. /* The map file contains long texts, with or without line breaks. This
  106. * method takes such a text and breaks it into into several lines. */
  107. std::vector<std::string> * CMessage::breakText(std::string text, size_t maxLineSize,
  108. bool userBreak, bool ifor)
  109. {
  110. std::vector<std::string> * ret = new std::vector<std::string>();
  111. boost::algorithm::trim_if(text,boost::algorithm::is_any_of(" "));
  112. while (text.length())
  113. {
  114. unsigned int z = 0;
  115. unsigned int braces = 0;
  116. bool opened = false;
  117. while((text[z] != 0) && (text[z] != 0x0a) && (z < maxLineSize+braces))
  118. {
  119. /* We don't count braces in string length. */
  120. if (text[z] == '{')
  121. {
  122. opened=true;
  123. braces++;
  124. }
  125. else if (text[z]=='}')
  126. {
  127. opened=false;
  128. braces++;
  129. }
  130. z++;
  131. }
  132. if ((text[z] != 0) && (text[z] != 0x0a))
  133. {
  134. /* We have a long line. Try to do a nice line break, if
  135. * possible. We backtrack on the line until we find a
  136. * suitable character. */
  137. int pos = z-1;
  138. // Do not break an ellipsis, backtrack until whitespace.
  139. if (text[pos] == '.' && text[z] == '.') {
  140. while (pos != 0 && text[pos] != ' ')
  141. pos--;
  142. } else {
  143. /* TODO: boost should have a nice method to do that. */
  144. while(pos > 0 &&
  145. text[pos] != ' ' &&
  146. text[pos] != ',' &&
  147. text[pos] != '.' &&
  148. text[pos] != ';' &&
  149. text[pos] != '!' &&
  150. text[pos] != '?')
  151. pos --;
  152. }
  153. if (pos > 0)
  154. z = pos+1;
  155. }
  156. if(z) //non-blank line
  157. {
  158. ret->push_back(text.substr(0, z));
  159. if (opened)
  160. /* Close the brace for the current line. */
  161. ret->back() += '}';
  162. text.erase(0, z);
  163. }
  164. else if(text[z] == 0x0a) //blank line
  165. {
  166. ret->push_back(""); //add empty string, no extra actions needed
  167. }
  168. if (text[0] == 0x0a)
  169. {
  170. /* Braces do not carry over lines. The map author forgot
  171. * to close it. */
  172. opened = false;
  173. /* Remove LF */
  174. text.erase(0, 1);
  175. }
  176. boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(" "));
  177. if (opened)
  178. {
  179. /* Add an opening brace for the next line. */
  180. if (text.length())
  181. text.insert(0, "{");
  182. }
  183. }
  184. /* Trim whitespaces of every line. */
  185. for (size_t i=0; i<ret->size(); i++)
  186. boost::algorithm::trim((*ret)[i]);
  187. return ret;
  188. }
  189. std::pair<int,int> CMessage::getMaxSizes(std::vector<std::vector<SDL_Surface*> > * txtg, int fontHeight)
  190. {
  191. std::pair<int,int> ret;
  192. ret.first = -1;
  193. ret.second=0;
  194. for (size_t i=0; i<txtg->size();i++) //we are searching widest line and total height
  195. {
  196. int lw=0;
  197. for (size_t j=0;j<(*txtg)[i].size();j++)
  198. {
  199. lw+=(*txtg)[i][j]->w;
  200. ret.second+=(*txtg)[i][j]->h;
  201. }
  202. if(!(*txtg)[i].size())
  203. ret.second+=fontHeight;
  204. if (ret.first<lw)
  205. ret.first=lw;
  206. }
  207. return ret;
  208. }
  209. // Blit the text in txtg onto one surface. txtg contains lines of
  210. // text. Each line can be split into pieces. Currently only lines with
  211. // the same height are supported (ie. fontHeight).
  212. SDL_Surface * CMessage::blitTextOnSur(std::vector<std::vector<SDL_Surface*> > * txtg, int fontHeight, int & curh, SDL_Surface * ret, int xCenterPos)
  213. {
  214. for (size_t i=0; i<txtg->size(); i++, curh += fontHeight)
  215. {
  216. int lw=0; //line width
  217. for (size_t j=0;j<(*txtg)[i].size();j++)
  218. lw+=(*txtg)[i][j]->w;
  219. int pw = (xCenterPos < 0) ? ret->w/2 : xCenterPos;
  220. pw -= lw/2; //x coord for the start of the text
  221. int tw = pw;
  222. for (size_t j=0;j<(*txtg)[i].size();j++) //blit text
  223. {
  224. SDL_Surface *surf = (*txtg)[i][j];
  225. blitAt(surf, tw, curh, ret);
  226. tw+=surf->w;
  227. SDL_FreeSurface(surf);
  228. (*txtg)[i][j] = NULL;
  229. }
  230. }
  231. return ret;
  232. }
  233. std::vector<std::vector<SDL_Surface*> > * CMessage::drawText(std::vector<std::string> * brtext, int &fontHeigh, TTF_Font *font)
  234. {
  235. if(!font) font = TNRB16;
  236. std::vector<std::vector<SDL_Surface*> > * txtg = new std::vector<std::vector<SDL_Surface*> >();
  237. txtg->resize(brtext->size());
  238. fontHeigh = TTF_FontHeight(font);
  239. for (size_t i=0; i<brtext->size();i++) //foreach line
  240. {
  241. while((*brtext)[i].length()) //if something left
  242. {
  243. size_t z;
  244. /* Handle normal text. */
  245. z = 0;
  246. while((*brtext)[i][z]!= 0 && (*brtext)[i][z] != ('{'))
  247. z++;
  248. if (z)
  249. (*txtg)[i].push_back(TTF_RenderText_Blended(font, (*brtext)[i].substr(0,z).c_str(), zwykly));
  250. (*brtext)[i].erase(0,z);
  251. if ((*brtext)[i][0] == '{')
  252. /* Remove '{' */
  253. (*brtext)[i].erase(0,1);
  254. if ((*brtext)[i].length()==0)
  255. /* End of line */
  256. continue;
  257. /* This text will be highlighted. */
  258. z = 0;
  259. while((*brtext)[i][z]!= 0 && (*brtext)[i][z] != ('}'))
  260. z++;
  261. if (z)
  262. (*txtg)[i].push_back(TTF_RenderText_Blended(font, (*brtext)[i].substr(0,z).c_str(), tytulowy));
  263. (*brtext)[i].erase(0,z);
  264. if ((*brtext)[i][0] == '}')
  265. /* Remove '}' */
  266. (*brtext)[i].erase(0,1);
  267. } //ends while((*brtext)[i].length())
  268. } //ends for(int i=0; i<brtext->size();i++)
  269. return txtg;
  270. }
  271. CSimpleWindow * CMessage::genWindow(std::string text, int player, int Lmar, int Rmar, int Tmar, int Bmar)
  272. {
  273. CSimpleWindow * ret = new CSimpleWindow();
  274. int fontHeight;
  275. std::vector<std::string> * brtext = breakText(text,32,true,true);
  276. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext, fontHeight);
  277. std::pair<int,int> txts = getMaxSizes(txtg, fontHeight);
  278. ret->bitmap = drawBox1(txts.first+Lmar+Rmar,txts.second+Tmar+Bmar,player);
  279. ret->pos.h = ret->bitmap->h;
  280. ret->pos.w = ret->bitmap->w;
  281. ret->pos.x = screen->w/2 - (ret->pos.w/2);
  282. ret->pos.y = screen->h/2 - (ret->pos.h/2);
  283. int curh = ret->bitmap->h/2 - (fontHeight*txtg->size())/2;
  284. blitTextOnSur(txtg,fontHeight,curh,ret->bitmap);
  285. delete brtext;
  286. delete txtg;
  287. return ret;
  288. }
  289. SDL_Surface * CMessage::drawBoxTextBitmapSub( int player, std::string text, SDL_Surface* bitmap, std::string sub, int charperline/*=30*/, int imgToBmp/*=55*/ )
  290. {
  291. int curh;
  292. int fontHeight;
  293. std::vector<std::string> * tekst = breakText(text,charperline);
  294. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(tekst, fontHeight);
  295. std::pair<int,int> txts = getMaxSizes(txtg, fontHeight), boxs;
  296. boxs.first = std::max(txts.first,bitmap->w) // text/bitmap max width
  297. + 50; //side margins
  298. boxs.second =
  299. (curh=45) //top margin
  300. + txts.second //text total height
  301. + imgToBmp //text <=> img
  302. + bitmap->h
  303. + 5 // to sibtitle
  304. + (*txtg)[0][0]->h
  305. + 30;
  306. SDL_Surface *ret = drawBox1(boxs.first,boxs.second,player);
  307. blitTextOnSur(txtg,fontHeight,curh,ret);
  308. curh += imgToBmp;
  309. blitAt(bitmap,(ret->w/2)-(bitmap->w/2),curh,ret);
  310. curh += bitmap->h + 5;
  311. CSDL_Ext::printAtMiddle(sub,ret->w/2,curh+10,GEOR13,zwykly,ret);
  312. delete tekst;
  313. delete txtg;
  314. return ret;
  315. }
  316. void CMessage::drawIWindow(CInfoWindow * ret, std::string text, int player, int charperline)
  317. {
  318. SDL_Surface * _or = NULL;
  319. int fontHeight;
  320. // Try to compute a reasonable number of characters per line
  321. if (!charperline)
  322. {
  323. if (text.size() < 30)
  324. charperline = 30;
  325. else if (text.size() < 200)
  326. charperline = 40;
  327. else
  328. charperline = 50;
  329. }
  330. if(dynamic_cast<CSelWindow*>(ret)) //it's selection window, so we'll blit "or" between components
  331. _or = TTF_RenderText_Blended(GEOR13,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  332. std::vector<std::string> * brtext = breakText(text,charperline,true,true); //text
  333. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext, fontHeight);
  334. std::pair<int,int> txts = getMaxSizes(txtg, fontHeight);
  335. ComponentsToBlit comps(ret->components,500,_or);
  336. if (ret->components.size())
  337. txts.second += 30 + comps.h; //space to first component
  338. if (ret->buttons.size())
  339. txts.second += 20 + //before button
  340. ok->ourImages[0].bitmap->h; //button
  341. // Clip window size
  342. amax(txts.first, 80);
  343. amax(txts.second, 50);
  344. amax(txts.first,comps.w);
  345. amin(txts.first, conf.cc.resx - 150);
  346. amin(txts.second, conf.cc.resy - 150);
  347. ret->bitmap = drawBox1(txts.first+2*SIDE_MARGIN,txts.second+2*SIDE_MARGIN,player);
  348. ret->pos.h=ret->bitmap->h;
  349. ret->pos.w=ret->bitmap->w;
  350. ret->pos.x=screen->w/2-(ret->pos.w/2);
  351. ret->pos.y=screen->h/2-(ret->pos.h/2);
  352. int curh = SIDE_MARGIN;
  353. blitTextOnSur(txtg, fontHeight, curh,ret->bitmap);
  354. if (ret->components.size())
  355. {
  356. curh += BEFORE_COMPONENTS;
  357. comps.blitCompsOnSur(_or, 10, curh, ret->bitmap);
  358. }
  359. if(ret->buttons.size())
  360. {
  361. // Position the buttons at the bottom of the window
  362. curh = ret->bitmap->h - SIDE_MARGIN - ret->buttons[0]->imgs[0][0]->h;
  363. // Compute total width of buttons
  364. int bw = 20*(ret->buttons.size()-1); // space between all buttons
  365. for(size_t i=0; i<ret->buttons.size(); i++) //and add buttons width
  366. bw+=ret->buttons[i]->imgs[0][0]->w;
  367. bw = (ret->bitmap->w/2) - (bw/2);
  368. for(size_t i=0; i<ret->buttons.size(); i++)
  369. {
  370. ret->buttons[i]->pos.x = bw + ret->pos.x;
  371. ret->buttons[i]->pos.y = curh + ret->pos.y;
  372. bw += ret->buttons[i]->imgs[0][0]->w + 20;
  373. }
  374. }
  375. for(size_t i=0; i<ret->components.size(); i++)
  376. {
  377. ret->components[i]->pos.x += ret->pos.x;
  378. ret->components[i]->pos.y += ret->pos.y;
  379. }
  380. delete brtext;
  381. delete txtg;
  382. if(_or)
  383. SDL_FreeSurface(_or);
  384. }
  385. SDL_Surface * CMessage::genMessage
  386. (std::string title, std::string text, EWindowType type, std::vector<CDefHandler*> *addPics, void * cb)
  387. {
  388. //max x 320 okolo 30 znakow
  389. std::vector<std::string> * tekst;
  390. if (text.length() < 30) //does not need breaking
  391. {
  392. tekst = new std::vector<std::string>();
  393. tekst->push_back(text);
  394. }
  395. else tekst = breakText(text);
  396. int ww, hh; //dimensions of box
  397. if (319>30+13*text.length())
  398. ww = 30+13*text.length();
  399. else ww = 319;
  400. if (title.length())
  401. hh=110+(21*tekst->size());
  402. else hh=60+(21*tekst->size());
  403. if (type==yesOrNO) //make place for buttons
  404. {
  405. if (ww<200) ww=200;
  406. hh+=70;
  407. }
  408. SDL_Surface * ret = drawBox1(ww,hh,0);
  409. //prepare title text
  410. if (title.length())
  411. {
  412. SDL_Surface * titleText = TTF_RenderText_Blended(TNRB16,title.c_str(),tytulowy);
  413. //draw title
  414. SDL_Rect tytul = genRect(titleText->h,titleText->w,((ret->w/2)-(titleText->w/2)),37);
  415. SDL_BlitSurface(titleText,NULL,ret,&tytul);
  416. SDL_FreeSurface(titleText);
  417. }
  418. //draw text
  419. for (size_t i=0; i<tekst->size(); i++)
  420. {
  421. int by = 37+i*21;
  422. if (title.length()) by+=40;
  423. SDL_Surface * tresc = TTF_RenderText_Blended(TNRB16,(*tekst)[i].c_str(),zwykly);
  424. SDL_Rect trescRect = genRect(tresc->h,tresc->w,((ret->w/2)-(tresc->w/2)),by);
  425. SDL_BlitSurface(tresc,NULL,ret,&trescRect);
  426. SDL_FreeSurface(tresc);
  427. }
  428. if (type==yesOrNO) // add buttons
  429. {
  430. int by = 77+tekst->size()*21;
  431. if (title.length()) by+=40;
  432. int hwo = (*addPics)[0]->ourImages[0].bitmap->w, hwc=(*addPics)[0]->ourImages[0].bitmap->w;
  433. //ok
  434. SDL_Rect trescRect = genRect((*addPics)[0]->ourImages[0].bitmap->h,hwo,((ret->w/2)-hwo-10),by);
  435. SDL_BlitSurface((*addPics)[0]->ourImages[0].bitmap,NULL,ret,&trescRect);
  436. reinterpret_cast<std::vector<SDL_Rect>*>(cb)->push_back(trescRect);
  437. //cancel
  438. trescRect = genRect((*addPics)[1]->ourImages[0].bitmap->h,hwc,((ret->w/2)+10),by);
  439. SDL_BlitSurface((*addPics)[1]->ourImages[0].bitmap,NULL,ret,&trescRect);
  440. reinterpret_cast<std::vector<SDL_Rect>*>(cb)->push_back(trescRect);
  441. }
  442. delete tekst;
  443. return ret;
  444. }
  445. void CMessage::drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int x, int y)
  446. {
  447. //obwodka I-szego rzedu pozioma //border of 1st series, horizontal
  448. for (int i=0; i<w-piecesOfBox[playerColor][6]->w; i+=piecesOfBox[playerColor][6]->w)
  449. {
  450. SDL_BlitSurface
  451. (piecesOfBox[playerColor][6],NULL,ret,&genRect(piecesOfBox[playerColor][6]->h,piecesOfBox[playerColor][6]->w,x+i,y+0));
  452. SDL_BlitSurface
  453. (piecesOfBox[playerColor][7],NULL,ret,&genRect(piecesOfBox[playerColor][7]->h,piecesOfBox[playerColor][7]->w,x+i,y+h-piecesOfBox[playerColor][7]->h));
  454. }
  455. //obwodka I-szego rzedu pionowa //border of 1st series, vertical
  456. for (int i=0; i<h-piecesOfBox[playerColor][4]->h; i+=piecesOfBox[playerColor][4]->h)
  457. {
  458. SDL_BlitSurface
  459. (piecesOfBox[playerColor][4],NULL,ret,&genRect(piecesOfBox[playerColor][4]->h,piecesOfBox[playerColor][4]->w,x+0,y+i));
  460. SDL_BlitSurface
  461. (piecesOfBox[playerColor][5],NULL,ret,&genRect(piecesOfBox[playerColor][5]->h,piecesOfBox[playerColor][5]->w,x+w-piecesOfBox[playerColor][5]->w,y+i));
  462. }
  463. //corners
  464. SDL_BlitSurface
  465. (piecesOfBox[playerColor][0],NULL,ret,&genRect(piecesOfBox[playerColor][0]->h,piecesOfBox[playerColor][0]->w,x+0,y+0));
  466. SDL_BlitSurface
  467. (piecesOfBox[playerColor][1],NULL,ret,&genRect(piecesOfBox[playerColor][1]->h,piecesOfBox[playerColor][1]->w,x+w-piecesOfBox[playerColor][1]->w,y+0));
  468. SDL_BlitSurface
  469. (piecesOfBox[playerColor][2],NULL,ret,&genRect(piecesOfBox[playerColor][2]->h,piecesOfBox[playerColor][2]->w,x+0,y+h-piecesOfBox[playerColor][2]->h));
  470. SDL_BlitSurface
  471. (piecesOfBox[playerColor][3],NULL,ret,&genRect(piecesOfBox[playerColor][3]->h,piecesOfBox[playerColor][3]->w,x+w-piecesOfBox[playerColor][3]->w,y+h-piecesOfBox[playerColor][3]->h));
  472. }
  473. ComponentResolved::ComponentResolved()
  474. {
  475. comp = NULL;
  476. img = NULL;
  477. txt = NULL;
  478. txtFontHeight = 0;
  479. }
  480. ComponentResolved::ComponentResolved( SComponent *Comp )
  481. {
  482. comp = Comp;
  483. img = comp->getImg();
  484. std::vector<std::string> * brtext = CMessage::breakText(comp->subtitle,13,true,true); //text
  485. txt = CMessage::drawText(brtext,txtFontHeight,GEOR13);
  486. delete brtext;
  487. //calculate dimensions
  488. std::pair<int,int> textSize = CMessage::getMaxSizes(txt, txtFontHeight);
  489. comp->pos.w = std::max(textSize.first, img->w); //bigger of: subtitle width and image width
  490. comp->pos.h = img->h + COMPONENT_TO_SUBTITLE + textSize.second;
  491. }
  492. ComponentResolved::~ComponentResolved()
  493. {
  494. for(size_t i = 0; i < txt->size(); i++)
  495. for(size_t j = 0; j < (*txt)[i].size(); j++)
  496. if((*txt)[i][j])
  497. SDL_FreeSurface((*txt)[i][j]);
  498. delete txt;
  499. }
  500. ComponentsToBlit::~ComponentsToBlit()
  501. {
  502. for(size_t i=0; i<comps.size(); i++)
  503. for(size_t j = 0; j < comps[i].size(); j++)
  504. delete comps[i][j];
  505. }
  506. ComponentsToBlit::ComponentsToBlit(std::vector<SComponent*> & SComps, int maxw, SDL_Surface* _or)
  507. {
  508. w = h = 0;
  509. if(!SComps.size())
  510. return;
  511. comps.resize(1);
  512. int curw = 0;
  513. int curr = 0; //current row
  514. for(size_t i=0;i<SComps.size();i++)
  515. {
  516. ComponentResolved *cur = new ComponentResolved(SComps[i]);
  517. int toadd = (cur->comp->pos.w + 12 + (_or ? _or->w : 0));
  518. if (curw + toadd > maxw)
  519. {
  520. curr++;
  521. amax(w,curw);
  522. curw = cur->comp->pos.w;
  523. comps.resize(curr+1);
  524. }
  525. else
  526. {
  527. curw += toadd;
  528. amax(w,curw);
  529. }
  530. comps[curr].push_back(cur);
  531. }
  532. for(size_t i=0;i<comps.size();i++)
  533. {
  534. int maxh = 0;
  535. for(size_t j=0;j<comps[i].size();j++)
  536. amax(maxh,comps[i][j]->comp->pos.h);
  537. h += maxh + BETWEEN_COMPS_ROWS;
  538. }
  539. }
  540. void ComponentsToBlit::blitCompsOnSur( SDL_Surface * _or, int inter, int &curh, SDL_Surface *ret )
  541. {
  542. for (size_t i=0;i<comps.size();i++)
  543. {
  544. int totalw=0, maxh=0;
  545. for(size_t j=0;j<(comps)[i].size();j++)
  546. {
  547. ComponentResolved *cur = (comps)[i][j];
  548. totalw += cur->comp->pos.w;
  549. amax(maxh,cur->comp->pos.h+BETWEEN_COMPS_ROWS);
  550. }
  551. if(_or)
  552. {
  553. totalw += (inter*2+_or->w) * ((comps)[i].size() - 1);
  554. }
  555. else
  556. {
  557. totalw += (inter) * ((comps)[i].size() - 1);
  558. }
  559. curh+=maxh/2;
  560. int curw = (ret->w/2)-(totalw/2);
  561. for(size_t j=0;j<(comps)[i].size();j++)
  562. {
  563. ComponentResolved *cur = (comps)[i][j];
  564. //blit img
  565. int hlp = curh-(cur->comp->pos.h)/2;
  566. blitAt(cur->img, curw + (cur->comp->pos.w - cur->comp->getImg()->w)/2, hlp, ret);
  567. cur->comp->pos.x = curw;
  568. cur->comp->pos.y = hlp;
  569. //blit subtitle
  570. hlp += cur->img->h + COMPONENT_TO_SUBTITLE;
  571. CMessage::blitTextOnSur(cur->txt, cur->txtFontHeight, hlp, ret, cur->comp->pos.x + cur->comp->pos.w/2 );
  572. //if there is subsequent component blit "or"
  573. curw += cur->comp->pos.w;
  574. if(j<((comps)[i].size()-1))
  575. {
  576. if(_or)
  577. {
  578. curw+=inter;
  579. blitAt(_or,curw,curh-(_or->h/2),ret);
  580. curw+=_or->w;
  581. }
  582. curw+=inter;
  583. }
  584. }
  585. curh+=maxh/2;
  586. }
  587. }