SDL_Extensions.cpp 35 KB

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