SDL_Extensions.cpp 21 KB

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