SDL_Extensions.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. enum class EScalingAlgorithm : int8_t;
  20. VCMI_LIB_NAMESPACE_BEGIN
  21. class PlayerColor;
  22. class Rect;
  23. class Point;
  24. VCMI_LIB_NAMESPACE_END
  25. namespace CSDL_Ext
  26. {
  27. /// creates Rect using provided rect
  28. Rect fromSDL(const SDL_Rect & rect);
  29. /// creates SDL_Rect using provided rect
  30. SDL_Rect toSDL(const Rect & rect);
  31. /// creates Color using provided SDL_Color
  32. ColorRGBA fromSDL(const SDL_Color & color);
  33. /// creates SDL_Color using provided Color
  34. SDL_Color toSDL(const ColorRGBA & color);
  35. void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
  36. void setAlpha(SDL_Surface * bg, int value);
  37. using TColorPutter = void (*)(uint8_t *&, const uint8_t &, const uint8_t &, const uint8_t &);
  38. using TColorPutterAlpha = void (*)(uint8_t *&, const uint8_t &, const uint8_t &, const uint8_t &, const uint8_t &);
  39. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst);
  40. void blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst);
  41. void setClipRect(SDL_Surface * src, const Rect & other);
  42. void getClipRect(SDL_Surface * src, Rect & other);
  43. void blitSurface(SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dest);
  44. void blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest);
  45. void fillSurface(SDL_Surface * dst, const SDL_Color & color);
  46. void fillRect(SDL_Surface * dst, const Rect & dstrect, const SDL_Color & color);
  47. void fillRectBlended(SDL_Surface * dst, const Rect & dstrect, const SDL_Color & color);
  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);
  57. template<int bpp, bool useAlpha>
  58. int blit8bppAlphaTo24bppT(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint, uint8_t alpha); //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, uint8_t alpha); //blits 8 bpp surface with alpha channel to 24 bpp surface
  60. uint32_t colorTouint32_t(const SDL_Color * color); //little endian only
  61. void drawLine(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color1, const SDL_Color & color2, int width);
  62. void drawLineDashed(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color);
  63. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color & color, int depth = 1);
  64. void drawBorder(SDL_Surface * sur, const Rect & r, const SDL_Color & color, int depth = 1);
  65. SDL_Surface * newSurface(const Point & dimensions, SDL_Surface * mod); //creates new surface, with flags/format same as in surface given
  66. SDL_Surface * newSurface(const Point & dimensions); //creates new surface, with flags/format same as in screen surface
  67. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  68. template<int bpp>
  69. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  70. // bilinear filtering. Always returns rgba surface
  71. SDL_Surface * scaleSurface(SDL_Surface * surf, int width, int height, EScalingAlgorithm scaler);
  72. SDL_Surface * scaleSurfaceIntegerFactor(SDL_Surface * surf, int factor, EScalingAlgorithm scaler);
  73. template<int bpp>
  74. void convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect);
  75. void convertToGrayscale(SDL_Surface * surf, const Rect & rect);
  76. void setColorKey(SDL_Surface * surface, SDL_Color color);
  77. ///set key-color to 0,255,255
  78. void setDefaultColorKey(SDL_Surface * surface);
  79. ///set key-color to 0,255,255 only if it exactly mapped
  80. void setDefaultColorKeyPresize(SDL_Surface * surface);
  81. /// helper that will safely set and un-set ClipRect for SDL_Surface
  82. class CClipRectGuard: boost::noncopyable
  83. {
  84. SDL_Surface * surf;
  85. Rect oldRect;
  86. int getScalingFactor() const;
  87. public:
  88. CClipRectGuard(SDL_Surface * surface, const Rect & rect): surf(surface)
  89. {
  90. CSDL_Ext::getClipRect(surf, oldRect);
  91. CSDL_Ext::setClipRect(surf, rect * getScalingFactor());
  92. }
  93. ~CClipRectGuard()
  94. {
  95. CSDL_Ext::setClipRect(surf, oldRect);
  96. }
  97. };
  98. }