SDL_Extensions.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. template<typename IntType>
  42. std::string makeNumberShort(IntType number) //the output is a string containing at most 5 characters [4 if positive] (eg. intead 10000 it gives 10k)
  43. {
  44. int initialLength;
  45. bool negative = (number < 0);
  46. std::ostringstream ost, rets;
  47. ost<<number;
  48. initialLength = ost.str().size();
  49. if(negative)
  50. {
  51. if(initialLength <= 4)
  52. return ost.str();
  53. }
  54. else
  55. {
  56. if(initialLength <= 5)
  57. return ost.str();
  58. }
  59. //make the number int
  60. char symbol[] = {'G', 'M', 'k'};
  61. if(negative) number = (-number); //absolute value
  62. for(int divisor = 1000000000, it = 0; divisor > 1; divisor /= 1000, ++it)
  63. {
  64. if(number >= divisor)
  65. {
  66. if(negative) rets <<'-';
  67. rets << (number / divisor) << symbol[it];
  68. return rets.str();
  69. }
  70. }
  71. throw std::runtime_error("We shouldn't be here - makeNumberShort");
  72. }
  73. typedef void (*TColorPutter)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  74. typedef void (*TColorPutterAlpha)(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  75. inline SDL_Rect genRect(const int & hh, const int & ww, const int & xx, const int & yy)
  76. {
  77. SDL_Rect ret;
  78. ret.h=hh;
  79. ret.w=ww;
  80. ret.x=xx;
  81. ret.y=yy;
  82. return ret;
  83. }
  84. //TODO: inlining will work only if functions are defined in each compilation unit (like placing them in headers)
  85. //however here PutColor defined in SDL_Extensions.cpp, but used in CAnimation.cpp and CCreatureAnimation.cpp
  86. template<int bpp, int incrementPtr>
  87. struct ColorPutter
  88. {
  89. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  90. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  91. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  92. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  93. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  94. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  95. };
  96. template <int incrementPtr>
  97. struct ColorPutter<2, incrementPtr>
  98. {
  99. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B);
  100. static STRONG_INLINE void PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  101. static STRONG_INLINE void PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A);
  102. static STRONG_INLINE void PutColor(Uint8 *&ptr, const SDL_Color & Color);
  103. static STRONG_INLINE void PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color);
  104. static STRONG_INLINE void PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count);
  105. };
  106. typedef void (*BlitterWithRotationVal)(SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation);
  107. namespace CSDL_Ext
  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. extern SDL_Surface * std32bppSurface;
  112. void SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  113. void SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A = 255);
  114. SDL_Surface * rotate01(SDL_Surface * toRot); //vertical flip
  115. SDL_Surface * hFlip(SDL_Surface * toRot); //horizontal flip
  116. SDL_Surface * rotate02(SDL_Surface * toRot); //rotate 90 degrees left
  117. SDL_Surface * rotate03(SDL_Surface * toRot); //rotate 180 degrees
  118. SDL_Cursor * SurfaceToCursor(SDL_Surface *image, int hx, int hy); //creates cursor from bitmap
  119. Uint32 SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte = false);
  120. SDL_Color SDL_GetPixelColor(SDL_Surface *surface, int x, int y); //returns color of pixel at given position
  121. void alphaTransform(SDL_Surface * src); //adds transparency and shadows (partial handling only; see examples of using for details)
  122. bool isTransparent(SDL_Surface * srf, int x, int y); //checks if surface is transparent at given position
  123. Uint8 *getPxPtr(const SDL_Surface * const &srf, const int & x, const int & y);
  124. const TColorPutter getPutterFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  125. const TColorPutterAlpha getPutterAlphaFor(SDL_Surface * const &dest, int incrementing); //incrementing: -1, 0, 1
  126. BlitterWithRotationVal getBlitterWithRotation(SDL_Surface *dest);
  127. BlitterWithRotationVal getBlitterWithRotationAndAlpha(SDL_Surface *dest);
  128. 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
  129. 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
  130. 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
  131. 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
  132. 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
  133. 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
  134. 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
  135. 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
  136. 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
  137. 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
  138. template<int bpp>
  139. 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
  140. 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
  141. Uint32 colorToUint32(const SDL_Color * color); //little endian only
  142. void printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  143. void printAt(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  144. void printTo(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  145. void printAtMiddle(const std::string & text, int x, int y, EFonts font, SDL_Color kolor=Colors::Cornsilk, SDL_Surface * dst=screen);
  146. void printAtMiddleWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor=Colors::Jasmine, SDL_Surface * dst=screen);
  147. void update(SDL_Surface * what = screen); //updates whole surface (default - main screen)
  148. void drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color);
  149. void drawBorder(SDL_Surface * sur, const SDL_Rect &r, const int3 &color);
  150. void drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color);
  151. void setPlayerColor(SDL_Surface * sur, ui8 player); //sets correct color of flags; -1 for neutral
  152. std::string processStr(std::string str, std::vector<std::string> & tor); //replaces %s in string
  153. SDL_Surface * newSurface(int w, int h, SDL_Surface * mod=screen); //creates new surface, with flags/format same as in surface given
  154. SDL_Surface * copySurface(SDL_Surface * mod); //returns copy of given surface
  155. void VflipSurf(SDL_Surface * surf); //fluipis given surface by vertical axis
  156. void applyEffect(SDL_Surface * surf, const SDL_Rect * rect, int mode); //mode: 0 - sepia, 1 - grayscale
  157. std::string trimToFit(std::string text, int widthLimit, EFonts font);
  158. };
  159. template<int bpp, int incrementPtr>
  160. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  161. {
  162. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  163. }
  164. template<int bpp, int incrementPtr>
  165. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  166. {
  167. PutColor(ptr, Color.r, Color.g, Color.b);
  168. }
  169. template<int bpp, int incrementPtr>
  170. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  171. {
  172. switch (A)
  173. {
  174. case 255:
  175. ptr += bpp * incrementPtr;
  176. return;
  177. case 0:
  178. PutColor(ptr, R, G, B);
  179. return;
  180. case 128: // optimized
  181. PutColor(ptr, ((Uint16)R + (Uint16)ptr[2]) >> 1,
  182. ((Uint16)G + (Uint16)ptr[1]) >> 1,
  183. ((Uint16)B + (Uint16)ptr[0]) >> 1);
  184. return;
  185. default:
  186. PutColor(ptr, R, G, B, A);
  187. return;
  188. }
  189. }
  190. template<int bpp, int incrementPtr>
  191. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  192. {
  193. PutColor(ptr, ((((Uint32)ptr[2]-(Uint32)R)*(Uint32)A) >> 8 ) + (Uint32)R,
  194. ((((Uint32)ptr[1]-(Uint32)G)*(Uint32)A) >> 8 ) + (Uint32)G,
  195. ((((Uint32)ptr[0]-(Uint32)B)*(Uint32)A) >> 8 ) + (Uint32)B);
  196. }
  197. template<int bpp, int incrementPtr>
  198. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  199. {
  200. if(incrementPtr == 0)
  201. {
  202. ptr[0] = B;
  203. ptr[1] = G;
  204. ptr[2] = R;
  205. }
  206. else if(incrementPtr == 1)
  207. {
  208. *ptr++ = B;
  209. *ptr++ = G;
  210. *ptr++ = R;
  211. if(bpp == 4)
  212. *ptr++ = 0;
  213. }
  214. else if(incrementPtr == -1)
  215. {
  216. if(bpp == 4)
  217. *(--ptr) = 0;
  218. *(--ptr) = R;
  219. *(--ptr) = G;
  220. *(--ptr) = B;
  221. }
  222. else
  223. {
  224. assert(0);
  225. }
  226. }
  227. template<int bpp, int incrementPtr>
  228. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  229. {
  230. Uint32 pixel = ((Uint32)Color.b << 0 ) + ((Uint32)Color.g << 8) + ((Uint32)Color.r << 16);
  231. for (size_t i=0; i<count; i++)
  232. {
  233. memcpy(ptr, &pixel, bpp);
  234. if(incrementPtr == -1)
  235. ptr -= bpp;
  236. if(incrementPtr == 1)
  237. ptr += bpp;
  238. }
  239. }
  240. template <int incrementPtr>
  241. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  242. {
  243. if(incrementPtr == -1)
  244. ptr -= 2;
  245. Uint16 * const px = (Uint16*)ptr;
  246. *px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
  247. if(incrementPtr == 1)
  248. ptr += 2; //bpp
  249. }
  250. template <int incrementPtr>
  251. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  252. {
  253. switch (A)
  254. {
  255. case 255:
  256. ptr += 2 * incrementPtr;
  257. return;
  258. case 0:
  259. PutColor(ptr, R, G, B);
  260. return;
  261. default:
  262. PutColor(ptr, R, G, B, A);
  263. return;
  264. }
  265. }
  266. template <int incrementPtr>
  267. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  268. {
  269. const int rbit = 5, gbit = 6, bbit = 5; //bits per color
  270. const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
  271. const int rshift = 11, gshift = 5, bshift = 0;
  272. const Uint8 r5 = (*((Uint16 *)ptr) & rmask) >> rshift,
  273. b5 = (*((Uint16 *)ptr) & bmask) >> bshift,
  274. g5 = (*((Uint16 *)ptr) & gmask) >> gshift;
  275. const Uint32 r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
  276. g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
  277. b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
  278. PutColor(ptr,
  279. (((r8-R)*A) >> 8) + R,
  280. (((g8-G)*A) >> 8) + G,
  281. (((b8-B)*A) >> 8) + B);
  282. }
  283. template <int incrementPtr>
  284. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  285. {
  286. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  287. }
  288. template <int incrementPtr>
  289. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  290. {
  291. PutColor(ptr, Color.r, Color.g, Color.b);
  292. }
  293. template <int incrementPtr>
  294. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  295. {
  296. //drop least significant bits of 24 bpp encoded color
  297. Uint16 pixel = (Color.b>>3) + ((Color.g>>2) << 5) + ((Color.r>>3) << 11);
  298. for (size_t i=0; i<count; i++)
  299. {
  300. memcpy(ptr, &pixel, 2);
  301. if(incrementPtr == -1)
  302. ptr -= 2;
  303. if(incrementPtr == 1)
  304. ptr += 2;
  305. }
  306. }