SDL_Extensions.cpp 24 KB

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