SDL_Extensions.cpp 34 KB

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