2
0

SDL_Extensions.cpp 23 KB

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