2
0

CMessage.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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,
  105. bool userBreak, bool ifor)
  106. {
  107. std::vector<std::string> * ret = new std::vector<std::string>();
  108. boost::algorithm::trim_if(text,boost::algorithm::is_any_of(" "));
  109. while (text.length())
  110. {
  111. unsigned int z = 0;
  112. unsigned int braces = 0;
  113. bool opened = false;
  114. while(z < text.length() && (text[z] != 0x0a) && (z < maxLineSize+braces))
  115. {
  116. /* We don't count braces in string length. */
  117. if (text[z] == '{')
  118. {
  119. opened=true;
  120. braces++;
  121. }
  122. else if (text[z]=='}')
  123. {
  124. opened=false;
  125. braces++;
  126. }
  127. z++;
  128. }
  129. if (z < text.length() && (text[z] != 0x0a))
  130. {
  131. /* We have a long line. Try to do a nice line break, if
  132. * possible. We backtrack on the line until we find a
  133. * suitable character. */
  134. int pos = z-1;
  135. // Do not break an ellipsis, backtrack until whitespace.
  136. if (text[pos] == '.' && text[z] == '.') {
  137. while (pos != 0 && text[pos] != ' ')
  138. pos--;
  139. } else {
  140. /* TODO: boost should have a nice method to do that. */
  141. while(pos > 0 &&
  142. text[pos] != ' ' &&
  143. text[pos] != ',' &&
  144. text[pos] != '.' &&
  145. text[pos] != ';' &&
  146. text[pos] != '!' &&
  147. text[pos] != '?')
  148. pos --;
  149. }
  150. if (pos > 0)
  151. z = pos+1;
  152. }
  153. if(z) //non-blank line
  154. {
  155. ret->push_back(text.substr(0, z));
  156. if (opened)
  157. /* Close the brace for the current line. */
  158. ret->back() += '}';
  159. text.erase(0, z);
  160. }
  161. else if(text[z] == 0x0a) //blank line
  162. {
  163. ret->push_back(""); //add empty string, no extra actions needed
  164. }
  165. if (text.length() && text[0] == 0x0a)
  166. {
  167. /* Braces do not carry over lines. The map author forgot
  168. * to close it. */
  169. opened = false;
  170. /* Remove LF */
  171. text.erase(0, 1);
  172. }
  173. boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(" "));
  174. if (opened)
  175. {
  176. /* Add an opening brace for the next line. */
  177. if (text.length())
  178. text.insert(0, "{");
  179. }
  180. }
  181. /* Trim whitespaces of every line. */
  182. for (size_t i=0; i<ret->size(); i++)
  183. boost::algorithm::trim((*ret)[i]);
  184. return ret;
  185. }
  186. std::pair<int,int> CMessage::getMaxSizes(std::vector<std::vector<SDL_Surface*> > * txtg, int fontHeight)
  187. {
  188. std::pair<int,int> ret;
  189. ret.first = -1;
  190. ret.second=0;
  191. for (size_t i=0; i<txtg->size();i++) //we are searching widest line and total height
  192. {
  193. int lw=0;
  194. for (size_t j=0;j<(*txtg)[i].size();j++)
  195. {
  196. lw+=(*txtg)[i][j]->w;
  197. ret.second+=(*txtg)[i][j]->h;
  198. }
  199. if(!(*txtg)[i].size())
  200. ret.second+=fontHeight;
  201. if (ret.first<lw)
  202. ret.first=lw;
  203. }
  204. return ret;
  205. }
  206. // Blit the text in txtg onto one surface. txtg contains lines of
  207. // text. Each line can be split into pieces. Currently only lines with
  208. // the same height are supported (ie. fontHeight).
  209. SDL_Surface * CMessage::blitTextOnSur(std::vector<std::vector<SDL_Surface*> > * txtg, int fontHeight, int & curh, SDL_Surface * ret, int xCenterPos)
  210. {
  211. for (size_t i=0; i<txtg->size(); i++, curh += fontHeight)
  212. {
  213. int lw=0; //line width
  214. for (size_t j=0;j<(*txtg)[i].size();j++)
  215. lw+=(*txtg)[i][j]->w;
  216. int pw = (xCenterPos < 0) ? ret->w/2 : xCenterPos;
  217. pw -= lw/2; //x coord for the start of the text
  218. int tw = pw;
  219. for (size_t j=0;j<(*txtg)[i].size();j++) //blit text
  220. {
  221. SDL_Surface *surf = (*txtg)[i][j];
  222. blitAt(surf, tw, curh, ret);
  223. tw+=surf->w;
  224. SDL_FreeSurface(surf);
  225. (*txtg)[i][j] = NULL;
  226. }
  227. }
  228. return ret;
  229. }
  230. SDL_Surface * FNT_RenderText (EFonts font, std::string text, SDL_Color kolor= zwykly)
  231. {
  232. if (graphics->fontsTrueType[font])
  233. return TTF_RenderText_Blended(graphics->fontsTrueType[font], text.c_str(), kolor);
  234. const Font *f = graphics->fonts[font];
  235. int w = f->getWidth(text.c_str()),
  236. h = f->height;
  237. SDL_Surface * ret = CSDL_Ext::newSurface(w, h, screen);
  238. SDL_FillRect (ret, NULL, SDL_MapRGB(ret->format,128,128,128));//if use default black - no shadowing
  239. SDL_SetColorKey(ret,SDL_SRCCOLORKEY,SDL_MapRGB(ret->format,128,128,128));
  240. CSDL_Ext::printAt(text.c_str(), 0, 0, font, kolor, ret);
  241. return ret;
  242. }
  243. std::vector<std::vector<SDL_Surface*> > * CMessage::drawText(std::vector<std::string> * brtext, int &fontHeigh, EFonts font)
  244. {
  245. std::vector<std::vector<SDL_Surface*> > * txtg = new std::vector<std::vector<SDL_Surface*> >();
  246. txtg->resize(brtext->size());
  247. if (graphics->fontsTrueType[font])
  248. fontHeigh = TTF_FontHeight(graphics->fontsTrueType[font]);
  249. else
  250. fontHeigh = graphics->fonts[font]->height;
  251. for (size_t i=0; i<brtext->size();i++) //foreach line
  252. {
  253. while((*brtext)[i].length()) //if something left
  254. {
  255. size_t z;
  256. /* Handle normal text. */
  257. z = 0;
  258. while(z < (*brtext)[i].length() && (*brtext)[i][z] != ('{'))
  259. z++;
  260. if (z)
  261. (*txtg)[i].push_back(FNT_RenderText(font, (*brtext)[i].substr(0,z), zwykly));
  262. (*brtext)[i].erase(0,z);
  263. if ((*brtext)[i].length() && (*brtext)[i][0] == '{')
  264. /* Remove '{' */
  265. (*brtext)[i].erase(0,1);
  266. if ((*brtext)[i].length()==0)
  267. /* End of line */
  268. continue;
  269. /* This text will be highlighted. */
  270. z = 0;
  271. while(z < (*brtext)[i].length() && (*brtext)[i][z] != ('}'))
  272. z++;
  273. if (z)
  274. (*txtg)[i].push_back(FNT_RenderText(font, (*brtext)[i].substr(0,z), tytulowy));
  275. (*brtext)[i].erase(0,z);
  276. if ((*brtext)[i].length() && (*brtext)[i][0] == '}')
  277. /* Remove '}' */
  278. (*brtext)[i].erase(0,1);
  279. } //ends while((*brtext)[i].length())
  280. } //ends for(int i=0; i<brtext->size();i++)
  281. return txtg;
  282. }
  283. CSimpleWindow * CMessage::genWindow(std::string text, int player, bool centerOnMouse, int Lmar, int Rmar, int Tmar, int Bmar)
  284. {
  285. CSimpleWindow * ret = new CSimpleWindow();
  286. int fontHeight;
  287. std::vector<std::string> * brtext = breakText(text,32,true,true);
  288. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext, fontHeight);
  289. std::pair<int,int> txts = getMaxSizes(txtg, fontHeight);
  290. ret->bitmap = drawBox1(txts.first+Lmar+Rmar,txts.second+Tmar+Bmar,player);
  291. ret->pos.h = ret->bitmap->h;
  292. ret->pos.w = ret->bitmap->w;
  293. if (centerOnMouse) {
  294. ret->pos.x = GH.current->motion.x - ret->pos.w/2;
  295. ret->pos.y = GH.current->motion.y - ret->pos.h/2;
  296. // Put the window back on screen if necessary
  297. amax(ret->pos.x, 0);
  298. amax(ret->pos.y, 0);
  299. amin(ret->pos.x, conf.cc.resx - ret->pos.w);
  300. amin(ret->pos.y, conf.cc.resy - ret->pos.h);
  301. } else {
  302. // Center on screen
  303. ret->pos.x = screen->w/2 - (ret->pos.w/2);
  304. ret->pos.y = screen->h/2 - (ret->pos.h/2);
  305. }
  306. int curh = ret->bitmap->h/2 - (fontHeight*txtg->size())/2;
  307. blitTextOnSur(txtg,fontHeight,curh,ret->bitmap);
  308. delete brtext;
  309. delete txtg;
  310. return ret;
  311. }
  312. SDL_Surface * CMessage::drawBoxTextBitmapSub( int player, std::string text, SDL_Surface* bitmap, std::string sub, int charperline/*=30*/, int imgToBmp/*=55*/ )
  313. {
  314. int curh;
  315. int fontHeight;
  316. std::vector<std::string> * tekst = breakText(text,charperline);
  317. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(tekst, fontHeight);
  318. std::pair<int,int> txts = getMaxSizes(txtg, fontHeight), boxs;
  319. boxs.first = std::max(txts.first,bitmap->w) // text/bitmap max width
  320. + 50; //side margins
  321. boxs.second =
  322. (curh=45) //top margin
  323. + txts.second //text total height
  324. + imgToBmp //text <=> img
  325. + bitmap->h
  326. + 5 // to sibtitle
  327. + (*txtg)[0][0]->h
  328. + 30;
  329. SDL_Surface *ret = drawBox1(boxs.first,boxs.second,player);
  330. blitTextOnSur(txtg,fontHeight,curh,ret);
  331. curh += imgToBmp;
  332. blitAt(bitmap,(ret->w/2)-(bitmap->w/2),curh,ret);
  333. curh += bitmap->h + 5;
  334. CSDL_Ext::printAtMiddle(sub,ret->w/2,curh+10,FONT_SMALL,zwykly,ret);
  335. delete tekst;
  336. delete txtg;
  337. return ret;
  338. }
  339. void CMessage::drawIWindow(CInfoWindow * ret, std::string text, int player, int charperline)
  340. {
  341. SDL_Surface * _or = NULL;
  342. int fontHeight;
  343. // Try to compute a reasonable number of characters per line
  344. if (!charperline)
  345. {
  346. if (text.size() < 30 && ret->buttons.size() < 2)
  347. charperline = 30;
  348. else if (text.size() < 200)
  349. charperline = 40;
  350. else if (text.size() < 750)
  351. charperline = 50;
  352. else
  353. charperline = 75; //TODO: add scrollbar for very long texts
  354. }
  355. if(dynamic_cast<CSelWindow*>(ret)) //it's selection window, so we'll blit "or" between components
  356. _or = FNT_RenderText(FONT_MEDIUM,CGI->generaltexth->allTexts[4],zwykly);
  357. std::vector<std::string> * brtext = breakText(text, charperline, true, true); //text
  358. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext, fontHeight);
  359. std::pair<int,int> txts = getMaxSizes(txtg, fontHeight);
  360. ComponentsToBlit comps(ret->components,500,_or);
  361. if (ret->components.size())
  362. txts.second += 30 + comps.h; //space to first component
  363. int bw = 0;
  364. if (ret->buttons.size())
  365. {
  366. // Compute total width of buttons
  367. bw = 20*(ret->buttons.size()-1); // space between all buttons
  368. for(size_t i=0; i<ret->buttons.size(); i++) //and add buttons width
  369. bw+=ret->buttons[i]->imgs[0][0]->w;
  370. txts.second += 20 + //before button
  371. ok->ourImages[0].bitmap->h; //button
  372. }
  373. // Clip window size
  374. amax(txts.second, 50);
  375. amax(txts.first, 80);
  376. amax(txts.first, comps.w);
  377. amax(txts.first, bw);
  378. amin(txts.first, conf.cc.resx - 150);
  379. ret->bitmap = drawBox1 (txts.first + 2*SIDE_MARGIN, txts.second + 2*SIDE_MARGIN, player);
  380. ret->pos.h=ret->bitmap->h;
  381. ret->pos.w=ret->bitmap->w;
  382. ret->pos.x=screen->w/2-(ret->pos.w/2);
  383. ret->pos.y=screen->h/2-(ret->pos.h/2);
  384. if (txts.second > conf.cc.resy - 150)
  385. {
  386. amin(txts.second, conf.cc.resy - 150);
  387. ret->slider = new CSlider(ret->pos.x + ret->pos.w - SIDE_MARGIN, ret->pos.y + SIDE_MARGIN,
  388. ret->pos.h - 2*SIDE_MARGIN, boost::bind (&CInfoWindow::sliderMoved, ret, _1), brtext->size(), brtext->size(), brtext->size()-1, false, 0);
  389. //ret->bitmap->w -= ret->slider->pos.w; //crop text so that slider has more place for itself
  390. }
  391. else
  392. ret->slider = NULL;
  393. int curh = SIDE_MARGIN;
  394. blitTextOnSur (txtg, fontHeight, curh, ret->bitmap);
  395. if (ret->components.size())
  396. {
  397. curh += BEFORE_COMPONENTS;
  398. comps.blitCompsOnSur (_or, 10, curh, ret->bitmap);
  399. }
  400. if(ret->buttons.size())
  401. {
  402. // Position the buttons at the bottom of the window
  403. bw = (ret->bitmap->w/2) - (bw/2);
  404. curh = ret->bitmap->h - SIDE_MARGIN - ret->buttons[0]->imgs[0][0]->h;
  405. for(size_t i=0; i<ret->buttons.size(); i++)
  406. {
  407. ret->buttons[i]->pos.x = bw + ret->pos.x;
  408. ret->buttons[i]->pos.y = curh + ret->pos.y;
  409. bw += ret->buttons[i]->imgs[0][0]->w + 20;
  410. }
  411. }
  412. for(size_t i=0; i<ret->components.size(); i++)
  413. {
  414. ret->components[i]->pos.x += ret->pos.x;
  415. ret->components[i]->pos.y += ret->pos.y;
  416. }
  417. delete brtext;
  418. delete txtg;
  419. if(_or)
  420. SDL_FreeSurface(_or);
  421. }
  422. SDL_Surface * CMessage::genMessage
  423. (std::string title, std::string text, EWindowType type, std::vector<CDefHandler*> *addPics, void * cb)
  424. {
  425. //max x 320 okolo 30 znakow
  426. std::vector<std::string> * tekst;
  427. if (text.length() < 30) //does not need breaking
  428. {
  429. tekst = new std::vector<std::string>();
  430. tekst->push_back(text);
  431. }
  432. else tekst = breakText(text);
  433. int ww, hh; //dimensions of box
  434. if (319>30+13*text.length())
  435. ww = 30+13*text.length();
  436. else ww = 319;
  437. if (title.length())
  438. hh=110+(21*tekst->size());
  439. else hh=60+(21*tekst->size());
  440. if (type==yesOrNO) //make place for buttons
  441. {
  442. if (ww<200) ww=200;
  443. hh+=70;
  444. }
  445. SDL_Surface * ret = drawBox1(ww,hh,0);
  446. //prepare title text
  447. if (title.length())
  448. {
  449. SDL_Surface * titleText = FNT_RenderText(FONT_BIG,title,tytulowy);
  450. //draw title
  451. SDL_Rect tytul = genRect(titleText->h,titleText->w,((ret->w/2)-(titleText->w/2)),37);
  452. SDL_BlitSurface(titleText,NULL,ret,&tytul);
  453. SDL_FreeSurface(titleText);
  454. }
  455. //draw text
  456. for (size_t i=0; i<tekst->size(); i++)
  457. {
  458. int by = 37+i*21;
  459. if (title.length()) by+=40;
  460. SDL_Surface * tresc = FNT_RenderText(FONT_BIG,(*tekst)[i],zwykly);
  461. SDL_Rect trescRect = genRect(tresc->h,tresc->w,((ret->w/2)-(tresc->w/2)),by);
  462. SDL_BlitSurface(tresc,NULL,ret,&trescRect);
  463. SDL_FreeSurface(tresc);
  464. }
  465. if (type==yesOrNO) // add buttons
  466. {
  467. int by = 77+tekst->size()*21;
  468. if (title.length()) by+=40;
  469. int hwo = (*addPics)[0]->ourImages[0].bitmap->w, hwc=(*addPics)[0]->ourImages[0].bitmap->w;
  470. //ok
  471. SDL_Rect trescRect = genRect((*addPics)[0]->ourImages[0].bitmap->h,hwo,((ret->w/2)-hwo-10),by);
  472. SDL_BlitSurface((*addPics)[0]->ourImages[0].bitmap,NULL,ret,&trescRect);
  473. reinterpret_cast<std::vector<SDL_Rect>*>(cb)->push_back(trescRect);
  474. //cancel
  475. trescRect = genRect((*addPics)[1]->ourImages[0].bitmap->h,hwc,((ret->w/2)+10),by);
  476. SDL_BlitSurface((*addPics)[1]->ourImages[0].bitmap,NULL,ret,&trescRect);
  477. reinterpret_cast<std::vector<SDL_Rect>*>(cb)->push_back(trescRect);
  478. }
  479. delete tekst;
  480. return ret;
  481. }
  482. void CMessage::drawBorder(int playerColor, SDL_Surface * ret, int w, int h, int x, int y)
  483. {
  484. //obwodka I-szego rzedu pozioma //border of 1st series, horizontal
  485. for (int i=0; i<w-piecesOfBox[playerColor][6]->w; i+=piecesOfBox[playerColor][6]->w)
  486. {
  487. SDL_BlitSurface
  488. (piecesOfBox[playerColor][6],NULL,ret,&genRect(piecesOfBox[playerColor][6]->h,piecesOfBox[playerColor][6]->w,x+i,y+0));
  489. SDL_BlitSurface
  490. (piecesOfBox[playerColor][7],NULL,ret,&genRect(piecesOfBox[playerColor][7]->h,piecesOfBox[playerColor][7]->w,x+i,y+h-piecesOfBox[playerColor][7]->h+1));
  491. }
  492. //obwodka I-szego rzedu pionowa //border of 1st series, vertical
  493. for (int i=0; i<h-piecesOfBox[playerColor][4]->h; i+=piecesOfBox[playerColor][4]->h)
  494. {
  495. SDL_BlitSurface
  496. (piecesOfBox[playerColor][4],NULL,ret,&genRect(piecesOfBox[playerColor][4]->h,piecesOfBox[playerColor][4]->w,x+0,y+i));
  497. SDL_BlitSurface
  498. (piecesOfBox[playerColor][5],NULL,ret,&genRect(piecesOfBox[playerColor][5]->h,piecesOfBox[playerColor][5]->w,x+w-piecesOfBox[playerColor][5]->w,y+i));
  499. }
  500. //corners
  501. SDL_BlitSurface
  502. (piecesOfBox[playerColor][0],NULL,ret,&genRect(piecesOfBox[playerColor][0]->h,piecesOfBox[playerColor][0]->w,x+0,y+0));
  503. SDL_BlitSurface
  504. (piecesOfBox[playerColor][1],NULL,ret,&genRect(piecesOfBox[playerColor][1]->h,piecesOfBox[playerColor][1]->w,x+w-piecesOfBox[playerColor][1]->w,y+0));
  505. SDL_BlitSurface
  506. (piecesOfBox[playerColor][2],NULL,ret,&genRect(piecesOfBox[playerColor][2]->h,piecesOfBox[playerColor][2]->w,x+0,y+h-piecesOfBox[playerColor][2]->h+1));
  507. SDL_BlitSurface
  508. (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));
  509. }
  510. ComponentResolved::ComponentResolved()
  511. {
  512. comp = NULL;
  513. img = NULL;
  514. txt = NULL;
  515. txtFontHeight = 0;
  516. }
  517. ComponentResolved::ComponentResolved( SComponent *Comp )
  518. {
  519. comp = Comp;
  520. img = comp->getImg();
  521. std::vector<std::string> * brtext = CMessage::breakText(comp->subtitle,13,true,true); //text
  522. txt = CMessage::drawText(brtext,txtFontHeight,FONT_MEDIUM);
  523. delete brtext;
  524. //calculate dimensions
  525. std::pair<int,int> textSize = CMessage::getMaxSizes(txt, txtFontHeight);
  526. comp->pos.w = std::max(textSize.first, img->w); //bigger of: subtitle width and image width
  527. comp->pos.h = img->h + COMPONENT_TO_SUBTITLE + textSize.second;
  528. }
  529. ComponentResolved::~ComponentResolved()
  530. {
  531. for(size_t i = 0; i < txt->size(); i++)
  532. for(size_t j = 0; j < (*txt)[i].size(); j++)
  533. if((*txt)[i][j])
  534. SDL_FreeSurface((*txt)[i][j]);
  535. delete txt;
  536. }
  537. ComponentsToBlit::~ComponentsToBlit()
  538. {
  539. for(size_t i=0; i<comps.size(); i++)
  540. for(size_t j = 0; j < comps[i].size(); j++)
  541. delete comps[i][j];
  542. }
  543. ComponentsToBlit::ComponentsToBlit(std::vector<SComponent*> & SComps, int maxw, SDL_Surface* _or)
  544. {
  545. w = h = 0;
  546. if(!SComps.size())
  547. return;
  548. comps.resize(1);
  549. int curw = 0;
  550. int curr = 0; //current row
  551. for(size_t i=0;i<SComps.size();i++)
  552. {
  553. ComponentResolved *cur = new ComponentResolved(SComps[i]);
  554. int toadd = (cur->comp->pos.w + 12 + (_or ? _or->w : 0));
  555. if (curw + toadd > maxw)
  556. {
  557. curr++;
  558. amax(w,curw);
  559. curw = cur->comp->pos.w;
  560. comps.resize(curr+1);
  561. }
  562. else
  563. {
  564. curw += toadd;
  565. amax(w,curw);
  566. }
  567. comps[curr].push_back(cur);
  568. }
  569. for(size_t i=0;i<comps.size();i++)
  570. {
  571. int maxh = 0;
  572. for(size_t j=0;j<comps[i].size();j++)
  573. amax(maxh,comps[i][j]->comp->pos.h);
  574. h += maxh + BETWEEN_COMPS_ROWS;
  575. }
  576. }
  577. void ComponentsToBlit::blitCompsOnSur( SDL_Surface * _or, int inter, int &curh, SDL_Surface *ret )
  578. {
  579. for (size_t i=0;i<comps.size();i++)
  580. {
  581. int totalw=0, maxh=0;
  582. for(size_t j=0;j<(comps)[i].size();j++)
  583. {
  584. ComponentResolved *cur = (comps)[i][j];
  585. totalw += cur->comp->pos.w;
  586. amax(maxh,cur->comp->pos.h+BETWEEN_COMPS_ROWS);
  587. }
  588. if(_or)
  589. {
  590. totalw += (inter*2+_or->w) * ((comps)[i].size() - 1);
  591. }
  592. else
  593. {
  594. totalw += (inter) * ((comps)[i].size() - 1);
  595. }
  596. curh+=maxh/2;
  597. int curw = (ret->w/2)-(totalw/2);
  598. for(size_t j=0;j<(comps)[i].size();j++)
  599. {
  600. ComponentResolved *cur = (comps)[i][j];
  601. //blit img
  602. int hlp = curh-(cur->comp->pos.h)/2;
  603. blitAt(cur->img, curw + (cur->comp->pos.w - cur->comp->getImg()->w)/2, hlp, ret);
  604. cur->comp->pos.x = curw;
  605. cur->comp->pos.y = hlp;
  606. //blit subtitle
  607. hlp += cur->img->h + COMPONENT_TO_SUBTITLE;
  608. CMessage::blitTextOnSur(cur->txt, cur->txtFontHeight, hlp, ret, cur->comp->pos.x + cur->comp->pos.w/2 );
  609. //if there is subsequent component blit "or"
  610. curw += cur->comp->pos.w;
  611. if(j<((comps)[i].size()-1))
  612. {
  613. if(_or)
  614. {
  615. curw+=inter;
  616. blitAt(_or,curw,curh-(_or->h/2),ret);
  617. curw+=_or->w;
  618. }
  619. curw+=inter;
  620. }
  621. }
  622. curh+=maxh/2;
  623. }
  624. }