SDL_Extensions.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. bool isItIn(const SDL_Rect * rect, int x, int y);
  32. /**
  33. * The colors class defines color constants of type SDL_Color.
  34. */
  35. class Colors
  36. {
  37. public:
  38. /** the h3 yellow color, typically used for headlines */
  39. static const SDL_Color YELLOW;
  40. /** the standard h3 white color */
  41. static const SDL_Color WHITE;
  42. /** the metallic gold color used mostly as a border around buttons */
  43. static const SDL_Color METALLIC_GOLD;
  44. /** green color used for in-game console */
  45. static const SDL_Color GREEN;
  46. };
  47. //MSVC gives an error when calling abs with ui64 -> we add template that will match calls with unsigned arg and return it
  48. template<typename T>
  49. typename boost::enable_if_c<boost::is_unsigned<T>::type, T>::type abs(T arg)
  50. {
  51. return arg;
  52. }
  53. template<typename IntType>
  54. std::string makeNumberShort(IntType number) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
  55. {
  56. if (abs(number) < 1000)
  57. return boost::lexical_cast<std::string>(number);
  58. std::string symbols = "kMGTPE";
  59. auto iter = symbols.begin();
  60. while (number >= 1000)
  61. {
  62. number /= 1000;
  63. iter++;
  64. assert(iter != symbols.end());//should be enough even for int64
  65. }
  66. return boost::lexical_cast<std::string>(number) + *iter;
  67. }
  68. typedef void (*TColorPutter)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  69. typedef void (*TColorPutterAlpha)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  70. inline SDL_Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy)
  71. {
  72. SDL_Rect ret;
  73. ret.h=hh;
  74. ret.w=ww;
  75. ret.x=xx;
  76. ret.y=yy;
  77. return ret;
  78. }
  79. template<int bpp, int incrementPtr>
  80. struct ColorPutter
  81. {
  82. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  83. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  84. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  85. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  86. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  87. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  88. };
  89. typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
  90. namespace CSDL_Ext
  91. {
  92. /// helper that will safely set and un-set ClipRect for SDL_Surface
  93. class CClipRectGuard
  94. {
  95. SDL_Surface * surf;
  96. SDL_Rect oldRect;
  97. public:
  98. CClipRectGuard(SDL_Surface * surface, const SDL_Rect & rect):
  99. surf(surface)
  100. {
  101. SDL_GetClipRect(surf, &oldRect);
  102. SDL_SetClipRect(surf, &rect);
  103. }
  104. ~CClipRectGuard()
  105. {
  106. SDL_SetClipRect(surf, &oldRect);
  107. }
  108. };
  109. void blitSurface(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect);
  110. void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
  111. //fill dest image with source texture.
  112. void fillTexture(SDL_Surface *dst, SDL_Surface * sourceTexture);
  113. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  114. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  115. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  116. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  117. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  118. void alphaTransform(SDL_Surface * src); //adds transparency and shadows (partial handling only; see examples of using for details)
  119. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  120. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  121. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  122. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  123. BlitterWithRotationVal getBlitterWithRotation(SDL_Surface *dest);
  124. BlitterWithRotationVal getBlitterWithRotationAndAlpha(SDL_Surface *dest);
  125. 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
  126. 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
  127. 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
  128. 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
  129. 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
  130. 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
  131. 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
  132. 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
  133. 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
  134. 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
  135. template<int bpp>
  136. 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
  137. 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
  138. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  139. SDL_Color makeColor(ui8 r, ui8 g, ui8 b, ui8 a);
  140. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  141. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color);
  142. void drawBorder(SDL_Surface * sur, const SDL_Rect &r, const int3 &color);
  143. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color);
  144. void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
  145. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  146. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  147. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  148. template<int bpp>
  149. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  150. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  151. //scale surface to required size.
  152. //nearest neighbour algorithm
  153. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  154. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  155. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  156. template<int bpp>
  157. void applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode );
  158. void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale
  159. }