SDL_Extensions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 <SDL_version.h>
  12. #include <SDL_render.h>
  13. #include <SDL_video.h>
  14. #include <SDL_events.h>
  15. #include "../../lib/int3.h"
  16. #include "Geometries.h"
  17. #include "../../lib/GameConstants.h"
  18. extern SDL_Window * mainWindow;
  19. extern SDL_Renderer * mainRenderer;
  20. extern SDL_Texture * screenTexture;
  21. inline void SDL_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
  22. {
  23. SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors);
  24. }
  25. inline void SDL_WarpMouse(int x, int y)
  26. {
  27. SDL_WarpMouseInWindow(mainWindow,x,y);
  28. }
  29. void SDL_UpdateRect(SDL_Surface *surface, int x, int y, int w, int h);
  30. inline bool isCtrlKeyDown()
  31. {
  32. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LCTRL] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RCTRL];
  33. }
  34. inline bool isAltKeyDown()
  35. {
  36. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LALT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RALT];
  37. }
  38. inline bool isShiftKeyDown()
  39. {
  40. return SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_LSHIFT] || SDL_GetKeyboardState(nullptr)[SDL_SCANCODE_RSHIFT];
  41. }
  42. namespace CSDL_Ext
  43. {
  44. template<typename Int>
  45. Int lerp(Int a, Int b, float f)
  46. {
  47. return a + std::round((b - a) * f);
  48. }
  49. //todo: should this better be assignment operator?
  50. STRONG_INLINE void colorAssign(SDL_Color & dest, const SDL_Color & source)
  51. {
  52. dest.r = source.r;
  53. dest.g = source.g;
  54. dest.b = source.b;
  55. dest.a = source.a;
  56. }
  57. inline void setAlpha(SDL_Surface * bg, int value)
  58. {
  59. SDL_SetSurfaceAlphaMod(bg, value);
  60. }
  61. }
  62. struct Rect;
  63. extern SDL_Surface * screen, *screen2, *screenBuf;
  64. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=screen);
  65. void blitAt(SDL_Surface * src, const SDL_Rect & pos, SDL_Surface * dst=screen);
  66. bool isItIn(const SDL_Rect * rect, int x, int y);
  67. bool isItInOrLowerBounds(const SDL_Rect * rect, int x, int y);
  68. /**
  69. * The colors class defines color constants of type SDL_Color.
  70. */
  71. class Colors
  72. {
  73. public:
  74. /** the h3 yellow color, typically used for headlines */
  75. static const SDL_Color YELLOW;
  76. /** the standard h3 white color */
  77. static const SDL_Color WHITE;
  78. /** the metallic gold color used mostly as a border around buttons */
  79. static const SDL_Color METALLIC_GOLD;
  80. /** green color used for in-game console */
  81. static const SDL_Color GREEN;
  82. /** the h3 orange color, used for blocked buttons */
  83. static const SDL_Color ORANGE;
  84. /** the h3 bright yellow color, used for selection border */
  85. static const SDL_Color BRIGHT_YELLOW;
  86. /** default key color for all 8 & 24 bit graphics */
  87. static const SDL_Color DEFAULT_KEY_COLOR;
  88. };
  89. //MSVC gives an error when calling abs with ui64 -> we add template that will match calls with unsigned arg and return it
  90. template<typename T>
  91. typename boost::enable_if_c<boost::is_unsigned<T>::type, T>::type abs(T arg)
  92. {
  93. return arg;
  94. }
  95. template<typename IntType>
  96. 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)
  97. {
  98. IntType max = pow(10, maxLength);
  99. if (std::abs(number) < max)
  100. return boost::lexical_cast<std::string>(number);
  101. std::string symbols = " kMGTPE";
  102. auto iter = symbols.begin();
  103. while (number >= max)
  104. {
  105. number /= 1000;
  106. iter++;
  107. assert(iter != symbols.end());//should be enough even for int64
  108. }
  109. return boost::lexical_cast<std::string>(number) + *iter;
  110. }
  111. typedef void (*TColorPutter)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  112. typedef void (*TColorPutterAlpha)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  113. inline SDL_Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy)
  114. {
  115. SDL_Rect ret;
  116. ret.h=hh;
  117. ret.w=ww;
  118. ret.x=xx;
  119. ret.y=yy;
  120. return ret;
  121. }
  122. template<int bpp, int incrementPtr>
  123. struct ColorPutter
  124. {
  125. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  126. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  127. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  128. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  129. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  130. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  131. };
  132. typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
  133. /// Base class for applying palette transformation on images
  134. class ColorShifter
  135. {
  136. public:
  137. ~ColorShifter() = default;
  138. virtual SDL_Color shiftColor(SDL_Color input) const = 0;
  139. };
  140. /// Generic class for palette transformation
  141. /// formula:
  142. /// result = input * factor + added
  143. class ColorShifterMultiplyAndAdd : public ColorShifter
  144. {
  145. SDL_Color added;
  146. SDL_Color factor;
  147. public:
  148. ColorShifterMultiplyAndAdd(SDL_Color factor, SDL_Color added) :
  149. factor(factor),
  150. added(added)
  151. {}
  152. SDL_Color shiftColor(SDL_Color input) const override
  153. {
  154. return {
  155. uint8_t(std::min(255.f, std::round(input.r * float(factor.r) / 255.f + added.r))),
  156. uint8_t(std::min(255.f, std::round(input.g * float(factor.g) / 255.f + added.g))),
  157. uint8_t(std::min(255.f, std::round(input.b * float(factor.b) / 255.f + added.b))),
  158. uint8_t(std::min(255.f, std::round(input.a * float(factor.a) / 255.f + added.a)))
  159. };
  160. }
  161. };
  162. /// Color shifter that allows to specify color to be excempt from changes
  163. class ColorShifterMultiplyAndAddExcept : public ColorShifterMultiplyAndAdd
  164. {
  165. SDL_Color ignored;
  166. public:
  167. ColorShifterMultiplyAndAddExcept(SDL_Color factor, SDL_Color added, SDL_Color ignored) :
  168. ColorShifterMultiplyAndAdd(factor, added),
  169. ignored(ignored)
  170. {}
  171. SDL_Color shiftColor(SDL_Color input) const override
  172. {
  173. if ( input.r == ignored.r && input.g == ignored.g && input.b == ignored.b && input.a == ignored.a)
  174. return input;
  175. return ColorShifterMultiplyAndAdd::shiftColor(input);
  176. }
  177. };
  178. namespace CSDL_Ext
  179. {
  180. /// helper that will safely set and un-set ClipRect for SDL_Surface
  181. class CClipRectGuard
  182. {
  183. SDL_Surface * surf;
  184. SDL_Rect oldRect;
  185. public:
  186. CClipRectGuard(SDL_Surface * surface, const SDL_Rect & rect):
  187. surf(surface)
  188. {
  189. SDL_GetClipRect(surf, &oldRect);
  190. SDL_SetClipRect(surf, &rect);
  191. }
  192. ~CClipRectGuard()
  193. {
  194. SDL_SetClipRect(surf, &oldRect);
  195. }
  196. };
  197. void blitSurface(SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect);
  198. void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
  199. void fillRectBlack(SDL_Surface * dst, SDL_Rect * dstrect);
  200. //fill dest image with source texture.
  201. void fillTexture(SDL_Surface *dst, SDL_Surface * sourceTexture);
  202. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  203. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  204. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  205. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  206. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  207. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  208. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  209. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  210. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  211. template<int bpp>
  212. int blit8bppAlphaTo24bppT(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect); //blits 8 bpp surface with alpha channel to 24 bpp surface
  213. int blit8bppAlphaTo24bpp(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect); //blits 8 bpp surface with alpha channel to 24 bpp surface
  214. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  215. SDL_Color makeColor(ui8 r, ui8 g, ui8 b, ui8 a);
  216. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  217. void drawLine(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2);
  218. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color);
  219. void drawBorder(SDL_Surface * sur, const SDL_Rect &r, const int3 &color);
  220. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color);
  221. void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
  222. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  223. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  224. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  225. template<int bpp>
  226. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  227. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  228. //scale surface to required size.
  229. //nearest neighbour algorithm
  230. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  231. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  232. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  233. template<int bpp>
  234. void applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode );
  235. void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale
  236. void startTextInput(SDL_Rect * where);
  237. void stopTextInput();
  238. void setColorKey(SDL_Surface * surface, SDL_Color color);
  239. ///set key-color to 0,255,255
  240. void setDefaultColorKey(SDL_Surface * surface);
  241. ///set key-color to 0,255,255 only if it exactly mapped
  242. void setDefaultColorKeyPresize(SDL_Surface * surface);
  243. }