SDL_Extensions.cpp 22 KB

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