SDL_Extensions.h 9.6 KB

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