CMessage.cpp 19 KB

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