SDL_Extensions.cpp 10 KB

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