SDL_Extensions.cpp 28 KB

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