2
0

SDL_Extensions.cpp 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. #include "../stdafx.h"
  2. #include "SDL_Extensions.h"
  3. #include "SDL_ttf.h"
  4. #include "CGameInfo.h"
  5. #include <iostream>
  6. #include <utility>
  7. #include <algorithm>
  8. #include "CMessage.h"
  9. #include <boost/algorithm/string.hpp>
  10. #include "../hch/CDefHandler.h"
  11. #include <map>
  12. #include "Graphics.h"
  13. #include "GUIBase.h"
  14. /*
  15. * SDL_Extensions.cpp, part of VCMI engine
  16. *
  17. * Authors: listed in file AUTHORS in main folder
  18. *
  19. * License: GNU General Public License v2.0 or later
  20. * Full text of license available in license.txt file, in main folder
  21. *
  22. */
  23. template<int bpp, int incrementPtr>
  24. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  25. {
  26. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  27. }
  28. template<int bpp, int incrementPtr>
  29. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  30. {
  31. PutColor(ptr, Color.r, Color.g, Color.b);
  32. }
  33. template<int bpp, int incrementPtr>
  34. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  35. {
  36. switch (A)
  37. {
  38. case 255:
  39. ptr += bpp * incrementPtr;
  40. return;
  41. case 0:
  42. PutColor(ptr, R, G, B);
  43. return;
  44. case 128: // optimized
  45. PutColor(ptr, ((Uint16)R + (Uint16)ptr[2]) >> 1,
  46. ((Uint16)G + (Uint16)ptr[1]) >> 1,
  47. ((Uint16)B + (Uint16)ptr[0]) >> 1);
  48. return;
  49. default:
  50. PutColor(ptr, R, G, B, A);
  51. return;
  52. }
  53. }
  54. template<int bpp, int incrementPtr>
  55. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  56. {
  57. PutColor(ptr, (((Uint32)ptr[2]-(Uint32)R)*(Uint32)A) >> 8 + (Uint32)R,
  58. (((Uint32)ptr[1]-(Uint32)G)*(Uint32)A) >> 8 + (Uint32)G,
  59. (((Uint32)ptr[0]-(Uint32)B)*(Uint32)A) >> 8 + (Uint32)B);
  60. }
  61. template<int bpp, int incrementPtr>
  62. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  63. {
  64. if(incrementPtr == 0)
  65. {
  66. ptr[0] = B;
  67. ptr[1] = G;
  68. ptr[2] = R;
  69. }
  70. else if(incrementPtr == 1)
  71. {
  72. *ptr++ = B;
  73. *ptr++ = G;
  74. *ptr++ = R;
  75. if(bpp == 4)
  76. *ptr++ = 0;
  77. }
  78. else if(incrementPtr == -1)
  79. {
  80. if(bpp == 4)
  81. *(--ptr) = 0;
  82. *(--ptr) = R;
  83. *(--ptr) = G;
  84. *(--ptr) = B;
  85. }
  86. else
  87. {
  88. assert(0);
  89. }
  90. }
  91. template <int incrementPtr>
  92. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  93. {
  94. if(incrementPtr == -1)
  95. ptr -= 2;
  96. Uint16 * const px = (Uint16*)ptr;
  97. *px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
  98. if(incrementPtr == 1)
  99. ptr += 2; //bpp
  100. }
  101. template <int incrementPtr>
  102. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  103. {
  104. switch (A)
  105. {
  106. case 255:
  107. ptr += 2 * incrementPtr;
  108. return;
  109. case 0:
  110. PutColor(ptr, R, G, B);
  111. return;
  112. default:
  113. PutColor(ptr, R, G, B, A);
  114. return;
  115. }
  116. }
  117. template <int incrementPtr>
  118. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  119. {
  120. const int rbit = 5, gbit = 6, bbit = 5; //bits per color
  121. const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
  122. const int rshift = 11, gshift = 5, bshift = 0;
  123. const Uint8 r5 = (*((Uint16 *)ptr) & rmask) >> rshift,
  124. b5 = (*((Uint16 *)ptr) & bmask) >> bshift,
  125. g5 = (*((Uint16 *)ptr) & gmask) >> gshift;
  126. const Uint32 r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
  127. g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
  128. b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
  129. PutColor(ptr,
  130. (((r8-R)*A) >> 8) + R,
  131. (((g8-G)*A) >> 8) + G,
  132. (((b8-B)*A) >> 8) + B);
  133. }
  134. template <int incrementPtr>
  135. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  136. {
  137. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  138. }
  139. template <int incrementPtr>
  140. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  141. {
  142. PutColor(ptr, Color.r, Color.g, Color.b);
  143. }
  144. SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  145. {
  146. return SDL_CreateRGBSurface(mod->flags,w,h,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  147. }
  148. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  149. {
  150. //return SDL_DisplayFormat(mod);
  151. return SDL_ConvertSurface(mod, mod->format, mod->flags);
  152. }
  153. bool isItIn(const SDL_Rect * rect, int x, int y)
  154. {
  155. if ((x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h))
  156. return true;
  157. else return false;
  158. }
  159. void blitAtWR(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  160. {
  161. SDL_Rect pom = genRect(src->h,src->w,x,y);
  162. SDL_BlitSurface(src,NULL,dst,&pom);
  163. SDL_UpdateRect(dst,x,y,src->w,src->h);
  164. }
  165. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  166. {
  167. if(!dst) dst = screen;
  168. SDL_Rect pom = genRect(src->h,src->w,x,y);
  169. SDL_BlitSurface(src,NULL,dst,&pom);
  170. }
  171. void blitAtWR(SDL_Surface * src, const SDL_Rect & pos, SDL_Surface * dst)
  172. {
  173. blitAtWR(src,pos.x,pos.y,dst);
  174. }
  175. void blitAt(SDL_Surface * src, const SDL_Rect & pos, SDL_Surface * dst)
  176. {
  177. blitAt(src,pos.x,pos.y,dst);
  178. }
  179. SDL_Color genRGB(int r, int g, int b, int a=0)
  180. {
  181. SDL_Color ret;
  182. ret.b=b;
  183. ret.g=g;
  184. ret.r=r;
  185. ret.unused=a;
  186. return ret;
  187. }
  188. void updateRect (SDL_Rect * rect, SDL_Surface * scr)
  189. {
  190. SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
  191. }
  192. void printAtMiddleWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  193. {
  194. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  195. std::vector<SDL_Surface*> wesu;
  196. wesu.resize(ws.size());
  197. for (size_t i=0; i < wesu.size(); ++i)
  198. {
  199. wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
  200. }
  201. int tox=0, toy=0;
  202. for (size_t i=0; i < wesu.size(); ++i)
  203. {
  204. toy+=wesu[i]->h;
  205. if (tox < wesu[i]->w)
  206. tox=wesu[i]->w;
  207. }
  208. int evx, evy = y - (toy/2);
  209. for (size_t i=0; i < wesu.size(); ++i)
  210. {
  211. evx = (x - (tox/2)) + ((tox-wesu[i]->w)/2);
  212. blitAt(wesu[i],evx,evy,dst);
  213. evy+=wesu[i]->h;
  214. }
  215. for (size_t i=0; i < wesu.size(); ++i)
  216. SDL_FreeSurface(wesu[i]);
  217. }
  218. void printAtWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  219. {
  220. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  221. std::vector<SDL_Surface*> wesu;
  222. wesu.resize(ws.size());
  223. for (size_t i=0; i < wesu.size(); ++i)
  224. wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
  225. int evy = y;
  226. for (size_t i=0; i < wesu.size(); ++i)
  227. {
  228. blitAt(wesu[i],x,evy,dst);
  229. evy+=wesu[i]->h;
  230. }
  231. for (size_t i=0; i < wesu.size(); ++i)
  232. SDL_FreeSurface(wesu[i]);
  233. }
  234. void CSDL_Ext::printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst, bool refresh)
  235. {
  236. if (graphics->fontsTrueType[font])
  237. {
  238. printAtWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
  239. return;
  240. }
  241. const Font *f = graphics->fonts[font];
  242. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  243. int cury = y;
  244. for (size_t i=0; i < ws.size(); ++i)
  245. {
  246. printAt(ws[i], x, cury, font, kolor, dst, refresh);
  247. cury += f->height;
  248. }
  249. }
  250. void CSDL_Ext::printAtMiddleWB( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor/*=tytulowy*/, SDL_Surface * dst/*=screen*/, bool refrsh /*= false*/ )
  251. {
  252. if (graphics->fontsTrueType[font])
  253. {
  254. printAtMiddleWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
  255. return;
  256. }
  257. const Font *f = graphics->fonts[font];
  258. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  259. int totalHeight = ws.size() * f->height;
  260. int cury = y - totalHeight/2;
  261. for (size_t i=0; i < ws.size(); ++i)
  262. {
  263. printAt(ws[i], x - f->getWidth(ws[i].c_str())/2, cury, font, kolor, dst, refrsh);
  264. cury += f->height;
  265. }
  266. }
  267. void printAtMiddle(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality=2, bool refresh=false)
  268. {
  269. if(text.length()==0) return;
  270. SDL_Surface * temp;
  271. switch (quality)
  272. {
  273. case 0:
  274. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  275. break;
  276. case 1:
  277. SDL_Color tem;
  278. tem.b = 0xff-kolor.b;
  279. tem.g = 0xff-kolor.g;
  280. tem.r = 0xff-kolor.r;
  281. tem.unused = 0xff-kolor.unused;
  282. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  283. break;
  284. case 2:
  285. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  286. break;
  287. default:
  288. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  289. break;
  290. }
  291. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-(temp->w/2),y-(temp->h/2)));
  292. if(refresh)
  293. SDL_UpdateRect(dst,x-(temp->w/2),y-(temp->h/2),temp->w,temp->h);
  294. SDL_FreeSurface(temp);
  295. }
  296. void CSDL_Ext::printAtMiddle( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  297. {
  298. if (graphics->fontsTrueType[font])
  299. {
  300. printAtMiddle(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  301. return;
  302. }
  303. const Font *f = graphics->fonts[font];
  304. int nx = x - f->getWidth(text.c_str())/2,
  305. ny = y - f->height/2;
  306. printAt(text, nx, ny, font, kolor, dst, refresh);
  307. }
  308. void printAt(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality=2, bool refresh=false)
  309. {
  310. if (text.length()==0)
  311. return;
  312. SDL_Surface * temp;
  313. switch (quality)
  314. {
  315. case 0:
  316. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  317. break;
  318. case 1:
  319. SDL_Color tem;
  320. tem.b = 0xff-kolor.b;
  321. tem.g = 0xff-kolor.g;
  322. tem.r = 0xff-kolor.r;
  323. tem.unused = 0xff-kolor.unused;
  324. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  325. break;
  326. case 2:
  327. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  328. break;
  329. default:
  330. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  331. break;
  332. }
  333. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x,y));
  334. if(refresh)
  335. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  336. SDL_FreeSurface(temp);
  337. }
  338. void CSDL_Ext::printAt( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  339. {
  340. if(!text.size())
  341. return;
  342. if (graphics->fontsTrueType[font])
  343. {
  344. printAt(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  345. return;
  346. }
  347. assert(dst);
  348. assert(font < Graphics::FONTS_NUMBER);
  349. //assume BGR dst surface, TODO - make it general in a tidy way
  350. assert(dst->format->Rshift > dst->format->Gshift);
  351. assert(dst->format->Gshift > dst->format->Bshift);
  352. const Font *f = graphics->fonts[font];
  353. const Uint8 bpp = dst->format->BytesPerPixel;
  354. Uint8 *px = NULL;
  355. Uint8 *src = NULL;
  356. TColorPutter colorPutter = getPutterFor(dst, false);
  357. //if text is in {} braces, we'll ommit them
  358. const int first = (text[0] == '{' ? 1 : 0);
  359. const int beyondEnd = (text[text.size()-1] == '}' ? text.size()-1 : text.size());
  360. for(int txti = first; txti < beyondEnd; txti++)
  361. {
  362. const unsigned char c = text[txti];
  363. x += f->chars[c].unknown1;
  364. for(int i = 0; i < f->height && (y + i) < (dst->h - 1); i++)
  365. {
  366. px = (Uint8*)dst->pixels;
  367. px += (y+i) * dst->pitch + x * bpp;
  368. src = f->chars[c].pixels;
  369. src += i * f->chars[c].width;//if we have reached end of surface in previous line
  370. for(int j = 0; j < f->chars[c].width && (j + x) < (dst->w - 1); j++)
  371. {
  372. switch(*src)
  373. {
  374. case 1: //black "shadow"
  375. memset(px, 0, bpp);
  376. break;
  377. case 255: //text colour
  378. colorPutter(px, kolor.r, kolor.g, kolor.b);
  379. break;
  380. }
  381. src++;
  382. px += bpp;
  383. }
  384. }
  385. x += f->chars[c].width;
  386. x += f->chars[c].unknown2;
  387. }
  388. if(refresh)
  389. {
  390. SDL_UpdateRect(dst, x, y, f->getWidth(text.c_str()), f->height);
  391. }
  392. }
  393. void printTo(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality=2)
  394. {
  395. if (text.length()==0)
  396. return;
  397. SDL_Surface * temp;
  398. switch (quality)
  399. {
  400. case 0:
  401. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  402. break;
  403. case 1:
  404. SDL_Color tem;
  405. tem.b = 0xff-kolor.b;
  406. tem.g = 0xff-kolor.g;
  407. tem.r = 0xff-kolor.r;
  408. tem.unused = 0xff-kolor.unused;
  409. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  410. break;
  411. case 2:
  412. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  413. break;
  414. default:
  415. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  416. break;
  417. }
  418. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-temp->w,y-temp->h));
  419. SDL_UpdateRect(dst,x-temp->w,y-temp->h,temp->w,temp->h);
  420. SDL_FreeSurface(temp);
  421. }
  422. void CSDL_Ext::printTo( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/, bool refresh /*= false*/ )
  423. {
  424. if (graphics->fontsTrueType[font])
  425. {
  426. printTo(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  427. return;
  428. }
  429. const Font *f = graphics->fonts[font];
  430. printAt(text, x - f->getWidth(text.c_str()), y - f->height, font, kolor, dst, refresh);
  431. }
  432. void printToWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality=2)
  433. {
  434. if (text.length()==0)
  435. return;
  436. SDL_Surface * temp;
  437. switch (quality)
  438. {
  439. case 0:
  440. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  441. break;
  442. case 1:
  443. SDL_Color tem;
  444. tem.b = 0xff-kolor.b;
  445. tem.g = 0xff-kolor.g;
  446. tem.r = 0xff-kolor.r;
  447. tem.unused = 0xff-kolor.unused;
  448. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  449. break;
  450. case 2:
  451. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  452. break;
  453. default:
  454. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  455. break;
  456. }
  457. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-temp->w,y-temp->h));
  458. SDL_FreeSurface(temp);
  459. }
  460. void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A)
  461. {
  462. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel;
  463. p[0] = B;
  464. p[1] = G;
  465. p[2] = R;
  466. if(ekran->format->BytesPerPixel==4)
  467. p[3] = A;
  468. SDL_UpdateRect(ekran, x, y, 1, 1);
  469. }
  470. // Vertical flip
  471. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
  472. {
  473. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  474. const int bpl = ret->pitch;
  475. const int bpp = ret->format->BytesPerPixel;
  476. for(int i=0; i<ret->h; i++) {
  477. char *src = (char *)toRot->pixels + i*bpl;
  478. char *dst = (char *)ret->pixels + i*bpl;
  479. for(int j=0; j<ret->w; j++) {
  480. for (int k=0; k<bpp; k++) {
  481. dst[j*bpp + k] = src[(ret->w-j-1)*bpp + k];
  482. }
  483. }
  484. }
  485. return ret;
  486. }
  487. // Horizontal flip
  488. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  489. {
  490. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  491. int bpl = ret->pitch;
  492. for(int i=0; i<ret->h; i++) {
  493. memcpy((char *)ret->pixels + i*bpl, (char *)toRot->pixels + (ret->h-i-1)*bpl, bpl);
  494. }
  495. return ret;
  496. };
  497. ///**************/
  498. ///Rotates toRot surface by 90 degrees left
  499. ///**************/
  500. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  501. {
  502. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  503. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  504. for(int i=0; i<ret->w; ++i)
  505. {
  506. for(int j=0; j<ret->h; ++j)
  507. {
  508. {
  509. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  510. /*
  511. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  512. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2]);
  513. #else
  514. */
  515. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0]);
  516. //#endif
  517. }
  518. }
  519. }
  520. return ret;
  521. }
  522. ///*************/
  523. ///Rotates toRot surface by 180 degrees
  524. ///*************/
  525. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  526. {
  527. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  528. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  529. if(ret->format->BytesPerPixel!=1)
  530. {
  531. for(int i=0; i<ret->w; ++i)
  532. {
  533. for(int j=0; j<ret->h; ++j)
  534. {
  535. {
  536. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  537. /*
  538. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  539. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2], 0);
  540. #else
  541. */
  542. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0], 0);
  543. //#endif
  544. }
  545. }
  546. }
  547. }
  548. else
  549. {
  550. for(int i=0; i<ret->w; ++i)
  551. {
  552. for(int j=0; j<ret->h; ++j)
  553. {
  554. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  555. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  556. }
  557. }
  558. }
  559. return ret;
  560. }
  561. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  562. {
  563. int bpp = surface->format->BytesPerPixel;
  564. /* Here p is the address to the pixel we want to retrieve */
  565. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  566. switch(bpp)
  567. {
  568. case 1:
  569. if(colorByte)
  570. {
  571. return colorToUint32(surface->format->palette->colors+(*p));
  572. }
  573. else
  574. return *p;
  575. case 2:
  576. return *(Uint16 *)p;
  577. case 3:
  578. /*
  579. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  580. return p[0] << 16 | p[1] << 8 | p[2];
  581. #else
  582. */
  583. return p[0] | p[1] << 8 | p[2] << 16;
  584. //#endif
  585. case 4:
  586. return *(Uint32 *)p;
  587. default:
  588. return 0; // shouldn't happen, but avoids warnings
  589. }
  590. }
  591. void CSDL_Ext::alphaTransform(SDL_Surface *src)
  592. {
  593. assert(src->format->BitsPerPixel == 8);
  594. SDL_Color colors[] = {{0,0,0,255}, {0,0,0,214}, {0,0,0,164}, {0,0,0,82}, {0,0,0,128},
  595. {255,0,0,0}, {255,0,0,0}, {255,0,0,0}, {0,0,0,192}, {0,0,0,192}};
  596. SDL_SetColors(src, colors, 0, ARRAY_COUNT(colors));
  597. SDL_SetColorKey(src, SDL_SRCCOLORKEY, SDL_MapRGBA(src->format, 0, 0, 0, 255));
  598. }
  599. // <=>
  600. static void prepareOutRect(SDL_Rect *src, SDL_Rect *dst, const SDL_Rect & clip_rect)
  601. {
  602. const int xoffset = std::max(clip_rect.x - dst->x, 0),
  603. yoffset = std::max(clip_rect.y - dst->y, 0);
  604. src->x += xoffset;
  605. src->y += yoffset;
  606. dst->x += xoffset;
  607. dst->y += yoffset;
  608. src->w = dst->w = std::max(0,std::min(dst->w - xoffset, clip_rect.x + clip_rect.w - dst->x));
  609. src->h = dst->h = std::max(0,std::min(dst->h - yoffset, clip_rect.y + clip_rect.h - dst->y));
  610. }
  611. template<int bpp>
  612. 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
  613. {
  614. static void (*blitWithRotate[])(const SDL_Surface *, const SDL_Rect *, SDL_Surface *, const SDL_Rect *) = {blitWithRotate1<bpp>, blitWithRotate2<bpp>, blitWithRotate3<bpp>};
  615. if(!rotation)
  616. {
  617. SDL_BlitSurface(src, srcRect, dst, dstRect);
  618. }
  619. else
  620. {
  621. prepareOutRect(srcRect, dstRect, dst->clip_rect);
  622. blitWithRotate[rotation-1](src, srcRect, dst, dstRect);
  623. }
  624. }
  625. template<int bpp>
  626. void CSDL_Ext::blitWithRotateClipVal( SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation )
  627. {
  628. blitWithRotateClip<bpp>(src, &srcRect, dst, &dstRect, rotation);
  629. }
  630. template<int bpp>
  631. 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
  632. {
  633. static void (*blitWithRotate[])(const SDL_Surface *, const SDL_Rect *, SDL_Surface *, const SDL_Rect *) = {blitWithRotate1WithAlpha<bpp>, blitWithRotate2WithAlpha<bpp>, blitWithRotate3WithAlpha<bpp>};
  634. if(!rotation)
  635. {
  636. blit8bppAlphaTo24bpp(src, srcRect, dst, dstRect);
  637. }
  638. else
  639. {
  640. prepareOutRect(srcRect, dstRect, dst->clip_rect);
  641. blitWithRotate[rotation-1](src, srcRect, dst, dstRect);
  642. }
  643. }
  644. template<int bpp>
  645. void CSDL_Ext::blitWithRotateClipValWithAlpha( SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation )
  646. {
  647. blitWithRotateClipWithAlpha<bpp>(src, &srcRect, dst, &dstRect, rotation);
  648. }
  649. template<int bpp>
  650. 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
  651. {
  652. Uint8 *sp = getPxPtr(src, src->w - srcRect->w - srcRect->x, srcRect->y);
  653. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  654. const SDL_Color * const colors = src->format->palette->colors;
  655. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  656. {
  657. Uint8 *dp = dporg;
  658. for(int j=dstRect->w; j>0; j--, sp++)
  659. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  660. sp += src->w - dstRect->w;
  661. }
  662. }
  663. template<int bpp>
  664. 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
  665. {
  666. Uint8 *sp = getPxPtr(src, srcRect->x, src->h - srcRect->h - srcRect->y);
  667. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  668. const SDL_Color * const colors = src->format->palette->colors;
  669. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  670. {
  671. Uint8 *dp = dporg;
  672. for(int j=dstRect->w; j>0; j--, sp++)
  673. ColorPutter<bpp, 1>::PutColor(dp, colors[*sp]);
  674. sp += src->w - dstRect->w;
  675. }
  676. }
  677. template<int bpp>
  678. 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
  679. {
  680. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + (src->w - srcRect->w - srcRect->x);
  681. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  682. const SDL_Color * const colors = src->format->palette->colors;
  683. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  684. {
  685. Uint8 *dp = dporg;
  686. for(int j=dstRect->w; j>0; j--, sp++)
  687. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  688. sp += src->w - dstRect->w;
  689. }
  690. }
  691. template<int bpp>
  692. 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
  693. {
  694. Uint8 *sp = (Uint8 *)src->pixels + srcRect->y*src->pitch + (src->w - srcRect->w - srcRect->x);
  695. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  696. const SDL_Color * const colors = src->format->palette->colors;
  697. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  698. {
  699. Uint8 *dp = dporg;
  700. for(int j=dstRect->w; j>0; j--, sp++)
  701. {
  702. if(*sp)
  703. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  704. else
  705. dp -= bpp;
  706. }
  707. sp += src->w - dstRect->w;
  708. }
  709. }
  710. template<int bpp>
  711. 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
  712. {
  713. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + srcRect->x;
  714. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  715. const SDL_Color * const colors = src->format->palette->colors;
  716. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  717. {
  718. Uint8 *dp = dporg;
  719. for(int j=dstRect->w; j>0; j--, sp++)
  720. {
  721. if(*sp)
  722. ColorPutter<bpp, 1>::PutColor(dp, colors[*sp]);
  723. else
  724. dp += bpp;
  725. }
  726. sp += src->w - dstRect->w;
  727. }
  728. }
  729. template<int bpp>
  730. 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
  731. {
  732. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + (src->w - srcRect->w - srcRect->x);
  733. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  734. const SDL_Color * const colors = src->format->palette->colors;
  735. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  736. {
  737. Uint8 *dp = dporg;
  738. for(int j=dstRect->w; j>0; j--, sp++)
  739. {
  740. if(*sp)
  741. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  742. else
  743. dp -= bpp;
  744. }
  745. sp += src->w - dstRect->w;
  746. }
  747. }
  748. template<int bpp>
  749. int CSDL_Ext::blit8bppAlphaTo24bppT(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  750. {
  751. if (src && src->format->BytesPerPixel==1 && dst && (bpp==3 || bpp==4 || bpp==2)) //everything's ok
  752. {
  753. SDL_Rect fulldst;
  754. int srcx, srcy, w, h;
  755. /* Make sure the surfaces aren't locked */
  756. if ( ! src || ! dst )
  757. {
  758. SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  759. return -1;
  760. }
  761. if ( src->locked || dst->locked )
  762. {
  763. SDL_SetError("Surfaces must not be locked during blit");
  764. return -1;
  765. }
  766. /* If the destination rectangle is NULL, use the entire dest surface */
  767. if ( dstRect == NULL )
  768. {
  769. fulldst.x = fulldst.y = 0;
  770. dstRect = &fulldst;
  771. }
  772. /* clip the source rectangle to the source surface */
  773. if(srcRect)
  774. {
  775. int maxw, maxh;
  776. srcx = srcRect->x;
  777. w = srcRect->w;
  778. if(srcx < 0)
  779. {
  780. w += srcx;
  781. dstRect->x -= srcx;
  782. srcx = 0;
  783. }
  784. maxw = src->w - srcx;
  785. if(maxw < w)
  786. w = maxw;
  787. srcy = srcRect->y;
  788. h = srcRect->h;
  789. if(srcy < 0)
  790. {
  791. h += srcy;
  792. dstRect->y -= srcy;
  793. srcy = 0;
  794. }
  795. maxh = src->h - srcy;
  796. if(maxh < h)
  797. h = maxh;
  798. }
  799. else
  800. {
  801. srcx = srcy = 0;
  802. w = src->w;
  803. h = src->h;
  804. }
  805. /* clip the destination rectangle against the clip rectangle */
  806. {
  807. SDL_Rect *clip = &dst->clip_rect;
  808. int dx, dy;
  809. dx = clip->x - dstRect->x;
  810. if(dx > 0)
  811. {
  812. w -= dx;
  813. dstRect->x += dx;
  814. srcx += dx;
  815. }
  816. dx = dstRect->x + w - clip->x - clip->w;
  817. if(dx > 0)
  818. w -= dx;
  819. dy = clip->y - dstRect->y;
  820. if(dy > 0)
  821. {
  822. h -= dy;
  823. dstRect->y += dy;
  824. srcy += dy;
  825. }
  826. dy = dstRect->y + h - clip->y - clip->h;
  827. if(dy > 0)
  828. h -= dy;
  829. }
  830. if(w > 0 && h > 0)
  831. {
  832. dstRect->w = w;
  833. dstRect->h = h;
  834. if(SDL_LockSurface(dst))
  835. return -1; //if we cannot lock the surface
  836. const SDL_Color *colors = src->format->palette->colors;
  837. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  838. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  839. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  840. {
  841. Uint8 *color = colory;
  842. Uint8 *p = py;
  843. for(int x = w; x; x--)
  844. {
  845. const SDL_Color &tbc = colors[*color++]; //color to blit
  846. ColorPutter<bpp, +1>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, tbc.unused);
  847. }
  848. }
  849. SDL_UnlockSurface(dst);
  850. }
  851. }
  852. return 0;
  853. }
  854. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  855. {
  856. switch(dst->format->BytesPerPixel)
  857. {
  858. case 2: return blit8bppAlphaTo24bppT<2>(src, srcRect, dst, dstRect);
  859. case 3: return blit8bppAlphaTo24bppT<3>(src, srcRect, dst, dstRect);
  860. case 4: return blit8bppAlphaTo24bppT<4>(src, srcRect, dst, dstRect);
  861. default:
  862. tlog1 << (int)dst->format->BitsPerPixel << " bpp is not supported!!!\n";
  863. return -1;
  864. }
  865. }
  866. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  867. {
  868. Uint32 ret = 0;
  869. ret+=color->unused;
  870. ret<<=8; //*=256
  871. ret+=color->b;
  872. ret<<=8; //*=256
  873. ret+=color->g;
  874. ret<<=8; //*=256
  875. ret+=color->r;
  876. return ret;
  877. }
  878. void CSDL_Ext::update(SDL_Surface * what)
  879. {
  880. if(what)
  881. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  882. }
  883. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color)
  884. {
  885. for(int i=0;i<w;i++)
  886. {
  887. SDL_PutPixelWithoutRefresh(sur,x+i,y,color.x,color.y,color.z);
  888. SDL_PutPixelWithoutRefresh(sur,x+i,y+h-1,color.x,color.y,color.z);
  889. }
  890. for(int i=0; i<h;i++)
  891. {
  892. SDL_PutPixelWithoutRefresh(sur,x,y+i,color.x,color.y,color.z);
  893. SDL_PutPixelWithoutRefresh(sur,x+w-1,y+i,color.x,color.y,color.z);
  894. }
  895. }
  896. void CSDL_Ext::drawBorder( SDL_Surface * sur, const SDL_Rect &r, const int3 &color )
  897. {
  898. drawBorder(sur, r.x, r.y, r.w, r.h, color);
  899. }
  900. void CSDL_Ext::drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color)
  901. {
  902. const int y1 = r.y, y2 = r.y + r.h-1;
  903. for (int i=0; i<r.w; i++)
  904. {
  905. const int x = r.x + i;
  906. if (i%4 || (i==0))
  907. {
  908. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y1, color.x, color.y, color.z);
  909. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y2, color.x, color.y, color.z);
  910. }
  911. }
  912. const int x1 = r.x, x2 = r.x + r.w-1;
  913. for (int i=0; i<r.h; i++)
  914. {
  915. const int y = r.y + i;
  916. if ((i%4) || (i==0))
  917. {
  918. SDL_PutPixelWithoutRefreshIfInSurf(sur, x1, y, color.x, color.y, color.z);
  919. SDL_PutPixelWithoutRefreshIfInSurf(sur, x2, y, color.x, color.y, color.z);
  920. }
  921. }
  922. }
  923. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, unsigned char player)
  924. {
  925. if(player==254)
  926. return;
  927. if(sur->format->BitsPerPixel==8)
  928. {
  929. SDL_Color *color = (player == 255
  930. ? graphics->neutralColor
  931. : &graphics->playerColors[player]);
  932. SDL_SetColors(sur, color, 5, 1);
  933. }
  934. else
  935. tlog3 << "Warning, setPlayerColor called on not 8bpp surface!\n";
  936. }
  937. int readNormalNr (std::istream &in, int bytCon)
  938. {
  939. int ret=0;
  940. int amp=1;
  941. unsigned char byte;
  942. if (in.good())
  943. {
  944. for (int i=0; i<bytCon; i++)
  945. {
  946. in.read((char*)&byte,1);
  947. ret+=byte*amp;
  948. amp<<=8;
  949. }
  950. }
  951. else return -1;
  952. return ret;
  953. }
  954. const TColorPutter CSDL_Ext::getPutterFor(SDL_Surface * const &dest, int incrementing)
  955. {
  956. #define CASE_BPP(BytesPerPixel) \
  957. case BytesPerPixel: \
  958. if(incrementing > 0) \
  959. return ColorPutter<BytesPerPixel, 1>::PutColor; \
  960. else if(incrementing == 0) \
  961. return ColorPutter<BytesPerPixel, 0>::PutColor; \
  962. else \
  963. return ColorPutter<BytesPerPixel, -1>::PutColor;\
  964. break;
  965. switch(dest->format->BytesPerPixel)
  966. {
  967. CASE_BPP(2)
  968. CASE_BPP(3)
  969. CASE_BPP(4)
  970. default:
  971. tlog1 << (int)dest->format->BitsPerPixel << "bpp is not supported!\n";
  972. return NULL;
  973. }
  974. }
  975. const TColorPutterAlpha CSDL_Ext::getPutterAlphaFor(SDL_Surface * const &dest, int incrementing)
  976. {
  977. switch(dest->format->BytesPerPixel)
  978. {
  979. CASE_BPP(2)
  980. CASE_BPP(3)
  981. CASE_BPP(4)
  982. default:
  983. tlog1 << (int)dest->format->BitsPerPixel << "bpp is not supported!\n";
  984. return NULL;
  985. }
  986. #undef CASE_BPP
  987. }
  988. Uint8 * CSDL_Ext::getPxPtr(const SDL_Surface * const &srf, const int & x, const int & y)
  989. {
  990. return (Uint8 *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel;
  991. }
  992. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  993. {
  994. for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
  995. {
  996. boost::replace_first(str,"%s",tor[i]);
  997. }
  998. return str;
  999. }
  1000. bool CSDL_Ext::isTransparent( SDL_Surface * srf, int x, int y )
  1001. {
  1002. if(srf->format->BytesPerPixel == 1)
  1003. {
  1004. return ((ui8*)srf->pixels)[x + srf->pitch * y] == 0;
  1005. }
  1006. else
  1007. {
  1008. assert(!"isTransparent called with non-8bpp surface!");
  1009. }
  1010. return false;
  1011. }
  1012. void CSDL_Ext::VflipSurf(SDL_Surface * surf)
  1013. {
  1014. char buf[4]; //buffer
  1015. int bpp = surf->format->BytesPerPixel;
  1016. for (int y=0; y<surf->h; ++y)
  1017. {
  1018. char * base = (char*)surf->pixels + y * surf->pitch;
  1019. for (int x=0; x<surf->w/2; ++x)
  1020. {
  1021. memcpy(buf, base + x * bpp, bpp);
  1022. memcpy(base + x * bpp, base + (surf->w - x - 1) * bpp, bpp);
  1023. memcpy(base + (surf->w - x - 1) * bpp, buf, bpp);
  1024. }
  1025. }
  1026. }
  1027. 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*/)
  1028. {
  1029. Uint8 *p = getPxPtr(ekran, x, y);
  1030. getPutterFor(ekran, false)(p, R, G, B);
  1031. //needed?
  1032. if(ekran->format->BytesPerPixel==4)
  1033. p[3] = A;
  1034. }
  1035. 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*/)
  1036. {
  1037. if(x >= 0 && x < ekran->w && y >= 0 && y < ekran->w)
  1038. SDL_PutPixelWithoutRefresh(ekran, x, y, R, G, B, A);
  1039. }
  1040. BlitterWithRotationVal CSDL_Ext::getBlitterWithRotation(SDL_Surface *dest)
  1041. {
  1042. switch(dest->format->BytesPerPixel)
  1043. {
  1044. case 2: return blitWithRotateClipVal<2>;
  1045. case 3: return blitWithRotateClipVal<3>;
  1046. case 4: return blitWithRotateClipVal<4>;
  1047. default:
  1048. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  1049. break;
  1050. }
  1051. assert(0);
  1052. return NULL;
  1053. }
  1054. BlitterWithRotationVal CSDL_Ext::getBlitterWithRotationAndAlpha(SDL_Surface *dest)
  1055. {
  1056. switch(dest->format->BytesPerPixel)
  1057. {
  1058. case 2: return blitWithRotateClipValWithAlpha<2>;
  1059. case 3: return blitWithRotateClipValWithAlpha<3>;
  1060. case 4: return blitWithRotateClipValWithAlpha<4>;
  1061. default:
  1062. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  1063. break;
  1064. }
  1065. assert(0);
  1066. return NULL;
  1067. }
  1068. void CSDL_Ext::applyEffect( SDL_Surface * surf, const SDL_Rect * rect, int mode )
  1069. {
  1070. switch(mode)
  1071. {
  1072. case 0: //sepia
  1073. {
  1074. const int sepiaDepth = 20;
  1075. const int sepiaIntensity = 30;
  1076. for(int xp = rect->x; xp < rect->x + rect->w; ++xp)
  1077. {
  1078. for(int yp = rect->y; yp < rect->y + rect->h; ++yp)
  1079. {
  1080. unsigned char * pixels = (unsigned char*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  1081. int b = pixels[0];
  1082. int g = pixels[1];
  1083. int r = pixels[2];
  1084. int gry = (r + g + b) / 3;
  1085. r = g = b = gry;
  1086. r = r + (sepiaDepth * 2);
  1087. g = g + sepiaDepth;
  1088. if (r>255) r=255;
  1089. if (g>255) g=255;
  1090. if (b>255) b=255;
  1091. // Darken blue color to increase sepia effect
  1092. b -= sepiaIntensity;
  1093. // normalize if out of bounds
  1094. if (b<0) b=0;
  1095. if (b>255) b=255;
  1096. pixels[0] = b;
  1097. pixels[1] = g;
  1098. pixels[2] = r;
  1099. }
  1100. }
  1101. }
  1102. break;
  1103. case 1: //grayscale
  1104. {
  1105. for(int xp = rect->x; xp < rect->x + rect->w; ++xp)
  1106. {
  1107. for(int yp = rect->y; yp < rect->y + rect->h; ++yp)
  1108. {
  1109. unsigned char * pixels = (unsigned char*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  1110. int b = pixels[0];
  1111. int g = pixels[1];
  1112. int r = pixels[2];
  1113. int gry = (r + g + b) / 3;
  1114. pixels[0] = pixels[1] = pixels[2] = gry;
  1115. }
  1116. }
  1117. }
  1118. break;
  1119. default:
  1120. throw std::string("Unsuppoerted efftct!");
  1121. }
  1122. }
  1123. SDL_Surface * CSDL_Ext::std32bppSurface = NULL;