SDL_Extensions.h 11 KB

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