2
0

CMessage.cpp 19 KB

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