SDL_Extensions.cpp 27 KB

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