SDL_Extensions.cpp 23 KB

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