SDL_Extensions.cpp 27 KB

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