2
0

SDL_Extensions.cpp 20 KB

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