SDL_Extensions.cpp 23 KB

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