SDL_Extensions.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. extern SDL_Window * mainWindow;
  21. extern SDL_Renderer * mainRenderer;
  22. extern SDL_Texture * screenTexture;
  23. extern SDL_Surface * screen, *screen2, *screenBuf;
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. class Rect;
  26. class Point;
  27. VCMI_LIB_NAMESPACE_END
  28. namespace CSDL_Ext
  29. {
  30. /// creates Rect using provided rect
  31. Rect fromSDL(const SDL_Rect & rect);
  32. /// creates SDL_Rect using provided rect
  33. SDL_Rect toSDL(const Rect & rect);
  34. /// creates Color using provided SDL_Color
  35. ColorRGBA fromSDL(const SDL_Color & color);
  36. /// creates SDL_Color using provided Color
  37. SDL_Color toSDL(const ColorRGBA & color);
  38. void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
  39. void setAlpha(SDL_Surface * bg, int value);
  40. template<typename IntType>
  41. std::string makeNumberShort(IntType number, IntType maxLength = 3) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
  42. {
  43. IntType max = pow(10, maxLength);
  44. if (std::abs(number) < max)
  45. return boost::lexical_cast<std::string>(number);
  46. std::string symbols = " kMGTPE";
  47. auto iter = symbols.begin();
  48. while (number >= max)
  49. {
  50. number /= 1000;
  51. iter++;
  52. assert(iter != symbols.end());//should be enough even for int64
  53. }
  54. return boost::lexical_cast<std::string>(number) + *iter;
  55. }
  56. Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy);
  57. typedef void (*TColorPutter)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B);
  58. typedef void (*TColorPutterAlpha)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  59. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=screen);
  60. void blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst=screen);
  61. void setClipRect(SDL_Surface * src, const Rect & other);
  62. void getClipRect(SDL_Surface * src, Rect & other);
  63. void blitSurface(SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dest);
  64. void blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest);
  65. void fillSurface(SDL_Surface *dst, const SDL_Color & color);
  66. void fillRect(SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color);
  67. void updateRect(SDL_Surface *surface, const Rect & rect);
  68. 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);
  69. 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);
  70. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  71. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  72. uint32_t getPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  73. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  74. bool isTransparent(SDL_Surface * srf, const Point & position); //checks if surface is transparent at given position
  75. uint8_t *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  76. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  77. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  78. template<int bpp>
  79. 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
  80. 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
  81. uint32_t colorTouint32_t(const SDL_Color * color); //little endian only
  82. SDL_Color makeColor(ui8 r, ui8 g, ui8 b, ui8 a);
  83. void drawLine(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2);
  84. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color &color, int depth = 1);
  85. void drawBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color, int depth = 1);
  86. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color);
  87. void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
  88. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  89. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  90. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  91. template<int bpp>
  92. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  93. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  94. //scale surface to required size.
  95. //nearest neighbour algorithm
  96. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  97. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  98. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  99. template<int bpp>
  100. void applyEffectBpp( SDL_Surface * surf, const Rect & rect, int mode );
  101. void applyEffect(SDL_Surface * surf, const Rect & rect, int mode); //mode: 0 - sepia, 1 - grayscale
  102. void setColorKey(SDL_Surface * surface, SDL_Color color);
  103. ///set key-color to 0,255,255
  104. void setDefaultColorKey(SDL_Surface * surface);
  105. ///set key-color to 0,255,255 only if it exactly mapped
  106. void setDefaultColorKeyPresize(SDL_Surface * surface);
  107. /// helper that will safely set and un-set ClipRect for SDL_Surface
  108. class CClipRectGuard
  109. {
  110. SDL_Surface * surf;
  111. Rect oldRect;
  112. public:
  113. CClipRectGuard(SDL_Surface * surface, const Rect & rect):
  114. surf(surface)
  115. {
  116. CSDL_Ext::getClipRect(surf, oldRect);
  117. CSDL_Ext::setClipRect(surf, rect);
  118. }
  119. ~CClipRectGuard()
  120. {
  121. CSDL_Ext::setClipRect(surf, oldRect);
  122. }
  123. };
  124. }