CPicture.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include "StdInc.h"
  2. #include "CPicture.h"
  3. #include "../CBitmapHandler.h"
  4. #include "../SDL_Extensions.h"
  5. #include "../Graphics.h"
  6. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  7. {
  8. init();
  9. bg = BG;
  10. freeSurf = Free;
  11. pos.x += x;
  12. pos.y += y;
  13. pos.w = BG->w;
  14. pos.h = BG->h;
  15. }
  16. CPicture::CPicture( const std::string &bmpname, int x, int y )
  17. {
  18. init();
  19. bg = BitmapHandler::loadBitmap(bmpname);
  20. freeSurf = true;;
  21. pos.x += x;
  22. pos.y += y;
  23. if(bg)
  24. {
  25. pos.w = bg->w;
  26. pos.h = bg->h;
  27. }
  28. else
  29. {
  30. pos.w = pos.h = 0;
  31. }
  32. }
  33. CPicture::CPicture(const SRect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  34. {
  35. init();
  36. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  37. }
  38. CPicture::CPicture(const SRect &r, ui32 color, bool screenFormat /*= false*/)
  39. {
  40. init();
  41. createSimpleRect(r, screenFormat, color);
  42. }
  43. CPicture::CPicture(SDL_Surface *BG, const SRect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  44. {
  45. needRefresh = false;
  46. srcRect = new SRect(SrcRect);
  47. pos.x += x;
  48. pos.y += y;
  49. pos.w = srcRect->w;
  50. pos.h = srcRect->h;
  51. bg = BG;
  52. freeSurf = free;
  53. }
  54. CPicture::~CPicture()
  55. {
  56. if(freeSurf)
  57. SDL_FreeSurface(bg);
  58. delete srcRect;
  59. }
  60. void CPicture::init()
  61. {
  62. needRefresh = false;
  63. srcRect = NULL;
  64. }
  65. void CPicture::show( SDL_Surface * to )
  66. {
  67. if (needRefresh)
  68. showAll(to);
  69. }
  70. void CPicture::showAll( SDL_Surface * to )
  71. {
  72. if(bg)
  73. {
  74. if(srcRect)
  75. {
  76. SDL_Rect srcRectCpy = *srcRect;
  77. SDL_Rect dstRect = srcRectCpy;
  78. dstRect.x = pos.x;
  79. dstRect.y = pos.y;
  80. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  81. }
  82. else
  83. blitAt(bg, pos, to);
  84. }
  85. }
  86. void CPicture::convertToScreenBPP()
  87. {
  88. SDL_Surface *hlp = bg;
  89. bg = SDL_ConvertSurface(hlp,screen->format,0);
  90. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  91. SDL_FreeSurface(hlp);
  92. }
  93. void CPicture::createSimpleRect(const SRect &r, bool screenFormat, ui32 color)
  94. {
  95. pos += r;
  96. pos.w = r.w;
  97. pos.h = r.h;
  98. if(screenFormat)
  99. bg = CSDL_Ext::newSurface(r.w, r.h);
  100. else
  101. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  102. SDL_FillRect(bg, NULL, color);
  103. freeSurf = true;
  104. }
  105. void CPicture::colorizeAndConvert(int player)
  106. {
  107. assert(bg);
  108. colorize(player);
  109. convertToScreenBPP();
  110. }
  111. void CPicture::colorize(int player)
  112. {
  113. assert(bg);
  114. assert(bg->format->BitsPerPixel == 8);
  115. graphics->blueToPlayersAdv(bg, player);
  116. }