CMessage.cpp 5.7 KB

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