SDL_Extensions.cpp 29 KB

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