SDL_Extensions.cpp 25 KB

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