2
0

SDL_Extensions.cpp 24 KB

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