SDL_Extensions.cpp 18 KB

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