SDL_Extensions.cpp 19 KB

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