2
0

SDL_Extensions.cpp 30 KB

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