SDL_Extensions.cpp 28 KB

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