2
0

SDL_Extensions.cpp 29 KB

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