SDL_Extensions.cpp 28 KB

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