SDL_Extensions.cpp 35 KB

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