CMessage.cpp 19 KB

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