CMessage.cpp 19 KB

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