SDL_Extensions.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. /*
  2. * SDL_Extensions.cpp, 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. #include "StdInc.h"
  11. #include "SDL_Extensions.h"
  12. #include "SDL_PixelAccess.h"
  13. #include "../render/Graphics.h"
  14. #include "../render/Colors.h"
  15. #include "../CMT.h"
  16. #include "../../lib/GameConstants.h"
  17. #include <SDL_render.h>
  18. #include <SDL_surface.h>
  19. #include <SDL_version.h>
  20. Rect CSDL_Ext::fromSDL(const SDL_Rect & rect)
  21. {
  22. return Rect(Point(rect.x, rect.y), Point(rect.w, rect.h));
  23. }
  24. SDL_Rect CSDL_Ext::toSDL(const Rect & rect)
  25. {
  26. SDL_Rect result;
  27. result.x = rect.x;
  28. result.y = rect.y;
  29. result.w = rect.w;
  30. result.h = rect.h;
  31. return result;
  32. }
  33. ColorRGBA CSDL_Ext::fromSDL(const SDL_Color & color)
  34. {
  35. return { color.r, color.g, color.b, color.a };
  36. }
  37. SDL_Color CSDL_Ext::toSDL(const ColorRGBA & color)
  38. {
  39. SDL_Color result;
  40. result.r = color.r;
  41. result.g = color.g;
  42. result.b = color.b;
  43. result.a = color.a;
  44. return result;
  45. }
  46. void CSDL_Ext::setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
  47. {
  48. SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors);
  49. }
  50. void CSDL_Ext::setAlpha(SDL_Surface * bg, int value)
  51. {
  52. SDL_SetSurfaceAlphaMod(bg, value);
  53. }
  54. SDL_Surface * CSDL_Ext::newSurface(const Point & dimensions)
  55. {
  56. return newSurface(dimensions, screen);
  57. }
  58. SDL_Surface * CSDL_Ext::newSurface(const Point & dimensions, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  59. {
  60. SDL_Surface * ret = SDL_CreateRGBSurface(0,dimensions.x,dimensions.y,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  61. if(ret == nullptr)
  62. {
  63. const char * error = SDL_GetError();
  64. std::string messagePattern = "Failed to create SDL Surface of size %d x %d, %d bpp. Reason: %s";
  65. std::string message = boost::str(boost::format(messagePattern) % dimensions.x % dimensions.y % mod->format->BitsPerPixel % error);
  66. handleFatalError(message, true);
  67. }
  68. if (mod->format->palette)
  69. {
  70. assert(ret->format->palette);
  71. assert(ret->format->palette->ncolors == mod->format->palette->ncolors);
  72. memcpy(ret->format->palette->colors, mod->format->palette->colors, mod->format->palette->ncolors * sizeof(SDL_Color));
  73. }
  74. return ret;
  75. }
  76. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  77. {
  78. //return SDL_DisplayFormat(mod);
  79. return SDL_ConvertSurface(mod, mod->format, mod->flags);
  80. }
  81. template<int bpp>
  82. SDL_Surface * CSDL_Ext::createSurfaceWithBpp(int width, int height)
  83. {
  84. uint32_t rMask = 0, gMask = 0, bMask = 0, aMask = 0;
  85. Channels::px<bpp>::r.set((uint8_t*)&rMask, 255);
  86. Channels::px<bpp>::g.set((uint8_t*)&gMask, 255);
  87. Channels::px<bpp>::b.set((uint8_t*)&bMask, 255);
  88. Channels::px<bpp>::a.set((uint8_t*)&aMask, 255);
  89. return SDL_CreateRGBSurface(0, width, height, bpp * 8, rMask, gMask, bMask, aMask);
  90. }
  91. void CSDL_Ext::blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  92. {
  93. CSDL_Ext::blitSurface(src, dst, Point(x, y));
  94. }
  95. void CSDL_Ext::blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst)
  96. {
  97. if (src)
  98. blitAt(src,pos.x,pos.y,dst);
  99. }
  100. // Vertical flip
  101. SDL_Surface * CSDL_Ext::verticalFlip(SDL_Surface * toRot)
  102. {
  103. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  104. SDL_LockSurface(ret);
  105. SDL_LockSurface(toRot);
  106. const int bpp = ret->format->BytesPerPixel;
  107. char * src = reinterpret_cast<char *>(toRot->pixels);
  108. char * dst = reinterpret_cast<char *>(ret->pixels);
  109. for(int i=0; i<ret->h; i++)
  110. {
  111. //FIXME: optimization bugged
  112. // if (bpp == 1)
  113. // {
  114. // // much faster for 8-bit surfaces (majority of our data)
  115. // std::reverse_copy(src, src + toRot->pitch, dst);
  116. // }
  117. // else
  118. // {
  119. char * srcPxl = src;
  120. char * dstPxl = dst + ret->w * bpp;
  121. for(int j=0; j<ret->w; j++)
  122. {
  123. dstPxl -= bpp;
  124. std::copy(srcPxl, srcPxl + bpp, dstPxl);
  125. srcPxl += bpp;
  126. }
  127. // }
  128. src += toRot->pitch;
  129. dst += ret->pitch;
  130. }
  131. SDL_UnlockSurface(ret);
  132. SDL_UnlockSurface(toRot);
  133. return ret;
  134. }
  135. // Horizontal flip
  136. SDL_Surface * CSDL_Ext::horizontalFlip(SDL_Surface * toRot)
  137. {
  138. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  139. SDL_LockSurface(ret);
  140. SDL_LockSurface(toRot);
  141. char * src = reinterpret_cast<char *>(toRot->pixels);
  142. char * dst = reinterpret_cast<char *>(ret->pixels) + ret->h * ret->pitch;
  143. for(int i=0; i<ret->h; i++)
  144. {
  145. dst -= ret->pitch;
  146. std::copy(src, src + toRot->pitch, dst);
  147. src += toRot->pitch;
  148. }
  149. SDL_UnlockSurface(ret);
  150. SDL_UnlockSurface(toRot);
  151. return ret;
  152. }
  153. uint32_t CSDL_Ext::getPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  154. {
  155. int bpp = surface->format->BytesPerPixel;
  156. /* Here p is the address to the pixel we want to retrieve */
  157. uint8_t *p = (uint8_t *)surface->pixels + y * surface->pitch + x * bpp;
  158. switch(bpp)
  159. {
  160. case 1:
  161. if(colorByte)
  162. return colorTouint32_t(surface->format->palette->colors+(*p));
  163. else
  164. return *p;
  165. case 2:
  166. return *(uint16_t *)p;
  167. case 3:
  168. return p[0] | p[1] << 8 | p[2] << 16;
  169. case 4:
  170. return *(uint32_t *)p;
  171. default:
  172. return 0; // shouldn't happen, but avoids warnings
  173. }
  174. }
  175. template<int bpp, bool useAlpha>
  176. int CSDL_Ext::blit8bppAlphaTo24bppT(const SDL_Surface * src, const Rect & srcRectInput, SDL_Surface * dst, const Point & dstPointInput, [[maybe_unused]] uint8_t alpha)
  177. {
  178. SDL_Rect srcRectInstance = CSDL_Ext::toSDL(srcRectInput);
  179. SDL_Rect dstRectInstance = CSDL_Ext::toSDL(Rect(dstPointInput, srcRectInput.dimensions()));
  180. SDL_Rect * srcRect =&srcRectInstance;
  181. SDL_Rect * dstRect =&dstRectInstance;
  182. /* Make sure the surfaces aren't locked */
  183. if ( ! src || ! dst )
  184. {
  185. SDL_SetError("SDL_UpperBlit: passed a nullptr surface");
  186. return -1;
  187. }
  188. if ( src->locked || dst->locked )
  189. {
  190. SDL_SetError("Surfaces must not be locked during blit");
  191. return -1;
  192. }
  193. if (src->format->BytesPerPixel==1 && (bpp==3 || bpp==4 || bpp==2)) //everything's ok
  194. {
  195. SDL_Rect fulldst;
  196. int srcx;
  197. int srcy;
  198. int w;
  199. int h;
  200. /* If the destination rectangle is nullptr, use the entire dest surface */
  201. if ( dstRect == nullptr )
  202. {
  203. fulldst.x = fulldst.y = 0;
  204. dstRect = &fulldst;
  205. }
  206. /* clip the source rectangle to the source surface */
  207. if(srcRect)
  208. {
  209. int maxw;
  210. int maxh;
  211. srcx = srcRect->x;
  212. w = srcRect->w;
  213. if(srcx < 0)
  214. {
  215. w += srcx;
  216. dstRect->x -= srcx;
  217. srcx = 0;
  218. }
  219. maxw = src->w - srcx;
  220. if(maxw < w)
  221. w = maxw;
  222. srcy = srcRect->y;
  223. h = srcRect->h;
  224. if(srcy < 0)
  225. {
  226. h += srcy;
  227. dstRect->y -= srcy;
  228. srcy = 0;
  229. }
  230. maxh = src->h - srcy;
  231. if(maxh < h)
  232. h = maxh;
  233. }
  234. else
  235. {
  236. srcx = srcy = 0;
  237. w = src->w;
  238. h = src->h;
  239. }
  240. /* clip the destination rectangle against the clip rectangle */
  241. {
  242. SDL_Rect *clip = &dst->clip_rect;
  243. int dx;
  244. int dy;
  245. dx = clip->x - dstRect->x;
  246. if(dx > 0)
  247. {
  248. w -= dx;
  249. dstRect->x += dx;
  250. srcx += dx;
  251. }
  252. dx = dstRect->x + w - clip->x - clip->w;
  253. if(dx > 0)
  254. w -= dx;
  255. dy = clip->y - dstRect->y;
  256. if(dy > 0)
  257. {
  258. h -= dy;
  259. dstRect->y += dy;
  260. srcy += dy;
  261. }
  262. dy = dstRect->y + h - clip->y - clip->h;
  263. if(dy > 0)
  264. h -= dy;
  265. }
  266. if(w > 0 && h > 0)
  267. {
  268. dstRect->w = w;
  269. dstRect->h = h;
  270. if(SDL_LockSurface(dst))
  271. return -1; //if we cannot lock the surface
  272. const SDL_Color *colors = src->format->palette->colors;
  273. uint8_t *colory = (uint8_t*)src->pixels + srcy*src->pitch + srcx;
  274. uint8_t *py = (uint8_t*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  275. for(int y=0; y<h; ++y, colory+=src->pitch, py+=dst->pitch)
  276. {
  277. uint8_t *color = colory;
  278. uint8_t *p = py;
  279. for(int x = 0; x < w; ++x)
  280. {
  281. const SDL_Color &tbc = colors[*color++]; //color to blit
  282. if constexpr (useAlpha)
  283. ColorPutter<bpp>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, int(alpha) * tbc.a / 255 );
  284. else
  285. ColorPutter<bpp>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, tbc.a);
  286. p += bpp;
  287. }
  288. }
  289. SDL_UnlockSurface(dst);
  290. }
  291. }
  292. return 0;
  293. }
  294. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint, uint8_t alpha)
  295. {
  296. if (alpha == SDL_ALPHA_OPAQUE)
  297. {
  298. switch(dst->format->BytesPerPixel)
  299. {
  300. case 3: return blit8bppAlphaTo24bppT<3, false>(src, srcRect, dst, dstPoint, alpha);
  301. case 4: return blit8bppAlphaTo24bppT<4, false>(src, srcRect, dst, dstPoint, alpha);
  302. }
  303. }
  304. else
  305. {
  306. switch(dst->format->BytesPerPixel)
  307. {
  308. case 3: return blit8bppAlphaTo24bppT<3, true>(src, srcRect, dst, dstPoint, alpha);
  309. case 4: return blit8bppAlphaTo24bppT<4, true>(src, srcRect, dst, dstPoint, alpha);
  310. }
  311. }
  312. logGlobal->error("%d bpp is not supported!", (int)dst->format->BitsPerPixel);
  313. return -1;
  314. }
  315. uint32_t CSDL_Ext::colorTouint32_t(const SDL_Color * color)
  316. {
  317. uint32_t ret = 0;
  318. ret+=color->a;
  319. ret<<=8; //*=256
  320. ret+=color->b;
  321. ret<<=8; //*=256
  322. ret+=color->g;
  323. ret<<=8; //*=256
  324. ret+=color->r;
  325. return ret;
  326. }
  327. static void drawLineXDashed(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color)
  328. {
  329. double length(x2 - x1);
  330. for(int x = x1; x <= x2; x++)
  331. {
  332. double f = (x - x1) / length;
  333. int y = vstd::lerp(y1, y2, f);
  334. if (std::abs(x - x1) % 5 != 4)
  335. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x, y, color.r, color.g, color.b);
  336. }
  337. }
  338. static void drawLineYDashed(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color)
  339. {
  340. double length(y2 - y1);
  341. for(int y = y1; y <= y2; y++)
  342. {
  343. double f = (y - y1) / length;
  344. int x = vstd::lerp(x1, x2, f);
  345. if (std::abs(y - y1) % 5 != 4)
  346. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x, y, color.r, color.g, color.b);
  347. }
  348. }
  349. static void drawLineX(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2)
  350. {
  351. double length(x2 - x1);
  352. for(int x = x1; x <= x2; x++)
  353. {
  354. double f = (x - x1) / length;
  355. int y = vstd::lerp(y1, y2, f);
  356. uint8_t r = vstd::lerp(color1.r, color2.r, f);
  357. uint8_t g = vstd::lerp(color1.g, color2.g, f);
  358. uint8_t b = vstd::lerp(color1.b, color2.b, f);
  359. uint8_t a = vstd::lerp(color1.a, color2.a, f);
  360. uint8_t *p = CSDL_Ext::getPxPtr(sur, x, y);
  361. ColorPutter<4>::PutColor(p, r,g,b,a);
  362. }
  363. }
  364. static void drawLineY(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2)
  365. {
  366. double length(y2 - y1);
  367. for(int y = y1; y <= y2; y++)
  368. {
  369. double f = (y - y1) / length;
  370. int x = vstd::lerp(x1, x2, f);
  371. uint8_t r = vstd::lerp(color1.r, color2.r, f);
  372. uint8_t g = vstd::lerp(color1.g, color2.g, f);
  373. uint8_t b = vstd::lerp(color1.b, color2.b, f);
  374. uint8_t a = vstd::lerp(color1.a, color2.a, f);
  375. uint8_t *p = CSDL_Ext::getPxPtr(sur, x, y);
  376. ColorPutter<4>::PutColor(p, r,g,b,a);
  377. }
  378. }
  379. void CSDL_Ext::drawLine(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color1, const SDL_Color & color2)
  380. {
  381. //FIXME: duplicated code with drawLineDashed
  382. int width = std::abs(from.x - dest.x);
  383. int height = std::abs(from.y - dest.y);
  384. if ( width == 0 && height == 0)
  385. {
  386. uint8_t *p = CSDL_Ext::getPxPtr(sur, from.x, from.y);
  387. ColorPutter<4>::PutColorAlpha(p, color1);
  388. return;
  389. }
  390. if (width > height)
  391. {
  392. if ( from.x < dest.x)
  393. drawLineX(sur, from.x, from.y, dest.x, dest.y, color1, color2);
  394. else
  395. drawLineX(sur, dest.x, dest.y, from.x, from.y, color2, color1);
  396. }
  397. else
  398. {
  399. if ( from.y < dest.y)
  400. drawLineY(sur, from.x, from.y, dest.x, dest.y, color1, color2);
  401. else
  402. drawLineY(sur, dest.x, dest.y, from.x, from.y, color2, color1);
  403. }
  404. }
  405. void CSDL_Ext::drawLineDashed(SDL_Surface * sur, const Point & from, const Point & dest, const SDL_Color & color)
  406. {
  407. //FIXME: duplicated code with drawLine
  408. int width = std::abs(from.x - dest.x);
  409. int height = std::abs(from.y - dest.y);
  410. if ( width == 0 && height == 0)
  411. {
  412. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, from.x, from.y, color.r, color.g, color.b);
  413. return;
  414. }
  415. if (width > height)
  416. {
  417. if ( from.x < dest.x)
  418. drawLineXDashed(sur, from.x, from.y, dest.x, dest.y, color);
  419. else
  420. drawLineXDashed(sur, dest.x, dest.y, from.x, from.y, color);
  421. }
  422. else
  423. {
  424. if ( from.y < dest.y)
  425. drawLineYDashed(sur, from.x, from.y, dest.x, dest.y, color);
  426. else
  427. drawLineYDashed(sur, dest.x, dest.y, from.x, from.y, color);
  428. }
  429. }
  430. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color &color, int depth)
  431. {
  432. depth = std::max(1, depth);
  433. for(int depthIterator = 0; depthIterator < depth; depthIterator++)
  434. {
  435. for(int i = 0; i < w; i++)
  436. {
  437. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+i,y+depthIterator,color.r,color.g,color.b);
  438. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+i,y+h-1-depthIterator,color.r,color.g,color.b);
  439. }
  440. for(int i = 0; i < h; i++)
  441. {
  442. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+depthIterator,y+i,color.r,color.g,color.b);
  443. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+w-1-depthIterator,y+i,color.r,color.g,color.b);
  444. }
  445. }
  446. }
  447. void CSDL_Ext::drawBorder( SDL_Surface * sur, const Rect &r, const SDL_Color &color, int depth)
  448. {
  449. drawBorder(sur, r.x, r.y, r.w, r.h, color, depth);
  450. }
  451. CSDL_Ext::TColorPutter CSDL_Ext::getPutterFor(SDL_Surface * const &dest)
  452. {
  453. switch(dest->format->BytesPerPixel)
  454. {
  455. case 3:
  456. return ColorPutter<3>::PutColor;
  457. case 4:
  458. return ColorPutter<4>::PutColor;
  459. default:
  460. logGlobal->error("%d bpp is not supported!", (int)dest->format->BitsPerPixel);
  461. return nullptr;
  462. }
  463. }
  464. uint8_t * CSDL_Ext::getPxPtr(const SDL_Surface * const &srf, const int x, const int y)
  465. {
  466. return (uint8_t *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel;
  467. }
  468. bool CSDL_Ext::isTransparent( SDL_Surface * srf, const Point & position )
  469. {
  470. return isTransparent(srf, position.x, position.y);
  471. }
  472. bool CSDL_Ext::isTransparent( SDL_Surface * srf, int x, int y )
  473. {
  474. if (x < 0 || y < 0 || x >= srf->w || y >= srf->h)
  475. return true;
  476. SDL_Color color;
  477. SDL_GetRGBA(CSDL_Ext::getPixel(srf, x, y), srf->format, &color.r, &color.g, &color.b, &color.a);
  478. bool pixelTransparent = color.a < 128;
  479. bool pixelCyan = (color.r == 0 && color.g == 255 && color.b == 255);
  480. return pixelTransparent || pixelCyan;
  481. }
  482. void CSDL_Ext::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)
  483. {
  484. uint8_t *p = getPxPtr(ekran, x, y);
  485. getPutterFor(ekran)(p, R, G, B);
  486. switch(ekran->format->BytesPerPixel)
  487. {
  488. case 3: Channels::px<3>::a.set(p, A); break;
  489. case 4: Channels::px<4>::a.set(p, A); break;
  490. }
  491. }
  492. void CSDL_Ext::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)
  493. {
  494. const SDL_Rect & rect = ekran->clip_rect;
  495. if(x >= rect.x && x < rect.w + rect.x
  496. && y >= rect.y && y < rect.h + rect.y)
  497. CSDL_Ext::putPixelWithoutRefresh(ekran, x, y, R, G, B, A);
  498. }
  499. template<int bpp>
  500. void CSDL_Ext::convertToGrayscaleBpp(SDL_Surface * surf, const Rect & rect )
  501. {
  502. uint8_t * pixels = static_cast<uint8_t*>(surf->pixels);
  503. for(int yp = rect.top(); yp < rect.bottom(); ++yp)
  504. {
  505. uint8_t * pixel_from = pixels + yp * surf->pitch + rect.left() * surf->format->BytesPerPixel;
  506. uint8_t * pixel_dest = pixels + yp * surf->pitch + rect.right() * surf->format->BytesPerPixel;
  507. for (uint8_t * pixel = pixel_from; pixel < pixel_dest; pixel += surf->format->BytesPerPixel)
  508. {
  509. int r = Channels::px<bpp>::r.get(pixel);
  510. int g = Channels::px<bpp>::g.get(pixel);
  511. int b = Channels::px<bpp>::b.get(pixel);
  512. int gray = static_cast<int>(0.299 * r + 0.587 * g + 0.114 *b);
  513. Channels::px<bpp>::r.set(pixel, gray);
  514. Channels::px<bpp>::g.set(pixel, gray);
  515. Channels::px<bpp>::b.set(pixel, gray);
  516. }
  517. }
  518. }
  519. void CSDL_Ext::convertToGrayscale( SDL_Surface * surf, const Rect & rect )
  520. {
  521. switch(surf->format->BytesPerPixel)
  522. {
  523. case 3: convertToGrayscaleBpp<3>(surf, rect); break;
  524. case 4: convertToGrayscaleBpp<4>(surf, rect); break;
  525. }
  526. }
  527. // scaling via bilinear interpolation algorithm.
  528. // NOTE: best results are for scaling in range 50%...200%.
  529. // And upscaling looks awful right now - should be fixed somehow
  530. SDL_Surface * CSDL_Ext::scaleSurface(SDL_Surface * surf, int width, int height)
  531. {
  532. if(!surf || !width || !height)
  533. return nullptr;
  534. SDL_Surface * intermediate = SDL_ConvertSurface(surf, screen->format, 0);
  535. SDL_Surface * ret = newSurface(Point(width, height), intermediate);
  536. #if SDL_VERSION_ATLEAST(2,0,16)
  537. SDL_SoftStretchLinear(intermediate, nullptr, ret, nullptr);
  538. #else
  539. SDL_SoftStretch(intermediate, nullptr, ret, nullptr);
  540. #endif
  541. SDL_FreeSurface(intermediate);
  542. return ret;
  543. }
  544. void CSDL_Ext::blitSurface(SDL_Surface * src, const Rect & srcRectInput, SDL_Surface * dst, const Point & dstPoint)
  545. {
  546. SDL_Rect srcRect = CSDL_Ext::toSDL(srcRectInput);
  547. SDL_Rect dstRect = CSDL_Ext::toSDL(Rect(dstPoint, srcRectInput.dimensions()));
  548. int result = SDL_UpperBlit(src, &srcRect, dst, &dstRect);
  549. if (result != 0)
  550. logGlobal->error("SDL_UpperBlit failed! %s", SDL_GetError());
  551. }
  552. void CSDL_Ext::blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest)
  553. {
  554. Rect allSurface( Point(0,0), Point(src->w, src->h));
  555. blitSurface(src, allSurface, dst, dest);
  556. }
  557. void CSDL_Ext::fillSurface( SDL_Surface *dst, const SDL_Color & color )
  558. {
  559. Rect allSurface( Point(0,0), Point(dst->w, dst->h));
  560. fillRect(dst, allSurface, color);
  561. }
  562. void CSDL_Ext::fillRect( SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color)
  563. {
  564. SDL_Rect newRect = CSDL_Ext::toSDL(dstrect);
  565. uint32_t sdlColor = SDL_MapRGBA(dst->format, color.r, color.g, color.b, color.a);
  566. SDL_FillRect(dst, &newRect, sdlColor);
  567. }
  568. void CSDL_Ext::fillRectBlended( SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color)
  569. {
  570. SDL_Rect newRect = CSDL_Ext::toSDL(dstrect);
  571. uint32_t sdlColor = SDL_MapRGBA(dst->format, color.r, color.g, color.b, color.a);
  572. SDL_Surface * tmp = SDL_CreateRGBSurface(0, newRect.w, newRect.h, dst->format->BitsPerPixel, dst->format->Rmask, dst->format->Gmask, dst->format->Bmask, dst->format->Amask);
  573. SDL_FillRect(tmp, nullptr, sdlColor);
  574. SDL_BlitSurface(tmp, nullptr, dst, &newRect);
  575. SDL_FreeSurface(tmp);
  576. }
  577. STRONG_INLINE static uint32_t mapColor(SDL_Surface * surface, SDL_Color color)
  578. {
  579. return SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a);
  580. }
  581. void CSDL_Ext::setColorKey(SDL_Surface * surface, SDL_Color color)
  582. {
  583. uint32_t key = mapColor(surface,color);
  584. SDL_SetColorKey(surface, SDL_TRUE, key);
  585. }
  586. void CSDL_Ext::setDefaultColorKey(SDL_Surface * surface)
  587. {
  588. setColorKey(surface, toSDL(Colors::DEFAULT_KEY_COLOR));
  589. }
  590. void CSDL_Ext::setDefaultColorKeyPresize(SDL_Surface * surface)
  591. {
  592. uint32_t key = mapColor(surface, toSDL(Colors::DEFAULT_KEY_COLOR));
  593. auto & color = surface->format->palette->colors[key];
  594. // set color key only if exactly such color was found
  595. if (color.r == Colors::DEFAULT_KEY_COLOR.r && color.g == Colors::DEFAULT_KEY_COLOR.g && color.b == Colors::DEFAULT_KEY_COLOR.b)
  596. {
  597. SDL_SetColorKey(surface, SDL_TRUE, key);
  598. color.a = SDL_ALPHA_TRANSPARENT;
  599. }
  600. }
  601. void CSDL_Ext::setClipRect(SDL_Surface * src, const Rect & other)
  602. {
  603. SDL_Rect rect = CSDL_Ext::toSDL(other);
  604. SDL_SetClipRect(src, &rect);
  605. }
  606. void CSDL_Ext::getClipRect(SDL_Surface * src, Rect & other)
  607. {
  608. SDL_Rect rect;
  609. SDL_GetClipRect(src, &rect);
  610. other = CSDL_Ext::fromSDL(rect);
  611. }
  612. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<3>(int, int);
  613. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<4>(int, int);