SDL_Extensions.cpp 24 KB

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