SDL_Extensions.cpp 24 KB

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