CMessage.cpp 6.7 KB

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