SDL_Extensions.cpp 29 KB

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