SDL_Extensions.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 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);
  56. template<int bpp, bool useAlpha>
  57. 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
  58. 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
  59. uint32_t colorTouint32_t(const SDL_Color * color); //little endian only
  60. void drawLine(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color1, const SDL_Color & color2, int width);
  61. void drawLineDashed(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color);
  62. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color & color, int depth = 1);
  63. void drawBorder(SDL_Surface * sur, const Rect & r, const SDL_Color & color, int depth = 1);
  64. SDL_Surface * newSurface(const Point & dimensions, SDL_Surface * mod); //creates new surface, with flags/format same as in surface given
  65. SDL_Surface * newSurface(const Point & dimensions); //creates new surface, with flags/format same as in screen surface
  66. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  67. template<int bpp>
  68. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  69. // bilinear filtering. Always returns rgba surface
  70. SDL_Surface * scaleSurface(SDL_Surface * surf, int width, int height);
  71. SDL_Surface * scaleSurfaceIntegerFactor(SDL_Surface * surf, int factor);
  72. template<int bpp>
  73. void convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect);
  74. void convertToGrayscale(SDL_Surface * surf, const Rect & rect);
  75. void setColorKey(SDL_Surface * surface, SDL_Color color);
  76. ///set key-color to 0,255,255
  77. void setDefaultColorKey(SDL_Surface * surface);
  78. ///set key-color to 0,255,255 only if it exactly mapped
  79. void setDefaultColorKeyPresize(SDL_Surface * surface);
  80. /// helper that will safely set and un-set ClipRect for SDL_Surface
  81. class CClipRectGuard: boost::noncopyable
  82. {
  83. SDL_Surface * surf;
  84. Rect oldRect;
  85. int getScalingFactor() const;
  86. public:
  87. CClipRectGuard(SDL_Surface * surface, const Rect & rect): surf(surface)
  88. {
  89. CSDL_Ext::getClipRect(surf, oldRect);
  90. CSDL_Ext::setClipRect(surf, rect * getScalingFactor());
  91. }
  92. ~CClipRectGuard()
  93. {
  94. CSDL_Ext::setClipRect(surf, oldRect);
  95. }
  96. };
  97. }