SDL_Extensions.cpp 36 KB

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