SDL_Extensions.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
  123. {
  124. if (abs(number) < 1000)
  125. return boost::lexical_cast<std::string>(number);
  126. std::string symbols = "kMGTPE";
  127. auto iter = symbols.begin();
  128. while (number >= 1000)
  129. {
  130. number /= 1000;
  131. iter++;
  132. assert(iter != symbols.end());//should be enough even for int64
  133. }
  134. return boost::lexical_cast<std::string>(number) + *iter;
  135. }
  136. typedef void (*TColorPutter)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  137. typedef void (*TColorPutterAlpha)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  138. inline SDL_Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy)
  139. {
  140. SDL_Rect ret;
  141. ret.h=hh;
  142. ret.w=ww;
  143. ret.x=xx;
  144. ret.y=yy;
  145. return ret;
  146. }
  147. template<int bpp, int incrementPtr>
  148. struct ColorPutter
  149. {
  150. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  151. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  152. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  153. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  154. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  155. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  156. };
  157. typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
  158. namespace CSDL_Ext
  159. {
  160. /// helper that will safely set and un-set ClipRect for SDL_Surface
  161. class CClipRectGuard
  162. {
  163. SDL_Surface * surf;
  164. SDL_Rect oldRect;
  165. public:
  166. CClipRectGuard(SDL_Surface * surface, const SDL_Rect & rect):
  167. surf(surface)
  168. {
  169. SDL_GetClipRect(surf, &oldRect);
  170. SDL_SetClipRect(surf, &rect);
  171. }
  172. ~CClipRectGuard()
  173. {
  174. SDL_SetClipRect(surf, &oldRect);
  175. }
  176. };
  177. void blitSurface(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect);
  178. void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
  179. void fillRectBlack(SDL_Surface * dst, SDL_Rect * dstrect);
  180. //fill dest image with source texture.
  181. void fillTexture(SDL_Surface *dst, SDL_Surface * sourceTexture);
  182. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  183. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  184. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  185. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  186. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  187. void alphaTransform(SDL_Surface * src); //adds transparency and shadows (partial handling only; see examples of using for details)
  188. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  189. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  190. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  191. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  192. BlitterWithRotationVal getBlitterWithRotation(SDL_Surface *dest);
  193. BlitterWithRotationVal getBlitterWithRotationAndAlpha(SDL_Surface *dest);
  194. 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
  195. 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
  196. 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
  197. 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
  198. 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
  199. 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
  200. 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
  201. 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
  202. 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
  203. 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
  204. template<int bpp>
  205. 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
  206. 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
  207. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  208. SDL_Color makeColor(ui8 r, ui8 g, ui8 b, ui8 a);
  209. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  210. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color);
  211. void drawBorder(SDL_Surface * sur, const SDL_Rect &r, const int3 &color);
  212. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color);
  213. void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
  214. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  215. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  216. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  217. template<int bpp>
  218. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  219. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  220. //scale surface to required size.
  221. //nearest neighbour algorithm
  222. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  223. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  224. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  225. template<int bpp>
  226. void applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode );
  227. void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale
  228. void startTextInput(SDL_Rect * where);
  229. void stopTextInput();
  230. void setColorKey(SDL_Surface * surface, SDL_Color color);
  231. ///set key-color to 0,255,255
  232. void setDefaultColorKey(SDL_Surface * surface);
  233. ///set key-color to 0,255,255 only if it exactly mapped
  234. void setDefaultColorKeyPresize(SDL_Surface * surface);
  235. }