SDL_Extensions.cpp 23 KB

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