SDL_Extensions.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. bool isItIn(const SDL_Rect * rect, int x, int y)
  9. {
  10. if ((x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h))
  11. return true;
  12. else return false;
  13. }
  14. SDL_Rect genRect(int hh, int ww, int xx, int yy)
  15. {
  16. SDL_Rect ret;
  17. ret.h=hh;
  18. ret.w=ww;
  19. ret.x=xx;
  20. ret.y=yy;
  21. return ret;
  22. }
  23. void blitAtWR(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  24. {
  25. SDL_Rect pom = genRect(src->h,src->w,x,y);
  26. SDL_BlitSurface(src,NULL,dst,&pom);
  27. SDL_UpdateRect(dst,x,y,src->w,src->h);
  28. }
  29. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  30. {
  31. SDL_Rect pom = genRect(src->h,src->w,x,y);
  32. SDL_BlitSurface(src,NULL,dst,&pom);
  33. }
  34. SDL_Color genRGB(int r, int g, int b, int a=0)
  35. {
  36. SDL_Color ret;
  37. ret.b=b;
  38. ret.g=g;
  39. ret.r=r;
  40. ret.unused=a;
  41. return ret;
  42. }
  43. void updateRect (SDL_Rect * rect, SDL_Surface * scr)
  44. {
  45. SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
  46. }
  47. void CSDL_Ext::printAtMiddle(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  48. {
  49. SDL_Surface * temp;
  50. switch (quality)
  51. {
  52. case 0:
  53. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  54. break;
  55. case 1:
  56. SDL_Color tem;
  57. tem.b = 0xff-kolor.b;
  58. tem.g = 0xff-kolor.g;
  59. tem.r = 0xff-kolor.r;
  60. tem.unused = 0xff-kolor.unused;
  61. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  62. break;
  63. case 2:
  64. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  65. break;
  66. default:
  67. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  68. break;
  69. }
  70. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-(temp->w/2),y-(temp->h/2)));
  71. SDL_UpdateRect(dst,x-(temp->w/2),y-(temp->h/2),temp->w,temp->h);
  72. SDL_FreeSurface(temp);
  73. }
  74. void CSDL_Ext::printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  75. {
  76. SDL_Surface * temp;
  77. switch (quality)
  78. {
  79. case 0:
  80. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  81. break;
  82. case 1:
  83. SDL_Color tem;
  84. tem.b = 0xff-kolor.b;
  85. tem.g = 0xff-kolor.g;
  86. tem.r = 0xff-kolor.r;
  87. tem.unused = 0xff-kolor.unused;
  88. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  89. break;
  90. case 2:
  91. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  92. break;
  93. default:
  94. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  95. break;
  96. }
  97. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x,y));
  98. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  99. SDL_FreeSurface(temp);
  100. }
  101. void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC, Uint8 A)
  102. {
  103. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC;
  104. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  105. {
  106. p[0] = R;
  107. p[1] = G;
  108. p[2] = B;
  109. }
  110. else
  111. {
  112. p[0] = B;
  113. p[1] = G;
  114. p[2] = R;
  115. }
  116. SDL_UpdateRect(ekran, x, y, 1, 1);
  117. }
  118. ///**************/
  119. ///Reverses the toRot surface by the vertical axis
  120. ///**************/
  121. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot, int myC)
  122. {
  123. 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);
  124. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  125. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  126. if(toRot->format->BytesPerPixel!=1)
  127. {
  128. for(int i=0; i<ret->w; ++i)
  129. {
  130. for(int j=0; j<ret->h; ++j)
  131. {
  132. {
  133. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  134. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  135. {
  136. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], myC);
  137. }
  138. else
  139. {
  140. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], myC);
  141. }
  142. }
  143. }
  144. }
  145. }
  146. else
  147. {
  148. for(int i=0; i<ret->w; ++i)
  149. {
  150. for(int j=0; j<ret->h; ++j)
  151. {
  152. {
  153. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  154. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  155. }
  156. }
  157. }
  158. }
  159. SDL_FreeSurface(first);
  160. return ret;
  161. }
  162. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  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(ret->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 + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel-2;
  175. int k=2;
  176. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  177. {
  178. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k);
  179. }
  180. else
  181. {
  182. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k);
  183. }
  184. }
  185. }
  186. }
  187. }
  188. else
  189. {
  190. for(int i=0; i<ret->w; ++i)
  191. {
  192. for(int j=0; j<ret->h; ++j)
  193. {
  194. {
  195. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel;
  196. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  197. }
  198. }
  199. }
  200. }
  201. SDL_FreeSurface(first);
  202. return ret;
  203. };
  204. ///**************/
  205. ///Rotates toRot surface by 90 degrees left
  206. ///**************/
  207. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  208. {
  209. 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);
  210. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  211. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  212. for(int i=0; i<ret->w; ++i)
  213. {
  214. for(int j=0; j<ret->h; ++j)
  215. {
  216. {
  217. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  218. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  219. {
  220. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  221. }
  222. else
  223. {
  224. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  225. }
  226. }
  227. }
  228. }
  229. SDL_FreeSurface(first);
  230. return ret;
  231. }
  232. ///*************/
  233. ///Rotates toRot surface by 180 degrees
  234. ///*************/
  235. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  236. {
  237. 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);
  238. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  239. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  240. if(ret->format->BytesPerPixel!=1)
  241. {
  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 + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  248. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  249. {
  250. SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  251. }
  252. else
  253. {
  254. SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  255. }
  256. }
  257. }
  258. }
  259. }
  260. else
  261. {
  262. for(int i=0; i<ret->w; ++i)
  263. {
  264. for(int j=0; j<ret->h; ++j)
  265. {
  266. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  267. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  268. }
  269. }
  270. }
  271. SDL_FreeSurface(first);
  272. return ret;
  273. }
  274. //converts surface to cursor
  275. SDL_Cursor * CSDL_Ext::SurfaceToCursor(SDL_Surface *image, int hx, int hy)
  276. {
  277. int w, x, y;
  278. Uint8 *data, *mask, *d, *m, r, g, b;
  279. Uint32 color;
  280. SDL_Cursor *cursor;
  281. w = (image->w + 7) / 8;
  282. data = (Uint8 *)alloca(w * image->h * 2);
  283. if (data == NULL)
  284. return NULL;
  285. memset(data, 0, w * image->h * 2);
  286. mask = data + w * image->h;
  287. if (SDL_MUSTLOCK(image))
  288. SDL_LockSurface(image);
  289. for (y = 0; y < image->h; y++)
  290. {
  291. d = data + y * w;
  292. m = mask + y * w;
  293. for (x = 0; x < image->w; x++)
  294. {
  295. color = CSDL_Ext::SDL_GetPixel(image, x, y);
  296. if ((image->flags & SDL_SRCCOLORKEY) == 0 || color != image->format->colorkey)
  297. {
  298. SDL_GetRGB(color, image->format, &r, &g, &b);
  299. color = (r + g + b) / 3;
  300. m[x / 8] |= 128 >> (x & 7);
  301. if (color < 128)
  302. d[x / 8] |= 128 >> (x & 7);
  303. }
  304. }
  305. }
  306. if (SDL_MUSTLOCK(image))
  307. SDL_UnlockSurface(image);
  308. cursor = SDL_CreateCursor(data, mask, w, image->h, hx, hy);
  309. return cursor;
  310. }
  311. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y, bool colorByte)
  312. {
  313. int bpp = surface->format->BytesPerPixel;
  314. /* Here p is the address to the pixel we want to retrieve */
  315. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  316. switch(bpp) {
  317. case 1:
  318. if(colorByte)
  319. {
  320. return colorToUint32(surface->format->palette->colors+(*p));
  321. }
  322. else
  323. return *p;
  324. case 2:
  325. return *(Uint16 *)p;
  326. case 3:
  327. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  328. return p[0] << 16 | p[1] << 8 | p[2];
  329. else
  330. return p[0] | p[1] << 8 | p[2] << 16;
  331. case 4:
  332. return *(Uint32 *)p;
  333. default:
  334. return 0; /* shouldn't happen, but avoids warnings */
  335. }
  336. }
  337. SDL_Surface * CSDL_Ext::alphaTransform(SDL_Surface *src)
  338. {
  339. Uint32 trans = SDL_MapRGBA(src->format, 0, 255, 255, 255);
  340. SDL_SetColorKey(src, 0, trans);
  341. src->flags|=SDL_SRCALPHA;
  342. if(src->format->BitsPerPixel == 8)
  343. {
  344. for(int yy=0; yy<src->format->palette->ncolors; ++yy)
  345. {
  346. SDL_Color cur = *(src->format->palette->colors+yy);
  347. //if(cur.r == 255 && cur.b == 255)
  348. if(yy==1 || yy==2 || yy==3 || yy==4 || yy==8)
  349. {
  350. SDL_Color shadow;
  351. shadow.b = shadow.g = shadow.r = 0;
  352. switch(cur.g) //change this values; make diffrerent for objects and shadows (?)
  353. {
  354. case 0:
  355. shadow.unused = 128;
  356. break;
  357. case 50:
  358. shadow.unused = 50+32;
  359. break;
  360. case 100:
  361. shadow.unused = 100+64;
  362. break;
  363. case 128:
  364. shadow.unused = 128+64;
  365. break;
  366. case 150:
  367. shadow.unused = 150+64;
  368. break;
  369. default:
  370. shadow.unused = 255;
  371. break;
  372. }
  373. SDL_SetColors(src, &shadow, yy, 1);
  374. }
  375. if(yy==0 || (cur.r == 255 && cur.g == 0 && cur.b == 0))
  376. {
  377. SDL_Color transp;
  378. transp.b = transp.g = transp.r = 0;
  379. transp.unused = 255;
  380. SDL_SetColors(src, &transp, yy, 1);
  381. }
  382. }
  383. }
  384. SDL_UpdateRect(src, 0, 0, src->w, src->h);
  385. return src;
  386. }
  387. SDL_Surface * CSDL_Ext::secondAlphaTransform(SDL_Surface * src, SDL_Surface * alpha)
  388. {
  389. Uint32 pompom[256][256];
  390. for(int i=0; i<src->w; ++i)
  391. {
  392. for(int j=0; j<src->h; ++j)
  393. {
  394. pompom[i][j] = 0xffffffff - (SDL_GetPixel(src, i, j, true) & 0xff000000);
  395. }
  396. }
  397. Uint32 pompom2[256][256];
  398. for(int i=0; i<src->w; ++i)
  399. {
  400. for(int j=0; j<src->h; ++j)
  401. {
  402. pompom2[i][j] = pompom[i][j]>>24;
  403. }
  404. }
  405. SDL_Surface * hide2 = SDL_ConvertSurface(src, alpha->format, SDL_SWSURFACE);
  406. for(int i=0; i<hide2->w; ++i)
  407. {
  408. for(int j=0; j<hide2->h; ++j)
  409. {
  410. Uint32 * place = (Uint32*)( (Uint8*)hide2->pixels + j * hide2->pitch + i * hide2->format->BytesPerPixel);
  411. (*place)&=pompom[i][j];
  412. int ffgg=0;
  413. }
  414. }
  415. return hide2;
  416. }
  417. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  418. {
  419. Uint32 ret = 0;
  420. ret+=color->unused;
  421. ret*=256;
  422. ret+=color->b;
  423. ret*=256;
  424. ret+=color->g;
  425. ret*=256;
  426. ret+=color->r;
  427. return ret;
  428. }
  429. void CSDL_Ext::update(SDL_Surface * what)
  430. {
  431. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  432. }
  433. void CSDL_Ext::blueToPlayers(SDL_Surface * sur, int player)
  434. {
  435. if(sur->format->BitsPerPixel == 8)
  436. {
  437. for(int i=0; i<sur->format->palette->ncolors; ++i)
  438. {
  439. SDL_Color * cc = sur->format->palette->colors+i;
  440. if(cc->r==0 && cc->g==0 && cc->b==255)
  441. {
  442. cc->r = CGameInfo::mainObj->playerColors[player].r;
  443. cc->g = CGameInfo::mainObj->playerColors[player].g;
  444. cc->b = CGameInfo::mainObj->playerColors[player].b;
  445. }
  446. }
  447. }
  448. else if(sur->format->BitsPerPixel == 24)
  449. {
  450. for(int y=0; y<sur->h; ++y)
  451. {
  452. for(int x=0; x<sur->w; ++x)
  453. {
  454. Uint8* cp = (Uint8*)sur->pixels + y+sur->pitch + x*3;
  455. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  456. {
  457. if(cp[0]==0 && cp[1]==0 && cp[2]==255)
  458. {
  459. cp[0] = CGameInfo::mainObj->playerColors[player].r;
  460. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  461. cp[2] = CGameInfo::mainObj->playerColors[player].b;
  462. }
  463. }
  464. else
  465. {
  466. if(cp[0]==255 && cp[1]==0 && cp[2]==0)
  467. {
  468. cp[0] = CGameInfo::mainObj->playerColors[player].b;
  469. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  470. cp[2] = CGameInfo::mainObj->playerColors[player].r;
  471. }
  472. }
  473. }
  474. }
  475. }
  476. }
  477. void CSDL_Ext::blueToPlayersAdv(SDL_Surface * sur, int player)
  478. {
  479. if(sur->format->BitsPerPixel == 8)
  480. {
  481. for(int i=0; i<sur->format->palette->ncolors; ++i)
  482. {
  483. SDL_Color * cc = sur->format->palette->colors+i;
  484. if(cc->b>cc->g && cc->b>cc->r)
  485. {
  486. std::vector<long long int> sort1;
  487. sort1.push_back(cc->r);
  488. sort1.push_back(cc->g);
  489. sort1.push_back(cc->b);
  490. std::vector< std::pair<long long int, Uint8*> > sort2;
  491. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cc->r)));
  492. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cc->g)));
  493. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cc->b)));
  494. std::sort(sort1.begin(), sort1.end());
  495. if(sort2[0].first>sort2[1].first)
  496. std::swap(sort2[0], sort2[1]);
  497. if(sort2[1].first>sort2[2].first)
  498. std::swap(sort2[1], sort2[2]);
  499. if(sort2[0].first>sort2[1].first)
  500. std::swap(sort2[0], sort2[1]);
  501. for(int hh=0; hh<3; ++hh)
  502. {
  503. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  504. }
  505. }
  506. }
  507. }
  508. else if(sur->format->BitsPerPixel == 24)
  509. {
  510. for(int y=0; y<sur->h; ++y)
  511. {
  512. for(int x=0; x<sur->w; ++x)
  513. {
  514. Uint8* cp = (Uint8*)sur->pixels + y+sur->pitch + x*3;
  515. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  516. {
  517. if(cp[2]>cp[1] && cp[2]>cp[0])
  518. {
  519. std::vector<long long int> sort1;
  520. sort1.push_back(cp[0]);
  521. sort1.push_back(cp[1]);
  522. sort1.push_back(cp[2]);
  523. std::vector< std::pair<long long int, Uint8*> > sort2;
  524. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[0])));
  525. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1])));
  526. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[2])));
  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] + sort2[hh].first)/2.2;
  537. }
  538. }
  539. }
  540. else
  541. {
  542. if(cp[0]>cp[1] && cp[0]>cp[2])
  543. {
  544. std::vector<long long int> sort1;
  545. sort1.push_back(cp[2]);
  546. sort1.push_back(cp[1]);
  547. sort1.push_back(cp[0]);
  548. std::vector< std::pair<long long int, Uint8*> > sort2;
  549. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cp[2])));
  550. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cp[1])));
  551. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cp[0])));
  552. std::sort(sort1.begin(), sort1.end());
  553. if(sort2[0].first>sort2[1].first)
  554. std::swap(sort2[0], sort2[1]);
  555. if(sort2[1].first>sort2[2].first)
  556. std::swap(sort2[1], sort2[2]);
  557. if(sort2[0].first>sort2[1].first)
  558. std::swap(sort2[0], sort2[1]);
  559. for(int hh=0; hh<3; ++hh)
  560. {
  561. (*sort2[hh].second) = (sort1[hh]*0.8 + sort2[hh].first)/2;
  562. }
  563. }
  564. }
  565. }
  566. }
  567. }
  568. }
  569. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, int player)
  570. {
  571. if(sur->format->BitsPerPixel==8)
  572. {
  573. if(player != -1)
  574. *(sur->format->palette->colors+5) = CGameInfo::mainObj->playerColors[player];
  575. else
  576. *(sur->format->palette->colors+5) = CGameInfo::mainObj->neutralColor;
  577. }
  578. }