SDL_Extensions.cpp 25 KB

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