SDL_Extensions.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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, SDL_Rect pos, SDL_Surface * dst)
  50. {
  51. blitAtWR(src,pos.x,pos.y,dst);
  52. }
  53. void blitAt(SDL_Surface * src, 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 CSDL_Ext::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 CSDL_Ext::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::printAtMiddle(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality, bool refresh)
  115. {
  116. if(text.length()==0) return;
  117. SDL_Surface * temp;
  118. switch (quality)
  119. {
  120. case 0:
  121. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  122. break;
  123. case 1:
  124. SDL_Color tem;
  125. tem.b = 0xff-kolor.b;
  126. tem.g = 0xff-kolor.g;
  127. tem.r = 0xff-kolor.r;
  128. tem.unused = 0xff-kolor.unused;
  129. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  130. break;
  131. case 2:
  132. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  133. break;
  134. default:
  135. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  136. break;
  137. }
  138. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-(temp->w/2),y-(temp->h/2)));
  139. if(refresh)
  140. SDL_UpdateRect(dst,x-(temp->w/2),y-(temp->h/2),temp->w,temp->h);
  141. SDL_FreeSurface(temp);
  142. }
  143. void CSDL_Ext::printAt(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality, bool refresh)
  144. {
  145. if (text.length()==0)
  146. return;
  147. SDL_Surface * temp;
  148. switch (quality)
  149. {
  150. case 0:
  151. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  152. break;
  153. case 1:
  154. SDL_Color tem;
  155. tem.b = 0xff-kolor.b;
  156. tem.g = 0xff-kolor.g;
  157. tem.r = 0xff-kolor.r;
  158. tem.unused = 0xff-kolor.unused;
  159. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  160. break;
  161. case 2:
  162. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  163. break;
  164. default:
  165. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  166. break;
  167. }
  168. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x,y));
  169. if(refresh)
  170. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  171. SDL_FreeSurface(temp);
  172. }
  173. void CSDL_Ext::printAtWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  174. {
  175. printAt(text,x,y,font,kolor,dst,quality, true);
  176. }
  177. void CSDL_Ext::printTo(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  178. {
  179. if (text.length()==0)
  180. return;
  181. SDL_Surface * temp;
  182. switch (quality)
  183. {
  184. case 0:
  185. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  186. break;
  187. case 1:
  188. SDL_Color tem;
  189. tem.b = 0xff-kolor.b;
  190. tem.g = 0xff-kolor.g;
  191. tem.r = 0xff-kolor.r;
  192. tem.unused = 0xff-kolor.unused;
  193. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  194. break;
  195. case 2:
  196. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  197. break;
  198. default:
  199. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  200. break;
  201. }
  202. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-temp->w,y-temp->h));
  203. SDL_UpdateRect(dst,x-temp->w,y-temp->h,temp->w,temp->h);
  204. SDL_FreeSurface(temp);
  205. }
  206. void CSDL_Ext::printToWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  207. {
  208. if (text.length()==0)
  209. return;
  210. SDL_Surface * temp;
  211. switch (quality)
  212. {
  213. case 0:
  214. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  215. break;
  216. case 1:
  217. SDL_Color tem;
  218. tem.b = 0xff-kolor.b;
  219. tem.g = 0xff-kolor.g;
  220. tem.r = 0xff-kolor.r;
  221. tem.unused = 0xff-kolor.unused;
  222. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  223. break;
  224. case 2:
  225. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  226. break;
  227. default:
  228. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  229. break;
  230. }
  231. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-temp->w,y-temp->h));
  232. SDL_FreeSurface(temp);
  233. }
  234. inline 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)
  235. {
  236. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel;
  237. p[0] = B;
  238. p[1] = G;
  239. p[2] = R;
  240. if(ekran->format->BytesPerPixel==4)
  241. p[3] = A;
  242. SDL_UpdateRect(ekran, x, y, 1, 1);
  243. }
  244. // Vertical flip
  245. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
  246. {
  247. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  248. const int bpl = ret->pitch;
  249. const int bpp = ret->format->BytesPerPixel;
  250. for(int i=0; i<ret->h; i++) {
  251. char *src = (char *)toRot->pixels + i*bpl;
  252. char *dst = (char *)ret->pixels + i*bpl;
  253. for(int j=0; j<ret->w; j++) {
  254. for (int k=0; k<bpp; k++) {
  255. dst[j*bpp + k] = src[(ret->w-j-1)*bpp + k];
  256. }
  257. }
  258. }
  259. return ret;
  260. }
  261. // Horizontal flip
  262. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  263. {
  264. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  265. int bpl = ret->pitch;
  266. for(int i=0; i<ret->h; i++) {
  267. memcpy((char *)ret->pixels + i*bpl, (char *)toRot->pixels + (ret->h-i-1)*bpl, bpl);
  268. }
  269. return ret;
  270. };
  271. ///**************/
  272. ///Rotates toRot surface by 90 degrees left
  273. ///**************/
  274. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  275. {
  276. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  277. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  278. for(int i=0; i<ret->w; ++i)
  279. {
  280. for(int j=0; j<ret->h; ++j)
  281. {
  282. {
  283. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  284. /*
  285. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  286. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2]);
  287. #else
  288. */
  289. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0]);
  290. //#endif
  291. }
  292. }
  293. }
  294. return ret;
  295. }
  296. ///*************/
  297. ///Rotates toRot surface by 180 degrees
  298. ///*************/
  299. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  300. {
  301. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  302. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  303. if(ret->format->BytesPerPixel!=1)
  304. {
  305. for(int i=0; i<ret->w; ++i)
  306. {
  307. for(int j=0; j<ret->h; ++j)
  308. {
  309. {
  310. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  311. /*
  312. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  313. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2], 0);
  314. #else
  315. */
  316. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0], 0);
  317. //#endif
  318. }
  319. }
  320. }
  321. }
  322. else
  323. {
  324. for(int i=0; i<ret->w; ++i)
  325. {
  326. for(int j=0; j<ret->h; ++j)
  327. {
  328. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  329. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  330. }
  331. }
  332. }
  333. return ret;
  334. }
  335. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  336. {
  337. int bpp = surface->format->BytesPerPixel;
  338. /* Here p is the address to the pixel we want to retrieve */
  339. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  340. switch(bpp)
  341. {
  342. case 1:
  343. if(colorByte)
  344. {
  345. return colorToUint32(surface->format->palette->colors+(*p));
  346. }
  347. else
  348. return *p;
  349. case 2:
  350. return *(Uint16 *)p;
  351. case 3:
  352. /*
  353. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  354. return p[0] << 16 | p[1] << 8 | p[2];
  355. #else
  356. */
  357. return p[0] | p[1] << 8 | p[2] << 16;
  358. //#endif
  359. case 4:
  360. return *(Uint32 *)p;
  361. default:
  362. return 0; // shouldn't happen, but avoids warnings
  363. }
  364. }
  365. SDL_Surface * CSDL_Ext::alphaTransform(SDL_Surface *src)
  366. {
  367. Uint32 trans = SDL_MapRGBA(src->format, 0, 255, 255, 255);
  368. SDL_SetColorKey(src, 0, trans);
  369. src->flags|=SDL_SRCALPHA;
  370. SDL_Color transp;
  371. transp.b = transp.g = transp.r = 0;
  372. transp.unused = 255;
  373. if(src->format->BitsPerPixel == 8)
  374. {
  375. for(int yy=0; yy<src->format->palette->ncolors; ++yy)
  376. {
  377. SDL_Color cur = *(src->format->palette->colors+yy);
  378. //if(cur.r == 255 && cur.b == 255)
  379. if(yy==1 || yy==2 || yy==3 || yy==4 || yy==8 || yy==9)
  380. {
  381. SDL_Color shadow;
  382. shadow.b = shadow.g = shadow.r = 0;
  383. switch(cur.g) //change this values; make diffrerent for objects and shadows (?)
  384. {
  385. case 0:
  386. shadow.unused = 128;
  387. break;
  388. case 50:
  389. shadow.unused = 50+32;
  390. break;
  391. case 100:
  392. shadow.unused = 100+64;
  393. break;
  394. case 125:
  395. shadow.unused = 125+64;
  396. break;
  397. case 128:
  398. shadow.unused = 128+64;
  399. break;
  400. case 150:
  401. shadow.unused = 150+64;
  402. break;
  403. default:
  404. shadow.unused = 255;
  405. break;
  406. }
  407. SDL_SetColors(src, &shadow, yy, 1);
  408. }
  409. if(yy==0 || (cur.r == 255 && cur.g == 0 && cur.b == 0))
  410. {
  411. SDL_SetColors(src, &transp, yy, 1);
  412. }
  413. }
  414. }
  415. return src;
  416. }
  417. // <=>
  418. static void prepareOutRect(SDL_Rect &dst, const SDL_Rect *dstRect, const SDL_Rect *clip_rect)
  419. {
  420. dst.x = std::max(dstRect->x,clip_rect->x);
  421. dst.y = std::max(dstRect->y,clip_rect->y);
  422. dst.w = std::max(0,std::min(dstRect->w - (dst.x - dstRect->x), clip_rect->x + clip_rect->w - dst.x));
  423. dst.h = std::max(0,std::min(dstRect->h - (dst.y - dstRect->y), clip_rect->y + clip_rect->h - dst.y));
  424. }
  425. void CSDL_Ext::blitWithRotate1(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24/32 bpp dests
  426. {
  427. Uint8 *sp = (Uint8 *)src->pixels;
  428. const int bpp = dst->format->BytesPerPixel;
  429. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  430. const SDL_Color * const colors = src->format->palette->colors;
  431. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  432. {
  433. Uint8 *dp = dporg;
  434. sp += src->w - dstRect->w;
  435. for(int j=dstRect->w; j>0; j--, sp++)
  436. {
  437. if (bpp == 4)
  438. *(--dp) = 0;
  439. const SDL_Color color = colors[*sp];
  440. *(--dp) = color.r;
  441. *(--dp) = color.g;
  442. *(--dp) = color.b;
  443. }
  444. }
  445. }
  446. void CSDL_Ext::blitWithRotate1clip(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  447. {
  448. SDL_Rect realDest;
  449. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  450. blitWithRotate1(src,srcRect,dst,&realDest);
  451. }
  452. 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
  453. {
  454. Uint8 *sp = (Uint8 *)src->pixels;
  455. const int bpp = dst->format->BytesPerPixel;
  456. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  457. const SDL_Color * const colors = src->format->palette->colors;
  458. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  459. {
  460. Uint8 *dp = dporg;
  461. for(int j=dstRect->w; j>0; j--, sp++)
  462. {
  463. const SDL_Color color = colors[*sp];
  464. *(dp++) = color.b;
  465. *(dp++) = color.g;
  466. *(dp++) = color.r;
  467. if (bpp == 4)
  468. *(dp++) = 0;
  469. }
  470. sp += src->w - dstRect->w;
  471. }
  472. }
  473. void CSDL_Ext::blitWithRotate2clip(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  474. {
  475. SDL_Rect realDest;
  476. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  477. blitWithRotate2(src,srcRect,dst,&realDest);
  478. }
  479. void CSDL_Ext::blitWithRotate3(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  480. {
  481. Uint8 *dp, *sp = (Uint8 *)src->pixels;
  482. for(int i=0; i<dstRect->h; i++)
  483. {
  484. sp += src->w - dstRect->w;
  485. dp = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1 - i)*dst->pitch + (dstRect->x+dstRect->w)*dst->format->BytesPerPixel;
  486. for(int j=0; j<dstRect->w; j++, sp++)
  487. {
  488. const SDL_Color * const color = src->format->palette->colors+(*sp);
  489. if (dst->format->BytesPerPixel == 4)
  490. *(--dp) = 0;
  491. *(--dp) = color->r;
  492. *(--dp) = color->g;
  493. *(--dp) = color->b;
  494. }
  495. }
  496. }
  497. void CSDL_Ext::blitWithRotate3clip(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  498. {
  499. SDL_Rect realDest;
  500. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  501. blitWithRotate3(src,srcRect,dst,&realDest);
  502. }
  503. int CSDL_Ext::blit8bppAlphaTo24bpp(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  504. {
  505. if(src && src->format->BytesPerPixel==1 && dst && (dst->format->BytesPerPixel==3 || dst->format->BytesPerPixel==4)) //everything's ok
  506. {
  507. SDL_Rect fulldst;
  508. int srcx, srcy, w, h;
  509. /* Make sure the surfaces aren't locked */
  510. if ( ! src || ! dst )
  511. {
  512. SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  513. return(-1);
  514. }
  515. if ( src->locked || dst->locked )
  516. {
  517. SDL_SetError("Surfaces must not be locked during blit");
  518. return(-1);
  519. }
  520. /* If the destination rectangle is NULL, use the entire dest surface */
  521. if ( dstRect == NULL )
  522. {
  523. fulldst.x = fulldst.y = 0;
  524. dstRect = &fulldst;
  525. }
  526. /* clip the source rectangle to the source surface */
  527. if(srcRect)
  528. {
  529. int maxw, maxh;
  530. srcx = srcRect->x;
  531. w = srcRect->w;
  532. if(srcx < 0)
  533. {
  534. w += srcx;
  535. dstRect->x -= srcx;
  536. srcx = 0;
  537. }
  538. maxw = src->w - srcx;
  539. if(maxw < w)
  540. w = maxw;
  541. srcy = srcRect->y;
  542. h = srcRect->h;
  543. if(srcy < 0)
  544. {
  545. h += srcy;
  546. dstRect->y -= srcy;
  547. srcy = 0;
  548. }
  549. maxh = src->h - srcy;
  550. if(maxh < h)
  551. h = maxh;
  552. }
  553. else
  554. {
  555. srcx = srcy = 0;
  556. w = src->w;
  557. h = src->h;
  558. }
  559. /* clip the destination rectangle against the clip rectangle */
  560. {
  561. SDL_Rect *clip = &dst->clip_rect;
  562. int dx, dy;
  563. dx = clip->x - dstRect->x;
  564. if(dx > 0)
  565. {
  566. w -= dx;
  567. dstRect->x += dx;
  568. srcx += dx;
  569. }
  570. dx = dstRect->x + w - clip->x - clip->w;
  571. if(dx > 0)
  572. w -= dx;
  573. dy = clip->y - dstRect->y;
  574. if(dy > 0)
  575. {
  576. h -= dy;
  577. dstRect->y += dy;
  578. srcy += dy;
  579. }
  580. dy = dstRect->y + h - clip->y - clip->h;
  581. if(dy > 0)
  582. h -= dy;
  583. }
  584. if(w > 0 && h > 0)
  585. {
  586. dstRect->w = w;
  587. dstRect->h = h;
  588. if(SDL_LockSurface(dst))
  589. return -1; //if we cannot lock the surface
  590. const int bpp = dst->format->BytesPerPixel;
  591. const SDL_Color *colors = src->format->palette->colors;
  592. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  593. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  594. if(dst->format->Rshift==0) //like in most surfaces
  595. {
  596. for(int y=0; y<h; y++, colory+=src->pitch, py+=dst->pitch)
  597. {
  598. Uint8 *color = colory;
  599. Uint8 *p = py;
  600. for(int x=0; x<w; ++x, color++, p += bpp)
  601. {
  602. const SDL_Color tbc = colors[*color]; //color to blit
  603. // According analyze, the values of tbc.unused are fixed,
  604. // and the approximate ratios are as following:
  605. //
  606. // tbc.unused numbers
  607. // 192 2679
  608. // 164 326907
  609. // 82 705590
  610. // 214 1292625
  611. // 128 4842923
  612. // 0 72138078
  613. // 255 77547326
  614. //
  615. // By making use of such characteristic, we may implement a
  616. // very fast algorithm for heroes3 without loose much quality.
  617. switch (tbc.unused)
  618. {
  619. case 255:
  620. break;
  621. case 0:
  622. p[0] = tbc.r;
  623. p[1] = tbc.g;
  624. p[2] = tbc.b;
  625. break;
  626. case 128: // optimized
  627. p[0] = ((Uint16)tbc.r + (Uint16)p[0]) >> 1;
  628. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  629. p[2] = ((Uint16)tbc.b + (Uint16)p[2]) >> 1;
  630. break;
  631. default:
  632. p[0] = ((((Uint32)p[0]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  633. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  634. p[2] = ((((Uint32)p[2]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  635. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  636. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  637. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  638. break;
  639. }
  640. }
  641. }
  642. }
  643. else if(dst->format->Rshift==16) //such as screen
  644. {
  645. for(int y=0; y<h; y++, colory+=src->pitch, py+=dst->pitch)
  646. {
  647. Uint8 *color = colory;
  648. Uint8 *p = py;
  649. for(int x=0; x<w; x++, color++, p += bpp)
  650. {
  651. const SDL_Color tbc = colors[*color]; //color to blit
  652. switch (tbc.unused)
  653. {
  654. case 255:
  655. break;
  656. case 0:
  657. p[0] = tbc.b;
  658. p[1] = tbc.g;
  659. p[2] = tbc.r;
  660. break;
  661. case 128: // optimized
  662. p[0] = ((Uint16)tbc.b + (Uint16)p[0]) >> 1;
  663. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  664. p[2] = ((Uint16)tbc.r + (Uint16)p[2]) >> 1;
  665. break;
  666. default:
  667. p[0] = ((((Uint32)p[0]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  668. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  669. p[2] = ((((Uint32)p[2]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  670. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  671. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  672. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  673. break;
  674. }
  675. }
  676. }
  677. }
  678. SDL_UnlockSurface(dst);
  679. }
  680. }
  681. return 0;
  682. }
  683. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  684. {
  685. Uint32 ret = 0;
  686. ret+=color->unused;
  687. ret<<=8; //*=256
  688. ret+=color->b;
  689. ret<<=8; //*=256
  690. ret+=color->g;
  691. ret<<=8; //*=256
  692. ret+=color->r;
  693. return ret;
  694. }
  695. void CSDL_Ext::update(SDL_Surface * what)
  696. {
  697. if(what)
  698. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  699. }
  700. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, int3 color)
  701. {
  702. for(int i=0;i<w;i++)
  703. {
  704. SDL_PutPixelWithoutRefresh(sur,x+i,y,color.x,color.y,color.z);
  705. SDL_PutPixelWithoutRefresh(sur,x+i,y+h-1,color.x,color.y,color.z);
  706. }
  707. for(int i=0; i<h;i++)
  708. {
  709. SDL_PutPixelWithoutRefresh(sur,x,y+i,color.x,color.y,color.z);
  710. SDL_PutPixelWithoutRefresh(sur,x+w-1,y+i,color.x,color.y,color.z);
  711. }
  712. }
  713. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, unsigned char player)
  714. {
  715. if(player==254)
  716. return;
  717. if(sur->format->BitsPerPixel==8)
  718. {
  719. if(player != 255)
  720. *(sur->format->palette->colors+5) = graphics->playerColors[player];
  721. else
  722. *(sur->format->palette->colors+5) = *graphics->neutralColor;
  723. }
  724. }
  725. int readNormalNr (std::istream &in, int bytCon)
  726. {
  727. int ret=0;
  728. int amp=1;
  729. unsigned char byte;
  730. if (in.good())
  731. {
  732. for (int i=0; i<bytCon; i++)
  733. {
  734. in.read((char*)&byte,1);
  735. ret+=byte*amp;
  736. amp<<=8;
  737. }
  738. }
  739. else return -1;
  740. return ret;
  741. }
  742. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  743. {
  744. for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
  745. {
  746. boost::replace_first(str,"%s",tor[i]);
  747. }
  748. return str;
  749. }
  750. SDL_Surface * CSDL_Ext::std32bppSurface = NULL;