SDL_Extensions.h 12 KB

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