SDL_Extensions.cpp 24 KB

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