SDL_Extensions.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. #include "StdInc.h"
  2. #include "SDL_Extensions.h"
  3. #include "SDL_Pixels.h"
  4. #include "../CGameInfo.h"
  5. #include "../CMessage.h"
  6. #include "../Graphics.h"
  7. #include "../CMT.h"
  8. const SDL_Color Colors::YELLOW = { 229, 215, 123, 0 };
  9. const SDL_Color Colors::WHITE = { 255, 243, 222, 0 };
  10. const SDL_Color Colors::METALLIC_GOLD = { 173, 142, 66, 0 };
  11. const SDL_Color Colors::GREEN = { 0, 255, 0, 0 };
  12. const SDL_Color Colors::DEFAULT_KEY_COLOR = {0, 255, 255, 0};
  13. void SDL_UpdateRect(SDL_Surface *surface, int x, int y, int w, int h)
  14. {
  15. Rect rect(x,y,w,h);
  16. if(0 !=SDL_UpdateTexture(screenTexture, &rect, surface->pixels, surface->pitch))
  17. logGlobal->errorStream() << __FUNCTION__ << "SDL_UpdateTexture " << SDL_GetError();
  18. SDL_RenderClear(mainRenderer);
  19. if(0 != SDL_RenderCopy(mainRenderer, screenTexture, NULL, NULL))
  20. logGlobal->errorStream() << __FUNCTION__ << "SDL_RenderCopy " << SDL_GetError();
  21. SDL_RenderPresent(mainRenderer);
  22. }
  23. SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  24. {
  25. SDL_Surface * ret = SDL_CreateRGBSurface(mod->flags,w,h,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  26. if (mod->format->palette)
  27. {
  28. assert(ret->format->palette);
  29. assert(ret->format->palette->ncolors == mod->format->palette->ncolors);
  30. memcpy(ret->format->palette->colors, mod->format->palette->colors, mod->format->palette->ncolors * sizeof(SDL_Color));
  31. }
  32. return ret;
  33. }
  34. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  35. {
  36. //return SDL_DisplayFormat(mod);
  37. return SDL_ConvertSurface(mod, mod->format, mod->flags);
  38. }
  39. template<int bpp>
  40. SDL_Surface * CSDL_Ext::createSurfaceWithBpp(int width, int height)
  41. {
  42. Uint32 rMask = 0, gMask = 0, bMask = 0, aMask = 0;
  43. Channels::px<bpp>::r.set((Uint8*)&rMask, 255);
  44. Channels::px<bpp>::g.set((Uint8*)&gMask, 255);
  45. Channels::px<bpp>::b.set((Uint8*)&bMask, 255);
  46. Channels::px<bpp>::a.set((Uint8*)&aMask, 255);
  47. return SDL_CreateRGBSurface( SDL_SWSURFACE, width, height, bpp * 8, rMask, gMask, bMask, aMask);
  48. }
  49. bool isItIn(const SDL_Rect * rect, int x, int y)
  50. {
  51. return (x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h);
  52. }
  53. bool isItInOrLowerBounds(const SDL_Rect * rect, int x, int y)
  54. {
  55. return (x >= rect->x && x < rect->x + rect->w) && (y >= rect->y && y < rect->y + rect->h);
  56. }
  57. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  58. {
  59. if(!dst) dst = screen;
  60. SDL_Rect pom = genRect(src->h,src->w,x,y);
  61. CSDL_Ext::blitSurface(src,nullptr,dst,&pom);
  62. }
  63. void blitAt(SDL_Surface * src, const SDL_Rect & pos, SDL_Surface * dst)
  64. {
  65. if (src)
  66. blitAt(src,pos.x,pos.y,dst);
  67. }
  68. // Vertical flip
  69. SDL_Surface * CSDL_Ext::verticalFlip(SDL_Surface * toRot)
  70. {
  71. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  72. SDL_LockSurface(ret);
  73. SDL_LockSurface(toRot);
  74. const int bpp = ret->format->BytesPerPixel;
  75. char * src = reinterpret_cast<char *>(toRot->pixels);
  76. char * dst = reinterpret_cast<char *>(ret->pixels);
  77. for(int i=0; i<ret->h; i++)
  78. {
  79. //FIXME: optimization bugged
  80. // if (bpp == 1)
  81. // {
  82. // // much faster for 8-bit surfaces (majority of our data)
  83. // std::reverse_copy(src, src + toRot->pitch, dst);
  84. // }
  85. // else
  86. // {
  87. char * srcPxl = src;
  88. char * dstPxl = dst + ret->w * bpp;
  89. for(int j=0; j<ret->w; j++)
  90. {
  91. dstPxl -= bpp;
  92. std::copy(srcPxl, srcPxl + bpp, dstPxl);
  93. srcPxl += bpp;
  94. }
  95. // }
  96. src += toRot->pitch;
  97. dst += ret->pitch;
  98. }
  99. SDL_UnlockSurface(ret);
  100. SDL_UnlockSurface(toRot);
  101. return ret;
  102. }
  103. // Horizontal flip
  104. SDL_Surface * CSDL_Ext::horizontalFlip(SDL_Surface * toRot)
  105. {
  106. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  107. SDL_LockSurface(ret);
  108. SDL_LockSurface(toRot);
  109. char * src = reinterpret_cast<char *>(toRot->pixels);
  110. char * dst = reinterpret_cast<char *>(ret->pixels) + ret->h * ret->pitch;
  111. for(int i=0; i<ret->h; i++)
  112. {
  113. dst -= ret->pitch;
  114. std::copy(src, src + toRot->pitch, dst);
  115. src += toRot->pitch;
  116. }
  117. SDL_UnlockSurface(ret);
  118. SDL_UnlockSurface(toRot);
  119. return ret;
  120. }
  121. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  122. {
  123. int bpp = surface->format->BytesPerPixel;
  124. /* Here p is the address to the pixel we want to retrieve */
  125. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  126. switch(bpp)
  127. {
  128. case 1:
  129. if(colorByte)
  130. return colorToUint32(surface->format->palette->colors+(*p));
  131. else
  132. return *p;
  133. case 2:
  134. return *(Uint16 *)p;
  135. case 3:
  136. return p[0] | p[1] << 8 | p[2] << 16;
  137. case 4:
  138. return *(Uint32 *)p;
  139. default:
  140. return 0; // shouldn't happen, but avoids warnings
  141. }
  142. }
  143. void CSDL_Ext::alphaTransform(SDL_Surface *src)
  144. {
  145. assert(src->format->BitsPerPixel == 8);
  146. SDL_Color colors[] =
  147. {
  148. { 0, 0, 0, 0}, { 0, 0, 0, 32}, { 0, 0, 0, 64},
  149. { 0, 0, 0, 128}, { 0, 0, 0, 128}
  150. };
  151. for (size_t i=0; i< ARRAY_COUNT(colors); i++ )
  152. {
  153. SDL_Color & palColor = src->format->palette->colors[i];
  154. palColor = colors[i];
  155. }
  156. SDL_SetColorKey(src, SDL_TRUE, 0);
  157. }
  158. template<int bpp>
  159. int CSDL_Ext::blit8bppAlphaTo24bppT(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  160. {
  161. /* Make sure the surfaces aren't locked */
  162. if ( ! src || ! dst )
  163. {
  164. SDL_SetError("SDL_UpperBlit: passed a nullptr surface");
  165. return -1;
  166. }
  167. if ( src->locked || dst->locked )
  168. {
  169. SDL_SetError("Surfaces must not be locked during blit");
  170. return -1;
  171. }
  172. if (src->format->BytesPerPixel==1 && (bpp==3 || bpp==4 || bpp==2)) //everything's ok
  173. {
  174. SDL_Rect fulldst;
  175. int srcx, srcy, w, h;
  176. /* If the destination rectangle is nullptr, use the entire dest surface */
  177. if ( dstRect == nullptr )
  178. {
  179. fulldst.x = fulldst.y = 0;
  180. dstRect = &fulldst;
  181. }
  182. /* clip the source rectangle to the source surface */
  183. if(srcRect)
  184. {
  185. int maxw, maxh;
  186. srcx = srcRect->x;
  187. w = srcRect->w;
  188. if(srcx < 0)
  189. {
  190. w += srcx;
  191. dstRect->x -= srcx;
  192. srcx = 0;
  193. }
  194. maxw = src->w - srcx;
  195. if(maxw < w)
  196. w = maxw;
  197. srcy = srcRect->y;
  198. h = srcRect->h;
  199. if(srcy < 0)
  200. {
  201. h += srcy;
  202. dstRect->y -= srcy;
  203. srcy = 0;
  204. }
  205. maxh = src->h - srcy;
  206. if(maxh < h)
  207. h = maxh;
  208. }
  209. else
  210. {
  211. srcx = srcy = 0;
  212. w = src->w;
  213. h = src->h;
  214. }
  215. /* clip the destination rectangle against the clip rectangle */
  216. {
  217. SDL_Rect *clip = &dst->clip_rect;
  218. int dx, dy;
  219. dx = clip->x - dstRect->x;
  220. if(dx > 0)
  221. {
  222. w -= dx;
  223. dstRect->x += dx;
  224. srcx += dx;
  225. }
  226. dx = dstRect->x + w - clip->x - clip->w;
  227. if(dx > 0)
  228. w -= dx;
  229. dy = clip->y - dstRect->y;
  230. if(dy > 0)
  231. {
  232. h -= dy;
  233. dstRect->y += dy;
  234. srcy += dy;
  235. }
  236. dy = dstRect->y + h - clip->y - clip->h;
  237. if(dy > 0)
  238. h -= dy;
  239. }
  240. if(w > 0 && h > 0)
  241. {
  242. dstRect->w = w;
  243. dstRect->h = h;
  244. if(SDL_LockSurface(dst))
  245. return -1; //if we cannot lock the surface
  246. const SDL_Color *colors = src->format->palette->colors;
  247. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  248. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  249. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  250. {
  251. Uint8 *color = colory;
  252. Uint8 *p = py;
  253. for(int x = w; x; x--)
  254. {
  255. const SDL_Color &tbc = colors[*color++]; //color to blit
  256. ColorPutter<bpp, +1>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, tbc.a);
  257. }
  258. }
  259. SDL_UnlockSurface(dst);
  260. }
  261. }
  262. return 0;
  263. }
  264. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  265. {
  266. switch(dst->format->BytesPerPixel)
  267. {
  268. case 2: return blit8bppAlphaTo24bppT<2>(src, srcRect, dst, dstRect);
  269. case 3: return blit8bppAlphaTo24bppT<3>(src, srcRect, dst, dstRect);
  270. case 4: return blit8bppAlphaTo24bppT<4>(src, srcRect, dst, dstRect);
  271. default:
  272. logGlobal->errorStream() << (int)dst->format->BitsPerPixel << " bpp is not supported!!!";
  273. return -1;
  274. }
  275. }
  276. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  277. {
  278. Uint32 ret = 0;
  279. ret+=color->a;
  280. ret<<=8; //*=256
  281. ret+=color->b;
  282. ret<<=8; //*=256
  283. ret+=color->g;
  284. ret<<=8; //*=256
  285. ret+=color->r;
  286. return ret;
  287. }
  288. void CSDL_Ext::update(SDL_Surface * what)
  289. {
  290. if(!what)
  291. return;
  292. if(0 !=SDL_UpdateTexture(screenTexture, nullptr, what->pixels, what->pitch))
  293. logGlobal->errorStream() << __FUNCTION__ << "SDL_UpdateTexture " << SDL_GetError();
  294. }
  295. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color)
  296. {
  297. for(int i = 0; i < w; i++)
  298. {
  299. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+i,y,color.x,color.y,color.z);
  300. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+i,y+h-1,color.x,color.y,color.z);
  301. }
  302. for(int i = 0; i < h; i++)
  303. {
  304. SDL_PutPixelWithoutRefreshIfInSurf(sur,x,y+i,color.x,color.y,color.z);
  305. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+w-1,y+i,color.x,color.y,color.z);
  306. }
  307. }
  308. void CSDL_Ext::drawBorder( SDL_Surface * sur, const SDL_Rect &r, const int3 &color )
  309. {
  310. drawBorder(sur, r.x, r.y, r.w, r.h, color);
  311. }
  312. void CSDL_Ext::drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color)
  313. {
  314. const int y1 = r.y, y2 = r.y + r.h-1;
  315. for (int i=0; i<r.w; i++)
  316. {
  317. const int x = r.x + i;
  318. if (i%4 || (i==0))
  319. {
  320. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y1, color.x, color.y, color.z);
  321. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y2, color.x, color.y, color.z);
  322. }
  323. }
  324. const int x1 = r.x, x2 = r.x + r.w-1;
  325. for (int i=0; i<r.h; i++)
  326. {
  327. const int y = r.y + i;
  328. if ((i%4) || (i==0))
  329. {
  330. SDL_PutPixelWithoutRefreshIfInSurf(sur, x1, y, color.x, color.y, color.z);
  331. SDL_PutPixelWithoutRefreshIfInSurf(sur, x2, y, color.x, color.y, color.z);
  332. }
  333. }
  334. }
  335. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, PlayerColor player)
  336. {
  337. if(player==PlayerColor::UNFLAGGABLE)
  338. return;
  339. if(sur->format->BitsPerPixel==8)
  340. {
  341. SDL_Color *color = (player == PlayerColor::NEUTRAL
  342. ? graphics->neutralColor
  343. : &graphics->playerColors[player.getNum()]);
  344. SDL_SetColors(sur, color, 5, 1);
  345. }
  346. else
  347. logGlobal->warnStream() << "Warning, setPlayerColor called on not 8bpp surface!";
  348. }
  349. TColorPutter CSDL_Ext::getPutterFor(SDL_Surface * const &dest, int incrementing)
  350. {
  351. #define CASE_BPP(BytesPerPixel) \
  352. case BytesPerPixel: \
  353. if(incrementing > 0) \
  354. return ColorPutter<BytesPerPixel, 1>::PutColor; \
  355. else if(incrementing == 0) \
  356. return ColorPutter<BytesPerPixel, 0>::PutColor; \
  357. else \
  358. return ColorPutter<BytesPerPixel, -1>::PutColor;\
  359. break;
  360. switch(dest->format->BytesPerPixel)
  361. {
  362. CASE_BPP(2)
  363. CASE_BPP(3)
  364. CASE_BPP(4)
  365. default:
  366. logGlobal->errorStream() << (int)dest->format->BitsPerPixel << "bpp is not supported!";
  367. return nullptr;
  368. }
  369. }
  370. TColorPutterAlpha CSDL_Ext::getPutterAlphaFor(SDL_Surface * const &dest, int incrementing)
  371. {
  372. switch(dest->format->BytesPerPixel)
  373. {
  374. CASE_BPP(2)
  375. CASE_BPP(3)
  376. CASE_BPP(4)
  377. default:
  378. logGlobal->errorStream() << (int)dest->format->BitsPerPixel << "bpp is not supported!";
  379. return nullptr;
  380. }
  381. #undef CASE_BPP
  382. }
  383. Uint8 * CSDL_Ext::getPxPtr(const SDL_Surface * const &srf, const int x, const int y)
  384. {
  385. return (Uint8 *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel;
  386. }
  387. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  388. {
  389. for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
  390. {
  391. boost::replace_first(str,"%s",tor[i]);
  392. }
  393. return str;
  394. }
  395. bool CSDL_Ext::isTransparent( SDL_Surface * srf, int x, int y )
  396. {
  397. if (x < 0 || y < 0 || x >= srf->w || y >= srf->h)
  398. return true;
  399. SDL_Color color;
  400. SDL_GetRGBA(SDL_GetPixel(srf, x, y), srf->format, &color.r, &color.g, &color.b, &color.a);
  401. // color is considered transparent here if
  402. // a) image has aplha: less than 50% transparency
  403. // b) no alpha: color is cyan
  404. if (srf->format->Amask)
  405. return color.a < 128; // almost transparent
  406. else
  407. return (color.r == 0 && color.g == 255 && color.b == 255);
  408. }
  409. void CSDL_Ext::VflipSurf(SDL_Surface * surf)
  410. {
  411. char buf[4]; //buffer
  412. int bpp = surf->format->BytesPerPixel;
  413. for (int y=0; y<surf->h; ++y)
  414. {
  415. char * base = (char*)surf->pixels + y * surf->pitch;
  416. for (int x=0; x<surf->w/2; ++x)
  417. {
  418. memcpy(buf, base + x * bpp, bpp);
  419. memcpy(base + x * bpp, base + (surf->w - x - 1) * bpp, bpp);
  420. memcpy(base + (surf->w - x - 1) * bpp, buf, bpp);
  421. }
  422. }
  423. }
  424. void CSDL_Ext::SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A /*= 255*/)
  425. {
  426. Uint8 *p = getPxPtr(ekran, x, y);
  427. getPutterFor(ekran, false)(p, R, G, B);
  428. switch(ekran->format->BytesPerPixel)
  429. {
  430. case 2: Channels::px<2>::a.set(p, A); break;
  431. case 3: Channels::px<3>::a.set(p, A); break;
  432. case 4: Channels::px<4>::a.set(p, A); break;
  433. }
  434. }
  435. void CSDL_Ext::SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A /*= 255*/)
  436. {
  437. const SDL_Rect & rect = ekran->clip_rect;
  438. if(x >= rect.x && x < rect.w + rect.x
  439. && y >= rect.y && y < rect.h + rect.y)
  440. SDL_PutPixelWithoutRefresh(ekran, x, y, R, G, B, A);
  441. }
  442. template<int bpp>
  443. void CSDL_Ext::applyEffectBpp( SDL_Surface * surf, const SDL_Rect * rect, int mode )
  444. {
  445. switch(mode)
  446. {
  447. case 0: //sepia
  448. {
  449. const int sepiaDepth = 20;
  450. const int sepiaIntensity = 30;
  451. for(int xp = rect->x; xp < rect->x + rect->w; ++xp)
  452. {
  453. for(int yp = rect->y; yp < rect->y + rect->h; ++yp)
  454. {
  455. Uint8 * pixel = (ui8*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  456. int r = Channels::px<bpp>::r.get(pixel);
  457. int g = Channels::px<bpp>::g.get(pixel);
  458. int b = Channels::px<bpp>::b.get(pixel);
  459. int gray = 0.299 * r + 0.587 * g + 0.114 *b;
  460. r = g = b = gray;
  461. r = r + (sepiaDepth * 2);
  462. g = g + sepiaDepth;
  463. if (r>255) r=255;
  464. if (g>255) g=255;
  465. if (b>255) b=255;
  466. // Darken blue color to increase sepia effect
  467. b -= sepiaIntensity;
  468. // normalize if out of bounds
  469. if (b<0) b=0;
  470. Channels::px<bpp>::r.set(pixel, r);
  471. Channels::px<bpp>::g.set(pixel, g);
  472. Channels::px<bpp>::b.set(pixel, b);
  473. }
  474. }
  475. }
  476. break;
  477. case 1: //grayscale
  478. {
  479. for(int xp = rect->x; xp < rect->x + rect->w; ++xp)
  480. {
  481. for(int yp = rect->y; yp < rect->y + rect->h; ++yp)
  482. {
  483. Uint8 * pixel = (ui8*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  484. int r = Channels::px<bpp>::r.get(pixel);
  485. int g = Channels::px<bpp>::g.get(pixel);
  486. int b = Channels::px<bpp>::b.get(pixel);
  487. int gray = 0.299 * r + 0.587 * g + 0.114 *b;
  488. vstd::amax(gray, 255);
  489. Channels::px<bpp>::r.set(pixel, gray);
  490. Channels::px<bpp>::g.set(pixel, gray);
  491. Channels::px<bpp>::b.set(pixel, gray);
  492. }
  493. }
  494. }
  495. break;
  496. default:
  497. throw std::runtime_error("Unsupported effect!");
  498. }
  499. }
  500. void CSDL_Ext::applyEffect( SDL_Surface * surf, const SDL_Rect * rect, int mode )
  501. {
  502. switch(surf->format->BytesPerPixel)
  503. {
  504. case 2: applyEffectBpp<2>(surf, rect, mode); break;
  505. case 3: applyEffectBpp<3>(surf, rect, mode); break;
  506. case 4: applyEffectBpp<4>(surf, rect, mode); break;
  507. }
  508. }
  509. template<int bpp>
  510. void scaleSurfaceFastInternal(SDL_Surface *surf, SDL_Surface *ret)
  511. {
  512. const float factorX = float(surf->w) / float(ret->w),
  513. factorY = float(surf->h) / float(ret->h);
  514. for(int y = 0; y < ret->h; y++)
  515. {
  516. for(int x = 0; x < ret->w; x++)
  517. {
  518. //coordinates we want to calculate
  519. int origX = floor(factorX * x),
  520. origY = floor(factorY * y);
  521. // Get pointers to source pixels
  522. Uint8 *srcPtr = (Uint8*)surf->pixels + origY * surf->pitch + origX * bpp;
  523. Uint8 *destPtr = (Uint8*)ret->pixels + y * ret->pitch + x * bpp;
  524. memcpy(destPtr, srcPtr, bpp);
  525. }
  526. }
  527. }
  528. SDL_Surface * CSDL_Ext::scaleSurfaceFast(SDL_Surface *surf, int width, int height)
  529. {
  530. if (!surf || !width || !height)
  531. return nullptr;
  532. //Same size? return copy - this should more be faster
  533. if (width == surf->w && height == surf->h)
  534. return copySurface(surf);
  535. SDL_Surface *ret = newSurface(width, height, surf);
  536. switch(surf->format->BytesPerPixel)
  537. {
  538. case 1: scaleSurfaceFastInternal<1>(surf, ret); break;
  539. case 2: scaleSurfaceFastInternal<2>(surf, ret); break;
  540. case 3: scaleSurfaceFastInternal<3>(surf, ret); break;
  541. case 4: scaleSurfaceFastInternal<4>(surf, ret); break;
  542. }
  543. return ret;
  544. }
  545. template<int bpp>
  546. void scaleSurfaceInternal(SDL_Surface *surf, SDL_Surface *ret)
  547. {
  548. const float factorX = float(surf->w - 1) / float(ret->w),
  549. factorY = float(surf->h - 1) / float(ret->h);
  550. for(int y = 0; y < ret->h; y++)
  551. {
  552. for(int x = 0; x < ret->w; x++)
  553. {
  554. //coordinates we want to interpolate
  555. float origX = factorX * x,
  556. origY = factorY * y;
  557. float x1 = floor(origX), x2 = floor(origX+1),
  558. y1 = floor(origY), y2 = floor(origY+1);
  559. //assert( x1 >= 0 && y1 >= 0 && x2 < surf->w && y2 < surf->h);//All pixels are in range
  560. // Calculate weights of each source pixel
  561. float w11 = ((origX - x1) * (origY - y1));
  562. float w12 = ((origX - x1) * (y2 - origY));
  563. float w21 = ((x2 - origX) * (origY - y1));
  564. float w22 = ((x2 - origX) * (y2 - origY));
  565. //assert( w11 + w12 + w21 + w22 > 0.99 && w11 + w12 + w21 + w22 < 1.01);//total weight is ~1.0
  566. // Get pointers to source pixels
  567. Uint8 *p11 = (Uint8*)surf->pixels + int(y1) * surf->pitch + int(x1) * bpp;
  568. Uint8 *p12 = p11 + bpp;
  569. Uint8 *p21 = p11 + surf->pitch;
  570. Uint8 *p22 = p21 + bpp;
  571. // Calculate resulting channels
  572. #define PX(X, PTR) Channels::px<bpp>::X.get(PTR)
  573. int resR = PX(r, p11) * w11 + PX(r, p12) * w12 + PX(r, p21) * w21 + PX(r, p22) * w22;
  574. int resG = PX(g, p11) * w11 + PX(g, p12) * w12 + PX(g, p21) * w21 + PX(g, p22) * w22;
  575. int resB = PX(b, p11) * w11 + PX(b, p12) * w12 + PX(b, p21) * w21 + PX(b, p22) * w22;
  576. int resA = PX(a, p11) * w11 + PX(a, p12) * w12 + PX(a, p21) * w21 + PX(a, p22) * w22;
  577. //assert(resR < 256 && resG < 256 && resB < 256 && resA < 256);
  578. #undef PX
  579. Uint8 *dest = (Uint8*)ret->pixels + y * ret->pitch + x * bpp;
  580. Channels::px<bpp>::r.set(dest, resR);
  581. Channels::px<bpp>::g.set(dest, resG);
  582. Channels::px<bpp>::b.set(dest, resB);
  583. Channels::px<bpp>::a.set(dest, resA);
  584. }
  585. }
  586. }
  587. // scaling via bilinear interpolation algorithm.
  588. // NOTE: best results are for scaling in range 50%...200%.
  589. // And upscaling looks awful right now - should be fixed somehow
  590. SDL_Surface * CSDL_Ext::scaleSurface(SDL_Surface *surf, int width, int height)
  591. {
  592. if (!surf || !width || !height)
  593. return nullptr;
  594. if (surf->format->palette)
  595. return scaleSurfaceFast(surf, width, height);
  596. //Same size? return copy - this should more be faster
  597. if (width == surf->w && height == surf->h)
  598. return copySurface(surf);
  599. SDL_Surface *ret = newSurface(width, height, surf);
  600. switch(surf->format->BytesPerPixel)
  601. {
  602. case 2: scaleSurfaceInternal<2>(surf, ret); break;
  603. case 3: scaleSurfaceInternal<3>(surf, ret); break;
  604. case 4: scaleSurfaceInternal<4>(surf, ret); break;
  605. }
  606. return ret;
  607. }
  608. void CSDL_Ext::blitSurface( SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect )
  609. {
  610. if (dst != screen)
  611. {
  612. SDL_UpperBlit(src, srcRect, dst, dstRect);
  613. }
  614. else
  615. {
  616. SDL_Rect betterDst;
  617. if (dstRect)
  618. {
  619. betterDst = *dstRect;
  620. }
  621. else
  622. {
  623. betterDst = Rect(0, 0, dst->w, dst->h);
  624. }
  625. SDL_UpperBlit(src, srcRect, dst, &betterDst);
  626. }
  627. }
  628. void CSDL_Ext::fillRect( SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color )
  629. {
  630. SDL_Rect newRect;
  631. if (dstrect)
  632. {
  633. newRect = *dstrect;
  634. }
  635. else
  636. {
  637. newRect = Rect(0, 0, dst->w, dst->h);
  638. }
  639. SDL_FillRect(dst, &newRect, color);
  640. }
  641. void CSDL_Ext::fillRectBlack( SDL_Surface *dst, SDL_Rect *dstrect)
  642. {
  643. const Uint32 black = SDL_MapRGB(dst->format,0,0,0);
  644. fillRect(dst,dstrect,black);
  645. }
  646. void CSDL_Ext::fillTexture(SDL_Surface *dst, SDL_Surface * src)
  647. {
  648. SDL_Rect srcRect;
  649. SDL_Rect dstRect;
  650. SDL_GetClipRect(src, &srcRect);
  651. SDL_GetClipRect(dst, &dstRect);
  652. for (int y=dstRect.y; y < dstRect.y + dstRect.h; y+=srcRect.h)
  653. {
  654. for (int x=dstRect.x; x < dstRect.x + dstRect.w; x+=srcRect.w)
  655. {
  656. int xLeft = std::min<int>(srcRect.w, dstRect.x + dstRect.w - x);
  657. int yLeft = std::min<int>(srcRect.h, dstRect.y + dstRect.h - y);
  658. Rect currentDest(x, y, xLeft, yLeft);
  659. SDL_BlitSurface(src, &srcRect, dst, &currentDest);
  660. }
  661. }
  662. }
  663. SDL_Color CSDL_Ext::makeColor(ui8 r, ui8 g, ui8 b, ui8 a)
  664. {
  665. SDL_Color ret = {r, g, b, a};
  666. return ret;
  667. }
  668. void CSDL_Ext::startTextInput(SDL_Rect * where)
  669. {
  670. if (SDL_IsTextInputActive() == SDL_FALSE)
  671. {
  672. SDL_StartTextInput();
  673. }
  674. SDL_SetTextInputRect(where);
  675. }
  676. void CSDL_Ext::stopTextInput()
  677. {
  678. if (SDL_IsTextInputActive() == SDL_TRUE)
  679. {
  680. SDL_StopTextInput();
  681. }
  682. }
  683. STRONG_INLINE static uint32_t mapColor(SDL_Surface * surface, SDL_Color color)
  684. {
  685. return SDL_MapRGBA(surface->format, color.r, color.g, color.b, color.a);
  686. }
  687. void CSDL_Ext::setColorKey(SDL_Surface * surface, SDL_Color color)
  688. {
  689. uint32_t key = mapColor(surface,color);
  690. SDL_SetColorKey(surface, SDL_TRUE, key);
  691. }
  692. void CSDL_Ext::setDefaultColorKey(SDL_Surface * surface)
  693. {
  694. setColorKey(surface, Colors::DEFAULT_KEY_COLOR);
  695. }
  696. void CSDL_Ext::setDefaultColorKeyPresize(SDL_Surface * surface)
  697. {
  698. uint32_t key = mapColor(surface,Colors::DEFAULT_KEY_COLOR);
  699. auto & color = surface->format->palette->colors[key];
  700. // set color key only if exactly such color was found
  701. if (color.r == Colors::DEFAULT_KEY_COLOR.r && color.g == Colors::DEFAULT_KEY_COLOR.g && color.b == Colors::DEFAULT_KEY_COLOR.b)
  702. SDL_SetColorKey(surface, SDL_TRUE, key);
  703. }
  704. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<2>(int, int);
  705. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<3>(int, int);
  706. template SDL_Surface * CSDL_Ext::createSurfaceWithBpp<4>(int, int);