SDL_Extensions.cpp 23 KB

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