CMessage.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. std::vector<std::vector<SDL_Surface*> > * CMessage::drawText(std::vector<std::string> * brtext)
  195. {
  196. std::vector<std::vector<SDL_Surface*> > * txtg = new std::vector<std::vector<SDL_Surface*> >();
  197. txtg->resize(brtext->size());
  198. for (int i=0; i<brtext->size();i++) //foreach line
  199. {
  200. while((*brtext)[i].length()) //jesli zostalo cos
  201. {
  202. int z=0; bool br=true;
  203. while( ((*brtext)[i][z]) != ('{') )
  204. {
  205. if (z >= (((*brtext)[i].length())-1))
  206. {
  207. br=false;
  208. break;
  209. }
  210. z++;
  211. }
  212. if (!br)
  213. z++;
  214. if (z)
  215. (*txtg)[i].push_back(TTF_RenderText_Blended(TNRB16,(*brtext)[i].substr(0,z).c_str(),zwykly));
  216. (*brtext)[i].erase(0,z);
  217. z=0;
  218. if ( ((*brtext)[i].length()==0) || ((*brtext)[i][z]!='{') )
  219. {
  220. continue;
  221. }
  222. while( ((*brtext)[i][++z]) != ('}') )
  223. {}
  224. //tyemp = (*brtext)[i].substr(1,z-1); //od 1 bo pomijamy otwierajaca klamre
  225. (*txtg)[i].push_back(TTF_RenderText_Blended(TNRB16,(*brtext)[i].substr(1,z-1).c_str(),tytulowy));
  226. (*brtext)[i].erase(0,z+1); //z+1 bo dajemy zamykajaca klamre
  227. } //ends while((*brtext)[i].length())
  228. } //ends for(int i=0; i<brtext->size();i++)
  229. return txtg;
  230. }
  231. CSimpleWindow * CMessage::genWindow(std::string text, int player, int Lmar, int Rmar, int Tmar, int Bmar)
  232. {
  233. CSimpleWindow * ret = new CSimpleWindow();
  234. std::vector<std::string> * brtext = breakText(text,32,true,true);
  235. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext);
  236. std::pair<int,int> txts = getMaxSizes(txtg);
  237. ret->bitmap = drawBox1(txts.first+Lmar+Rmar,txts.second+Tmar+Bmar,0);
  238. ret->pos.h=ret->bitmap->h;
  239. ret->pos.w=ret->bitmap->w;
  240. for (int i=0; i<txtg->size();i++)
  241. {
  242. int lw=0;
  243. for (int j=0;j<(*txtg)[i].size();j++)
  244. lw+=(*txtg)[i][j]->w;
  245. int pw = ret->bitmap->w/2, ph = ret->bitmap->h/2;
  246. //int pw = Tmar, ph = Lmar;
  247. pw -= lw/2;
  248. ph -= (19*txtg->size())/2;
  249. int tw = pw;
  250. for (int j=0;j<(*txtg)[i].size();j++)
  251. {
  252. //std::stringstream n;
  253. //n <<"temp_"<<i<<"__"<<j<<".bmp";
  254. blitAt((*txtg)[i][j],tw,ph+i*19,ret->bitmap);
  255. //SDL_SaveBMP(ret->bitmap,n.str().c_str());
  256. tw+=(*txtg)[i][j]->w;
  257. SDL_FreeSurface((*txtg)[i][j]);
  258. }
  259. }
  260. return ret;
  261. }
  262. CInfoWindow * CMessage::genIWindow(std::string text, int player, int charperline, std::vector<SComponent*> & comps)
  263. {
  264. //TODO: support for more than one component
  265. CInfoWindow * ret = new CInfoWindow();
  266. ret->components = comps;
  267. //for (int i=0;i<comps.size();i++)
  268. // comps[i]->
  269. std::vector<std::string> * brtext = breakText(text,charperline,true,true);
  270. std::vector<std::string> * brdtext;
  271. if (comps.size())
  272. brdtext = breakText(comps[0]->subtitle,12,true,true);
  273. else
  274. brdtext = NULL;
  275. std::vector<std::vector<SDL_Surface*> > * txtg = drawText(brtext);
  276. std::pair<int,int> txts = getMaxSizes(txtg);
  277. txts.second = txts.second
  278. + ok->ourImages[0].bitmap->h //button
  279. + 15 //after button
  280. + 20; // space between subtitle and button
  281. if (comps.size())
  282. txts.second = txts.second
  283. + 30 //space to first component
  284. + comps[0]->getImg()->h
  285. + 5 //img <-> subtitle
  286. + brdtext->size() * 10; //subtitle //!!!!!!!!!!!!!!!!!!!!
  287. ret->bitmap = drawBox1(txts.first+70,txts.second+70,0);
  288. ret->pos.h=ret->bitmap->h;
  289. ret->pos.w=ret->bitmap->w;
  290. int curh = 30; //gorny margines
  291. for (int i=0; i<txtg->size();i++)
  292. {
  293. int lw=0;
  294. for (int j=0;j<(*txtg)[i].size();j++)
  295. lw+=(*txtg)[i][j]->w;
  296. int pw = ret->bitmap->w/2;
  297. //int pw = Tmar, ph = Lmar;
  298. pw -= lw/2;
  299. int tw = pw;
  300. for (int j=0;j<(*txtg)[i].size();j++) //blit text
  301. {
  302. //std::stringstream n;
  303. //n <<"temp_"<<i<<"__"<<j<<".bmp";
  304. //SDL_SaveBMP(ret->bitmap,n.str().c_str());
  305. blitAt((*txtg)[i][j],tw,curh+i*19,ret->bitmap);
  306. tw+=(*txtg)[i][j]->w;
  307. SDL_FreeSurface((*txtg)[i][j]);
  308. }
  309. }
  310. curh += (19 * txtg->size()); //wys. tekstu
  311. if (comps.size())
  312. {
  313. curh += 30;
  314. comps[0]->pos.x = (ret->bitmap->w/2) - ((comps[0]->getImg()->w)/2);
  315. comps[0]->pos.y = curh;
  316. blitAt(comps[0]->getImg(),comps[0]->pos.x,comps[0]->pos.y,ret->bitmap);
  317. curh += comps[0]->getImg()->h + 5; //obrazek + przerwa
  318. for (int i=0; i<brdtext->size();i++) //descr.
  319. {
  320. SDL_Surface * tesu = TTF_RenderText_Blended(GEOR13,(*brdtext)[i].c_str(),zwykly);
  321. blitAt(tesu,((comps[0]->getImg()->w - tesu->w)/2)+comps[0]->pos.x,curh,ret->bitmap);
  322. curh+=tesu->h;
  323. SDL_FreeSurface(tesu);
  324. }
  325. }
  326. curh += 20; //to buttton
  327. ret->okb.posr.x = (ret->bitmap->w/2) - (ret->okb.imgs[0][0]->w/2);
  328. ret->okb.posr.y = curh;
  329. ret->okb.show();
  330. curh+=ret->okb.imgs[0][0]->h;
  331. return ret;
  332. }
  333. SDL_Surface * CMessage::genMessage
  334. (std::string title, std::string text, EWindowType type, std::vector<CDefHandler*> *addPics, void * cb)
  335. {
  336. //max x 320 okolo 30 znakow
  337. std::vector<std::string> * tekst;
  338. if (text.length() < 30) //nie trzeba polamac
  339. {
  340. tekst = new std::vector<std::string>();
  341. tekst->push_back(text);
  342. }
  343. else tekst = breakText(text);
  344. int ww, hh; //wymiary boksa
  345. if (319>30+13*text.length())
  346. ww = 30+13*text.length();
  347. else ww = 319;
  348. if (title.length())
  349. hh=110+(21*tekst->size());
  350. else hh=60+(21*tekst->size());
  351. if (type==EWindowType::yesOrNO) //make place for buttons
  352. {
  353. if (ww<200) ww=200;
  354. hh+=70;
  355. }
  356. SDL_Surface * ret = drawBox1(ww,hh,0);
  357. //prepare title text
  358. if (title.length())
  359. {
  360. //SDL_Surface * titleText = TTF_RenderText_Shaded(TNRB16,title.c_str(),tytulowy,tlo);
  361. SDL_Surface * titleText = TTF_RenderText_Blended(TNRB16,title.c_str(),tytulowy);
  362. //draw title
  363. SDL_Rect tytul = genRect(titleText->h,titleText->w,((ret->w/2)-(titleText->w/2)),37);
  364. SDL_BlitSurface(titleText,NULL,ret,&tytul);
  365. SDL_FreeSurface(titleText);
  366. }
  367. //draw text
  368. for (int i=0; i<tekst->size(); i++)
  369. {
  370. int by = 37+i*21;
  371. if (title.length()) by+=40;
  372. //SDL_Surface * tresc = TTF_RenderText_Shaded(TNRB16,(*tekst)[i].c_str(),zwykly,tlo);
  373. SDL_Surface * tresc = TTF_RenderText_Blended(TNRB16,(*tekst)[i].c_str(),zwykly);
  374. SDL_Rect trescRect = genRect(tresc->h,tresc->w,((ret->w/2)-(tresc->w/2)),by);
  375. SDL_BlitSurface(tresc,NULL,ret,&trescRect);
  376. SDL_FreeSurface(tresc);
  377. }
  378. if (type==EWindowType::yesOrNO) // add buttons
  379. {
  380. int by = 77+tekst->size()*21;
  381. if (title.length()) by+=40;
  382. int hwo = (*addPics)[0]->ourImages[0].bitmap->w, hwc=(*addPics)[0]->ourImages[0].bitmap->w;
  383. //ok
  384. SDL_Rect trescRect = genRect((*addPics)[0]->ourImages[0].bitmap->h,hwo,((ret->w/2)-hwo-10),by);
  385. SDL_BlitSurface((*addPics)[0]->ourImages[0].bitmap,NULL,ret,&trescRect);
  386. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  387. //cancel
  388. trescRect = genRect((*addPics)[1]->ourImages[0].bitmap->h,hwc,((ret->w/2)+10),by);
  389. SDL_BlitSurface((*addPics)[1]->ourImages[0].bitmap,NULL,ret,&trescRect);
  390. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  391. }
  392. delete tekst;
  393. return ret;
  394. }