SDL_Extensions.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #pragma once
  2. #include <SDL_video.h>
  3. #include <SDL_ttf.h>
  4. #include "../../lib/int3.h"
  5. #include "../FontBase.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. static const SDL_Color MetallicGold;
  44. static const SDL_Color Maize;
  45. private:
  46. /**
  47. * Creates a SDL_Color object.
  48. *
  49. * @param r the red value ranging from 0 to 255
  50. * @param g the green value ranging from 0 to 255
  51. * @param b the blue value ranging from 0 to 255
  52. * @param a the alpha value ranging from 0(opaque) to 255(transparent)
  53. * @return the created SDL_Color object
  54. */
  55. static SDL_Color createColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 0);
  56. };
  57. //MSVC gives an error when calling abs with ui64 -> we add template that will match calls with unsigned arg and return it
  58. template<typename T>
  59. typename boost::enable_if_c<boost::is_unsigned<T>::type, T>::type abs(T arg)
  60. {
  61. return arg;
  62. }
  63. template<typename IntType>
  64. std::string makeNumberShort(IntType number) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
  65. {
  66. if (abs(number) < 1000)
  67. return boost::lexical_cast<std::string>(number);
  68. std::string symbols = "kMGTPE";
  69. auto iter = symbols.begin();
  70. while (number >= 1000)
  71. {
  72. number /= 1000;
  73. iter++;
  74. assert(iter != symbols.end());//should be enough even for int64
  75. }
  76. return boost::lexical_cast<std::string>(number) + *iter;
  77. }
  78. typedef void (*TColorPutter)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  79. typedef void (*TColorPutterAlpha)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  80. inline SDL_Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy)
  81. {
  82. SDL_Rect ret;
  83. ret.h=hh;
  84. ret.w=ww;
  85. ret.x=xx;
  86. ret.y=yy;
  87. return ret;
  88. }
  89. template<int bpp, int incrementPtr>
  90. struct ColorPutter
  91. {
  92. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  93. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  94. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  95. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  96. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  97. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  98. };
  99. typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
  100. namespace CSDL_Ext
  101. {
  102. /// helper that will safely set and un-set ClipRect for SDL_Surface
  103. class CClipRectGuard
  104. {
  105. SDL_Surface * surf;
  106. SDL_Rect oldRect;
  107. public:
  108. CClipRectGuard(SDL_Surface * surface, const SDL_Rect & rect):
  109. surf(surface)
  110. {
  111. SDL_GetClipRect(surf, &oldRect);
  112. SDL_SetClipRect(surf, &rect);
  113. }
  114. ~CClipRectGuard()
  115. {
  116. SDL_SetClipRect(surf, &oldRect);
  117. }
  118. };
  119. void blitSurface(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect);
  120. void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
  121. //fill dest image with source texture.
  122. void fillTexture(SDL_Surface *dst, SDL_Surface * sourceTexture);
  123. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  124. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  125. SDL_Surface * rotate01(SDL_Surface * toRot); //vertical flip
  126. SDL_Surface * hFlip(SDL_Surface * toRot); //horizontal flip
  127. SDL_Surface * rotate02(SDL_Surface * toRot); //rotate 90 degrees left
  128. SDL_Surface * rotate03(SDL_Surface * toRot); //rotate 180 degrees
  129. SDL_Cursor * SurfaceToCursor(SDL_Surface *image, int hx, int hy); //creates cursor from bitmap
  130. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  131. SDL_Color SDL_GetPixelColor(SDL_Surface *surface, int x, int y); //returns color of pixel at given position
  132. void alphaTransform(SDL_Surface * src); //adds transparency and shadows (partial handling only; see examples of using for details)
  133. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  134. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  135. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  136. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  137. BlitterWithRotationVal getBlitterWithRotation(SDL_Surface *dest);
  138. BlitterWithRotationVal getBlitterWithRotationAndAlpha(SDL_Surface *dest);
  139. 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
  140. 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
  141. 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
  142. 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
  143. 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
  144. 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
  145. 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
  146. 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
  147. 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
  148. 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
  149. template<int bpp>
  150. 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
  151. 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
  152. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  153. void printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::WHITE, SDL_Surface * dst=screen);
  154. void printAt(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::WHITE, SDL_Surface * dst=screen);
  155. void printTo(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::WHITE, SDL_Surface * dst=screen);
  156. void printAtMiddle(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::WHITE, SDL_Surface * dst=screen);
  157. void printAtMiddleWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::YELLOW, SDL_Surface * dst=screen);
  158. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  159. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color);
  160. void drawBorder(SDL_Surface * sur, const SDL_Rect &r, const int3 &color);
  161. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color);
  162. void setPlayerColor(SDL_Surface * sur, ui8 player); //sets correct color of flags; -1 for neutral
  163. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  164. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  165. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  166. template<int bpp>
  167. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  168. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  169. //scale surface to required size.
  170. //nearest neighbour algorithm
  171. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  172. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  173. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  174. template<int bpp>
  175. void applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode );
  176. void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale
  177. std::string trimToFit(std::string text, int widthLimit, EFonts font);
  178. }