SDL_Extensions.cpp 28 KB

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