SDL_Extensions.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804
  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. 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(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  426. {
  427. Uint8 *dp, *sp = (Uint8 *)src->pixels;
  428. for(int i=0; i<dstRect->h; i++)
  429. {
  430. sp += src->w - dstRect->w;
  431. dp = (Uint8 *)dst->pixels + (i+dstRect->y)*dst->pitch + (dstRect->x+dstRect->w)*dst->format->BytesPerPixel;
  432. for(int j=0; j<dstRect->w; j++, sp++)
  433. {
  434. const SDL_Color * const color = src->format->palette->colors+(*sp);
  435. if (dst->format->BytesPerPixel == 4)
  436. *(--dp) = 0;
  437. *(--dp) = color->r;
  438. *(--dp) = color->g;
  439. *(--dp) = color->b;
  440. }
  441. }
  442. }
  443. 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
  444. {
  445. SDL_Rect realDest;
  446. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  447. blitWithRotate1(src,srcRect,dst,&realDest);
  448. }
  449. void CSDL_Ext::blitWithRotate2(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  450. {
  451. Uint8 *dp, *sp = (Uint8 *)src->pixels;
  452. for(int i=0; i<dstRect->h; i++)
  453. {
  454. dp = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1 - i)*dst->pitch + dstRect->x*dst->format->BytesPerPixel;
  455. for(int j=0; j<dstRect->w; j++, sp++)
  456. {
  457. const SDL_Color * const color = src->format->palette->colors+(*sp);
  458. *(dp++) = color->b;
  459. *(dp++) = color->g;
  460. *(dp++) = color->r;
  461. if (dst->format->BytesPerPixel == 4)
  462. *(dp++) = 0;
  463. }
  464. sp += src->w - dstRect->w;
  465. }
  466. }
  467. void CSDL_Ext::blitWithRotate2clip(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  468. {
  469. SDL_Rect realDest;
  470. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  471. blitWithRotate2(src,srcRect,dst,&realDest);
  472. }
  473. 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
  474. {
  475. Uint8 *dp, *sp = (Uint8 *)src->pixels;
  476. for(int i=0; i<dstRect->h; i++)
  477. {
  478. sp += src->w - dstRect->w;
  479. dp = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1 - i)*dst->pitch + (dstRect->x+dstRect->w)*dst->format->BytesPerPixel;
  480. for(int j=0; j<dstRect->w; j++, sp++)
  481. {
  482. const SDL_Color * const color = src->format->palette->colors+(*sp);
  483. if (dst->format->BytesPerPixel == 4)
  484. *(--dp) = 0;
  485. *(--dp) = color->r;
  486. *(--dp) = color->g;
  487. *(--dp) = color->b;
  488. }
  489. }
  490. }
  491. 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
  492. {
  493. SDL_Rect realDest;
  494. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  495. blitWithRotate3(src,srcRect,dst,&realDest);
  496. }
  497. int CSDL_Ext::blit8bppAlphaTo24bpp(SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  498. {
  499. if(src && src->format->BytesPerPixel==1 && dst && (dst->format->BytesPerPixel==3 || dst->format->BytesPerPixel==4)) //everything's ok
  500. {
  501. SDL_Rect fulldst;
  502. int srcx, srcy, w, h;
  503. /* Make sure the surfaces aren't locked */
  504. if ( ! src || ! dst )
  505. {
  506. SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  507. return(-1);
  508. }
  509. if ( src->locked || dst->locked )
  510. {
  511. SDL_SetError("Surfaces must not be locked during blit");
  512. return(-1);
  513. }
  514. /* If the destination rectangle is NULL, use the entire dest surface */
  515. if ( dstRect == NULL )
  516. {
  517. fulldst.x = fulldst.y = 0;
  518. dstRect = &fulldst;
  519. }
  520. /* clip the source rectangle to the source surface */
  521. if(srcRect)
  522. {
  523. int maxw, maxh;
  524. srcx = srcRect->x;
  525. w = srcRect->w;
  526. if(srcx < 0)
  527. {
  528. w += srcx;
  529. dstRect->x -= srcx;
  530. srcx = 0;
  531. }
  532. maxw = src->w - srcx;
  533. if(maxw < w)
  534. w = maxw;
  535. srcy = srcRect->y;
  536. h = srcRect->h;
  537. if(srcy < 0)
  538. {
  539. h += srcy;
  540. dstRect->y -= srcy;
  541. srcy = 0;
  542. }
  543. maxh = src->h - srcy;
  544. if(maxh < h)
  545. h = maxh;
  546. }
  547. else
  548. {
  549. srcx = srcy = 0;
  550. w = src->w;
  551. h = src->h;
  552. }
  553. /* clip the destination rectangle against the clip rectangle */
  554. {
  555. SDL_Rect *clip = &dst->clip_rect;
  556. int dx, dy;
  557. dx = clip->x - dstRect->x;
  558. if(dx > 0)
  559. {
  560. w -= dx;
  561. dstRect->x += dx;
  562. srcx += dx;
  563. }
  564. dx = dstRect->x + w - clip->x - clip->w;
  565. if(dx > 0)
  566. w -= dx;
  567. dy = clip->y - dstRect->y;
  568. if(dy > 0)
  569. {
  570. h -= dy;
  571. dstRect->y += dy;
  572. srcy += dy;
  573. }
  574. dy = dstRect->y + h - clip->y - clip->h;
  575. if(dy > 0)
  576. h -= dy;
  577. }
  578. if(w > 0 && h > 0)
  579. {
  580. dstRect->w = w;
  581. dstRect->h = h;
  582. if(SDL_LockSurface(dst))
  583. return -1; //if we cannot lock the surface
  584. const int bpp = dst->format->BytesPerPixel;
  585. const SDL_Color *colors = src->format->palette->colors;
  586. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  587. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  588. if(dst->format->Rshift==0) //like in most surfaces
  589. {
  590. for(int y=0; y<h; y++, colory+=src->pitch, py+=dst->pitch)
  591. {
  592. Uint8 *color = colory;
  593. Uint8 *p = py;
  594. for(int x=0; x<w; ++x, color++, p += bpp)
  595. {
  596. const SDL_Color tbc = colors[*color]; //color to blit
  597. // According analyze, the values of tbc.unused are fixed,
  598. // and the approximate ratios are as following:
  599. //
  600. // tbc.unused numbers
  601. // 192 2679
  602. // 164 326907
  603. // 82 705590
  604. // 214 1292625
  605. // 128 4842923
  606. // 0 72138078
  607. // 255 77547326
  608. //
  609. // By making use of such characteristic, we may implement a
  610. // very fast algorithm for heroes3 without loose much quality.
  611. switch (tbc.unused)
  612. {
  613. case 255:
  614. break;
  615. case 0:
  616. p[0] = tbc.r;
  617. p[1] = tbc.g;
  618. p[2] = tbc.b;
  619. break;
  620. case 128: // optimized
  621. p[0] = ((Uint16)tbc.r + (Uint16)p[0]) >> 1;
  622. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  623. p[2] = ((Uint16)tbc.b + (Uint16)p[2]) >> 1;
  624. break;
  625. default:
  626. p[0] = ((((Uint32)p[0]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  627. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  628. p[2] = ((((Uint32)p[2]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  629. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  630. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  631. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  632. break;
  633. }
  634. }
  635. }
  636. }
  637. else if(dst->format->Rshift==16) //such as screen
  638. {
  639. for(int y=0; y<h; y++, colory+=src->pitch, py+=dst->pitch)
  640. {
  641. Uint8 *color = colory;
  642. Uint8 *p = py;
  643. for(int x=0; x<w; x++, color++, p += bpp)
  644. {
  645. const SDL_Color tbc = colors[*color]; //color to blit
  646. switch (tbc.unused)
  647. {
  648. case 255:
  649. break;
  650. case 0:
  651. p[0] = tbc.b;
  652. p[1] = tbc.g;
  653. p[2] = tbc.r;
  654. break;
  655. case 128: // optimized
  656. p[0] = ((Uint16)tbc.b + (Uint16)p[0]) >> 1;
  657. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  658. p[2] = ((Uint16)tbc.r + (Uint16)p[2]) >> 1;
  659. break;
  660. default:
  661. p[0] = ((((Uint32)p[0]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  662. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  663. p[2] = ((((Uint32)p[2]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  664. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  665. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  666. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  667. break;
  668. }
  669. }
  670. }
  671. }
  672. SDL_UnlockSurface(dst);
  673. }
  674. }
  675. return 0;
  676. }
  677. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  678. {
  679. Uint32 ret = 0;
  680. ret+=color->unused;
  681. ret<<=8; //*=256
  682. ret+=color->b;
  683. ret<<=8; //*=256
  684. ret+=color->g;
  685. ret<<=8; //*=256
  686. ret+=color->r;
  687. return ret;
  688. }
  689. void CSDL_Ext::update(SDL_Surface * what)
  690. {
  691. if(what)
  692. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  693. }
  694. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, int3 color)
  695. {
  696. for(int i=0;i<w;i++)
  697. {
  698. SDL_PutPixelWithoutRefresh(sur,x+i,y,color.x,color.y,color.z);
  699. SDL_PutPixelWithoutRefresh(sur,x+i,y+h-1,color.x,color.y,color.z);
  700. }
  701. for(int i=0; i<h;i++)
  702. {
  703. SDL_PutPixelWithoutRefresh(sur,x,y+i,color.x,color.y,color.z);
  704. SDL_PutPixelWithoutRefresh(sur,x+w-1,y+i,color.x,color.y,color.z);
  705. }
  706. }
  707. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, unsigned char player)
  708. {
  709. if(player==254)
  710. return;
  711. if(sur->format->BitsPerPixel==8)
  712. {
  713. if(player != 255)
  714. *(sur->format->palette->colors+5) = graphics->playerColors[player];
  715. else
  716. *(sur->format->palette->colors+5) = *graphics->neutralColor;
  717. }
  718. }
  719. int readNormalNr (std::istream &in, int bytCon)
  720. {
  721. int ret=0;
  722. int amp=1;
  723. unsigned char byte;
  724. if (in.good())
  725. {
  726. for (int i=0; i<bytCon; i++)
  727. {
  728. in.read((char*)&byte,1);
  729. ret+=byte*amp;
  730. amp<<=8;
  731. }
  732. }
  733. else return -1;
  734. return ret;
  735. }
  736. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  737. {
  738. for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
  739. {
  740. boost::replace_first(str,"%s",tor[i]);
  741. }
  742. return str;
  743. }
  744. SDL_Surface * CSDL_Ext::std32bppSurface = NULL;