SDL_Extensions.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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 <SDL_render.h>
  17. Rect CSDL_Ext::fromSDL(const SDL_Rect & rect)
  18. {
  19. return Rect(Point(rect.x, rect.y), Point(rect.w, rect.h));
  20. }
  21. SDL_Rect CSDL_Ext::toSDL(const Rect & rect)
  22. {
  23. SDL_Rect result;
  24. result.x = rect.x;
  25. result.y = rect.y;
  26. result.w = rect.w;
  27. result.h = rect.h;
  28. return result;
  29. }
  30. ColorRGBA CSDL_Ext::fromSDL(const SDL_Color & color)
  31. {
  32. return { color.r, color.g, color.b, color.a };
  33. }
  34. SDL_Color CSDL_Ext::toSDL(const ColorRGBA & color)
  35. {
  36. SDL_Color result;
  37. result.r = color.r;
  38. result.g = color.g;
  39. result.b = color.b;
  40. result.a = color.a;
  41. return result;
  42. }
  43. Rect CSDL_Ext::getDisplayBounds()
  44. {
  45. SDL_Rect displayBounds;
  46. SDL_GetDisplayBounds(std::max(0, SDL_GetWindowDisplayIndex(mainWindow)), &displayBounds);
  47. return fromSDL(displayBounds);
  48. }
  49. void CSDL_Ext::setColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
  50. {
  51. SDL_SetPaletteColors(surface->format->palette,colors,firstcolor,ncolors);
  52. }
  53. void CSDL_Ext::setAlpha(SDL_Surface * bg, int value)
  54. {
  55. SDL_SetSurfaceAlphaMod(bg, value);
  56. }
  57. void CSDL_Ext::updateRect(SDL_Surface *surface, const Rect & rect )
  58. {
  59. SDL_Rect rectSDL = CSDL_Ext::toSDL(rect);
  60. if(0 !=SDL_UpdateTexture(screenTexture, &rectSDL, surface->pixels, surface->pitch))
  61. logGlobal->error("%sSDL_UpdateTexture %s", __FUNCTION__, SDL_GetError());
  62. SDL_RenderClear(mainRenderer);
  63. if(0 != SDL_RenderCopy(mainRenderer, screenTexture, NULL, NULL))
  64. logGlobal->error("%sSDL_RenderCopy %s", __FUNCTION__, SDL_GetError());
  65. SDL_RenderPresent(mainRenderer);
  66. }
  67. SDL_Surface * CSDL_Ext::newSurface(int w, int h)
  68. {
  69. return newSurface(w, h, screen);
  70. }
  71. SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  72. {
  73. SDL_Surface * ret = SDL_CreateRGBSurface(0,w,h,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  74. if (mod->format->palette)
  75. {
  76. assert(ret->format->palette);
  77. assert(ret->format->palette->ncolors == mod->format->palette->ncolors);
  78. memcpy(ret->format->palette->colors, mod->format->palette->colors, mod->format->palette->ncolors * sizeof(SDL_Color));
  79. }
  80. return ret;
  81. }
  82. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  83. {
  84. //return SDL_DisplayFormat(mod);
  85. return SDL_ConvertSurface(mod, mod->format, mod->flags);
  86. }
  87. template<int bpp>
  88. SDL_Surface * CSDL_Ext::createSurfaceWithBpp(int width, int height)
  89. {
  90. uint32_t rMask = 0, gMask = 0, bMask = 0, aMask = 0;
  91. Channels::px<bpp>::r.set((uint8_t*)&rMask, 255);
  92. Channels::px<bpp>::g.set((uint8_t*)&gMask, 255);
  93. Channels::px<bpp>::b.set((uint8_t*)&bMask, 255);
  94. Channels::px<bpp>::a.set((uint8_t*)&aMask, 255);
  95. return SDL_CreateRGBSurface(0, width, height, bpp * 8, rMask, gMask, bMask, aMask);
  96. }
  97. void CSDL_Ext::blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  98. {
  99. if(!dst)
  100. dst = screen;
  101. CSDL_Ext::blitSurface(src, dst, Point(x, y));
  102. }
  103. void CSDL_Ext::blitAt(SDL_Surface * src, const Rect & pos, SDL_Surface * dst)
  104. {
  105. if (src)
  106. blitAt(src,pos.x,pos.y,dst);
  107. }
  108. // Vertical flip
  109. SDL_Surface * CSDL_Ext::verticalFlip(SDL_Surface * toRot)
  110. {
  111. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  112. SDL_LockSurface(ret);
  113. SDL_LockSurface(toRot);
  114. const int bpp = ret->format->BytesPerPixel;
  115. char * src = reinterpret_cast<char *>(toRot->pixels);
  116. char * dst = reinterpret_cast<char *>(ret->pixels);
  117. for(int i=0; i<ret->h; i++)
  118. {
  119. //FIXME: optimization bugged
  120. // if (bpp == 1)
  121. // {
  122. // // much faster for 8-bit surfaces (majority of our data)
  123. // std::reverse_copy(src, src + toRot->pitch, dst);
  124. // }
  125. // else
  126. // {
  127. char * srcPxl = src;
  128. char * dstPxl = dst + ret->w * bpp;
  129. for(int j=0; j<ret->w; j++)
  130. {
  131. dstPxl -= bpp;
  132. std::copy(srcPxl, srcPxl + bpp, dstPxl);
  133. srcPxl += bpp;
  134. }
  135. // }
  136. src += toRot->pitch;
  137. dst += ret->pitch;
  138. }
  139. SDL_UnlockSurface(ret);
  140. SDL_UnlockSurface(toRot);
  141. return ret;
  142. }
  143. // Horizontal flip
  144. SDL_Surface * CSDL_Ext::horizontalFlip(SDL_Surface * toRot)
  145. {
  146. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  147. SDL_LockSurface(ret);
  148. SDL_LockSurface(toRot);
  149. char * src = reinterpret_cast<char *>(toRot->pixels);
  150. char * dst = reinterpret_cast<char *>(ret->pixels) + ret->h * ret->pitch;
  151. for(int i=0; i<ret->h; i++)
  152. {
  153. dst -= ret->pitch;
  154. std::copy(src, src + toRot->pitch, dst);
  155. src += toRot->pitch;
  156. }
  157. SDL_UnlockSurface(ret);
  158. SDL_UnlockSurface(toRot);
  159. return ret;
  160. }
  161. uint32_t CSDL_Ext::getPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  162. {
  163. int bpp = surface->format->BytesPerPixel;
  164. /* Here p is the address to the pixel we want to retrieve */
  165. uint8_t *p = (uint8_t *)surface->pixels + y * surface->pitch + x * bpp;
  166. switch(bpp)
  167. {
  168. case 1:
  169. if(colorByte)
  170. return colorTouint32_t(surface->format->palette->colors+(*p));
  171. else
  172. return *p;
  173. case 2:
  174. return *(uint16_t *)p;
  175. case 3:
  176. return p[0] | p[1] << 8 | p[2] << 16;
  177. case 4:
  178. return *(uint32_t *)p;
  179. default:
  180. return 0; // shouldn't happen, but avoids warnings
  181. }
  182. }
  183. template<int bpp>
  184. int CSDL_Ext::blit8bppAlphaTo24bppT(const SDL_Surface * src, const Rect & srcRectInput, SDL_Surface * dst, const Point & dstPointInput)
  185. {
  186. SDL_Rect srcRectInstance = CSDL_Ext::toSDL(srcRectInput);
  187. SDL_Rect dstRectInstance = CSDL_Ext::toSDL(Rect(dstPointInput, srcRectInput.dimensions()));
  188. SDL_Rect * srcRect =&srcRectInstance;
  189. SDL_Rect * dstRect =&dstRectInstance;
  190. /* Make sure the surfaces aren't locked */
  191. if ( ! src || ! dst )
  192. {
  193. SDL_SetError("SDL_UpperBlit: passed a nullptr surface");
  194. return -1;
  195. }
  196. if ( src->locked || dst->locked )
  197. {
  198. SDL_SetError("Surfaces must not be locked during blit");
  199. return -1;
  200. }
  201. if (src->format->BytesPerPixel==1 && (bpp==3 || bpp==4 || bpp==2)) //everything's ok
  202. {
  203. SDL_Rect fulldst;
  204. int srcx, srcy, w, h;
  205. /* If the destination rectangle is nullptr, use the entire dest surface */
  206. if ( dstRect == nullptr )
  207. {
  208. fulldst.x = fulldst.y = 0;
  209. dstRect = &fulldst;
  210. }
  211. /* clip the source rectangle to the source surface */
  212. if(srcRect)
  213. {
  214. int maxw, maxh;
  215. srcx = srcRect->x;
  216. w = srcRect->w;
  217. if(srcx < 0)
  218. {
  219. w += srcx;
  220. dstRect->x -= srcx;
  221. srcx = 0;
  222. }
  223. maxw = src->w - srcx;
  224. if(maxw < w)
  225. w = maxw;
  226. srcy = srcRect->y;
  227. h = srcRect->h;
  228. if(srcy < 0)
  229. {
  230. h += srcy;
  231. dstRect->y -= srcy;
  232. srcy = 0;
  233. }
  234. maxh = src->h - srcy;
  235. if(maxh < h)
  236. h = maxh;
  237. }
  238. else
  239. {
  240. srcx = srcy = 0;
  241. w = src->w;
  242. h = src->h;
  243. }
  244. /* clip the destination rectangle against the clip rectangle */
  245. {
  246. SDL_Rect *clip = &dst->clip_rect;
  247. int dx, dy;
  248. dx = clip->x - dstRect->x;
  249. if(dx > 0)
  250. {
  251. w -= dx;
  252. dstRect->x += dx;
  253. srcx += dx;
  254. }
  255. dx = dstRect->x + w - clip->x - clip->w;
  256. if(dx > 0)
  257. w -= dx;
  258. dy = clip->y - dstRect->y;
  259. if(dy > 0)
  260. {
  261. h -= dy;
  262. dstRect->y += dy;
  263. srcy += dy;
  264. }
  265. dy = dstRect->y + h - clip->y - clip->h;
  266. if(dy > 0)
  267. h -= dy;
  268. }
  269. if(w > 0 && h > 0)
  270. {
  271. dstRect->w = w;
  272. dstRect->h = h;
  273. if(SDL_LockSurface(dst))
  274. return -1; //if we cannot lock the surface
  275. const SDL_Color *colors = src->format->palette->colors;
  276. uint8_t *colory = (uint8_t*)src->pixels + srcy*src->pitch + srcx;
  277. uint8_t *py = (uint8_t*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  278. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  279. {
  280. uint8_t *color = colory;
  281. uint8_t *p = py;
  282. for(int x = w; x; x--)
  283. {
  284. const SDL_Color &tbc = colors[*color++]; //color to blit
  285. ColorPutter<bpp, +1>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, tbc.a);
  286. }
  287. }
  288. SDL_UnlockSurface(dst);
  289. }
  290. }
  291. return 0;
  292. }
  293. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const Rect & srcRect, SDL_Surface * dst, const Point & dstPoint)
  294. {
  295. switch(dst->format->BytesPerPixel)
  296. {
  297. case 2: return blit8bppAlphaTo24bppT<2>(src, srcRect, dst, dstPoint);
  298. case 3: return blit8bppAlphaTo24bppT<3>(src, srcRect, dst, dstPoint);
  299. case 4: return blit8bppAlphaTo24bppT<4>(src, srcRect, dst, dstPoint);
  300. default:
  301. logGlobal->error("%d bpp is not supported!", (int)dst->format->BitsPerPixel);
  302. return -1;
  303. }
  304. }
  305. uint32_t CSDL_Ext::colorTouint32_t(const SDL_Color * color)
  306. {
  307. uint32_t ret = 0;
  308. ret+=color->a;
  309. ret<<=8; //*=256
  310. ret+=color->b;
  311. ret<<=8; //*=256
  312. ret+=color->g;
  313. ret<<=8; //*=256
  314. ret+=color->r;
  315. return ret;
  316. }
  317. static void drawLineX(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2)
  318. {
  319. for(int x = x1; x <= x2; x++)
  320. {
  321. float f = float(x - x1) / float(x2 - x1);
  322. int y = vstd::lerp(y1, y2, f);
  323. uint8_t r = vstd::lerp(color1.r, color2.r, f);
  324. uint8_t g = vstd::lerp(color1.g, color2.g, f);
  325. uint8_t b = vstd::lerp(color1.b, color2.b, f);
  326. uint8_t a = vstd::lerp(color1.a, color2.a, f);
  327. uint8_t *p = CSDL_Ext::getPxPtr(sur, x, y);
  328. ColorPutter<4, 0>::PutColor(p, r,g,b,a);
  329. }
  330. }
  331. static void drawLineY(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2)
  332. {
  333. for(int y = y1; y <= y2; y++)
  334. {
  335. float f = float(y - y1) / float(y2 - y1);
  336. int x = vstd::lerp(x1, x2, f);
  337. uint8_t r = vstd::lerp(color1.r, color2.r, f);
  338. uint8_t g = vstd::lerp(color1.g, color2.g, f);
  339. uint8_t b = vstd::lerp(color1.b, color2.b, f);
  340. uint8_t a = vstd::lerp(color1.a, color2.a, f);
  341. uint8_t *p = CSDL_Ext::getPxPtr(sur, x, y);
  342. ColorPutter<4, 0>::PutColor(p, r,g,b,a);
  343. }
  344. }
  345. void CSDL_Ext::drawLine(SDL_Surface * sur, int x1, int y1, int x2, int y2, const SDL_Color & color1, const SDL_Color & color2)
  346. {
  347. int width = std::abs(x1-x2);
  348. int height = std::abs(y1-y2);
  349. if ( width == 0 && height == 0)
  350. {
  351. uint8_t *p = CSDL_Ext::getPxPtr(sur, x1, y1);
  352. ColorPutter<4, 0>::PutColorAlpha(p, color1);
  353. return;
  354. }
  355. if (width > height)
  356. {
  357. if ( x1 < x2)
  358. drawLineX(sur, x1,y1,x2,y2, color1, color2);
  359. else
  360. drawLineX(sur, x2,y2,x1,y1, color2, color1);
  361. }
  362. else
  363. {
  364. if ( y1 < y2)
  365. drawLineY(sur, x1,y1,x2,y2, color1, color2);
  366. else
  367. drawLineY(sur, x2,y2,x1,y1, color2, color1);
  368. }
  369. }
  370. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const SDL_Color &color, int depth)
  371. {
  372. depth = std::max(1, depth);
  373. for(int depthIterator = 0; depthIterator < depth; depthIterator++)
  374. {
  375. for(int i = 0; i < w; i++)
  376. {
  377. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+i,y+depthIterator,color.r,color.g,color.b);
  378. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+i,y+h-1-depthIterator,color.r,color.g,color.b);
  379. }
  380. for(int i = 0; i < h; i++)
  381. {
  382. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+depthIterator,y+i,color.r,color.g,color.b);
  383. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur,x+w-1-depthIterator,y+i,color.r,color.g,color.b);
  384. }
  385. }
  386. }
  387. void CSDL_Ext::drawBorder( SDL_Surface * sur, const Rect &r, const SDL_Color &color, int depth)
  388. {
  389. drawBorder(sur, r.x, r.y, r.w, r.h, color, depth);
  390. }
  391. void CSDL_Ext::drawDashedBorder(SDL_Surface * sur, const Rect &r, const SDL_Color &color)
  392. {
  393. const int y1 = r.y, y2 = r.y + r.h-1;
  394. for (int i=0; i<r.w; i++)
  395. {
  396. const int x = r.x + i;
  397. if (i%4 || (i==0))
  398. {
  399. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x, y1, color.r, color.g, color.b);
  400. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x, y2, color.r, color.g, color.b);
  401. }
  402. }
  403. const int x1 = r.x, x2 = r.x + r.w-1;
  404. for (int i=0; i<r.h; i++)
  405. {
  406. const int y = r.y + i;
  407. if ((i%4) || (i==0))
  408. {
  409. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x1, y, color.r, color.g, color.b);
  410. CSDL_Ext::putPixelWithoutRefreshIfInSurf(sur, x2, y, color.r, color.g, color.b);
  411. }
  412. }
  413. }
  414. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, PlayerColor player)
  415. {
  416. if(player==PlayerColor::UNFLAGGABLE)
  417. return;
  418. if(sur->format->BitsPerPixel==8)
  419. {
  420. SDL_Color *color = (player == PlayerColor::NEUTRAL
  421. ? graphics->neutralColor
  422. : &graphics->playerColors[player.getNum()]);
  423. CSDL_Ext::setColors(sur, color, 5, 1);
  424. }
  425. else
  426. logGlobal->warn("Warning, setPlayerColor called on not 8bpp surface!");
  427. }
  428. CSDL_Ext::TColorPutter CSDL_Ext::getPutterFor(SDL_Surface * const &dest, int incrementing)
  429. {
  430. #define CASE_BPP(BytesPerPixel) \
  431. case BytesPerPixel: \
  432. if(incrementing > 0) \
  433. return ColorPutter<BytesPerPixel, 1>::PutColor; \
  434. else if(incrementing == 0) \
  435. return ColorPutter<BytesPerPixel, 0>::PutColor; \
  436. else \
  437. return ColorPutter<BytesPerPixel, -1>::PutColor;\
  438. break;
  439. switch(dest->format->BytesPerPixel)
  440. {
  441. CASE_BPP(2)
  442. CASE_BPP(3)
  443. CASE_BPP(4)
  444. default:
  445. logGlobal->error("%d bpp is not supported!", (int)dest->format->BitsPerPixel);
  446. return nullptr;
  447. }
  448. }
  449. CSDL_Ext::TColorPutterAlpha CSDL_Ext::getPutterAlphaFor(SDL_Surface * const &dest, int incrementing)
  450. {
  451. switch(dest->format->BytesPerPixel)
  452. {
  453. CASE_BPP(2)
  454. CASE_BPP(3)
  455. CASE_BPP(4)
  456. default:
  457. logGlobal->error("%d bpp is not supported!", (int)dest->format->BitsPerPixel);
  458. return nullptr;
  459. }
  460. #undef CASE_BPP
  461. }
  462. uint8_t * CSDL_Ext::getPxPtr(const SDL_Surface * const &srf, const int x, const int y)
  463. {
  464. return (uint8_t *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel;
  465. }
  466. bool CSDL_Ext::isTransparent( SDL_Surface * srf, const Point & position )
  467. {
  468. return isTransparent(srf, position.x, position.y);
  469. }
  470. bool CSDL_Ext::isTransparent( SDL_Surface * srf, int x, int y )
  471. {
  472. if (x < 0 || y < 0 || x >= srf->w || y >= srf->h)
  473. return true;
  474. SDL_Color color;
  475. SDL_GetRGBA(CSDL_Ext::getPixel(srf, x, y), srf->format, &color.r, &color.g, &color.b, &color.a);
  476. bool pixelTransparent = color.a < 128;
  477. bool pixelCyan = (color.r == 0 && color.g == 255 && color.b == 255);
  478. return pixelTransparent || pixelCyan;
  479. }
  480. void CSDL_Ext::VflipSurf(SDL_Surface * surf)
  481. {
  482. char buf[4]; //buffer
  483. int bpp = surf->format->BytesPerPixel;
  484. for (int y=0; y<surf->h; ++y)
  485. {
  486. char * base = (char*)surf->pixels + y * surf->pitch;
  487. for (int x=0; x<surf->w/2; ++x)
  488. {
  489. memcpy(buf, base + x * bpp, bpp);
  490. memcpy(base + x * bpp, base + (surf->w - x - 1) * bpp, bpp);
  491. memcpy(base + (surf->w - x - 1) * bpp, buf, bpp);
  492. }
  493. }
  494. }
  495. 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)
  496. {
  497. uint8_t *p = getPxPtr(ekran, x, y);
  498. getPutterFor(ekran, false)(p, R, G, B);
  499. switch(ekran->format->BytesPerPixel)
  500. {
  501. case 2: Channels::px<2>::a.set(p, A); break;
  502. case 3: Channels::px<3>::a.set(p, A); break;
  503. case 4: Channels::px<4>::a.set(p, A); break;
  504. }
  505. }
  506. 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)
  507. {
  508. const SDL_Rect & rect = ekran->clip_rect;
  509. if(x >= rect.x && x < rect.w + rect.x
  510. && y >= rect.y && y < rect.h + rect.y)
  511. CSDL_Ext::putPixelWithoutRefresh(ekran, x, y, R, G, B, A);
  512. }
  513. template<int bpp>
  514. void CSDL_Ext::applyEffectBpp(SDL_Surface * surf, const Rect & rect, int mode )
  515. {
  516. switch(mode)
  517. {
  518. case 0: //sepia
  519. {
  520. const int sepiaDepth = 20;
  521. const int sepiaIntensity = 30;
  522. for(int xp = rect.x; xp < rect.x + rect.w; ++xp)
  523. {
  524. for(int yp = rect.y; yp < rect.y + rect.h; ++yp)
  525. {
  526. uint8_t * pixel = (ui8*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  527. int r = Channels::px<bpp>::r.get(pixel);
  528. int g = Channels::px<bpp>::g.get(pixel);
  529. int b = Channels::px<bpp>::b.get(pixel);
  530. int gray = static_cast<int>(0.299 * r + 0.587 * g + 0.114 * b);
  531. r = g = b = gray;
  532. r = r + (sepiaDepth * 2);
  533. g = g + sepiaDepth;
  534. if (r>255) r=255;
  535. if (g>255) g=255;
  536. if (b>255) b=255;
  537. // Darken blue color to increase sepia effect
  538. b -= sepiaIntensity;
  539. // normalize if out of bounds
  540. if (b<0) b=0;
  541. Channels::px<bpp>::r.set(pixel, r);
  542. Channels::px<bpp>::g.set(pixel, g);
  543. Channels::px<bpp>::b.set(pixel, b);
  544. }
  545. }
  546. }
  547. break;
  548. case 1: //grayscale
  549. {
  550. for(int xp = rect.x; xp < rect.x + rect.w; ++xp)
  551. {
  552. for(int yp = rect.y; yp < rect.y + rect.h; ++yp)
  553. {
  554. uint8_t * pixel = (ui8*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  555. int r = Channels::px<bpp>::r.get(pixel);
  556. int g = Channels::px<bpp>::g.get(pixel);
  557. int b = Channels::px<bpp>::b.get(pixel);
  558. int gray = static_cast<int>(0.299 * r + 0.587 * g + 0.114 *b);
  559. vstd::amax(gray, 255);
  560. Channels::px<bpp>::r.set(pixel, gray);
  561. Channels::px<bpp>::g.set(pixel, gray);
  562. Channels::px<bpp>::b.set(pixel, gray);
  563. }
  564. }
  565. }
  566. break;
  567. default:
  568. throw std::runtime_error("Unsupported effect!");
  569. }
  570. }
  571. void CSDL_Ext::applyEffect( SDL_Surface * surf, const Rect & rect, int mode )
  572. {
  573. switch(surf->format->BytesPerPixel)
  574. {
  575. case 2: applyEffectBpp<2>(surf, rect, mode); break;
  576. case 3: applyEffectBpp<3>(surf, rect, mode); break;
  577. case 4: applyEffectBpp<4>(surf, rect, mode); break;
  578. }
  579. }
  580. template<int bpp>
  581. void scaleSurfaceFastInternal(SDL_Surface *surf, SDL_Surface *ret)
  582. {
  583. const float factorX = float(surf->w) / float(ret->w),
  584. factorY = float(surf->h) / float(ret->h);
  585. for(int y = 0; y < ret->h; y++)
  586. {
  587. for(int x = 0; x < ret->w; x++)
  588. {
  589. //coordinates we want to calculate
  590. int origX = static_cast<int>(floor(factorX * x)),
  591. origY = static_cast<int>(floor(factorY * y));
  592. // Get pointers to source pixels
  593. uint8_t *srcPtr = (uint8_t*)surf->pixels + origY * surf->pitch + origX * bpp;
  594. uint8_t *destPtr = (uint8_t*)ret->pixels + y * ret->pitch + x * bpp;
  595. memcpy(destPtr, srcPtr, bpp);
  596. }
  597. }
  598. }
  599. SDL_Surface * CSDL_Ext::scaleSurfaceFast(SDL_Surface *surf, int width, int height)
  600. {
  601. if (!surf || !width || !height)
  602. return nullptr;
  603. //Same size? return copy - this should more be faster
  604. if (width == surf->w && height == surf->h)
  605. return copySurface(surf);
  606. SDL_Surface *ret = newSurface(width, height, surf);
  607. switch(surf->format->BytesPerPixel)
  608. {
  609. case 1: scaleSurfaceFastInternal<1>(surf, ret); break;
  610. case 2: scaleSurfaceFastInternal<2>(surf, ret); break;
  611. case 3: scaleSurfaceFastInternal<3>(surf, ret); break;
  612. case 4: scaleSurfaceFastInternal<4>(surf, ret); break;
  613. }
  614. return ret;
  615. }
  616. template<int bpp>
  617. void scaleSurfaceInternal(SDL_Surface *surf, SDL_Surface *ret)
  618. {
  619. const float factorX = float(surf->w - 1) / float(ret->w),
  620. factorY = float(surf->h - 1) / float(ret->h);
  621. for(int y = 0; y < ret->h; y++)
  622. {
  623. for(int x = 0; x < ret->w; x++)
  624. {
  625. //coordinates we want to interpolate
  626. float origX = factorX * x,
  627. origY = factorY * y;
  628. float x1 = floor(origX), x2 = floor(origX+1),
  629. y1 = floor(origY), y2 = floor(origY+1);
  630. //assert( x1 >= 0 && y1 >= 0 && x2 < surf->w && y2 < surf->h);//All pixels are in range
  631. // Calculate weights of each source pixel
  632. float w11 = ((origX - x1) * (origY - y1));
  633. float w12 = ((origX - x1) * (y2 - origY));
  634. float w21 = ((x2 - origX) * (origY - y1));
  635. float w22 = ((x2 - origX) * (y2 - origY));
  636. //assert( w11 + w12 + w21 + w22 > 0.99 && w11 + w12 + w21 + w22 < 1.01);//total weight is ~1.0
  637. // Get pointers to source pixels
  638. uint8_t *p11 = (uint8_t*)surf->pixels + int(y1) * surf->pitch + int(x1) * bpp;
  639. uint8_t *p12 = p11 + bpp;
  640. uint8_t *p21 = p11 + surf->pitch;
  641. uint8_t *p22 = p21 + bpp;
  642. // Calculate resulting channels
  643. #define PX(X, PTR) Channels::px<bpp>::X.get(PTR)
  644. int resR = static_cast<int>(PX(r, p11) * w11 + PX(r, p12) * w12 + PX(r, p21) * w21 + PX(r, p22) * w22);
  645. int resG = static_cast<int>(PX(g, p11) * w11 + PX(g, p12) * w12 + PX(g, p21) * w21 + PX(g, p22) * w22);
  646. int resB = static_cast<int>(PX(b, p11) * w11 + PX(b, p12) * w12 + PX(b, p21) * w21 + PX(b, p22) * w22);
  647. int resA = static_cast<int>(PX(a, p11) * w11 + PX(a, p12) * w12 + PX(a, p21) * w21 + PX(a, p22) * w22);
  648. //assert(resR < 256 && resG < 256 && resB < 256 && resA < 256);
  649. #undef PX
  650. uint8_t *dest = (uint8_t*)ret->pixels + y * ret->pitch + x * bpp;
  651. Channels::px<bpp>::r.set(dest, resR);
  652. Channels::px<bpp>::g.set(dest, resG);
  653. Channels::px<bpp>::b.set(dest, resB);
  654. Channels::px<bpp>::a.set(dest, resA);
  655. }
  656. }
  657. }
  658. // scaling via bilinear interpolation algorithm.
  659. // NOTE: best results are for scaling in range 50%...200%.
  660. // And upscaling looks awful right now - should be fixed somehow
  661. SDL_Surface * CSDL_Ext::scaleSurface(SDL_Surface *surf, int width, int height)
  662. {
  663. if (!surf || !width || !height)
  664. return nullptr;
  665. if (surf->format->palette)
  666. return scaleSurfaceFast(surf, width, height);
  667. //Same size? return copy - this should more be faster
  668. if (width == surf->w && height == surf->h)
  669. return copySurface(surf);
  670. SDL_Surface *ret = newSurface(width, height, surf);
  671. switch(surf->format->BytesPerPixel)
  672. {
  673. case 2: scaleSurfaceInternal<2>(surf, ret); break;
  674. case 3: scaleSurfaceInternal<3>(surf, ret); break;
  675. case 4: scaleSurfaceInternal<4>(surf, ret); break;
  676. }
  677. return ret;
  678. }
  679. void CSDL_Ext::blitSurface(SDL_Surface * src, const Rect & srcRectInput, SDL_Surface * dst, const Point & dstPoint)
  680. {
  681. SDL_Rect srcRect = CSDL_Ext::toSDL(srcRectInput);
  682. SDL_Rect dstRect = CSDL_Ext::toSDL(Rect(dstPoint, srcRectInput.dimensions()));
  683. int result = SDL_UpperBlit(src, &srcRect, dst, &dstRect);
  684. if (result != 0)
  685. logGlobal->error("SDL_UpperBlit failed! %s", SDL_GetError());
  686. }
  687. void CSDL_Ext::blitSurface(SDL_Surface * src, SDL_Surface * dst, const Point & dest)
  688. {
  689. Rect allSurface( Point(0,0), Point(src->w, src->h));
  690. blitSurface(src, allSurface, dst, dest);
  691. }
  692. void CSDL_Ext::fillSurface( SDL_Surface *dst, const SDL_Color & color )
  693. {
  694. Rect allSurface( Point(0,0), Point(dst->w, dst->h));
  695. fillRect(dst, allSurface, color);
  696. }
  697. void CSDL_Ext::fillRect( SDL_Surface *dst, const Rect & dstrect, const SDL_Color & color )
  698. {
  699. SDL_Rect newRect = CSDL_Ext::toSDL(dstrect);
  700. uint32_t sdlColor = SDL_MapRGBA(dst->format, color.r, color.g, color.b, color.a);
  701. SDL_FillRect(dst, &newRect, sdlColor);
  702. }
  703. SDL_Color CSDL_Ext::makeColor(ui8 r, ui8 g, ui8 b, ui8 a)
  704. {
  705. SDL_Color ret = {r, g, b, a};
  706. return ret;
  707. }
  708. STRONG_INLINE static uint32_t mapColor(SDL_Surface * surface, SDL_Color color)
  709. {
  710. return SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a);
  711. }
  712. void CSDL_Ext::setColorKey(SDL_Surface * surface, SDL_Color color)
  713. {
  714. uint32_t key = mapColor(surface,color);
  715. SDL_SetColorKey(surface, SDL_TRUE, key);
  716. }
  717. void CSDL_Ext::setDefaultColorKey(SDL_Surface * surface)
  718. {
  719. setColorKey(surface, Colors::DEFAULT_KEY_COLOR);
  720. }
  721. void CSDL_Ext::setDefaultColorKeyPresize(SDL_Surface * surface)
  722. {
  723. uint32_t key = mapColor(surface, Colors::DEFAULT_KEY_COLOR);
  724. auto & color = surface->format->palette->colors[key];
  725. // set color key only if exactly such color was found
  726. if (color.r == Colors::DEFAULT_KEY_COLOR.r && color.g == Colors::DEFAULT_KEY_COLOR.g && color.b == Colors::DEFAULT_KEY_COLOR.b)
  727. {
  728. SDL_SetColorKey(surface, SDL_TRUE, key);
  729. color.a = SDL_ALPHA_TRANSPARENT;
  730. }
  731. }
  732. void CSDL_Ext::setClipRect(SDL_Surface * src, const Rect & other)
  733. {
  734. SDL_Rect rect = CSDL_Ext::toSDL(other);
  735. SDL_SetClipRect(src, &rect);
  736. }
  737. void CSDL_Ext::getClipRect(SDL_Surface * src, Rect & other)
  738. {
  739. SDL_Rect rect;
  740. SDL_GetClipRect(src, &rect);
  741. other = CSDL_Ext::fromSDL(rect);
  742. }
  743. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<2>(int, int);
  744. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<3>(int, int);
  745. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<4>(int, int);