SDL_Extensions.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * SDL_Extensions.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/GameConstants.h"
  12. #include "../../lib/Rect.h"
  13. #include "../../lib/Color.h"
  14. struct SDL_Rect;
  15. struct SDL_Window;
  16. struct SDL_Renderer;
  17. struct SDL_Texture;
  18. struct SDL_Surface;
  19. struct SDL_Color;
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. class Rect;
  22. class Point;
  23. VCMI_LIB_NAMESPACE_END
  24. namespace CSDL_Ext
  25. {
  26. /// creates Rect using provided rect
  27. Rect fromSDL(const SDL_Rect & rect);
  28. /// creates SDL_Rect using provided rect
  29. SDL_Rect toSDL(const Rect & rect);
  30. /// creates Color using provided SDL_Color
  31. ColorRGBA fromSDL(const SDL_Color & color);
  32. /// creates SDL_Color using provided Color
  33. SDL_Color toSDL(const ColorRGBA & color);
  34. void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
  35. void setAlpha(SDL_Surface * bg, int value);
  36. using TColorPutter = void (*)(uint8_t *&, const uint8_t &, const uint8_t &, const uint8_t &);
  37. using TColorPutterAlpha = void (*)(uint8_t *&, const uint8_t &, const uint8_t &, const uint8_t &, const uint8_t &);
  38. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  39. void blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst);
  40. void setClipRect(SDL_Surface * src, const Rect & other);
  41. void getClipRect(SDL_Surface * src, Rect & other);
  42. void blitSurface(SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dest);
  43. void blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest);
  44. void fillSurface(SDL_Surface * dst, const SDL_Color & color);
  45. void fillRect(SDL_Surface * dst, const Rect & dstrect, const SDL_Color & color);
  46. void updateRect(SDL_Surface * surface, const Rect & rect);
  47. void putPixelWithoutRefresh(SDL_Surface * ekran, const int & x, const int & y, const uint8_t & R, const uint8_t & G, const uint8_t & B, uint8_t A = 255);
  48. void putPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const uint8_t & R, const uint8_t & G, const uint8_t & B, uint8_t A = 255);
  49. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  50. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  51. uint32_t getPixel(SDL_Surface * surface, const int & x, const int & y, bool colorByte = false);
  52. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  53. bool isTransparent(SDL_Surface * srf, const Point & position); //checks if surface is transparent at given position
  54. uint8_t * getPxPtr(const SDL_Surface * const & srf, const int x, const int y);
  55. TColorPutter getPutterFor(SDL_Surface * const & dest, int incrementing); //incrementing: -1, 0, 1
  56. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const & dest, int incrementing); //incrementing: -1, 0, 1
  57. template<int bpp>
  58. int blit8bppAlphaTo24bppT(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint); //blits 8 bpp surface with alpha channel to 24 bpp surface
  59. int blit8bppAlphaTo24bpp(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint); //blits 8 bpp surface with alpha channel to 24 bpp surface
  60. uint32_t colorTouint32_t(const SDL_Color * color); //little endian only
  61. SDL_Color makeColor(ui8 r, ui8 g, ui8 b, ui8 a);
  62. void drawLine(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color1, const SDL_Color & color2);
  63. void drawLineDashed(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color);
  64. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color & color, int depth = 1);
  65. void drawBorder(SDL_Surface * sur, const Rect & r, const SDL_Color & color, int depth = 1);
  66. void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
  67. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod); //creates new surface, with flags/format same as in surface given
  68. SDL_Surface * newSurface(int w, int h); //creates new surface, with flags/format same as in screen surface
  69. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  70. template<int bpp>
  71. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  72. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  73. //scale surface to required size.
  74. //nearest neighbour algorithm
  75. SDL_Surface * scaleSurfaceFast(SDL_Surface * surf, int width, int height);
  76. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  77. SDL_Surface * scaleSurface(SDL_Surface * surf, int width, int height);
  78. template<int bpp>
  79. void convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect);
  80. void convertToGrayscale(SDL_Surface * surf, const Rect & rect);
  81. void setColorKey(SDL_Surface * surface, SDL_Color color);
  82. ///set key-color to 0,255,255
  83. void setDefaultColorKey(SDL_Surface * surface);
  84. ///set key-color to 0,255,255 only if it exactly mapped
  85. void setDefaultColorKeyPresize(SDL_Surface * surface);
  86. /// helper that will safely set and un-set ClipRect for SDL_Surface
  87. class CClipRectGuard: boost::noncopyable
  88. {
  89. SDL_Surface * surf;
  90. Rect oldRect;
  91. public:
  92. CClipRectGuard(SDL_Surface * surface, const Rect & rect): surf(surface)
  93. {
  94. CSDL_Ext::getClipRect(surf, oldRect);
  95. CSDL_Ext::setClipRect(surf, rect);
  96. }
  97. ~CClipRectGuard()
  98. {
  99. CSDL_Ext::setClipRect(surf, oldRect);
  100. }
  101. };
  102. }