2
0

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