SDL_Extensions.cpp 21 KB

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