SDL_Extensions.cpp 24 KB

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