SDL_Extensions.cpp 21 KB

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