SDL_Extensions.cpp 24 KB

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