SDL_Extensions.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. namespace Colors
  34. {
  35. SDL_Color createColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a = 0);
  36. const SDL_Color Jasmine = createColor(229, 215, 123, 0); // http://en.wikipedia.org/wiki/Jasmine_%28color%29
  37. const SDL_Color Cornsilk = createColor(255, 243, 222, 0); // http://en.wikipedia.org/wiki/Shades_of_white
  38. const SDL_Color MetallicGold = createColor(173, 142, 66); // http://en.wikipedia.org/wiki/Gold_%28color%29
  39. const SDL_Color Maize = createColor(242, 226, 110); // http://en.wikipedia.org/wiki/Maize_%28color%29
  40. }
  41. //MSVC gives an error when calling abs with ui64 -> we add template that will match calls with unsigned arg and return it
  42. template<typename T>
  43. typename boost::enable_if_c<boost::is_unsigned<T>::type, T>::type abs(T arg)
  44. {
  45. return arg;
  46. }
  47. template<typename IntType>
  48. std::string makeNumberShort(IntType number) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
  49. {
  50. if (abs(number) < 1000)
  51. return boost::lexical_cast<std::string>(number);
  52. std::string symbols = "kMGTPE";
  53. auto iter = symbols.begin();
  54. while (number >= 1000)
  55. {
  56. number /= 1000;
  57. iter++;
  58. assert(iter != symbols.end());//should be enough even for int64
  59. }
  60. return boost::lexical_cast<std::string>(number) + *iter;
  61. }
  62. typedef void (*TColorPutter)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  63. typedef void (*TColorPutterAlpha)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  64. inline SDL_Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy)
  65. {
  66. SDL_Rect ret;
  67. ret.h=hh;
  68. ret.w=ww;
  69. ret.x=xx;
  70. ret.y=yy;
  71. return ret;
  72. }
  73. template<int bpp, int incrementPtr>
  74. struct ColorPutter
  75. {
  76. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  77. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  78. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  79. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  80. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  81. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  82. };
  83. typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
  84. namespace CSDL_Ext
  85. {
  86. /// helper that will safely set and un-set ClipRect for SDL_Surface
  87. class CClipRectGuard
  88. {
  89. SDL_Surface * surf;
  90. SDL_Rect oldRect;
  91. public:
  92. CClipRectGuard(SDL_Surface * surface, const SDL_Rect & rect):
  93. surf(surface)
  94. {
  95. SDL_GetClipRect(surf, &oldRect);
  96. SDL_SetClipRect(surf, &rect);
  97. }
  98. ~CClipRectGuard()
  99. {
  100. SDL_SetClipRect(surf, &oldRect);
  101. }
  102. };
  103. void blitSurface(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect);
  104. void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
  105. //fill dest image with source texture.
  106. void fillTexture(SDL_Surface *dst, SDL_Surface * sourceTexture);
  107. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  108. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  109. SDL_Surface * rotate01(SDL_Surface * toRot); //vertical flip
  110. SDL_Surface * hFlip(SDL_Surface * toRot); //horizontal flip
  111. SDL_Surface * rotate02(SDL_Surface * toRot); //rotate 90 degrees left
  112. SDL_Surface * rotate03(SDL_Surface * toRot); //rotate 180 degrees
  113. SDL_Cursor * SurfaceToCursor(SDL_Surface *image, int hx, int hy); //creates cursor from bitmap
  114. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  115. SDL_Color SDL_GetPixelColor(SDL_Surface *surface, int x, int y); //returns color of pixel at given position
  116. void alphaTransform(SDL_Surface * src); //adds transparency and shadows (partial handling only; see examples of using for details)
  117. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  118. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  119. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  120. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  121. BlitterWithRotationVal getBlitterWithRotation(SDL_Surface *dest);
  122. BlitterWithRotationVal getBlitterWithRotationAndAlpha(SDL_Surface *dest);
  123. 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
  124. 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
  125. 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
  126. 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
  127. 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
  128. 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
  129. 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
  130. 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
  131. 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
  132. 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
  133. template<int bpp>
  134. 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
  135. 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
  136. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  137. void printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  138. void printAt(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  139. void printTo(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  140. void printAtMiddle(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  141. void printAtMiddleWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::Jasmine, SDL_Surface * dst=screen);
  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 Rect &r, const int3 &color);
  146. void setPlayerColor(SDL_Surface * sur, ui8 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. std::string trimToFit(std::string text, int widthLimit, EFonts font);
  162. };