2
0

SDL_Extensions.cpp 23 KB

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