SDL_Extensions.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  12. {
  13. return SDL_CreateRGBSurface(mod->flags,w,h,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  14. }
  15. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  16. {
  17. SDL_Surface * ret = newSurface(mod->w,mod->h,mod);
  18. SDL_BlitSurface(mod,NULL,ret,NULL);
  19. return ret;
  20. }
  21. bool isItIn(const SDL_Rect * rect, int x, int y)
  22. {
  23. if ((x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h))
  24. return true;
  25. else return false;
  26. }
  27. SDL_Rect genRect(int hh, int ww, int xx, int yy)
  28. {
  29. SDL_Rect ret;
  30. ret.h=hh;
  31. ret.w=ww;
  32. ret.x=xx;
  33. ret.y=yy;
  34. return ret;
  35. }
  36. void blitAtWR(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  37. {
  38. SDL_Rect pom = genRect(src->h,src->w,x,y);
  39. SDL_BlitSurface(src,NULL,dst,&pom);
  40. SDL_UpdateRect(dst,x,y,src->w,src->h);
  41. }
  42. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  43. {
  44. SDL_Rect pom = genRect(src->h,src->w,x,y);
  45. SDL_BlitSurface(src,NULL,dst,&pom);
  46. }
  47. SDL_Color genRGB(int r, int g, int b, int a=0)
  48. {
  49. SDL_Color ret;
  50. ret.b=b;
  51. ret.g=g;
  52. ret.r=r;
  53. ret.unused=a;
  54. return ret;
  55. }
  56. void updateRect (SDL_Rect * rect, SDL_Surface * scr)
  57. {
  58. SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
  59. }
  60. void CSDL_Ext::printAtMiddleWB(std::string text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  61. {
  62. std::vector<std::string> * ws = CMessage::breakText(text,charpr);
  63. std::vector<SDL_Surface*> wesu;
  64. wesu.resize(ws->size());
  65. for (int i=0;i<wesu.size();i++)
  66. wesu[i]=TTF_RenderText_Blended(font,(*ws)[i].c_str(),kolor);
  67. int tox=0, toy=0;
  68. for (int i=0;i<wesu.size();i++)
  69. {
  70. toy+=wesu[i]->h;
  71. if (tox < wesu[i]->w)
  72. tox=wesu[i]->w;
  73. }
  74. int evx, evy = y - (toy/2);
  75. for (int i=0;i<wesu.size();i++)
  76. {
  77. evx = (x - (tox/2)) + ((tox-wesu[i]->w)/2);
  78. blitAt(wesu[i],evx,evy);
  79. evy+=wesu[i]->h;
  80. }
  81. for (int i=0;i<wesu.size();i++)
  82. SDL_FreeSurface(wesu[i]);
  83. delete ws;
  84. }
  85. void CSDL_Ext::printAtMiddle(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  86. {
  87. if(text.length()==0) return;
  88. SDL_Surface * temp;
  89. switch (quality)
  90. {
  91. case 0:
  92. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  93. break;
  94. case 1:
  95. SDL_Color tem;
  96. tem.b = 0xff-kolor.b;
  97. tem.g = 0xff-kolor.g;
  98. tem.r = 0xff-kolor.r;
  99. tem.unused = 0xff-kolor.unused;
  100. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  101. break;
  102. case 2:
  103. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  104. break;
  105. default:
  106. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  107. break;
  108. }
  109. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-(temp->w/2),y-(temp->h/2)));
  110. SDL_UpdateRect(dst,x-(temp->w/2),y-(temp->h/2),temp->w,temp->h);
  111. SDL_FreeSurface(temp);
  112. }
  113. void CSDL_Ext::printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  114. {
  115. if (text.length()==0)
  116. 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,y));
  139. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  140. SDL_FreeSurface(temp);
  141. }
  142. void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC, Uint8 A)
  143. {
  144. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC;
  145. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  146. {
  147. p[0] = R;
  148. p[1] = G;
  149. p[2] = B;
  150. }
  151. else
  152. {
  153. p[0] = B;
  154. p[1] = G;
  155. p[2] = R;
  156. }
  157. SDL_UpdateRect(ekran, x, y, 1, 1);
  158. }
  159. ///**************/
  160. ///Reverses the toRot surface by the vertical axis
  161. ///**************/
  162. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot, int myC)
  163. {
  164. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->w, toRot->h, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  165. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  166. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  167. if(toRot->format->BytesPerPixel!=1)
  168. {
  169. for(int i=0; i<ret->w; ++i)
  170. {
  171. for(int j=0; j<ret->h; ++j)
  172. {
  173. {
  174. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  175. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  176. {
  177. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], myC);
  178. }
  179. else
  180. {
  181. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], myC);
  182. }
  183. }
  184. }
  185. }
  186. }
  187. else
  188. {
  189. for(int i=0; i<ret->w; ++i)
  190. {
  191. for(int j=0; j<ret->h; ++j)
  192. {
  193. {
  194. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  195. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  196. }
  197. }
  198. }
  199. }
  200. SDL_FreeSurface(first);
  201. return ret;
  202. }
  203. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  204. {
  205. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->w, toRot->h, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  206. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  207. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  208. if(ret->format->BytesPerPixel!=1)
  209. {
  210. for(int i=0; i<ret->w; ++i)
  211. {
  212. for(int j=0; j<ret->h; ++j)
  213. {
  214. {
  215. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel-2;
  216. int k=2;
  217. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  218. {
  219. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k);
  220. }
  221. else
  222. {
  223. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k);
  224. }
  225. }
  226. }
  227. }
  228. }
  229. else
  230. {
  231. for(int i=0; i<ret->w; ++i)
  232. {
  233. for(int j=0; j<ret->h; ++j)
  234. {
  235. {
  236. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel;
  237. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  238. }
  239. }
  240. }
  241. }
  242. SDL_FreeSurface(first);
  243. return ret;
  244. };
  245. ///**************/
  246. ///Rotates toRot surface by 90 degrees left
  247. ///**************/
  248. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  249. {
  250. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->h, toRot->w, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  251. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  252. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  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 + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  259. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  260. {
  261. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  262. }
  263. else
  264. {
  265. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  266. }
  267. }
  268. }
  269. }
  270. SDL_FreeSurface(first);
  271. return ret;
  272. }
  273. ///*************/
  274. ///Rotates toRot surface by 180 degrees
  275. ///*************/
  276. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  277. {
  278. SDL_Surface * first = SDL_CreateRGBSurface(toRot->flags, toRot->w, toRot->h, toRot->format->BitsPerPixel, toRot->format->Rmask, toRot->format->Gmask, toRot->format->Bmask, toRot->format->Amask);
  279. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  280. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  281. if(ret->format->BytesPerPixel!=1)
  282. {
  283. for(int i=0; i<ret->w; ++i)
  284. {
  285. for(int j=0; j<ret->h; ++j)
  286. {
  287. {
  288. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  289. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  290. {
  291. SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  292. }
  293. else
  294. {
  295. SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  296. }
  297. }
  298. }
  299. }
  300. }
  301. else
  302. {
  303. for(int i=0; i<ret->w; ++i)
  304. {
  305. for(int j=0; j<ret->h; ++j)
  306. {
  307. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  308. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  309. }
  310. }
  311. }
  312. SDL_FreeSurface(first);
  313. return ret;
  314. }
  315. //converts surface to cursor
  316. SDL_Cursor * CSDL_Ext::SurfaceToCursor(SDL_Surface *image, int hx, int hy)
  317. {
  318. int w, x, y;
  319. Uint8 *data, *mask, *d, *m, r, g, b;
  320. Uint32 color;
  321. SDL_Cursor *cursor;
  322. w = (image->w + 7) / 8;
  323. data = (Uint8 *)alloca(w * image->h * 2);
  324. if (data == NULL)
  325. return NULL;
  326. memset(data, 0, w * image->h * 2);
  327. mask = data + w * image->h;
  328. if (SDL_MUSTLOCK(image))
  329. SDL_LockSurface(image);
  330. for (y = 0; y < image->h; y++)
  331. {
  332. d = data + y * w;
  333. m = mask + y * w;
  334. for (x = 0; x < image->w; x++)
  335. {
  336. color = CSDL_Ext::SDL_GetPixel(image, x, y);
  337. if ((image->flags & SDL_SRCCOLORKEY) == 0 || color != image->format->colorkey)
  338. {
  339. SDL_GetRGB(color, image->format, &r, &g, &b);
  340. color = (r + g + b) / 3;
  341. m[x / 8] |= 128 >> (x & 7);
  342. if (color < 128)
  343. d[x / 8] |= 128 >> (x & 7);
  344. }
  345. }
  346. }
  347. if (SDL_MUSTLOCK(image))
  348. SDL_UnlockSurface(image);
  349. cursor = SDL_CreateCursor(data, mask, w, image->h, hx, hy);
  350. return cursor;
  351. }
  352. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y, bool colorByte)
  353. {
  354. int bpp = surface->format->BytesPerPixel;
  355. /* Here p is the address to the pixel we want to retrieve */
  356. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  357. switch(bpp) {
  358. case 1:
  359. if(colorByte)
  360. {
  361. return colorToUint32(surface->format->palette->colors+(*p));
  362. }
  363. else
  364. return *p;
  365. case 2:
  366. return *(Uint16 *)p;
  367. case 3:
  368. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  369. return p[0] << 16 | p[1] << 8 | p[2];
  370. else
  371. return p[0] | p[1] << 8 | p[2] << 16;
  372. case 4:
  373. return *(Uint32 *)p;
  374. default:
  375. return 0; /* shouldn't happen, but avoids warnings */
  376. }
  377. }
  378. SDL_Surface * CSDL_Ext::alphaTransform(SDL_Surface *src)
  379. {
  380. Uint32 trans = SDL_MapRGBA(src->format, 0, 255, 255, 255);
  381. SDL_SetColorKey(src, 0, trans);
  382. src->flags|=SDL_SRCALPHA;
  383. if(src->format->BitsPerPixel == 8)
  384. {
  385. for(int yy=0; yy<src->format->palette->ncolors; ++yy)
  386. {
  387. SDL_Color cur = *(src->format->palette->colors+yy);
  388. //if(cur.r == 255 && cur.b == 255)
  389. if(yy==1 || yy==2 || yy==3 || yy==4 || yy==8)
  390. {
  391. SDL_Color shadow;
  392. shadow.b = shadow.g = shadow.r = 0;
  393. switch(cur.g) //change this values; make diffrerent for objects and shadows (?)
  394. {
  395. case 0:
  396. shadow.unused = 128;
  397. break;
  398. case 50:
  399. shadow.unused = 50+32;
  400. break;
  401. case 100:
  402. shadow.unused = 100+64;
  403. break;
  404. case 128:
  405. shadow.unused = 128+64;
  406. break;
  407. case 150:
  408. shadow.unused = 150+64;
  409. break;
  410. default:
  411. shadow.unused = 255;
  412. break;
  413. }
  414. SDL_SetColors(src, &shadow, yy, 1);
  415. }
  416. if(yy==0 || (cur.r == 255 && cur.g == 0 && cur.b == 0))
  417. {
  418. SDL_Color transp;
  419. transp.b = transp.g = transp.r = 0;
  420. transp.unused = 255;
  421. SDL_SetColors(src, &transp, yy, 1);
  422. }
  423. }
  424. }
  425. SDL_UpdateRect(src, 0, 0, src->w, src->h);
  426. return src;
  427. }
  428. SDL_Surface * CSDL_Ext::secondAlphaTransform(SDL_Surface * src, SDL_Surface * alpha)
  429. {
  430. Uint32 pompom[256][256];
  431. for(int i=0; i<src->w; ++i)
  432. {
  433. for(int j=0; j<src->h; ++j)
  434. {
  435. pompom[i][j] = 0xffffffff - (SDL_GetPixel(src, i, j, true) & 0xff000000);
  436. }
  437. }
  438. Uint32 pompom2[256][256];
  439. for(int i=0; i<src->w; ++i)
  440. {
  441. for(int j=0; j<src->h; ++j)
  442. {
  443. pompom2[i][j] = pompom[i][j]>>24;
  444. }
  445. }
  446. SDL_Surface * hide2 = SDL_ConvertSurface(src, alpha->format, SDL_SWSURFACE);
  447. for(int i=0; i<hide2->w; ++i)
  448. {
  449. for(int j=0; j<hide2->h; ++j)
  450. {
  451. Uint32 * place = (Uint32*)( (Uint8*)hide2->pixels + j * hide2->pitch + i * hide2->format->BytesPerPixel);
  452. (*place)&=pompom[i][j];
  453. int ffgg=0;
  454. }
  455. }
  456. return hide2;
  457. }
  458. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  459. {
  460. Uint32 ret = 0;
  461. ret+=color->unused;
  462. ret*=256;
  463. ret+=color->b;
  464. ret*=256;
  465. ret+=color->g;
  466. ret*=256;
  467. ret+=color->r;
  468. return ret;
  469. }
  470. void CSDL_Ext::update(SDL_Surface * what)
  471. {
  472. if(what)
  473. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  474. }
  475. void CSDL_Ext::blueToPlayers(SDL_Surface * sur, int player)
  476. {
  477. if(sur->format->BitsPerPixel == 8)
  478. {
  479. for(int i=0; i<sur->format->palette->ncolors; ++i)
  480. {
  481. SDL_Color * cc = sur->format->palette->colors+i;
  482. if(cc->r==0 && cc->g==0 && cc->b==255)
  483. {
  484. cc->r = CGameInfo::mainObj->playerColors[player].r;
  485. cc->g = CGameInfo::mainObj->playerColors[player].g;
  486. cc->b = CGameInfo::mainObj->playerColors[player].b;
  487. }
  488. }
  489. }
  490. else if(sur->format->BitsPerPixel == 24)
  491. {
  492. for(int y=0; y<sur->h; ++y)
  493. {
  494. for(int x=0; x<sur->w; ++x)
  495. {
  496. Uint8* cp = (Uint8*)sur->pixels + y+sur->pitch + x*3;
  497. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  498. {
  499. if(cp[0]==0 && cp[1]==0 && cp[2]==255)
  500. {
  501. cp[0] = CGameInfo::mainObj->playerColors[player].r;
  502. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  503. cp[2] = CGameInfo::mainObj->playerColors[player].b;
  504. }
  505. }
  506. else
  507. {
  508. if(cp[0]==255 && cp[1]==0 && cp[2]==0)
  509. {
  510. cp[0] = CGameInfo::mainObj->playerColors[player].b;
  511. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  512. cp[2] = CGameInfo::mainObj->playerColors[player].r;
  513. }
  514. }
  515. }
  516. }
  517. }
  518. }
  519. void CSDL_Ext::blueToPlayersAdv(SDL_Surface * sur, int player)
  520. {
  521. if(player==1) //it is actually blue...
  522. return;
  523. if(sur->format->BitsPerPixel == 8)
  524. {
  525. for(int i=0; i<sur->format->palette->ncolors; ++i)
  526. {
  527. SDL_Color * cc = sur->format->palette->colors+i;
  528. if(cc->b>cc->g && cc->b>cc->r)
  529. {
  530. std::vector<long long int> sort1;
  531. sort1.push_back(cc->r);
  532. sort1.push_back(cc->g);
  533. sort1.push_back(cc->b);
  534. std::vector< std::pair<long long int, Uint8*> > sort2;
  535. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cc->r)));
  536. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cc->g)));
  537. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cc->b)));
  538. std::sort(sort1.begin(), sort1.end());
  539. if(sort2[0].first>sort2[1].first)
  540. std::swap(sort2[0], sort2[1]);
  541. if(sort2[1].first>sort2[2].first)
  542. std::swap(sort2[1], sort2[2]);
  543. if(sort2[0].first>sort2[1].first)
  544. std::swap(sort2[0], sort2[1]);
  545. for(int hh=0; hh<3; ++hh)
  546. {
  547. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  548. }
  549. }
  550. }
  551. }
  552. else if(sur->format->BitsPerPixel == 24)
  553. {
  554. for(int y=0; y<sur->h; ++y)
  555. {
  556. for(int x=0; x<sur->w; ++x)
  557. {
  558. Uint8* cp = (Uint8*)sur->pixels + y+sur->pitch + x*3;
  559. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  560. {
  561. if(cp[2]>cp[1] && cp[2]>cp[0])
  562. {
  563. std::vector<long long int> sort1;
  564. sort1.push_back(cp[0]);
  565. sort1.push_back(cp[1]);
  566. sort1.push_back(cp[2]);
  567. std::vector< std::pair<long long int, Uint8*> > sort2;
  568. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[0])));
  569. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1])));
  570. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[2])));
  571. std::sort(sort1.begin(), sort1.end());
  572. if(sort2[0].first>sort2[1].first)
  573. std::swap(sort2[0], sort2[1]);
  574. if(sort2[1].first>sort2[2].first)
  575. std::swap(sort2[1], sort2[2]);
  576. if(sort2[0].first>sort2[1].first)
  577. std::swap(sort2[0], sort2[1]);
  578. for(int hh=0; hh<3; ++hh)
  579. {
  580. (*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2.2;
  581. }
  582. }
  583. }
  584. else
  585. {
  586. if(cp[0]>cp[1] && cp[0]>cp[2])
  587. {
  588. std::vector<long long int> sort1;
  589. sort1.push_back(cp[2]);
  590. sort1.push_back(cp[1]);
  591. sort1.push_back(cp[0]);
  592. std::vector< std::pair<long long int, Uint8*> > sort2;
  593. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[2])));
  594. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1])));
  595. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[0])));
  596. std::sort(sort1.begin(), sort1.end());
  597. if(sort2[0].first>sort2[1].first)
  598. std::swap(sort2[0], sort2[1]);
  599. if(sort2[1].first>sort2[2].first)
  600. std::swap(sort2[1], sort2[2]);
  601. if(sort2[0].first>sort2[1].first)
  602. std::swap(sort2[0], sort2[1]);
  603. for(int hh=0; hh<3; ++hh)
  604. {
  605. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  606. }
  607. }
  608. }
  609. }
  610. }
  611. }
  612. }
  613. void CSDL_Ext::blueToPlayersNice(SDL_Surface * sur, int player) //incomplete, TODO: finish
  614. {
  615. if(sur->format->BitsPerPixel==8)
  616. {
  617. for(int a=0; a<sur->format->palette->ncolors; ++a)
  618. {
  619. for(int s=0; s<CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->ncolors; ++s)
  620. {
  621. if(abs((sur->format->palette->colors+a)->b - (CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->colors+s)->b) < 5
  622. && abs((sur->format->palette->colors+a)->g - (CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->colors+s)->g) < 5
  623. && abs((sur->format->palette->colors+a)->r - (CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->colors+s)->r) < 5
  624. )
  625. {
  626. (sur->format->palette->colors+a)->b = (CGI->playerColorInfo[0]->ourImages[player].bitmap->format->palette->colors+s)->b;
  627. (sur->format->palette->colors+a)->r = (CGI->playerColorInfo[0]->ourImages[player].bitmap->format->palette->colors+s)->g;
  628. (sur->format->palette->colors+a)->g = (CGI->playerColorInfo[0]->ourImages[player].bitmap->format->palette->colors+s)->r;
  629. break;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, unsigned char player)
  636. {
  637. if(player==254)
  638. return;
  639. if(sur->format->BitsPerPixel==8)
  640. {
  641. if(player != 255)
  642. *(sur->format->palette->colors+5) = CGameInfo::mainObj->playerColors[player];
  643. else
  644. *(sur->format->palette->colors+5) = CGameInfo::mainObj->neutralColor;
  645. }
  646. }
  647. int readNormalNr (std::istream &in, int bytCon)
  648. {
  649. int ret=0;
  650. int amp=1;
  651. unsigned char byte;
  652. if (in.good())
  653. {
  654. for (int i=0; i<bytCon; i++)
  655. {
  656. in.read((char*)&byte,1);
  657. ret+=byte*amp;
  658. amp*=256;
  659. }
  660. }
  661. else return -1;
  662. return ret;
  663. }
  664. void CSDL_Ext::fullAlphaTransform(SDL_Surface *& src)
  665. {
  666. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  667. int rmask = 0xff000000;
  668. int gmask = 0x00ff0000;
  669. int bmask = 0x0000ff00;
  670. int amask = 0x000000ff;
  671. #else
  672. int rmask = 0x000000ff;
  673. int gmask = 0x0000ff00;
  674. int bmask = 0x00ff0000;
  675. int amask = 0xff000000;
  676. #endif
  677. src = alphaTransform(src);
  678. SDL_Surface * hlp1, * hlp2;
  679. hlp1 = SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, rmask, gmask, bmask, amask);
  680. hlp2 = secondAlphaTransform(src, hlp1);
  681. SDL_FreeSurface(src);
  682. SDL_FreeSurface(hlp1);
  683. src = hlp2;
  684. }
  685. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  686. {
  687. for (int i=0;(i<tor.size())&&(boost::find_first(str,"%s"));i++)
  688. {
  689. boost::replace_first(str,"%s",tor[i]);
  690. }
  691. return str;
  692. }