SDL_Extensions.cpp 23 KB

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