CMessage.cpp 19 KB

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