SDL_Extensions.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. #include "stdafx.h"
  2. #include "SDL_Extensions.h"
  3. #include "SDL_TTF.h"
  4. #include "CGameInfo.h"
  5. #include <iostream>
  6. #include <utility>
  7. #include <algorithm>
  8. bool isItIn(const SDL_Rect * rect, int x, int y)
  9. {
  10. if ((x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h))
  11. return true;
  12. else return false;
  13. }
  14. SDL_Rect genRect(int hh, int ww, int xx, int yy)
  15. {
  16. SDL_Rect ret;
  17. ret.h=hh;
  18. ret.w=ww;
  19. ret.x=xx;
  20. ret.y=yy;
  21. return ret;
  22. }
  23. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=ekran)
  24. {
  25. SDL_Rect pom = genRect(src->h,src->w,x,y);
  26. SDL_BlitSurface(src,NULL,dst,&pom);
  27. }
  28. SDL_Color genRGB(int r, int g, int b, int a=0)
  29. {
  30. SDL_Color ret;
  31. ret.b=b;
  32. ret.g=g;
  33. ret.r=r;
  34. ret.unused=a;
  35. return ret;
  36. }
  37. void updateRect (SDL_Rect * rect, SDL_Surface * scr = ekran)
  38. {
  39. SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
  40. }
  41. void CSDL_Ext::printAtMiddle(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  42. {
  43. SDL_Surface * temp;
  44. switch (quality)
  45. {
  46. case 0:
  47. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  48. break;
  49. case 1:
  50. SDL_Color tem;
  51. tem.b = 0xff-kolor.b;
  52. tem.g = 0xff-kolor.g;
  53. tem.r = 0xff-kolor.r;
  54. tem.unused = 0xff-kolor.unused;
  55. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  56. break;
  57. case 2:
  58. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  59. break;
  60. default:
  61. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  62. break;
  63. }
  64. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-(temp->w/2),y-(temp->h/2)));
  65. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  66. SDL_FreeSurface(temp);
  67. }
  68. void CSDL_Ext::printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  69. {
  70. SDL_Surface * temp;
  71. switch (quality)
  72. {
  73. case 0:
  74. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  75. break;
  76. case 1:
  77. SDL_Color tem;
  78. tem.b = 0xff-kolor.b;
  79. tem.g = 0xff-kolor.g;
  80. tem.r = 0xff-kolor.r;
  81. tem.unused = 0xff-kolor.unused;
  82. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  83. break;
  84. case 2:
  85. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  86. break;
  87. default:
  88. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  89. break;
  90. }
  91. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x,y));
  92. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  93. SDL_FreeSurface(temp);
  94. }
  95. void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC, Uint8 A)
  96. {
  97. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC;
  98. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  99. {
  100. p[0] = R;
  101. p[1] = G;
  102. p[2] = B;
  103. }
  104. else
  105. {
  106. p[0] = B;
  107. p[1] = G;
  108. p[2] = R;
  109. }
  110. SDL_UpdateRect(ekran, x, y, 1, 1);
  111. }
  112. ///**************/
  113. ///Reverses the toRot surface by the vertical axis
  114. ///**************/
  115. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot, int myC)
  116. {
  117. 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);
  118. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  119. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  120. if(toRot->format->BytesPerPixel!=1)
  121. {
  122. for(int i=0; i<ret->w; ++i)
  123. {
  124. for(int j=0; j<ret->h; ++j)
  125. {
  126. {
  127. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  128. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  129. {
  130. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], myC);
  131. }
  132. else
  133. {
  134. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], myC);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. else
  141. {
  142. for(int i=0; i<ret->w; ++i)
  143. {
  144. for(int j=0; j<ret->h; ++j)
  145. {
  146. {
  147. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  148. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  149. }
  150. }
  151. }
  152. }
  153. SDL_FreeSurface(first);
  154. return ret;
  155. }
  156. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  157. {
  158. 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);
  159. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  160. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  161. if(ret->format->BytesPerPixel!=1)
  162. {
  163. for(int i=0; i<ret->w; ++i)
  164. {
  165. for(int j=0; j<ret->h; ++j)
  166. {
  167. {
  168. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel-2;
  169. int k=2;
  170. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  171. {
  172. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k);
  173. }
  174. else
  175. {
  176. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k);
  177. }
  178. }
  179. }
  180. }
  181. }
  182. else
  183. {
  184. for(int i=0; i<ret->w; ++i)
  185. {
  186. for(int j=0; j<ret->h; ++j)
  187. {
  188. {
  189. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel;
  190. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  191. }
  192. }
  193. }
  194. }
  195. SDL_FreeSurface(first);
  196. return ret;
  197. };
  198. ///**************/
  199. ///Rotates toRot surface by 90 degrees left
  200. ///**************/
  201. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  202. {
  203. 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);
  204. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  205. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  206. for(int i=0; i<ret->w; ++i)
  207. {
  208. for(int j=0; j<ret->h; ++j)
  209. {
  210. {
  211. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  212. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  213. {
  214. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  215. }
  216. else
  217. {
  218. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  219. }
  220. }
  221. }
  222. }
  223. SDL_FreeSurface(first);
  224. return ret;
  225. }
  226. ///*************/
  227. ///Rotates toRot surface by 180 degrees
  228. ///*************/
  229. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  230. {
  231. 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);
  232. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  233. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  234. if(ret->format->BytesPerPixel!=1)
  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 + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  242. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  243. {
  244. SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  245. }
  246. else
  247. {
  248. SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  249. }
  250. }
  251. }
  252. }
  253. }
  254. else
  255. {
  256. for(int i=0; i<ret->w; ++i)
  257. {
  258. for(int j=0; j<ret->h; ++j)
  259. {
  260. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  261. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  262. }
  263. }
  264. }
  265. SDL_FreeSurface(first);
  266. return ret;
  267. }
  268. //converts surface to cursor
  269. SDL_Cursor * CSDL_Ext::SurfaceToCursor(SDL_Surface *image, int hx, int hy)
  270. {
  271. int w, x, y;
  272. Uint8 *data, *mask, *d, *m, r, g, b;
  273. Uint32 color;
  274. SDL_Cursor *cursor;
  275. w = (image->w + 7) / 8;
  276. data = (Uint8 *)alloca(w * image->h * 2);
  277. if (data == NULL)
  278. return NULL;
  279. memset(data, 0, w * image->h * 2);
  280. mask = data + w * image->h;
  281. if (SDL_MUSTLOCK(image))
  282. SDL_LockSurface(image);
  283. for (y = 0; y < image->h; y++)
  284. {
  285. d = data + y * w;
  286. m = mask + y * w;
  287. for (x = 0; x < image->w; x++)
  288. {
  289. color = CSDL_Ext::SDL_GetPixel(image, x, y);
  290. if ((image->flags & SDL_SRCCOLORKEY) == 0 || color != image->format->colorkey)
  291. {
  292. SDL_GetRGB(color, image->format, &r, &g, &b);
  293. color = (r + g + b) / 3;
  294. m[x / 8] |= 128 >> (x & 7);
  295. if (color < 128)
  296. d[x / 8] |= 128 >> (x & 7);
  297. }
  298. }
  299. }
  300. if (SDL_MUSTLOCK(image))
  301. SDL_UnlockSurface(image);
  302. cursor = SDL_CreateCursor(data, mask, w, image->h, hx, hy);
  303. return cursor;
  304. }
  305. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y, bool colorByte)
  306. {
  307. int bpp = surface->format->BytesPerPixel;
  308. /* Here p is the address to the pixel we want to retrieve */
  309. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  310. switch(bpp) {
  311. case 1:
  312. if(colorByte)
  313. {
  314. return colorToUint32(surface->format->palette->colors+(*p));
  315. }
  316. else
  317. return *p;
  318. case 2:
  319. return *(Uint16 *)p;
  320. case 3:
  321. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  322. return p[0] << 16 | p[1] << 8 | p[2];
  323. else
  324. return p[0] | p[1] << 8 | p[2] << 16;
  325. case 4:
  326. return *(Uint32 *)p;
  327. default:
  328. return 0; /* shouldn't happen, but avoids warnings */
  329. }
  330. }
  331. SDL_Surface * CSDL_Ext::alphaTransform(SDL_Surface *src)
  332. {
  333. Uint32 trans = SDL_MapRGBA(src->format, 0, 255, 255, 255);
  334. SDL_SetColorKey(src, 0, trans);
  335. src->flags|=SDL_SRCALPHA;
  336. if(src->format->BitsPerPixel == 8)
  337. {
  338. for(int yy=0; yy<src->format->palette->ncolors; ++yy)
  339. {
  340. SDL_Color cur = *(src->format->palette->colors+yy);
  341. //if(cur.r == 255 && cur.b == 255)
  342. if(yy==1 || yy==2 || yy==3 || yy==4 || yy==8)
  343. {
  344. SDL_Color shadow;
  345. shadow.b = shadow.g = shadow.r = 0;
  346. switch(cur.g) //change this values; make diffrerent for objects and shadows (?)
  347. {
  348. case 0:
  349. shadow.unused = 0+32;
  350. break;
  351. case 50:
  352. shadow.unused = 50+32;
  353. break;
  354. case 100:
  355. shadow.unused = 100+64;
  356. break;
  357. case 128:
  358. shadow.unused = 128+64;
  359. break;
  360. case 150:
  361. shadow.unused = 150+64;
  362. break;
  363. default:
  364. shadow.unused = 255;
  365. break;
  366. }
  367. SDL_SetColors(src, &shadow, yy, 1);
  368. }
  369. if(yy==0 || (cur.r == 255 && cur.g == 0 && cur.b == 0))
  370. {
  371. SDL_Color transp;
  372. transp.b = transp.g = transp.r = 0;
  373. transp.unused = 255;
  374. SDL_SetColors(src, &transp, yy, 1);
  375. }
  376. }
  377. }
  378. SDL_UpdateRect(src, 0, 0, src->w, src->h);
  379. return src;
  380. }
  381. SDL_Surface * CSDL_Ext::secondAlphaTransform(SDL_Surface * src, SDL_Surface * alpha)
  382. {
  383. Uint32 pompom[256][256];
  384. for(int i=0; i<src->w; ++i)
  385. {
  386. for(int j=0; j<src->h; ++j)
  387. {
  388. pompom[i][j] = 0xffffffff - (SDL_GetPixel(src, i, j, true) & 0xff000000);
  389. }
  390. }
  391. Uint32 pompom2[256][256];
  392. for(int i=0; i<src->w; ++i)
  393. {
  394. for(int j=0; j<src->h; ++j)
  395. {
  396. pompom2[i][j] = pompom[i][j]>>24;
  397. }
  398. }
  399. SDL_Surface * hide2 = SDL_ConvertSurface(src, alpha->format, SDL_SWSURFACE);
  400. for(int i=0; i<hide2->w; ++i)
  401. {
  402. for(int j=0; j<hide2->h; ++j)
  403. {
  404. Uint32 * place = (Uint32*)( (Uint8*)hide2->pixels + j * hide2->pitch + i * hide2->format->BytesPerPixel);
  405. (*place)&=pompom[i][j];
  406. int ffgg=0;
  407. }
  408. }
  409. return hide2;
  410. }
  411. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  412. {
  413. Uint32 ret = 0;
  414. ret+=color->unused;
  415. ret*=256;
  416. ret+=color->b;
  417. ret*=256;
  418. ret+=color->g;
  419. ret*=256;
  420. ret+=color->r;
  421. return ret;
  422. }
  423. void CSDL_Ext::update(SDL_Surface * what)
  424. {
  425. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  426. }
  427. void CSDL_Ext::blueToPlayers(SDL_Surface * sur, int player)
  428. {
  429. if(sur->format->BitsPerPixel == 8)
  430. {
  431. for(int i=0; i<sur->format->palette->ncolors; ++i)
  432. {
  433. SDL_Color * cc = sur->format->palette->colors+i;
  434. if(cc->r==0 && cc->g==0 && cc->b==255)
  435. {
  436. cc->r = CGameInfo::mainObj->playerColors[player].r;
  437. cc->g = CGameInfo::mainObj->playerColors[player].g;
  438. cc->b = CGameInfo::mainObj->playerColors[player].b;
  439. }
  440. }
  441. }
  442. else if(sur->format->BitsPerPixel == 24)
  443. {
  444. for(int y=0; y<sur->h; ++y)
  445. {
  446. for(int x=0; x<sur->w; ++x)
  447. {
  448. Uint8* cp = (Uint8*)sur->pixels + y+sur->pitch + x*3;
  449. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  450. {
  451. if(cp[0]==0 && cp[1]==0 && cp[2]==255)
  452. {
  453. cp[0] = CGameInfo::mainObj->playerColors[player].r;
  454. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  455. cp[2] = CGameInfo::mainObj->playerColors[player].b;
  456. }
  457. }
  458. else
  459. {
  460. if(cp[0]==255 && cp[1]==0 && cp[2]==0)
  461. {
  462. cp[0] = CGameInfo::mainObj->playerColors[player].b;
  463. cp[1] = CGameInfo::mainObj->playerColors[player].g;
  464. cp[2] = CGameInfo::mainObj->playerColors[player].r;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. }
  471. void CSDL_Ext::blueToPlayersAdv(SDL_Surface * sur, int player)
  472. {
  473. if(sur->format->BitsPerPixel == 8)
  474. {
  475. for(int i=0; i<sur->format->palette->ncolors; ++i)
  476. {
  477. SDL_Color * cc = sur->format->palette->colors+i;
  478. if(cc->b>cc->g && cc->b>cc->r)
  479. {
  480. std::vector<long long int> sort1;
  481. sort1.push_back(cc->r);
  482. sort1.push_back(cc->g);
  483. sort1.push_back(cc->b);
  484. std::vector< std::pair<long long int, Uint8*> > sort2;
  485. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].r, &(cc->r)));
  486. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].g, &(cc->g)));
  487. sort2.push_back(std::make_pair(CGameInfo::mainObj->playerColors[player].b, &(cc->b)));
  488. std::sort(sort1.begin(), sort1.end());
  489. if(sort2[0].first>sort2[1].first)
  490. std::swap(sort2[0], sort2[1]);
  491. if(sort2[1].first>sort2[2].first)
  492. std::swap(sort2[1], sort2[2]);
  493. if(sort2[0].first>sort2[1].first)
  494. std::swap(sort2[0], sort2[1]);
  495. for(int hh=0; hh<3; ++hh)
  496. {
  497. (*sort2[hh].second) = (sort1[hh] + sort2[hh].first)/2;
  498. }
  499. }
  500. }
  501. }
  502. else if(sur->format->BitsPerPixel == 24)
  503. {
  504. }
  505. }