CMessage.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 "CGameInterface.h"
  12. #include "hch\CDefHandler.h"
  13. #include "hch\CSemiDefHandler.h"
  14. #include "CGameInfo.h"
  15. #include "SDL_Extensions.h"
  16. #include <sstream>
  17. SDL_Color tytulowy, tlo, zwykly ;
  18. SDL_Rect genRect(int hh, int ww, int xx, int yy);
  19. extern SDL_Surface * ekran;
  20. extern TTF_Font * TNRB16, *TNR, *GEOR13;
  21. SDL_Color genRGB(int r, int g, int b, int a=0);
  22. //void printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=ekran, unsigned char quality = 2);
  23. bool isItIn(const SDL_Rect * rect, int x, int y);
  24. using namespace NMessage;
  25. namespace NMessage
  26. {
  27. std::vector<std::vector<SDL_Surface*> > piecesOfBox; //in colors of all players
  28. SDL_Surface * background = NULL;
  29. }
  30. CMessage::CMessage()
  31. {
  32. //if (!NMessage::background)
  33. // init();
  34. }
  35. void CMessage::init()
  36. {
  37. {
  38. for (int i=0;i<PLAYER_LIMIT;i++)
  39. {
  40. CDefHandler * bluePieces = CGI->spriteh->giveDef("DIALGBOX.DEF");
  41. std::vector<SDL_Surface *> n;
  42. piecesOfBox.push_back(n);
  43. if (i==1)
  44. {
  45. for (int j=0;j<bluePieces->ourImages.size();j++)
  46. {
  47. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  48. }
  49. }
  50. for (int j=0;j<bluePieces->ourImages.size();j++)
  51. {
  52. CSDL_Ext::blueToPlayersAdv(bluePieces->ourImages[j].bitmap,i);
  53. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  54. }
  55. }
  56. NMessage::background = CGI->bitmaph->loadBitmap("DIBOXBCK.BMP");
  57. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  58. }
  59. }
  60. void CMessage::dispose()
  61. {
  62. for (int i=0;i<PLAYER_LIMIT;i++)
  63. {
  64. for (int j=0;j<piecesOfBox[i].size();j++)
  65. {
  66. SDL_FreeSurface(piecesOfBox[i][j]);
  67. }
  68. }
  69. SDL_FreeSurface(background);
  70. }
  71. SDL_Surface * CMessage::drawBox1(int w, int h, int playerColor)
  72. {
  73. //prepare surface
  74. SDL_Surface * ret = SDL_CreateRGBSurface(ekran->flags, w, h, ekran->format->BitsPerPixel, ekran->format->Rmask, ekran->format->Gmask, ekran->format->Bmask, ekran->format->Amask);
  75. for (int i=0; i<h; i+=background->h)//background
  76. {
  77. for (int j=0; j<w; j+=background->w-1)
  78. SDL_BlitSurface(background,&genRect(background->h,background->w-1,1,0),ret,&genRect(h,w,j,i));
  79. }
  80. //obwodka I-szego rzedu pozioma
  81. for (int i=0; i<w; i+=piecesOfBox[playerColor][6]->w)
  82. {
  83. SDL_BlitSurface
  84. (piecesOfBox[playerColor][6],NULL,ret,&genRect(piecesOfBox[playerColor][6]->h,piecesOfBox[playerColor][6]->w,i,0));
  85. SDL_BlitSurface
  86. (piecesOfBox[playerColor][7],NULL,ret,&genRect(piecesOfBox[playerColor][7]->h,piecesOfBox[playerColor][7]->w,i,h-piecesOfBox[playerColor][7]->h));
  87. }
  88. //obwodka I-szego rzedu pionowa
  89. for (int i=0; i<h; i+=piecesOfBox[playerColor][4]->h)
  90. {
  91. SDL_BlitSurface
  92. (piecesOfBox[playerColor][4],NULL,ret,&genRect(piecesOfBox[playerColor][4]->h,piecesOfBox[playerColor][4]->w,0,i));
  93. SDL_BlitSurface
  94. (piecesOfBox[playerColor][5],NULL,ret,&genRect(piecesOfBox[playerColor][5]->h,piecesOfBox[playerColor][5]->w,w-piecesOfBox[playerColor][5]->w,i));
  95. }
  96. //corners
  97. SDL_BlitSurface
  98. (piecesOfBox[playerColor][0],NULL,ret,&genRect(piecesOfBox[playerColor][0]->h,piecesOfBox[playerColor][0]->w,0,0));
  99. SDL_BlitSurface
  100. (piecesOfBox[playerColor][1],NULL,ret,&genRect(piecesOfBox[playerColor][1]->h,piecesOfBox[playerColor][1]->w,w-piecesOfBox[playerColor][1]->w,0));
  101. SDL_BlitSurface
  102. (piecesOfBox[playerColor][2],NULL,ret,&genRect(piecesOfBox[playerColor][2]->h,piecesOfBox[playerColor][2]->w,0,h-piecesOfBox[playerColor][2]->h));
  103. SDL_BlitSurface
  104. (piecesOfBox[playerColor][3],NULL,ret,&genRect(piecesOfBox[playerColor][3]->h,piecesOfBox[playerColor][3]->w,w-piecesOfBox[playerColor][3]->w,h-piecesOfBox[playerColor][3]->h));
  105. //box gotowy!
  106. return ret;
  107. }
  108. std::vector<std::string> * CMessage::breakText(std::string text, int line, bool userBreak, bool ifor)
  109. {
  110. std::vector<std::string> * ret = new std::vector<std::string>();
  111. while (text.length()>line)
  112. {
  113. int whereCut = -1, braces=0;
  114. bool pom = true, opened=false;
  115. for (int i=0; i<line+braces; i++)
  116. {
  117. if (text[i]==10) //end of line sign
  118. {
  119. whereCut=i+1;
  120. pom=false;
  121. break;
  122. }
  123. else if (ifor && (text[i]=='{') || (text[i]=='}')) // ignore braces
  124. {
  125. if (text[i]=='{')
  126. opened=true;
  127. else
  128. opened=false;
  129. braces++;
  130. }
  131. }
  132. for (int i=line+braces; i>0&&pom; i--)
  133. {
  134. if (text[i]==' ')
  135. {
  136. whereCut = i;
  137. break;
  138. }
  139. else if (opened && text[i]=='{')
  140. opened = false;
  141. else if (text[i]=='}')
  142. opened = true;
  143. }
  144. ret->push_back(text.substr(0,whereCut));
  145. text.erase(0,whereCut);
  146. boost::algorithm::trim_left_if(text,boost::algorithm::is_any_of(" "));
  147. if (opened)
  148. {
  149. (*(ret->end()-1))+='}';
  150. text.insert(0,"{");
  151. }
  152. }
  153. for (int i=0;i<text.length();i++)
  154. {
  155. if (text[i]==10) //end of line sign
  156. {
  157. ret->push_back(text.substr(0,i));
  158. text.erase(0,i);
  159. i=0;
  160. }
  161. }
  162. if (text.length() > 0)
  163. ret->push_back(text);
  164. for (int i=0; i<ret->size(); i++)
  165. {
  166. boost::algorithm::trim((*ret)[i]);
  167. }
  168. return ret;
  169. }
  170. CSimpleWindow * CMessage::genWindow(std::string text, int player, int Lmar, int Rmar, int Tmar, int Bmar)
  171. {
  172. CSimpleWindow * ret = new CSimpleWindow();
  173. std::vector<std::string> * brtext = breakText(text,32,true,true);
  174. std::vector<std::vector<SDL_Surface*> > txtg;
  175. txtg.resize(brtext->size());
  176. for (int i=0; i<brtext->size();i++) //foreach line
  177. {
  178. while((*brtext)[i].length()) //jesli zostalo cos
  179. {
  180. int z=0; bool br=true;
  181. while( ((*brtext)[i][z]) != ('{') )
  182. {
  183. if (z >= (((*brtext)[i].length())-1))
  184. {
  185. br=false;
  186. break;
  187. }
  188. z++;
  189. }
  190. if (!br)
  191. z++;
  192. if (z)
  193. txtg[i].push_back(TTF_RenderText_Blended(TNRB16,(*brtext)[i].substr(0,z).c_str(),zwykly));
  194. (*brtext)[i].erase(0,z);
  195. z=0;
  196. if ( ((*brtext)[i].length()==0) || ((*brtext)[i][z]!='{') )
  197. {
  198. continue;
  199. }
  200. while( ((*brtext)[i][++z]) != ('}') )
  201. {}
  202. //tyemp = (*brtext)[i].substr(1,z-1); //od 1 bo pomijamy otwierajaca klamre
  203. txtg[i].push_back(TTF_RenderText_Blended(TNRB16,(*brtext)[i].substr(1,z-1).c_str(),tytulowy));
  204. (*brtext)[i].erase(0,z+1); //z+1 bo dajemy zamykajaca klamre
  205. } //ends while((*brtext)[i].length())
  206. } //ends for(int i=0; i<brtext->size();i++)
  207. int max = -1, lh=0;
  208. for (int i=0; i<txtg.size();i++) //szukamy najszerszej linii i lacznej wysokosci
  209. {
  210. int lw=0;
  211. for (int j=0;j<txtg[i].size();j++)
  212. {
  213. lw+=txtg[i][j]->w;
  214. lh+=txtg[i][j]->h;
  215. }
  216. if (max<lw)
  217. max=lw;
  218. }
  219. ret->bitmap = drawBox1(max+Lmar+Rmar,lh+Tmar+Bmar,0);
  220. ret->pos.h=ret->bitmap->h;
  221. ret->pos.w=ret->bitmap->w;
  222. for (int i=0; i<txtg.size();i++)
  223. {
  224. int lw=0;
  225. for (int j=0;j<txtg[i].size();j++)
  226. lw+=txtg[i][j]->w;
  227. int pw = ret->bitmap->w/2, ph = ret->bitmap->h/2;
  228. //int pw = Tmar, ph = Lmar;
  229. pw -= lw/2;
  230. ph -= (19*txtg.size())/2;
  231. int tw = pw;
  232. for (int j=0;j<txtg[i].size();j++)
  233. {
  234. //std::stringstream n;
  235. //n <<"temp_"<<i<<"__"<<j<<".bmp";
  236. blitAt(txtg[i][j],tw,ph+i*19,ret->bitmap);
  237. //SDL_SaveBMP(ret->bitmap,n.str().c_str());
  238. tw+=txtg[i][j]->w;
  239. SDL_FreeSurface(txtg[i][j]);
  240. }
  241. }
  242. return ret;
  243. }
  244. SDL_Surface * CMessage::genMessage
  245. (std::string title, std::string text, EWindowType type, std::vector<CDefHandler*> *addPics, void * cb)
  246. {
  247. //max x 320 okolo 30 znakow
  248. std::vector<std::string> * tekst;
  249. if (text.length() < 30) //nie trzeba polamac
  250. {
  251. tekst = new std::vector<std::string>();
  252. tekst->push_back(text);
  253. }
  254. else tekst = breakText(text);
  255. int ww, hh; //wymiary boksa
  256. if (319>30+13*text.length())
  257. ww = 30+13*text.length();
  258. else ww = 319;
  259. if (title.length())
  260. hh=110+(21*tekst->size());
  261. else hh=60+(21*tekst->size());
  262. if (type==EWindowType::yesOrNO) //make place for buttons
  263. {
  264. if (ww<200) ww=200;
  265. hh+=70;
  266. }
  267. SDL_Surface * ret = drawBox1(ww,hh,0);
  268. //prepare title text
  269. if (title.length())
  270. {
  271. //SDL_Surface * titleText = TTF_RenderText_Shaded(TNRB16,title.c_str(),tytulowy,tlo);
  272. SDL_Surface * titleText = TTF_RenderText_Blended(TNRB16,title.c_str(),tytulowy);
  273. //draw title
  274. SDL_Rect tytul = genRect(titleText->h,titleText->w,((ret->w/2)-(titleText->w/2)),37);
  275. SDL_BlitSurface(titleText,NULL,ret,&tytul);
  276. SDL_FreeSurface(titleText);
  277. }
  278. //draw text
  279. for (int i=0; i<tekst->size(); i++)
  280. {
  281. int by = 37+i*21;
  282. if (title.length()) by+=40;
  283. //SDL_Surface * tresc = TTF_RenderText_Shaded(TNRB16,(*tekst)[i].c_str(),zwykly,tlo);
  284. SDL_Surface * tresc = TTF_RenderText_Blended(TNRB16,(*tekst)[i].c_str(),zwykly);
  285. SDL_Rect trescRect = genRect(tresc->h,tresc->w,((ret->w/2)-(tresc->w/2)),by);
  286. SDL_BlitSurface(tresc,NULL,ret,&trescRect);
  287. SDL_FreeSurface(tresc);
  288. }
  289. if (type==EWindowType::yesOrNO) // add buttons
  290. {
  291. int by = 77+tekst->size()*21;
  292. if (title.length()) by+=40;
  293. int hwo = (*addPics)[0]->ourImages[0].bitmap->w, hwc=(*addPics)[0]->ourImages[0].bitmap->w;
  294. //ok
  295. SDL_Rect trescRect = genRect((*addPics)[0]->ourImages[0].bitmap->h,hwo,((ret->w/2)-hwo-10),by);
  296. SDL_BlitSurface((*addPics)[0]->ourImages[0].bitmap,NULL,ret,&trescRect);
  297. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  298. //cancel
  299. trescRect = genRect((*addPics)[1]->ourImages[0].bitmap->h,hwc,((ret->w/2)+10),by);
  300. SDL_BlitSurface((*addPics)[1]->ourImages[0].bitmap,NULL,ret,&trescRect);
  301. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  302. }
  303. delete tekst;
  304. return ret;
  305. }