CMessage.cpp 18 KB

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