CMessage.cpp 18 KB

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