SDL_Extensions.cpp 20 KB

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