SDL_Extensions.cpp 21 KB

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