SDL_Extensions.cpp 27 KB

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