SDL_Extensions.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. //todo: should this better be assignment operator?
  45. STRONG_INLINE void colorAssign(SDL_Color & dest, const SDL_Color & source)
  46. {
  47. dest.r = source.r;
  48. dest.g = source.g;
  49. dest.b = source.b;
  50. dest.a = source.a;
  51. }
  52. inline void setAlpha(SDL_Surface * bg, int value)
  53. {
  54. SDL_SetSurfaceAlphaMod(bg, value);
  55. }
  56. }
  57. struct Rect;
  58. extern SDL_Surface * screen, *screen2, *screenBuf;
  59. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=screen);
  60. void blitAt(SDL_Surface * src, const SDL_Rect & pos, SDL_Surface * dst=screen);
  61. bool isItIn(const SDL_Rect * rect, int x, int y);
  62. bool isItInOrLowerBounds(const SDL_Rect * rect, int x, int y);
  63. /**
  64. * The colors class defines color constants of type SDL_Color.
  65. */
  66. class Colors
  67. {
  68. public:
  69. /** the h3 yellow color, typically used for headlines */
  70. static const SDL_Color YELLOW;
  71. /** the standard h3 white color */
  72. static const SDL_Color WHITE;
  73. /** the metallic gold color used mostly as a border around buttons */
  74. static const SDL_Color METALLIC_GOLD;
  75. /** green color used for in-game console */
  76. static const SDL_Color GREEN;
  77. /** default key color for all 8 & 24 bit graphics */
  78. static const SDL_Color DEFAULT_KEY_COLOR;
  79. };
  80. //MSVC gives an error when calling abs with ui64 -> we add template that will match calls with unsigned arg and return it
  81. template<typename T>
  82. typename boost::enable_if_c<boost::is_unsigned<T>::type, T>::type abs(T arg)
  83. {
  84. return arg;
  85. }
  86. template<typename IntType>
  87. 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)
  88. {
  89. IntType max = pow(10, maxLength);
  90. if (std::abs(number) < max)
  91. return boost::lexical_cast<std::string>(number);
  92. std::string symbols = " kMGTPE";
  93. auto iter = symbols.begin();
  94. while (number >= max)
  95. {
  96. number /= 1000;
  97. iter++;
  98. assert(iter != symbols.end());//should be enough even for int64
  99. }
  100. return boost::lexical_cast<std::string>(number) + *iter;
  101. }
  102. typedef void (*TColorPutter)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  103. typedef void (*TColorPutterAlpha)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  104. inline SDL_Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy)
  105. {
  106. SDL_Rect ret;
  107. ret.h=hh;
  108. ret.w=ww;
  109. ret.x=xx;
  110. ret.y=yy;
  111. return ret;
  112. }
  113. template<int bpp, int incrementPtr>
  114. struct ColorPutter
  115. {
  116. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  117. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  118. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  119. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  120. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  121. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  122. };
  123. typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
  124. namespace CSDL_Ext
  125. {
  126. /// helper that will safely set and un-set ClipRect for SDL_Surface
  127. class CClipRectGuard
  128. {
  129. SDL_Surface * surf;
  130. SDL_Rect oldRect;
  131. public:
  132. CClipRectGuard(SDL_Surface * surface, const SDL_Rect & rect):
  133. surf(surface)
  134. {
  135. SDL_GetClipRect(surf, &oldRect);
  136. SDL_SetClipRect(surf, &rect);
  137. }
  138. ~CClipRectGuard()
  139. {
  140. SDL_SetClipRect(surf, &oldRect);
  141. }
  142. };
  143. void blitSurface(SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect);
  144. void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
  145. void fillRectBlack(SDL_Surface * dst, SDL_Rect * dstrect);
  146. //fill dest image with source texture.
  147. void fillTexture(SDL_Surface *dst, SDL_Surface * sourceTexture);
  148. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  149. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  150. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  151. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  152. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  153. void alphaTransform(SDL_Surface * src); //adds transparency and shadows (partial handling only; see examples of using for details)
  154. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  155. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  156. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  157. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  158. template<int bpp>
  159. 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
  160. 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
  161. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  162. SDL_Color makeColor(ui8 r, ui8 g, ui8 b, ui8 a);
  163. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  164. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color);
  165. void drawBorder(SDL_Surface * sur, const SDL_Rect &r, const int3 &color);
  166. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color);
  167. void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
  168. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  169. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  170. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  171. template<int bpp>
  172. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  173. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  174. //scale surface to required size.
  175. //nearest neighbour algorithm
  176. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  177. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  178. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  179. template<int bpp>
  180. void applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode );
  181. void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale
  182. void startTextInput(SDL_Rect * where);
  183. void stopTextInput();
  184. void setColorKey(SDL_Surface * surface, SDL_Color color);
  185. ///set key-color to 0,255,255
  186. void setDefaultColorKey(SDL_Surface * surface);
  187. ///set key-color to 0,255,255 only if it exactly mapped
  188. void setDefaultColorKeyPresize(SDL_Surface * surface);
  189. }