SDL_Extensions.cpp 11 KB

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