SDL_Extensions.cpp 22 KB

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