SDL_Extensions.cpp 17 KB

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