SDL_Extensions.cpp 27 KB

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