SDL_Extensions.cpp 35 KB

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