SDL_Extensions.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. void blitSurface(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect);
  87. void fillRect(SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color);
  88. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  89. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  90. SDL_Surface * rotate01(SDL_Surface * toRot); //vertical flip
  91. SDL_Surface * hFlip(SDL_Surface * toRot); //horizontal flip
  92. SDL_Surface * rotate02(SDL_Surface * toRot); //rotate 90 degrees left
  93. SDL_Surface * rotate03(SDL_Surface * toRot); //rotate 180 degrees
  94. SDL_Cursor * SurfaceToCursor(SDL_Surface *image, int hx, int hy); //creates cursor from bitmap
  95. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  96. SDL_Color SDL_GetPixelColor(SDL_Surface *surface, int x, int y); //returns color of pixel at given position
  97. void alphaTransform(SDL_Surface * src); //adds transparency and shadows (partial handling only; see examples of using for details)
  98. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  99. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  100. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  101. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  102. BlitterWithRotationVal getBlitterWithRotation(SDL_Surface *dest);
  103. BlitterWithRotationVal getBlitterWithRotationAndAlpha(SDL_Surface *dest);
  104. 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
  105. 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
  106. 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
  107. 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
  108. 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
  109. 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
  110. 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
  111. 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
  112. 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
  113. 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
  114. template<int bpp>
  115. 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
  116. 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
  117. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  118. void printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  119. void printAt(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  120. void printTo(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  121. void printAtMiddle(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  122. void printAtMiddleWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::Jasmine, SDL_Surface * dst=screen);
  123. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  124. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color);
  125. void drawBorder(SDL_Surface * sur, const SDL_Rect &r, const int3 &color);
  126. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color);
  127. void setPlayerColor(SDL_Surface * sur, ui8 player); //sets correct color of flags; -1 for neutral
  128. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  129. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  130. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  131. template<int bpp>
  132. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  133. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  134. //scale surface to required size.
  135. //nearest neighbour algorithm
  136. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  137. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  138. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  139. template<int bpp>
  140. void applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode );
  141. void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale
  142. std::string trimToFit(std::string text, int widthLimit, EFonts font);
  143. };