CMessage.cpp 20 KB

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