CMessage.cpp 5.6 KB

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