CMessage.cpp 19 KB

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