SDL_Extensions.cpp 26 KB

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