SDL_Extensions.h 9.6 KB

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