SDL_Extensions.cpp 30 KB

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