2
0

SDL_Extensions.cpp 34 KB

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