CMessage.cpp 5.8 KB

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