CMessage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. #include "stdafx.h"
  2. #include "CMessage.h"
  3. #include "SDL_TTF.h"
  4. #include "hch\CSemiDefHandler.h"
  5. #include "hch\CDefHandler.h"
  6. #include "CGameInfo.h"
  7. #include "SDL_Extensions.h"
  8. #include "hch\CLodHandler.h"
  9. #include <boost/algorithm/string.hpp>
  10. #include <boost/algorithm/string/replace.hpp>
  11. #include "CPlayerInterface.h"
  12. #include "hch\CDefHandler.h"
  13. #include "hch\CSemiDefHandler.h"
  14. #include "CGameInfo.h"
  15. #include "SDL_Extensions.h"
  16. #include <sstream>
  17. #include "CLua.h"
  18. SDL_Color tytulowy, tlo, zwykly ;
  19. SDL_Rect genRect(int hh, int ww, int xx, int yy);
  20. extern SDL_Surface * ekran;
  21. extern TTF_Font * TNRB16, *TNR, *GEOR13;
  22. SDL_Color genRGB(int r, int g, int b, int a=0);
  23. //void printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=ekran, unsigned char quality = 2);
  24. bool isItIn(const SDL_Rect * rect, int x, int y);
  25. using namespace NMessage;
  26. namespace NMessage
  27. {
  28. CDefHandler * ok, *cancel;
  29. std::vector<std::vector<SDL_Surface*> > piecesOfBox; //in colors of all players
  30. SDL_Surface * background = NULL;
  31. }
  32. CMessage::CMessage()
  33. {
  34. //if (!NMessage::background)
  35. // init();
  36. }
  37. void CMessage::init()
  38. {
  39. {
  40. for (int i=0;i<PLAYER_LIMIT;i++)
  41. {
  42. CDefHandler * bluePieces = CGI->spriteh->giveDef("DIALGBOX.DEF");
  43. std::vector<SDL_Surface *> n;
  44. piecesOfBox.push_back(n);
  45. if (i==1)
  46. {
  47. for (int j=0;j<bluePieces->ourImages.size();j++)
  48. {
  49. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  50. }
  51. }
  52. for (int j=0;j<bluePieces->ourImages.size();j++)
  53. {
  54. CSDL_Ext::blueToPlayersAdv(bluePieces->ourImages[j].bitmap,i);
  55. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  56. }
  57. }
  58. NMessage::background = CGI->bitmaph->loadBitmap("DIBOXBCK.BMP");
  59. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  60. }
  61. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  62. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  63. }
  64. void CMessage::dispose()
  65. {
  66. for (int i=0;i<PLAYER_LIMIT;i++)
  67. {
  68. for (int j=0;j<piecesOfBox[i].size();j++)
  69. {
  70. SDL_FreeSurface(piecesOfBox[i][j]);
  71. }
  72. }
  73. SDL_FreeSurface(background);
  74. delete ok;
  75. delete cancel;
  76. }
  77. SDL_Surface * CMessage::drawBox1(int w, int h, int playerColor)
  78. {
  79. //prepare surface
  80. SDL_Surface * ret = SDL_CreateRGBSurface(ekran->flags, w, h, ekran->format->BitsPerPixel, ekran->format->Rmask, ekran->format->Gmask, ekran->format->Bmask, ekran->format->Amask);
  81. for (int i=0; i<h; i+=background->h)//background
  82. {
  83. for (int j=0; j<w; j+=background->w-1)
  84. SDL_BlitSurface(background,&genRect(background->h,background->w-1,1,0),ret,&genRect(h,w,j,i));
  85. }
  86. //obwodka I-szego rzedu pozioma
  87. for (int i=0; i<w; i+=piecesOfBox[playerColor][6]->w)
  88. {
  89. SDL_BlitSurface
  90. (piecesOfBox[playerColor][6],NULL,ret,&genRect(piecesOfBox[playerColor][6]->h,piecesOfBox[playerColor][6]->w,i,0));
  91. SDL_BlitSurface
  92. (piecesOfBox[playerColor][7],NULL,ret,&genRect(piecesOfBox[playerColor][7]->h,piecesOfBox[playerColor][7]->w,i,h-piecesOfBox[playerColor][7]->h));
  93. }
  94. //obwodka I-szego rzedu pionowa
  95. for (int i=0; i<h; i+=piecesOfBox[playerColor][4]->h)
  96. {
  97. SDL_BlitSurface
  98. (piecesOfBox[playerColor][4],NULL,ret,&genRect(piecesOfBox[playerColor][4]->h,piecesOfBox[playerColor][4]->w,0,i));
  99. SDL_BlitSurface
  100. (piecesOfBox[playerColor][5],NULL,ret,&genRect(piecesOfBox[playerColor][5]->h,piecesOfBox[playerColor][5]->w,w-piecesOfBox[playerColor][5]->w,i));
  101. }
  102. //corners
  103. SDL_BlitSurface
  104. (piecesOfBox[playerColor][0],NULL,ret,&genRect(piecesOfBox[playerColor][0]->h,piecesOfBox[playerColor][0]->w,0,0));
  105. SDL_BlitSurface
  106. (piecesOfBox[playerColor][1],NULL,ret,&genRect(piecesOfBox[playerColor][1]->h,piecesOfBox[playerColor][1]->w,w-piecesOfBox[playerColor][1]->w,0));
  107. SDL_BlitSurface
  108. (piecesOfBox[playerColor][2],NULL,ret,&genRect(piecesOfBox[playerColor][2]->h,piecesOfBox[playerColor][2]->w,0,h-piecesOfBox[playerColor][2]->h));
  109. SDL_BlitSurface
  110. (piecesOfBox[playerColor][3],NULL,ret,&genRect(piecesOfBox[playerColor][3]->h,piecesOfBox[playerColor][3]->w,w-piecesOfBox[playerColor][3]->w,h-piecesOfBox[playerColor][3]->h));
  111. //box gotowy!
  112. return ret;
  113. }
  114. std::vector<std::string> * CMessage::breakText(std::string text, int line, bool userBreak, bool ifor)
  115. {
  116. std::vector<std::string> * ret = new std::vector<std::string>();
  117. while (text.length()>line)
  118. {
  119. int whereCut = -1, braces=0;
  120. bool pom = true, opened=false;
  121. for (int i=0; i<line+braces; i++)
  122. {
  123. if (text[i]==10) //end of line sign
  124. {
  125. whereCut=i+1;
  126. pom=false;
  127. break;
  128. }
  129. else if (ifor && (text[i]=='{') || (text[i]=='}')) // ignore braces
  130. {
  131. if (text[i]=='{')
  132. opened=true;
  133. else
  134. opened=false;
  135. braces++;
  136. }
  137. }
  138. for (int i=line+braces; i>0&&pom; i--)
  139. {
  140. if (text[i]==' ')
  141. {
  142. whereCut = i;
  143. break;
  144. }
  145. else if (opened && text[i]=='{')
  146. opened = false;
  147. else if (text[i]=='}')
  148. opened = true;
  149. }
  150. ret->push_back(text.substr(0,whereCut));
  151. text.erase(0,whereCut);
  152. boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(" "));
  153. if (opened)
  154. {
  155. (*(ret->end()-1))+='}';
  156. text.insert(0,"{");
  157. }
  158. }
  159. for (int i=0;i<text.length();i++)
  160. {
  161. if (text[i]==10) //end of line sign
  162. {
  163. ret->push_back(text.substr(0,i));
  164. text.erase(0,i);
  165. i=0;
  166. }
  167. }
  168. if (text.length() > 0)
  169. ret->push_back(text);
  170. for (int i=0; i<ret->size(); i++)
  171. {
  172. boost::algorithm::trim((*ret)[i]);
  173. }
  174. return ret;
  175. }
  176. std::pair<int,int> CMessage::getMaxSizes(std::vector<std::vector<SDL_Surface*> > * txtg)
  177. {
  178. std::pair<int,int> ret;
  179. ret.first = -1;
  180. ret.second=0;
  181. for (int i=0; i<txtg->size();i++) //szukamy najszerszej linii i lacznej wysokosci
  182. {
  183. int lw=0;
  184. for (int j=0;j<(*txtg)[i].size();j++)
  185. {
  186. lw+=(*txtg)[i][j]->w;
  187. ret.second+=(*txtg)[i][j]->h;
  188. }
  189. if (ret.first<lw)
  190. ret.first=lw;
  191. }
  192. return ret;
  193. }
  194. SDL_Surface * CMessage::blitTextOnSur(std::vector<std::vector<SDL_Surface*> > * txtg, int & curh, SDL_Surface * ret)
  195. {
  196. for (int i=0; i<txtg->size();i++)
  197. {
  198. int lw=0;
  199. for (int j=0;j<(*txtg)[i].size();j++)
  200. lw+=(*txtg)[i][j]->w; //lw - laczna szerokosc linii
  201. int pw = ret->w/2;
  202. pw -= lw/2; //poczatek tekstu (x)
  203. int tw = pw;
  204. for (int j=0;j<(*txtg)[i].size();j++) //blit text
  205. {
  206. blitAt((*txtg)[i][j],tw,curh+i*19,ret);
  207. tw+=(*txtg)[i][j]->w;
  208. SDL_FreeSurface((*txtg)[i][j]);
  209. }
  210. }
  211. return ret;
  212. }
  213. SDL_Surface * CMessage::blitCompsOnSur(std::vector<SComponent*> & comps, int maxw, int inter, int & curh, SDL_Surface * ret)
  214. {
  215. std::vector<std::string> * brdtext;
  216. if (comps.size())
  217. brdtext = breakText(comps[0]->subtitle,12,true,true);
  218. else
  219. brdtext = NULL;
  220. curh += 30;
  221. comps[0]->pos.x = (ret->w/2) - ((comps[0]->getImg()->w)/2);
  222. comps[0]->pos.y = curh;
  223. blitAt(comps[0]->getImg(),comps[0]->pos.x,comps[0]->pos.y,ret);
  224. curh += comps[0]->getImg()->h + 5; //obrazek + przerwa
  225. for (int i=0; i<brdtext->size();i++) //descr.
  226. {
  227. SDL_Surface * tesu = TTF_RenderText_Blended(GEOR13,(*brdtext)[i].c_str(),zwykly);
  228. blitAt(tesu,((comps[0]->getImg()->w - tesu->w)/2)+comps[0]->pos.x,curh,ret);
  229. curh+=tesu->h;
  230. SDL_FreeSurface(tesu);
  231. }
  232. return ret;
  233. }
  234. std::vector<std::vector<SDL_Surface*> > * CMessage::drawText(std::vector<std::string> * brtext)
  235. {
  236. std::vector<std::vector<SDL_Surface*> > * txtg = new std::vector<std::vector<SDL_Surface*> >();
  237. txtg->resize(brtext->size());
  238. for (int i=0; i<brtext->size();i++) //foreach line
  239. {
  240. while((*brtext)[i].length()) //jesli zostalo cos
  241. {
  242. int z=0; bool br=true;
  243. while( ((*brtext)[i][z]) != ('{') )
  244. {
  245. if (z >= (((*brtext)[i].length())-1))
  246. {
  247. br=false;
  248. break;
  249. }
  250. z++;
  251. }
  252. if (!br)
  253. z++;
  254. if (z)
  255. (*txtg)[i].push_back(TTF_RenderText_Blended(TNRB16,(*brtext)[i].substr(0,z).c_str(),zwykly));
  256. (*brtext)[i].erase(0,z);
  257. z=0;
  258. if ( ((*brtext)[i].length()==0) || ((*brtext)[i][z]!='{') )
  259. {
  260. continue;
  261. }
  262. while( ((*brtext)[i][++z]) != ('}') )
  263. {}
  264. //tyemp = (*brtext)[i].substr(1,z-1); //od 1 bo pomijamy otwierajaca klamre
  265. (*txtg)[i].push_back(TTF_RenderText_Blended(TNRB16,(*brtext)[i].substr(1,z-1).c_str(),tytulowy));
  266. (*brtext)[i].erase(0,z+1); //z+1 bo dajemy zamykajaca klamre
  267. } //ends while((*brtext)[i].length())
  268. } //ends for(int i=0; i<brtext->size();i++)
  269. return txtg;
  270. }
  271. CSimpleWindow * CMessage::genWindow(std::string text, int player, int Lmar, int Rmar, int Tmar, int Bmar)
  272. {
  273. CSimpleWindow * ret = new CSimpleWindow();
  274. std::vector<std::string> * brtext = breakText(text,32,true,true);
  275. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext);
  276. std::pair<int,int> txts = getMaxSizes(txtg);
  277. ret->bitmap = drawBox1(txts.first+Lmar+Rmar,txts.second+Tmar+Bmar,0);
  278. ret->pos.h=ret->bitmap->h;
  279. ret->pos.w=ret->bitmap->w;
  280. for (int i=0; i<txtg->size();i++)
  281. {
  282. int lw=0;
  283. for (int j=0;j<(*txtg)[i].size();j++)
  284. lw+=(*txtg)[i][j]->w;
  285. int pw = ret->bitmap->w/2, ph = ret->bitmap->h/2;
  286. //int pw = Tmar, ph = Lmar;
  287. pw -= lw/2;
  288. ph -= (19*txtg->size())/2;
  289. int tw = pw;
  290. for (int j=0;j<(*txtg)[i].size();j++)
  291. {
  292. //std::stringstream n;
  293. //n <<"temp_"<<i<<"__"<<j<<".bmp";
  294. blitAt((*txtg)[i][j],tw,ph+i*19,ret->bitmap);
  295. //SDL_SaveBMP(ret->bitmap,n.str().c_str());
  296. tw+=(*txtg)[i][j]->w;
  297. SDL_FreeSurface((*txtg)[i][j]);
  298. }
  299. }
  300. return ret;
  301. }
  302. CInfoWindow * CMessage::genIWindow(std::string text, int player, int charperline, std::vector<SComponent*> & comps)
  303. {
  304. //TODO: support for more than one component
  305. CInfoWindow * ret = new CInfoWindow();
  306. ret->components = comps;
  307. std::vector<std::string> * brtext = breakText(text,charperline,true,true);
  308. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext);
  309. std::pair<int,int> txts = getMaxSizes(txtg);
  310. txts.second = txts.second
  311. + ok->ourImages[0].bitmap->h //button
  312. + 15 //after button
  313. + 20; // space between subtitle and button
  314. if (comps.size())
  315. txts.second = txts.second
  316. + 30 //space to first component
  317. + comps[0]->getImg()->h
  318. + 5 //img <-> subtitle
  319. + 20; //subtitle //!!!!!!!!!!!!!!!!!!!!
  320. ret->bitmap = drawBox1(txts.first+70,txts.second+70,0);
  321. ret->pos.h=ret->bitmap->h;
  322. ret->pos.w=ret->bitmap->w;
  323. int curh = 30; //gorny margines
  324. blitTextOnSur(txtg,curh,ret->bitmap);
  325. curh += (19 * txtg->size()); //wys. tekstu
  326. if (comps.size())
  327. {
  328. blitCompsOnSur(comps,200,0,curh,ret->bitmap);
  329. }
  330. curh += 20; //to buttton
  331. ret->okb.posr.x = (ret->bitmap->w/2) - (ret->okb.imgs[0][0]->w/2);
  332. ret->okb.posr.y = curh;
  333. ret->okb.show();
  334. curh+=ret->okb.imgs[0][0]->h;
  335. return ret;
  336. }
  337. SDL_Surface * CMessage::genMessage
  338. (std::string title, std::string text, EWindowType type, std::vector<CDefHandler*> *addPics, void * cb)
  339. {
  340. //max x 320 okolo 30 znakow
  341. std::vector<std::string> * tekst;
  342. if (text.length() < 30) //nie trzeba polamac
  343. {
  344. tekst = new std::vector<std::string>();
  345. tekst->push_back(text);
  346. }
  347. else tekst = breakText(text);
  348. int ww, hh; //wymiary boksa
  349. if (319>30+13*text.length())
  350. ww = 30+13*text.length();
  351. else ww = 319;
  352. if (title.length())
  353. hh=110+(21*tekst->size());
  354. else hh=60+(21*tekst->size());
  355. if (type==EWindowType::yesOrNO) //make place for buttons
  356. {
  357. if (ww<200) ww=200;
  358. hh+=70;
  359. }
  360. SDL_Surface * ret = drawBox1(ww,hh,0);
  361. //prepare title text
  362. if (title.length())
  363. {
  364. //SDL_Surface * titleText = TTF_RenderText_Shaded(TNRB16,title.c_str(),tytulowy,tlo);
  365. SDL_Surface * titleText = TTF_RenderText_Blended(TNRB16,title.c_str(),tytulowy);
  366. //draw title
  367. SDL_Rect tytul = genRect(titleText->h,titleText->w,((ret->w/2)-(titleText->w/2)),37);
  368. SDL_BlitSurface(titleText,NULL,ret,&tytul);
  369. SDL_FreeSurface(titleText);
  370. }
  371. //draw text
  372. for (int i=0; i<tekst->size(); i++)
  373. {
  374. int by = 37+i*21;
  375. if (title.length()) by+=40;
  376. //SDL_Surface * tresc = TTF_RenderText_Shaded(TNRB16,(*tekst)[i].c_str(),zwykly,tlo);
  377. SDL_Surface * tresc = TTF_RenderText_Blended(TNRB16,(*tekst)[i].c_str(),zwykly);
  378. SDL_Rect trescRect = genRect(tresc->h,tresc->w,((ret->w/2)-(tresc->w/2)),by);
  379. SDL_BlitSurface(tresc,NULL,ret,&trescRect);
  380. SDL_FreeSurface(tresc);
  381. }
  382. if (type==EWindowType::yesOrNO) // add buttons
  383. {
  384. int by = 77+tekst->size()*21;
  385. if (title.length()) by+=40;
  386. int hwo = (*addPics)[0]->ourImages[0].bitmap->w, hwc=(*addPics)[0]->ourImages[0].bitmap->w;
  387. //ok
  388. SDL_Rect trescRect = genRect((*addPics)[0]->ourImages[0].bitmap->h,hwo,((ret->w/2)-hwo-10),by);
  389. SDL_BlitSurface((*addPics)[0]->ourImages[0].bitmap,NULL,ret,&trescRect);
  390. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  391. //cancel
  392. trescRect = genRect((*addPics)[1]->ourImages[0].bitmap->h,hwc,((ret->w/2)+10),by);
  393. SDL_BlitSurface((*addPics)[1]->ourImages[0].bitmap,NULL,ret,&trescRect);
  394. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  395. }
  396. delete tekst;
  397. return ret;
  398. }