SDL_Extensions.cpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305
  1. #include "StdInc.h"
  2. #include "SDL_Extensions.h"
  3. #include "SDL_ttf.h"
  4. #include "CGameInfo.h"
  5. #include "CMessage.h"
  6. #include "CDefHandler.h"
  7. #include "Graphics.h"
  8. #include "GUIBase.h"
  9. /*
  10. * SDL_Extensions.cpp, part of VCMI engine
  11. *
  12. * Authors: listed in file AUTHORS in main folder
  13. *
  14. * License: GNU General Public License v2.0 or later
  15. * Full text of license available in license.txt file, in main folder
  16. *
  17. */
  18. template<int bpp, int incrementPtr>
  19. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  20. {
  21. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  22. }
  23. template<int bpp, int incrementPtr>
  24. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  25. {
  26. PutColor(ptr, Color.r, Color.g, Color.b);
  27. }
  28. template<int bpp, int incrementPtr>
  29. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  30. {
  31. switch (A)
  32. {
  33. case 255:
  34. ptr += bpp * incrementPtr;
  35. return;
  36. case 0:
  37. PutColor(ptr, R, G, B);
  38. return;
  39. case 128: // optimized
  40. PutColor(ptr, ((Uint16)R + (Uint16)ptr[2]) >> 1,
  41. ((Uint16)G + (Uint16)ptr[1]) >> 1,
  42. ((Uint16)B + (Uint16)ptr[0]) >> 1);
  43. return;
  44. default:
  45. PutColor(ptr, R, G, B, A);
  46. return;
  47. }
  48. }
  49. template<int bpp, int incrementPtr>
  50. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  51. {
  52. PutColor(ptr, ((((Uint32)ptr[2]-(Uint32)R)*(Uint32)A) >> 8 ) + (Uint32)R,
  53. ((((Uint32)ptr[1]-(Uint32)G)*(Uint32)A) >> 8 ) + (Uint32)G,
  54. ((((Uint32)ptr[0]-(Uint32)B)*(Uint32)A) >> 8 ) + (Uint32)B);
  55. }
  56. template<int bpp, int incrementPtr>
  57. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  58. {
  59. if(incrementPtr == 0)
  60. {
  61. ptr[0] = B;
  62. ptr[1] = G;
  63. ptr[2] = R;
  64. }
  65. else if(incrementPtr == 1)
  66. {
  67. *ptr++ = B;
  68. *ptr++ = G;
  69. *ptr++ = R;
  70. if(bpp == 4)
  71. *ptr++ = 0;
  72. }
  73. else if(incrementPtr == -1)
  74. {
  75. if(bpp == 4)
  76. *(--ptr) = 0;
  77. *(--ptr) = R;
  78. *(--ptr) = G;
  79. *(--ptr) = B;
  80. }
  81. else
  82. {
  83. assert(0);
  84. }
  85. }
  86. template<int bpp, int incrementPtr>
  87. STRONG_INLINE void ColorPutter<bpp, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  88. {
  89. Uint32 pixel = ((Uint32)Color.b << 0 ) + ((Uint32)Color.g << 8) + ((Uint32)Color.r << 16);
  90. for (size_t i=0; i<count; i++)
  91. {
  92. memcpy(ptr, &pixel, bpp);
  93. if(incrementPtr == -1)
  94. ptr -= bpp;
  95. if(incrementPtr == 1)
  96. ptr += bpp;
  97. }
  98. }
  99. template <int incrementPtr>
  100. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B)
  101. {
  102. if(incrementPtr == -1)
  103. ptr -= 2;
  104. Uint16 * const px = (Uint16*)ptr;
  105. *px = (B>>3) + ((G>>2) << 5) + ((R>>3) << 11); //drop least significant bits of 24 bpp encoded color
  106. if(incrementPtr == 1)
  107. ptr += 2; //bpp
  108. }
  109. template <int incrementPtr>
  110. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlphaSwitch(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  111. {
  112. switch (A)
  113. {
  114. case 255:
  115. ptr += 2 * incrementPtr;
  116. return;
  117. case 0:
  118. PutColor(ptr, R, G, B);
  119. return;
  120. default:
  121. PutColor(ptr, R, G, B, A);
  122. return;
  123. }
  124. }
  125. template <int incrementPtr>
  126. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const Uint8 & R, const Uint8 & G, const Uint8 & B, const Uint8 & A)
  127. {
  128. const int rbit = 5, gbit = 6, bbit = 5; //bits per color
  129. const int rmask = 0xF800, gmask = 0x7E0, bmask = 0x1F;
  130. const int rshift = 11, gshift = 5, bshift = 0;
  131. const Uint8 r5 = (*((Uint16 *)ptr) & rmask) >> rshift,
  132. b5 = (*((Uint16 *)ptr) & bmask) >> bshift,
  133. g5 = (*((Uint16 *)ptr) & gmask) >> gshift;
  134. const Uint32 r8 = (r5 << (8 - rbit)) | (r5 >> (2*rbit - 8)),
  135. g8 = (g5 << (8 - gbit)) | (g5 >> (2*gbit - 8)),
  136. b8 = (b5 << (8 - bbit)) | (b5 >> (2*bbit - 8));
  137. PutColor(ptr,
  138. (((r8-R)*A) >> 8) + R,
  139. (((g8-G)*A) >> 8) + G,
  140. (((b8-B)*A) >> 8) + B);
  141. }
  142. template <int incrementPtr>
  143. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorAlpha(Uint8 *&ptr, const SDL_Color & Color)
  144. {
  145. PutColor(ptr, Color.r, Color.g, Color.b, Color.unused);
  146. }
  147. template <int incrementPtr>
  148. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColor(Uint8 *&ptr, const SDL_Color & Color)
  149. {
  150. PutColor(ptr, Color.r, Color.g, Color.b);
  151. }
  152. template <int incrementPtr>
  153. STRONG_INLINE void ColorPutter<2, incrementPtr>::PutColorRow(Uint8 *&ptr, const SDL_Color & Color, size_t count)
  154. {
  155. //drop least significant bits of 24 bpp encoded color
  156. Uint16 pixel = (Color.b>>3) + ((Color.g>>2) << 5) + ((Color.r>>3) << 11);
  157. for (size_t i=0; i<count; i++)
  158. {
  159. memcpy(ptr, &pixel, 2);
  160. if(incrementPtr == -1)
  161. ptr -= 2;
  162. if(incrementPtr == 1)
  163. ptr += 2;
  164. }
  165. }
  166. SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  167. {
  168. return SDL_CreateRGBSurface(mod->flags,w,h,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  169. }
  170. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  171. {
  172. //return SDL_DisplayFormat(mod);
  173. return SDL_ConvertSurface(mod, mod->format, mod->flags);
  174. }
  175. bool isItIn(const SDL_Rect * rect, int x, int y)
  176. {
  177. return (x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h);
  178. }
  179. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  180. {
  181. if(!dst) dst = screen;
  182. SDL_Rect pom = genRect(src->h,src->w,x,y);
  183. CSDL_Ext::blitSurface(src,NULL,dst,&pom);
  184. }
  185. void blitAt(SDL_Surface * src, const SDL_Rect & pos, SDL_Surface * dst)
  186. {
  187. blitAt(src,pos.x,pos.y,dst);
  188. }
  189. SDL_Color genRGB(int r, int g, int b, int a=0)
  190. {
  191. SDL_Color ret;
  192. ret.b=b;
  193. ret.g=g;
  194. ret.r=r;
  195. ret.unused=a;
  196. return ret;
  197. }
  198. void updateRect (SDL_Rect * rect, SDL_Surface * scr)
  199. {
  200. SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
  201. }
  202. void printAtMiddleWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  203. {
  204. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  205. std::vector<SDL_Surface*> wesu;
  206. wesu.resize(ws.size());
  207. for (size_t i=0; i < wesu.size(); ++i)
  208. {
  209. wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
  210. }
  211. int tox=0, toy=0;
  212. for (size_t i=0; i < wesu.size(); ++i)
  213. {
  214. toy+=wesu[i]->h;
  215. if (tox < wesu[i]->w)
  216. tox=wesu[i]->w;
  217. }
  218. int evx, evy = y - (toy/2);
  219. for (size_t i=0; i < wesu.size(); ++i)
  220. {
  221. evx = (x - (tox/2)) + ((tox-wesu[i]->w)/2);
  222. blitAt(wesu[i],evx,evy,dst);
  223. evy+=wesu[i]->h;
  224. }
  225. for (size_t i=0; i < wesu.size(); ++i)
  226. SDL_FreeSurface(wesu[i]);
  227. }
  228. void printAtWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  229. {
  230. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  231. std::vector<SDL_Surface*> wesu;
  232. wesu.resize(ws.size());
  233. for (size_t i=0; i < wesu.size(); ++i)
  234. wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
  235. int evy = y;
  236. for (size_t i=0; i < wesu.size(); ++i)
  237. {
  238. blitAt(wesu[i],x,evy,dst);
  239. evy+=wesu[i]->h;
  240. }
  241. for (size_t i=0; i < wesu.size(); ++i)
  242. SDL_FreeSurface(wesu[i]);
  243. }
  244. void CSDL_Ext::printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  245. {
  246. if (graphics->fontsTrueType[font])
  247. {
  248. printAtWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
  249. return;
  250. }
  251. const Font *f = graphics->fonts[font];
  252. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  253. int cury = y;
  254. for (size_t i=0; i < ws.size(); ++i)
  255. {
  256. printAt(ws[i], x, cury, font, kolor, dst);
  257. cury += f->height;
  258. }
  259. }
  260. void CSDL_Ext::printAtMiddleWB( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor/*=tytulowy*/, SDL_Surface * dst/*=screen*/ )
  261. {
  262. if (graphics->fontsTrueType[font])
  263. {
  264. printAtMiddleWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
  265. return;
  266. }
  267. const Font *f = graphics->fonts[font];
  268. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  269. int totalHeight = ws.size() * f->height;
  270. int cury = y - totalHeight/2;
  271. for (size_t i=0; i < ws.size(); ++i)
  272. {
  273. printAt(ws[i], x - f->getWidth(ws[i].c_str())/2, cury, font, kolor, dst);
  274. cury += f->height;
  275. }
  276. }
  277. void printAtMiddle(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
  278. {
  279. if(text.length()==0) return;
  280. SDL_Surface * temp;
  281. switch (quality)
  282. {
  283. case 0:
  284. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  285. break;
  286. case 1:
  287. SDL_Color tem;
  288. tem.b = 0xff-kolor.b;
  289. tem.g = 0xff-kolor.g;
  290. tem.r = 0xff-kolor.r;
  291. tem.unused = 0xff-kolor.unused;
  292. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  293. break;
  294. case 2:
  295. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  296. break;
  297. default:
  298. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  299. break;
  300. }
  301. SDL_Rect dstRect = genRect(temp->h, temp->w, x-(temp->w/2), y-(temp->h/2));
  302. CSDL_Ext::blitSurface(temp, NULL, dst, &dstRect);
  303. SDL_FreeSurface(temp);
  304. }
  305. void CSDL_Ext::printAtMiddle( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  306. {
  307. if (graphics->fontsTrueType[font])
  308. {
  309. printAtMiddle(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  310. return;
  311. }
  312. const Font *f = graphics->fonts[font];
  313. int nx = x - f->getWidth(text.c_str())/2,
  314. ny = y - f->height/2;
  315. printAt(text, nx, ny, font, kolor, dst);
  316. }
  317. void printAt(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2, bool refresh=false)
  318. {
  319. if (text.length()==0)
  320. return;
  321. SDL_Surface * temp;
  322. switch (quality)
  323. {
  324. case 0:
  325. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  326. break;
  327. case 1:
  328. SDL_Color tem;
  329. tem.b = 0xff-kolor.b;
  330. tem.g = 0xff-kolor.g;
  331. tem.r = 0xff-kolor.r;
  332. tem.unused = 0xff-kolor.unused;
  333. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  334. break;
  335. case 2:
  336. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  337. break;
  338. default:
  339. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  340. break;
  341. }
  342. SDL_Rect dstRect = genRect(temp->h,temp->w,x,y);
  343. CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
  344. if(refresh)
  345. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  346. SDL_FreeSurface(temp);
  347. }
  348. void CSDL_Ext::printAt( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  349. {
  350. if(!text.size())
  351. return;
  352. if (graphics->fontsTrueType[font])
  353. {
  354. printAt(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  355. return;
  356. }
  357. assert(dst);
  358. assert(font < Graphics::FONTS_NUMBER);
  359. //assume BGR dst surface, TODO - make it general in a tidy way
  360. assert(dst->format->Rshift > dst->format->Gshift);
  361. assert(dst->format->Gshift > dst->format->Bshift);
  362. const Font *f = graphics->fonts[font];
  363. const Uint8 bpp = dst->format->BytesPerPixel;
  364. Uint8 *px = NULL;
  365. Uint8 *src = NULL;
  366. TColorPutter colorPutter = getPutterFor(dst, false);
  367. //if text is in {} braces, we'll ommit them
  368. const int first = (text[0] == '{' ? 1 : 0);
  369. const int beyondEnd = (text[text.size()-1] == '}' ? text.size()-1 : text.size());
  370. for(int txti = first; txti < beyondEnd; txti++)
  371. {
  372. const ui8 c = text[txti];
  373. x += f->chars[c].unknown1;
  374. for(int i = std::max(0, -y); i < f->height && (y + i) < (dst->h - 1); i++)
  375. {
  376. px = (Uint8*)dst->pixels;
  377. px += (y+i) * dst->pitch + x * bpp;
  378. src = f->chars[c].pixels;
  379. src += i * f->chars[c].width;//if we have reached end of surface in previous line
  380. for(int j = std::max(0, -x); j < f->chars[c].width && (j + x) < (dst->w - 1); j++)
  381. {
  382. switch(*src)
  383. {
  384. case 1: //black "shadow"
  385. memset(px, 0, bpp);
  386. break;
  387. case 255: //text colour
  388. colorPutter(px, kolor.r, kolor.g, kolor.b);
  389. break;
  390. }
  391. src++;
  392. px += bpp;
  393. }
  394. }
  395. x += f->chars[c].width;
  396. x += f->chars[c].unknown2;
  397. }
  398. }
  399. void printTo(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
  400. {
  401. if (text.length()==0)
  402. return;
  403. SDL_Surface * temp;
  404. switch (quality)
  405. {
  406. case 0:
  407. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  408. break;
  409. case 1:
  410. SDL_Color tem;
  411. tem.b = 0xff-kolor.b;
  412. tem.g = 0xff-kolor.g;
  413. tem.r = 0xff-kolor.r;
  414. tem.unused = 0xff-kolor.unused;
  415. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  416. break;
  417. case 2:
  418. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  419. break;
  420. default:
  421. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  422. break;
  423. }
  424. SDL_Rect dstRect = genRect(temp->h,temp->w,x-temp->w,y-temp->h);
  425. CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
  426. SDL_UpdateRect(dst,x-temp->w,y-temp->h,temp->w,temp->h);
  427. SDL_FreeSurface(temp);
  428. }
  429. void CSDL_Ext::printTo( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=zwykly*/, SDL_Surface * dst/*=screen*/ )
  430. {
  431. if (graphics->fontsTrueType[font])
  432. {
  433. printTo(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  434. return;
  435. }
  436. const Font *f = graphics->fonts[font];
  437. printAt(text, x - f->getWidth(text.c_str()), y - f->height, font, kolor, dst);
  438. }
  439. void printToWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
  440. {
  441. if (text.length()==0)
  442. return;
  443. SDL_Surface * temp;
  444. switch (quality)
  445. {
  446. case 0:
  447. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  448. break;
  449. case 1:
  450. SDL_Color tem;
  451. tem.b = 0xff-kolor.b;
  452. tem.g = 0xff-kolor.g;
  453. tem.r = 0xff-kolor.r;
  454. tem.unused = 0xff-kolor.unused;
  455. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  456. break;
  457. case 2:
  458. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  459. break;
  460. default:
  461. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  462. break;
  463. }
  464. SDL_Rect dstRect = genRect(temp->h,temp->w,x-temp->w,y-temp->h);
  465. CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
  466. SDL_FreeSurface(temp);
  467. }
  468. // Vertical flip
  469. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
  470. {
  471. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  472. const int bpl = ret->pitch;
  473. const int bpp = ret->format->BytesPerPixel;
  474. for(int i=0; i<ret->h; i++) {
  475. char *src = (char *)toRot->pixels + i*bpl;
  476. char *dst = (char *)ret->pixels + i*bpl;
  477. for(int j=0; j<ret->w; j++) {
  478. for (int k=0; k<bpp; k++) {
  479. dst[j*bpp + k] = src[(ret->w-j-1)*bpp + k];
  480. }
  481. }
  482. }
  483. return ret;
  484. }
  485. // Horizontal flip
  486. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  487. {
  488. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  489. int bpl = ret->pitch;
  490. for(int i=0; i<ret->h; i++) {
  491. memcpy((char *)ret->pixels + i*bpl, (char *)toRot->pixels + (ret->h-i-1)*bpl, bpl);
  492. }
  493. return ret;
  494. };
  495. ///**************/
  496. ///Rotates toRot surface by 90 degrees left
  497. ///**************/
  498. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  499. {
  500. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  501. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  502. for(int i=0; i<ret->w; ++i)
  503. {
  504. for(int j=0; j<ret->h; ++j)
  505. {
  506. {
  507. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  508. /*
  509. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  510. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2]);
  511. #else
  512. */
  513. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0]);
  514. //#endif
  515. }
  516. }
  517. }
  518. return ret;
  519. }
  520. ///*************/
  521. ///Rotates toRot surface by 180 degrees
  522. ///*************/
  523. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  524. {
  525. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  526. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  527. if(ret->format->BytesPerPixel!=1)
  528. {
  529. for(int i=0; i<ret->w; ++i)
  530. {
  531. for(int j=0; j<ret->h; ++j)
  532. {
  533. {
  534. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  535. /*
  536. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  537. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2], 0);
  538. #else
  539. */
  540. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0], 0);
  541. //#endif
  542. }
  543. }
  544. }
  545. }
  546. else
  547. {
  548. for(int i=0; i<ret->w; ++i)
  549. {
  550. for(int j=0; j<ret->h; ++j)
  551. {
  552. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  553. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  554. }
  555. }
  556. }
  557. return ret;
  558. }
  559. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  560. {
  561. int bpp = surface->format->BytesPerPixel;
  562. /* Here p is the address to the pixel we want to retrieve */
  563. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  564. switch(bpp)
  565. {
  566. case 1:
  567. if(colorByte)
  568. {
  569. return colorToUint32(surface->format->palette->colors+(*p));
  570. }
  571. else
  572. return *p;
  573. case 2:
  574. return *(Uint16 *)p;
  575. case 3:
  576. /*
  577. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  578. return p[0] << 16 | p[1] << 8 | p[2];
  579. #else
  580. */
  581. return p[0] | p[1] << 8 | p[2] << 16;
  582. //#endif
  583. case 4:
  584. return *(Uint32 *)p;
  585. default:
  586. return 0; // shouldn't happen, but avoids warnings
  587. }
  588. }
  589. void CSDL_Ext::alphaTransform(SDL_Surface *src)
  590. {
  591. assert(src->format->BitsPerPixel == 8);
  592. SDL_Color colors[] = {{0,0,0,255}, {0,0,0,214}, {0,0,0,164}, {0,0,0,82}, {0,0,0,128},
  593. {255,0,0,0}, {255,0,0,0}, {255,0,0,0}, {0,0,0,192}, {0,0,0,192}};
  594. SDL_SetColors(src, colors, 0, ARRAY_COUNT(colors));
  595. SDL_SetColorKey(src, SDL_SRCCOLORKEY, SDL_MapRGBA(src->format, 0, 0, 0, 255));
  596. }
  597. // <=>
  598. static void prepareOutRect(SDL_Rect *src, SDL_Rect *dst, const SDL_Rect & clip_rect)
  599. {
  600. const int xoffset = std::max(clip_rect.x - dst->x, 0),
  601. yoffset = std::max(clip_rect.y - dst->y, 0);
  602. src->x += xoffset;
  603. src->y += yoffset;
  604. dst->x += xoffset;
  605. dst->y += yoffset;
  606. src->w = dst->w = std::max(0,std::min(dst->w - xoffset, clip_rect.x + clip_rect.w - dst->x));
  607. src->h = dst->h = std::max(0,std::min(dst->h - yoffset, clip_rect.y + clip_rect.h - dst->y));
  608. }
  609. template<int bpp>
  610. 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
  611. {
  612. static void (*blitWithRotate[])(const SDL_Surface *, const SDL_Rect *, SDL_Surface *, const SDL_Rect *) = {blitWithRotate1<bpp>, blitWithRotate2<bpp>, blitWithRotate3<bpp>};
  613. if(!rotation)
  614. {
  615. CSDL_Ext::blitSurface(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::blitWithRotateClipVal( SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation )
  625. {
  626. blitWithRotateClip<bpp>(src, &srcRect, dst, &dstRect, rotation);
  627. }
  628. template<int bpp>
  629. 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
  630. {
  631. static void (*blitWithRotate[])(const SDL_Surface *, const SDL_Rect *, SDL_Surface *, const SDL_Rect *) = {blitWithRotate1WithAlpha<bpp>, blitWithRotate2WithAlpha<bpp>, blitWithRotate3WithAlpha<bpp>};
  632. if(!rotation)
  633. {
  634. blit8bppAlphaTo24bpp(src, srcRect, dst, dstRect);
  635. }
  636. else
  637. {
  638. prepareOutRect(srcRect, dstRect, dst->clip_rect);
  639. blitWithRotate[rotation-1](src, srcRect, dst, dstRect);
  640. }
  641. }
  642. template<int bpp>
  643. void CSDL_Ext::blitWithRotateClipValWithAlpha( SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation )
  644. {
  645. blitWithRotateClipWithAlpha<bpp>(src, &srcRect, dst, &dstRect, rotation);
  646. }
  647. template<int bpp>
  648. 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
  649. {
  650. Uint8 *sp = getPxPtr(src, src->w - srcRect->w - srcRect->x, srcRect->y);
  651. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  652. const SDL_Color * const colors = src->format->palette->colors;
  653. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  654. {
  655. Uint8 *dp = dporg;
  656. for(int j=dstRect->w; j>0; j--, sp++)
  657. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  658. sp += src->w - dstRect->w;
  659. }
  660. }
  661. template<int bpp>
  662. 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
  663. {
  664. Uint8 *sp = getPxPtr(src, srcRect->x, src->h - srcRect->h - srcRect->y);
  665. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  666. const SDL_Color * const colors = src->format->palette->colors;
  667. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  668. {
  669. Uint8 *dp = dporg;
  670. for(int j=dstRect->w; j>0; j--, sp++)
  671. ColorPutter<bpp, 1>::PutColor(dp, colors[*sp]);
  672. sp += src->w - dstRect->w;
  673. }
  674. }
  675. template<int bpp>
  676. 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
  677. {
  678. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + (src->w - srcRect->w - srcRect->x);
  679. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  680. const SDL_Color * const colors = src->format->palette->colors;
  681. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  682. {
  683. Uint8 *dp = dporg;
  684. for(int j=dstRect->w; j>0; j--, sp++)
  685. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  686. sp += src->w - dstRect->w;
  687. }
  688. }
  689. template<int bpp>
  690. 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
  691. {
  692. Uint8 *sp = (Uint8 *)src->pixels + srcRect->y*src->pitch + (src->w - srcRect->w - srcRect->x);
  693. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*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::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
  710. {
  711. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + srcRect->x;
  712. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*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. 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
  729. {
  730. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + (src->w - srcRect->w - srcRect->x);
  731. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  732. const SDL_Color * const colors = src->format->palette->colors;
  733. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  734. {
  735. Uint8 *dp = dporg;
  736. for(int j=dstRect->w; j>0; j--, sp++)
  737. {
  738. if(*sp)
  739. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  740. else
  741. dp -= bpp;
  742. }
  743. sp += src->w - dstRect->w;
  744. }
  745. }
  746. template<int bpp>
  747. int CSDL_Ext::blit8bppAlphaTo24bppT(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  748. {
  749. if (src && src->format->BytesPerPixel==1 && dst && (bpp==3 || bpp==4 || bpp==2)) //everything's ok
  750. {
  751. SDL_Rect fulldst;
  752. int srcx, srcy, w, h;
  753. /* Make sure the surfaces aren't locked */
  754. if ( ! src || ! dst )
  755. {
  756. SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  757. return -1;
  758. }
  759. if ( src->locked || dst->locked )
  760. {
  761. SDL_SetError("Surfaces must not be locked during blit");
  762. return -1;
  763. }
  764. /* If the destination rectangle is NULL, use the entire dest surface */
  765. if ( dstRect == NULL )
  766. {
  767. fulldst.x = fulldst.y = 0;
  768. dstRect = &fulldst;
  769. }
  770. /* clip the source rectangle to the source surface */
  771. if(srcRect)
  772. {
  773. int maxw, maxh;
  774. srcx = srcRect->x;
  775. w = srcRect->w;
  776. if(srcx < 0)
  777. {
  778. w += srcx;
  779. dstRect->x -= srcx;
  780. srcx = 0;
  781. }
  782. maxw = src->w - srcx;
  783. if(maxw < w)
  784. w = maxw;
  785. srcy = srcRect->y;
  786. h = srcRect->h;
  787. if(srcy < 0)
  788. {
  789. h += srcy;
  790. dstRect->y -= srcy;
  791. srcy = 0;
  792. }
  793. maxh = src->h - srcy;
  794. if(maxh < h)
  795. h = maxh;
  796. }
  797. else
  798. {
  799. srcx = srcy = 0;
  800. w = src->w;
  801. h = src->h;
  802. }
  803. /* clip the destination rectangle against the clip rectangle */
  804. {
  805. SDL_Rect *clip = &dst->clip_rect;
  806. int dx, dy;
  807. dx = clip->x - dstRect->x;
  808. if(dx > 0)
  809. {
  810. w -= dx;
  811. dstRect->x += dx;
  812. srcx += dx;
  813. }
  814. dx = dstRect->x + w - clip->x - clip->w;
  815. if(dx > 0)
  816. w -= dx;
  817. dy = clip->y - dstRect->y;
  818. if(dy > 0)
  819. {
  820. h -= dy;
  821. dstRect->y += dy;
  822. srcy += dy;
  823. }
  824. dy = dstRect->y + h - clip->y - clip->h;
  825. if(dy > 0)
  826. h -= dy;
  827. }
  828. if(w > 0 && h > 0)
  829. {
  830. dstRect->w = w;
  831. dstRect->h = h;
  832. if(SDL_LockSurface(dst))
  833. return -1; //if we cannot lock the surface
  834. const SDL_Color *colors = src->format->palette->colors;
  835. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  836. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  837. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  838. {
  839. Uint8 *color = colory;
  840. Uint8 *p = py;
  841. for(int x = w; x; x--)
  842. {
  843. const SDL_Color &tbc = colors[*color++]; //color to blit
  844. ColorPutter<bpp, +1>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, tbc.unused);
  845. }
  846. }
  847. SDL_UnlockSurface(dst);
  848. }
  849. }
  850. return 0;
  851. }
  852. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  853. {
  854. switch(dst->format->BytesPerPixel)
  855. {
  856. case 2: return blit8bppAlphaTo24bppT<2>(src, srcRect, dst, dstRect);
  857. case 3: return blit8bppAlphaTo24bppT<3>(src, srcRect, dst, dstRect);
  858. case 4: return blit8bppAlphaTo24bppT<4>(src, srcRect, dst, dstRect);
  859. default:
  860. tlog1 << (int)dst->format->BitsPerPixel << " bpp is not supported!!!\n";
  861. return -1;
  862. }
  863. }
  864. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  865. {
  866. Uint32 ret = 0;
  867. ret+=color->unused;
  868. ret<<=8; //*=256
  869. ret+=color->b;
  870. ret<<=8; //*=256
  871. ret+=color->g;
  872. ret<<=8; //*=256
  873. ret+=color->r;
  874. return ret;
  875. }
  876. void CSDL_Ext::update(SDL_Surface * what)
  877. {
  878. if(what)
  879. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  880. }
  881. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color)
  882. {
  883. for(int i = 0; i < w; i++)
  884. {
  885. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+i,y,color.x,color.y,color.z);
  886. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+i,y+h-1,color.x,color.y,color.z);
  887. }
  888. for(int i = 0; i < h; i++)
  889. {
  890. SDL_PutPixelWithoutRefreshIfInSurf(sur,x,y+i,color.x,color.y,color.z);
  891. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+w-1,y+i,color.x,color.y,color.z);
  892. }
  893. }
  894. void CSDL_Ext::drawBorder( SDL_Surface * sur, const SDL_Rect &r, const int3 &color )
  895. {
  896. drawBorder(sur, r.x, r.y, r.w, r.h, color);
  897. }
  898. void CSDL_Ext::drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color)
  899. {
  900. const int y1 = r.y, y2 = r.y + r.h-1;
  901. for (int i=0; i<r.w; i++)
  902. {
  903. const int x = r.x + i;
  904. if (i%4 || (i==0))
  905. {
  906. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y1, color.x, color.y, color.z);
  907. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y2, color.x, color.y, color.z);
  908. }
  909. }
  910. const int x1 = r.x, x2 = r.x + r.w-1;
  911. for (int i=0; i<r.h; i++)
  912. {
  913. const int y = r.y + i;
  914. if ((i%4) || (i==0))
  915. {
  916. SDL_PutPixelWithoutRefreshIfInSurf(sur, x1, y, color.x, color.y, color.z);
  917. SDL_PutPixelWithoutRefreshIfInSurf(sur, x2, y, color.x, color.y, color.z);
  918. }
  919. }
  920. }
  921. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, ui8 player)
  922. {
  923. if(player==254)
  924. return;
  925. if(sur->format->BitsPerPixel==8)
  926. {
  927. SDL_Color *color = (player == 255
  928. ? graphics->neutralColor
  929. : &graphics->playerColors[player]);
  930. SDL_SetColors(sur, color, 5, 1);
  931. }
  932. else
  933. tlog3 << "Warning, setPlayerColor called on not 8bpp surface!\n";
  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->h)
  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. ui8 * pixels = (ui8*)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. ui8 * pixels = (ui8*)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. 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. SDL_FillRect(dst, &newRect, color);
  1136. }
  1137. SDL_Surface * CSDL_Ext::std32bppSurface = NULL;
  1138. //instantiation of templates used in CAnimation and CCreatureAnimation, required for correct linking
  1139. template struct ColorPutter<2,-1>;
  1140. template struct ColorPutter<3,-1>;
  1141. template struct ColorPutter<4,-1>;
  1142. template struct ColorPutter<2, 0>;
  1143. template struct ColorPutter<3, 0>;
  1144. template struct ColorPutter<4, 0>;
  1145. template struct ColorPutter<2, 1>;
  1146. template struct ColorPutter<3, 1>;
  1147. template struct ColorPutter<4, 1>;