SDL_Extensions.cpp 24 KB

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