SDL_Extensions.cpp 25 KB

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