SDL_Extensions.cpp 27 KB

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