SDL_Extensions.cpp 20 KB

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