SDL_Extensions.cpp 22 KB

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