SDL_Extensions.cpp 24 KB

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