CMessage.cpp 19 KB

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