SDL_Extensions.cpp 19 KB

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