SDL_Extensions.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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. p[0] = R;
  145. p[1] = G;
  146. p[2] = B;
  147. #else
  148. p[0] = B;
  149. p[1] = G;
  150. p[2] = R;
  151. #endif
  152. SDL_UpdateRect(ekran, x, y, 1, 1);
  153. }
  154. void CSDL_Ext::SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC, Uint8 A)
  155. {
  156. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC;
  157. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  158. p[0] = B;
  159. p[1] = G;
  160. p[2] = R;
  161. #else
  162. p[0] = R;
  163. p[1] = G;
  164. p[2] = B;
  165. #endif
  166. }
  167. ///**************/
  168. ///Reverses the toRot surface by the vertical axis
  169. ///**************/
  170. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot, int myC)
  171. {
  172. 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);
  173. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  174. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  175. if(toRot->format->BytesPerPixel!=1)
  176. {
  177. for(int i=0; i<ret->w; ++i)
  178. {
  179. for(int j=0; j<ret->h; ++j)
  180. {
  181. {
  182. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  183. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  184. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], myC);
  185. #else
  186. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], myC);
  187. #endif
  188. }
  189. }
  190. }
  191. }
  192. else
  193. {
  194. for(int i=0; i<ret->w; ++i)
  195. {
  196. for(int j=0; j<ret->h; ++j)
  197. {
  198. {
  199. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  200. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  201. }
  202. }
  203. }
  204. }
  205. SDL_FreeSurface(first);
  206. return ret;
  207. }
  208. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  209. {
  210. 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);
  211. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  212. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  213. if(ret->format->BytesPerPixel!=1)
  214. {
  215. for(int i=0; i<ret->w; ++i)
  216. {
  217. for(int j=0; j<ret->h; ++j)
  218. {
  219. {
  220. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel-2;
  221. int k=2;
  222. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  223. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k);
  224. #else
  225. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k);
  226. #endif
  227. }
  228. }
  229. }
  230. }
  231. else
  232. {
  233. for(int i=0; i<ret->w; ++i)
  234. {
  235. for(int j=0; j<ret->h; ++j)
  236. {
  237. {
  238. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel;
  239. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  240. }
  241. }
  242. }
  243. }
  244. SDL_FreeSurface(first);
  245. return ret;
  246. };
  247. ///**************/
  248. ///Rotates toRot surface by 90 degrees left
  249. ///**************/
  250. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  251. {
  252. 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);
  253. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  254. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  255. for(int i=0; i<ret->w; ++i)
  256. {
  257. for(int j=0; j<ret->h; ++j)
  258. {
  259. {
  260. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  261. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  262. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  263. #else
  264. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  265. #endif
  266. }
  267. }
  268. }
  269. SDL_FreeSurface(first);
  270. return ret;
  271. }
  272. ///*************/
  273. ///Rotates toRot surface by 180 degrees
  274. ///*************/
  275. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  276. {
  277. 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);
  278. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  279. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  280. if(ret->format->BytesPerPixel!=1)
  281. {
  282. for(int i=0; i<ret->w; ++i)
  283. {
  284. for(int j=0; j<ret->h; ++j)
  285. {
  286. {
  287. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  288. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  289. SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  290. #else
  291. SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  292. #endif
  293. }
  294. }
  295. }
  296. }
  297. else
  298. {
  299. for(int i=0; i<ret->w; ++i)
  300. {
  301. for(int j=0; j<ret->h; ++j)
  302. {
  303. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  304. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  305. }
  306. }
  307. }
  308. SDL_FreeSurface(first);
  309. return ret;
  310. }
  311. //converts surface to cursor
  312. SDL_Cursor * CSDL_Ext::SurfaceToCursor(SDL_Surface *image, int hx, int hy)
  313. {
  314. int w, x, y;
  315. Uint8 *data, *mask, *d, *m, r, g, b;
  316. Uint32 color;
  317. SDL_Cursor *cursor;
  318. w = (image->w + 7) / 8;
  319. data = (Uint8 *)alloca(w * image->h * 2);
  320. if (data == NULL)
  321. return NULL;
  322. memset(data, 0, w * image->h * 2);
  323. mask = data + w * image->h;
  324. if (SDL_MUSTLOCK(image))
  325. SDL_LockSurface(image);
  326. for (y = 0; y < image->h; y++)
  327. {
  328. d = data + y * w;
  329. m = mask + y * w;
  330. for (x = 0; x < image->w; x++)
  331. {
  332. color = CSDL_Ext::SDL_GetPixel(image, x, y);
  333. if ((image->flags & SDL_SRCCOLORKEY) == 0 || color != image->format->colorkey)
  334. {
  335. SDL_GetRGB(color, image->format, &r, &g, &b);
  336. color = (r + g + b) / 3;
  337. m[x / 8] |= 128 >> (x & 7);
  338. if (color < 128)
  339. d[x / 8] |= 128 >> (x & 7);
  340. }
  341. }
  342. }
  343. if (SDL_MUSTLOCK(image))
  344. SDL_UnlockSurface(image);
  345. cursor = SDL_CreateCursor(data, mask, w, image->h, hx, hy);
  346. return cursor;
  347. }
  348. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y, bool colorByte)
  349. {
  350. int bpp = surface->format->BytesPerPixel;
  351. /* Here p is the address to the pixel we want to retrieve */
  352. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  353. switch(bpp) {
  354. case 1:
  355. if(colorByte)
  356. {
  357. return colorToUint32(surface->format->palette->colors+(*p));
  358. }
  359. else
  360. return *p;
  361. case 2:
  362. return *(Uint16 *)p;
  363. case 3:
  364. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  365. return p[0] << 16 | p[1] << 8 | p[2];
  366. #else
  367. return p[0] | p[1] << 8 | p[2] << 16;
  368. #endif
  369. case 4:
  370. return *(Uint32 *)p;
  371. default:
  372. return 0; /* shouldn't happen, but avoids warnings */
  373. }
  374. }
  375. SDL_Surface * CSDL_Ext::alphaTransform(SDL_Surface *src)
  376. {
  377. Uint32 trans = SDL_MapRGBA(src->format, 0, 255, 255, 255);
  378. SDL_SetColorKey(src, 0, trans);
  379. src->flags|=SDL_SRCALPHA;
  380. SDL_Color transp;
  381. transp.b = transp.g = transp.r = 0;
  382. transp.unused = 255;
  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_SetColors(src, &transp, yy, 1);
  419. }
  420. }
  421. }
  422. SDL_UpdateRect(src, 0, 0, src->w, src->h);
  423. return src;
  424. }
  425. SDL_Surface * CSDL_Ext::secondAlphaTransform(SDL_Surface * src, SDL_Surface * alpha)
  426. {
  427. Uint32 pompom[256][256];
  428. for(int i=0; i<src->w; ++i)
  429. {
  430. for(int j=0; j<src->h; ++j)
  431. {
  432. pompom[i][j] = 0xffffffff - (SDL_GetPixel(src, i, j, true) & 0xff000000);
  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. }
  443. }
  444. return hide2;
  445. }
  446. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  447. {
  448. Uint32 ret = 0;
  449. ret+=color->unused;
  450. ret*=256;
  451. ret+=color->b;
  452. ret*=256;
  453. ret+=color->g;
  454. ret*=256;
  455. ret+=color->r;
  456. return ret;
  457. }
  458. void CSDL_Ext::update(SDL_Surface * what)
  459. {
  460. if(what)
  461. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  462. }
  463. void CSDL_Ext::blueToPlayers(SDL_Surface * sur, int player)
  464. {
  465. if(sur->format->BitsPerPixel == 8)
  466. {
  467. for(int i=0; i<sur->format->palette->ncolors; ++i)
  468. {
  469. SDL_Color * cc = sur->format->palette->colors+i;
  470. if(cc->r==0 && cc->g==0 && cc->b==255)
  471. {
  472. cc->r = CGameInfo::mainObj->playerColors[player].r;
  473. cc->g = CGameInfo::mainObj->playerColors[player].g;
  474. cc->b = CGameInfo::mainObj->playerColors[player].b;
  475. }
  476. }
  477. }
  478. else if(sur->format->BitsPerPixel == 24)
  479. {
  480. for(int y=0; y<sur->h; ++y)
  481. {
  482. for(int x=0; x<sur->w; ++x)
  483. {
  484. Uint8* cp = (Uint8*)sur->pixels + y+sur->pitch + x*3;
  485. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  486. {
  487. if(cp[0]==0 && cp[1]==0 && cp[2]==255)
  488. {
  489. cp[0] = CGameInfo::mainObj->playerColors[player].r;
  490. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  491. cp[2] = CGameInfo::mainObj->playerColors[player].b;
  492. }
  493. }
  494. else
  495. {
  496. if(cp[0]==255 && cp[1]==0 && cp[2]==0)
  497. {
  498. cp[0] = CGameInfo::mainObj->playerColors[player].b;
  499. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  500. cp[2] = CGameInfo::mainObj->playerColors[player].r;
  501. }
  502. }
  503. }
  504. }
  505. }
  506. }
  507. void CSDL_Ext::blueToPlayersAdv(SDL_Surface * sur, int player, int mode, void* additionalInfo)
  508. {
  509. if(player==1) //it is actually blue...
  510. return;
  511. if(sur->format->BitsPerPixel == 8)
  512. {
  513. for(int i=0; i<sur->format->palette->ncolors; ++i) //message, button, avmap, resbar
  514. {
  515. SDL_Color * cc = sur->format->palette->colors+i;
  516. if(
  517. ((mode==0) && (cc->b>cc->g) && (cc->b>cc->r)) ||
  518. ((mode==1) && (cc->r<45) && (cc->b>80) && (cc->g<70) && ((cc->b-cc->r)>40)) ||
  519. ((mode==2) && (cc->r<110) && (cc->b>63) && (cc->g<122) && ((cc->b-cc->r)>44) && ((cc->b-cc->g)>32))
  520. )
  521. {
  522. if ((mode==2) && additionalInfo)
  523. {
  524. for (int vi=0; vi<((std::vector<SDL_Color>*)additionalInfo)->size(); vi++)
  525. {
  526. if
  527. (
  528. ((*((std::vector<SDL_Color>*)additionalInfo))[vi].r==cc->r) &&
  529. ((*((std::vector<SDL_Color>*)additionalInfo))[vi].g==cc->g) &&
  530. ((*((std::vector<SDL_Color>*)additionalInfo))[vi].b==cc->b)
  531. )
  532. goto main8bitloopend;
  533. }
  534. }
  535. std::vector<long long int> sort1;
  536. sort1.push_back(cc->r);
  537. sort1.push_back(cc->g);
  538. sort1.push_back(cc->b);
  539. std::vector< std::pair<long long int, Uint8*> > sort2;
  540. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cc->r)));
  541. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cc->g)));
  542. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cc->b)));
  543. std::sort(sort1.begin(), sort1.end());
  544. if(sort2[0].first>sort2[1].first)
  545. std::swap(sort2[0], sort2[1]);
  546. if(sort2[1].first>sort2[2].first)
  547. std::swap(sort2[1], sort2[2]);
  548. if(sort2[0].first>sort2[1].first)
  549. std::swap(sort2[0], sort2[1]);
  550. for(int hh=0; hh<3; ++hh)
  551. {
  552. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  553. }
  554. }
  555. main8bitloopend:
  556. ;
  557. }
  558. }
  559. else if(sur->format->BitsPerPixel == 24)
  560. {
  561. for(int y=0; y<sur->h; ++y)
  562. {
  563. for(int x=0; x<sur->w; ++x)
  564. {
  565. Uint8* cp = (Uint8*)sur->pixels + y*sur->pitch + x*3;
  566. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  567. {
  568. if(cp[2]>cp[1] && cp[2]>cp[0])
  569. {
  570. std::vector<long long int> sort1;
  571. sort1.push_back(cp[0]);
  572. sort1.push_back(cp[1]);
  573. sort1.push_back(cp[2]);
  574. std::vector< std::pair<long long int, Uint8*> > sort2;
  575. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[0])));
  576. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1])));
  577. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[2])));
  578. std::sort(sort1.begin(), sort1.end());
  579. if(sort2[0].first>sort2[1].first)
  580. std::swap(sort2[0], sort2[1]);
  581. if(sort2[1].first>sort2[2].first)
  582. std::swap(sort2[1], sort2[2]);
  583. if(sort2[0].first>sort2[1].first)
  584. std::swap(sort2[0], sort2[1]);
  585. for(int hh=0; hh<3; ++hh)
  586. {
  587. (*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2.2;
  588. }
  589. }
  590. }
  591. else
  592. {
  593. if(
  594. ((mode==0) && (cp[0]>cp[1]) && (cp[0]>cp[2])) ||
  595. ((mode==1) && (cp[2]<45) && (cp[0]>80) && (cp[1]<70) && ((cp[0]-cp[1])>40))
  596. )
  597. {
  598. std::vector<long long int> sort1;
  599. sort1.push_back(cp[2]);
  600. sort1.push_back(cp[1]);
  601. sort1.push_back(cp[0]);
  602. std::vector< std::pair<long long int, Uint8*> > sort2;
  603. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[2])));
  604. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1])));
  605. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[0])));
  606. std::sort(sort1.begin(), sort1.end());
  607. if(sort2[0].first>sort2[1].first)
  608. std::swap(sort2[0], sort2[1]);
  609. if(sort2[1].first>sort2[2].first)
  610. std::swap(sort2[1], sort2[2]);
  611. if(sort2[0].first>sort2[1].first)
  612. std::swap(sort2[0], sort2[1]);
  613. for(int hh=0; hh<3; ++hh)
  614. {
  615. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. }
  622. }
  623. void CSDL_Ext::blueToPlayersNice(SDL_Surface * sur, int player) //incomplete, TODO: finish
  624. {
  625. if(sur->format->BitsPerPixel==8)
  626. {
  627. for(int a=0; a<sur->format->palette->ncolors; ++a)
  628. {
  629. for(int s=0; s<CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->ncolors; ++s)
  630. {
  631. if(abs((sur->format->palette->colors+a)->b - (CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->colors+s)->b) < 5
  632. && abs((sur->format->palette->colors+a)->g - (CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->colors+s)->g) < 5
  633. && abs((sur->format->palette->colors+a)->r - (CGI->playerColorInfo[0]->ourImages[1].bitmap->format->palette->colors+s)->r) < 5
  634. )
  635. {
  636. (sur->format->palette->colors+a)->b = (CGI->playerColorInfo[0]->ourImages[player].bitmap->format->palette->colors+s)->b;
  637. (sur->format->palette->colors+a)->r = (CGI->playerColorInfo[0]->ourImages[player].bitmap->format->palette->colors+s)->g;
  638. (sur->format->palette->colors+a)->g = (CGI->playerColorInfo[0]->ourImages[player].bitmap->format->palette->colors+s)->r;
  639. break;
  640. }
  641. }
  642. }
  643. }
  644. }
  645. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, unsigned char player)
  646. {
  647. if(player==254)
  648. return;
  649. if(sur->format->BitsPerPixel==8)
  650. {
  651. if(player != 255)
  652. *(sur->format->palette->colors+5) = CGameInfo::mainObj->playerColors[player];
  653. else
  654. *(sur->format->palette->colors+5) = CGameInfo::mainObj->neutralColor;
  655. }
  656. }
  657. int readNormalNr (std::istream &in, int bytCon)
  658. {
  659. int ret=0;
  660. int amp=1;
  661. unsigned char byte;
  662. if (in.good())
  663. {
  664. for (int i=0; i<bytCon; i++)
  665. {
  666. in.read((char*)&byte,1);
  667. ret+=byte*amp;
  668. amp*=256;
  669. }
  670. }
  671. else return -1;
  672. return ret;
  673. }
  674. void CSDL_Ext::fullAlphaTransform(SDL_Surface *& src)
  675. {
  676. #if SDL_BYTEORDER == SDL_BIG_ENDIAN
  677. int rmask = 0xff000000;
  678. int gmask = 0x00ff0000;
  679. int bmask = 0x0000ff00;
  680. int amask = 0x000000ff;
  681. #else
  682. int rmask = 0x000000ff;
  683. int gmask = 0x0000ff00;
  684. int bmask = 0x00ff0000;
  685. int amask = 0xff000000;
  686. #endif
  687. src = alphaTransform(src);
  688. SDL_Surface * hlp1, * hlp2;
  689. hlp1 = SDL_CreateRGBSurface(SDL_SWSURFACE, src->w, src->h, 32, rmask, gmask, bmask, amask);
  690. hlp2 = secondAlphaTransform(src, hlp1);
  691. SDL_FreeSurface(src);
  692. SDL_FreeSurface(hlp1);
  693. src = hlp2;
  694. }
  695. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  696. {
  697. for (int i=0;(i<tor.size())&&(boost::find_first(str,"%s"));i++)
  698. {
  699. boost::replace_first(str,"%s",tor[i]);
  700. }
  701. return str;
  702. }