2
0

SDL_Extensions.cpp 23 KB

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