SDL_Extensions.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * SDL_Extensions.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../../lib/GameConstants.h"
  12. #include "../../lib/Rect.h"
  13. #include "../../lib/Color.h"
  14. struct SDL_Rect;
  15. struct SDL_Window;
  16. struct SDL_Renderer;
  17. struct SDL_Texture;
  18. struct SDL_Surface;
  19. struct SDL_Color;
  20. extern SDL_Window * mainWindow;
  21. extern SDL_Renderer * mainRenderer;
  22. extern SDL_Texture * screenTexture;
  23. extern SDL_Surface * screen, *screen2, *screenBuf;
  24. VCMI_LIB_NAMESPACE_BEGIN
  25. class Rect;
  26. class Point;
  27. VCMI_LIB_NAMESPACE_END
  28. /**
  29. * The colors class defines color constants of type SDL_Color.
  30. */
  31. class Colors
  32. {
  33. public:
  34. /** the h3 yellow color, typically used for headlines */
  35. static const SDL_Color YELLOW;
  36. /** the standard h3 white color */
  37. static const SDL_Color WHITE;
  38. /** the metallic gold color used mostly as a border around buttons */
  39. static const SDL_Color METALLIC_GOLD;
  40. /** green color used for in-game console */
  41. static const SDL_Color GREEN;
  42. /** the h3 orange color, used for blocked buttons */
  43. static const SDL_Color ORANGE;
  44. /** the h3 bright yellow color, used for selection border */
  45. static const SDL_Color BRIGHT_YELLOW;
  46. /** default key color for all 8 & 24 bit graphics */
  47. static const SDL_Color DEFAULT_KEY_COLOR;
  48. /// Selected creature card
  49. static const SDL_Color RED;
  50. /// Minimap border
  51. static const SDL_Color PURPLE;
  52. static const SDL_Color BLACK;
  53. static const SDL_Color TRANSPARENCY;
  54. };
  55. namespace CSDL_Ext
  56. {
  57. /// creates Rect using provided rect
  58. Rect fromSDL(const SDL_Rect & rect);
  59. /// creates SDL_Rect using provided rect
  60. SDL_Rect toSDL(const Rect & rect);
  61. /// creates Color using provided SDL_Color
  62. ColorRGBA fromSDL(const SDL_Color & color);
  63. /// creates SDL_Color using provided Color
  64. SDL_Color toSDL(const ColorRGBA & color);
  65. void setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors);
  66. void setAlpha(SDL_Surface * bg, int value);
  67. template<typename IntType>
  68. std::string makeNumberShort(IntType number, IntType maxLength = 3) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
  69. {
  70. IntType max = pow(10, maxLength);
  71. if (std::abs(number) < max)
  72. return boost::lexical_cast<std::string>(number);
  73. std::string symbols = " kMGTPE";
  74. auto iter = symbols.begin();
  75. while (number >= max)
  76. {
  77. number /= 1000;
  78. iter++;
  79. assert(iter != symbols.end());//should be enough even for int64
  80. }
  81. return boost::lexical_cast<std::string>(number) + *iter;
  82. }
  83. Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy);
  84. typedef void (*TColorPutter)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B);
  85. typedef void (*TColorPutterAlpha)(uint8_t *&ptr, const uint8_t & R, const uint8_t & G, const uint8_t & B, const uint8_t & A);
  86. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=screen);
  87. void blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst=screen);
  88. void setClipRect(SDL_Surface * src, const Rect & other);
  89. void getClipRect(SDL_Surface * src, Rect & other);
  90. void blitSurface(SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dest);
  91. void blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest);
  92. void fillSurface(SDL_Surface *dst, const SDL_Color & color);
  93. void fillRect(SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color);
  94. void updateRect(SDL_Surface *surface, const Rect & rect);
  95. void putPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const uint8_t & R, const uint8_t & G, const uint8_t & B, uint8_t A = 255);
  96. void putPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const uint8_t & R, const uint8_t & G, const uint8_t & B, uint8_t A = 255);
  97. SDL_Surface * verticalFlip(SDL_Surface * toRot); //vertical flip
  98. SDL_Surface * horizontalFlip(SDL_Surface * toRot); //horizontal flip
  99. uint32_t getPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  100. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  101. bool isTransparent(SDL_Surface * srf, const Point & position); //checks if surface is transparent at given position
  102. uint8_t *getPxPtr(const SDL_Surface * const &srf, const int x, const int y);
  103. TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  104. TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  105. template<int bpp>
  106. int blit8bppAlphaTo24bppT(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint); //blits 8 bpp surface with alpha channel to 24 bpp surface
  107. int blit8bppAlphaTo24bpp(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint); //blits 8 bpp surface with alpha channel to 24 bpp surface
  108. uint32_t colorTouint32_t(const SDL_Color * color); //little endian only
  109. SDL_Color makeColor(ui8 r, ui8 g, ui8 b, ui8 a);
  110. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  111. void drawLine(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2);
  112. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color &color, int depth = 1);
  113. void drawBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color, int depth = 1);
  114. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color);
  115. void setPlayerColor(SDL_Surface * sur, PlayerColor player); //sets correct color of flags; -1 for neutral
  116. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  117. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  118. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  119. template<int bpp>
  120. SDL_Surface * createSurfaceWithBpp(int width, int height); //create surface with give bits per pixels value
  121. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  122. //scale surface to required size.
  123. //nearest neighbour algorithm
  124. SDL_Surface * scaleSurfaceFast(SDL_Surface *surf, int width, int height);
  125. // bilinear filtering. Uses fallback to scaleSurfaceFast in case of indexed surfaces
  126. SDL_Surface * scaleSurface(SDL_Surface *surf, int width, int height);
  127. template<int bpp>
  128. void applyEffectBpp( SDL_Surface * surf, const Rect & rect, int mode );
  129. void applyEffect(SDL_Surface * surf, const Rect & rect, int mode); //mode: 0 - sepia, 1 - grayscale
  130. void setColorKey(SDL_Surface * surface, SDL_Color color);
  131. ///set key-color to 0,255,255
  132. void setDefaultColorKey(SDL_Surface * surface);
  133. ///set key-color to 0,255,255 only if it exactly mapped
  134. void setDefaultColorKeyPresize(SDL_Surface * surface);
  135. /// helper that will safely set and un-set ClipRect for SDL_Surface
  136. class CClipRectGuard
  137. {
  138. SDL_Surface * surf;
  139. Rect oldRect;
  140. public:
  141. CClipRectGuard(SDL_Surface * surface, const Rect & rect):
  142. surf(surface)
  143. {
  144. CSDL_Ext::getClipRect(surf, oldRect);
  145. CSDL_Ext::setClipRect(surf, rect);
  146. }
  147. ~CClipRectGuard()
  148. {
  149. CSDL_Ext::setClipRect(surf, oldRect);
  150. }
  151. };
  152. }