SDL_Extensions.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/Rect.h"
  12. #include "../../lib/Color.h"
  13. struct SDL_Rect;
  14. struct SDL_Window;
  15. struct SDL_Renderer;
  16. struct SDL_Texture;
  17. struct SDL_Surface;
  18. struct SDL_Color;
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. class PlayerColor;
  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 fillRectBlended(SDL_Surface * dst, const Rect & dstrect, const SDL_Color & color);
  47. void updateRect(SDL_Surface * surface, const Rect & rect);
  48. 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);
  49. 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);
  50. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  51. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  52. uint32_t getPixel(SDL_Surface * surface, const int & x, const int & y, bool colorByte = false);
  53. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  54. bool isTransparent(SDL_Surface * srf, const Point & position); //checks if surface is transparent at given position
  55. uint8_t * getPxPtr(const SDL_Surface * const & srf, const int x, const int y);
  56. TColorPutter getPutterFor(SDL_Surface * const & dest, int incrementing); //incrementing: -1, 0, 1
  57. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const & dest, int incrementing); //incrementing: -1, 0, 1
  58. template<int bpp>
  59. 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
  60. 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
  61. uint32_t colorTouint32_t(const SDL_Color * color); //little endian only
  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. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod); //creates new surface, with flags/format same as in surface given
  67. SDL_Surface * newSurface(int w, int h); //creates new surface, with flags/format same as in screen surface
  68. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  69. template<int bpp>
  70. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  71. //scale surface to required size.
  72. //nearest neighbour algorithm
  73. SDL_Surface * scaleSurfaceFast(SDL_Surface * surf, int width, int height);
  74. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  75. SDL_Surface * scaleSurface(SDL_Surface * surf, int width, int height);
  76. template<int bpp>
  77. void convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect);
  78. void convertToGrayscale(SDL_Surface * surf, const Rect & rect);
  79. void setColorKey(SDL_Surface * surface, SDL_Color color);
  80. ///set key-color to 0,255,255
  81. void setDefaultColorKey(SDL_Surface * surface);
  82. ///set key-color to 0,255,255 only if it exactly mapped
  83. void setDefaultColorKeyPresize(SDL_Surface * surface);
  84. /// helper that will safely set and un-set ClipRect for SDL_Surface
  85. class CClipRectGuard: boost::noncopyable
  86. {
  87. SDL_Surface * surf;
  88. Rect oldRect;
  89. public:
  90. CClipRectGuard(SDL_Surface * surface, const Rect & rect): surf(surface)
  91. {
  92. CSDL_Ext::getClipRect(surf, oldRect);
  93. CSDL_Ext::setClipRect(surf, rect);
  94. }
  95. ~CClipRectGuard()
  96. {
  97. CSDL_Ext::setClipRect(surf, oldRect);
  98. }
  99. };
  100. }