CMessage.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "stdafx.h"
  2. #include "CMessage.h"
  3. #include "SDL_TTF.h"
  4. #include "CSemiDefHandler.h"
  5. #include "CDefHandler.h"
  6. #include "CGameInfo.h"
  7. #include "SDL_Extensions.h"
  8. SDL_Color tytulowy, tlo, zwykly ;
  9. SDL_Rect genRect(int hh, int ww, int xx, int yy);
  10. extern SDL_Surface * ekran;
  11. extern TTF_Font * TNRB16, *TNR, *GEOR13;
  12. SDL_Color genRGB(int r, int g, int b, int a=0);
  13. //void printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=ekran, unsigned char quality = 2);
  14. bool isItIn(const SDL_Rect * rect, int x, int y);
  15. using namespace NMessage;
  16. namespace NMessage
  17. {
  18. std::vector<std::vector<SDL_Surface*> > piecesOfBox; //in colors of all players
  19. SDL_Surface * background = NULL;
  20. }
  21. CMessage::CMessage()
  22. {
  23. if (!NMessage::background)
  24. init();
  25. }
  26. void CMessage::init()
  27. {
  28. {
  29. for (int i=0;i<PLAYER_LIMIT;i++)
  30. {
  31. CDefHandler * bluePieces = CGI->spriteh->giveDef("DIALGBOX.DEF");
  32. std::vector<SDL_Surface *> n;
  33. piecesOfBox.push_back(n);
  34. if (i==1)
  35. {
  36. for (int j=0;j<bluePieces->ourImages.size();j++)
  37. {
  38. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  39. }
  40. }
  41. for (int j=0;j<bluePieces->ourImages.size();j++)
  42. {
  43. CSDL_Ext::blueToPlayersAdv(bluePieces->ourImages[j].bitmap,i);
  44. piecesOfBox[i].push_back(bluePieces->ourImages[j].bitmap);
  45. }
  46. }
  47. NMessage::background = CGI->bitmaph->loadBitmap("DIBOXBCK.BMP");
  48. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  49. }
  50. }
  51. void CMessage::dispose()
  52. {
  53. for (int i=0;i<PLAYER_LIMIT;i++)
  54. {
  55. for (int j=0;j<piecesOfBox[i].size();j++)
  56. {
  57. SDL_FreeSurface(piecesOfBox[i][j]);
  58. }
  59. }
  60. SDL_FreeSurface(background);
  61. }
  62. SDL_Surface * CMessage::drawBox1(int w, int h, int playerColor)
  63. {
  64. //prepare surface
  65. SDL_Surface * ret = SDL_CreateRGBSurface(ekran->flags, w, h, ekran->format->BitsPerPixel, ekran->format->Rmask, ekran->format->Gmask, ekran->format->Bmask, ekran->format->Amask);
  66. for (int i=0; i<h; i+=background->h)//background
  67. {
  68. for (int j=0; j<w; j+=background->w-1)
  69. SDL_BlitSurface(background,&genRect(background->h,background->w-1,1,0),ret,&genRect(h,w,j,i));
  70. }
  71. //obwodka I-szego rzedu pozioma
  72. for (int i=0; i<w; i+=piecesOfBox[playerColor][6]->w)
  73. {
  74. SDL_BlitSurface
  75. (piecesOfBox[playerColor][6],NULL,ret,&genRect(piecesOfBox[playerColor][6]->h,piecesOfBox[playerColor][6]->w,i,0));
  76. SDL_BlitSurface
  77. (piecesOfBox[playerColor][7],NULL,ret,&genRect(piecesOfBox[playerColor][7]->h,piecesOfBox[playerColor][7]->w,i,h-piecesOfBox[playerColor][7]->h));
  78. }
  79. //obwodka I-szego rzedu pionowa
  80. for (int i=0; i<h; i+=piecesOfBox[playerColor][4]->h)
  81. {
  82. SDL_BlitSurface
  83. (piecesOfBox[playerColor][4],NULL,ret,&genRect(piecesOfBox[playerColor][4]->h,piecesOfBox[playerColor][4]->w,0,i));
  84. SDL_BlitSurface
  85. (piecesOfBox[playerColor][5],NULL,ret,&genRect(piecesOfBox[playerColor][5]->h,piecesOfBox[playerColor][5]->w,w-piecesOfBox[playerColor][5]->w,i));
  86. }
  87. //corners
  88. SDL_BlitSurface
  89. (piecesOfBox[playerColor][0],NULL,ret,&genRect(piecesOfBox[playerColor][0]->h,piecesOfBox[playerColor][0]->w,0,0));
  90. SDL_BlitSurface
  91. (piecesOfBox[playerColor][1],NULL,ret,&genRect(piecesOfBox[playerColor][1]->h,piecesOfBox[playerColor][1]->w,w-piecesOfBox[playerColor][1]->w,0));
  92. SDL_BlitSurface
  93. (piecesOfBox[playerColor][2],NULL,ret,&genRect(piecesOfBox[playerColor][2]->h,piecesOfBox[playerColor][2]->w,0,h-piecesOfBox[playerColor][2]->h));
  94. SDL_BlitSurface
  95. (piecesOfBox[playerColor][3],NULL,ret,&genRect(piecesOfBox[playerColor][3]->h,piecesOfBox[playerColor][3]->w,w-piecesOfBox[playerColor][3]->w,h-piecesOfBox[playerColor][3]->h));
  96. //box gotowy!
  97. return ret;
  98. }
  99. std::vector<std::string> * CMessage::breakText(std::string text, int line, bool userBreak)
  100. {
  101. std::vector<std::string> * ret = new std::vector<std::string>();
  102. while (text.length()>line)
  103. {
  104. int whereCut = -1;
  105. bool pom = true;
  106. for (int i=0; i<line; i++)
  107. {
  108. if (text[i]==10) //end of line sign
  109. {
  110. whereCut=i+1;
  111. pom=false;
  112. break;
  113. }
  114. }
  115. for (int i=line; i>0&&pom; i--)
  116. {
  117. if (text[i]==' ')
  118. {
  119. whereCut = i;
  120. break;
  121. }
  122. }
  123. ret->push_back(text.substr(0,whereCut));
  124. text.erase(0,whereCut);
  125. }
  126. for (int i=0;i<text.length();i++)
  127. {
  128. if (text[i]==10) //end of line sign
  129. {
  130. ret->push_back(text.substr(0,i));
  131. text.erase(0,i);
  132. i=0;
  133. }
  134. }
  135. if (text.length() > 0)
  136. ret->push_back(text);
  137. return ret;
  138. }
  139. SDL_Surface * CMessage::genMessage
  140. (std::string title, std::string text, EWindowType type, std::vector<CDefHandler*> *addPics, void * cb)
  141. {
  142. //max x 320 okolo 30 znakow
  143. std::vector<std::string> * tekst;
  144. if (text.length() < 30) //nie trzeba polamac
  145. {
  146. tekst = new std::vector<std::string>();
  147. tekst->push_back(text);
  148. }
  149. else tekst = breakText(text);
  150. int ww, hh; //wymiary boksa
  151. if (319>30+13*text.length())
  152. ww = 30+13*text.length();
  153. else ww = 319;
  154. if (title.length())
  155. hh=110+(21*tekst->size());
  156. else hh=60+(21*tekst->size());
  157. if (type==EWindowType::yesOrNO) //make place for buttons
  158. {
  159. if (ww<200) ww=200;
  160. hh+=70;
  161. }
  162. SDL_Surface * ret = drawBox1(ww,hh,0);
  163. //prepare title text
  164. if (title.length())
  165. {
  166. //SDL_Surface * titleText = TTF_RenderText_Shaded(TNRB16,title.c_str(),tytulowy,tlo);
  167. SDL_Surface * titleText = TTF_RenderText_Blended(TNRB16,title.c_str(),tytulowy);
  168. //draw title
  169. SDL_Rect tytul = genRect(titleText->h,titleText->w,((ret->w/2)-(titleText->w/2)),37);
  170. SDL_BlitSurface(titleText,NULL,ret,&tytul);
  171. SDL_FreeSurface(titleText);
  172. }
  173. //draw text
  174. for (int i=0; i<tekst->size(); i++)
  175. {
  176. int by = 37+i*21;
  177. if (title.length()) by+=40;
  178. //SDL_Surface * tresc = TTF_RenderText_Shaded(TNRB16,(*tekst)[i].c_str(),zwykly,tlo);
  179. SDL_Surface * tresc = TTF_RenderText_Blended(TNRB16,(*tekst)[i].c_str(),zwykly);
  180. SDL_Rect trescRect = genRect(tresc->h,tresc->w,((ret->w/2)-(tresc->w/2)),by);
  181. SDL_BlitSurface(tresc,NULL,ret,&trescRect);
  182. SDL_FreeSurface(tresc);
  183. }
  184. if (type==EWindowType::yesOrNO) // add buttons
  185. {
  186. int by = 77+tekst->size()*21;
  187. if (title.length()) by+=40;
  188. int hwo = (*addPics)[0]->ourImages[0].bitmap->w, hwc=(*addPics)[0]->ourImages[0].bitmap->w;
  189. //ok
  190. SDL_Rect trescRect = genRect((*addPics)[0]->ourImages[0].bitmap->h,hwo,((ret->w/2)-hwo-10),by);
  191. SDL_BlitSurface((*addPics)[0]->ourImages[0].bitmap,NULL,ret,&trescRect);
  192. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  193. //cancel
  194. trescRect = genRect((*addPics)[1]->ourImages[0].bitmap->h,hwc,((ret->w/2)+10),by);
  195. SDL_BlitSurface((*addPics)[1]->ourImages[0].bitmap,NULL,ret,&trescRect);
  196. ((std::vector<SDL_Rect>*)(cb))->push_back(trescRect);
  197. }
  198. delete tekst;
  199. return ret;
  200. }