SDL_Extensions.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. for(int i=0; i<ret->w; ++i)
  72. {
  73. for(int j=0; j<ret->h; ++j)
  74. {
  75. {
  76. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  77. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  78. {
  79. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], myC);
  80. }
  81. else
  82. {
  83. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], myC);
  84. }
  85. }
  86. }
  87. }
  88. SDL_FreeSurface(first);
  89. return ret;
  90. }
  91. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  92. {
  93. 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);
  94. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  95. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  96. for(int i=0; i<ret->w; ++i)
  97. {
  98. for(int j=0; j<ret->h; ++j)
  99. {
  100. {
  101. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel-2;
  102. int k=2;
  103. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  104. {
  105. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k);
  106. }
  107. else
  108. {
  109. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k);
  110. }
  111. }
  112. }
  113. }
  114. SDL_FreeSurface(first);
  115. return ret;
  116. };
  117. ///**************/
  118. ///Rotates toRot surface by 90 degrees left
  119. ///**************/
  120. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  121. {
  122. 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);
  123. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  124. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  125. for(int i=0; i<ret->w; ++i)
  126. {
  127. for(int j=0; j<ret->h; ++j)
  128. {
  129. {
  130. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  131. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  132. {
  133. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  134. }
  135. else
  136. {
  137. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  138. }
  139. }
  140. }
  141. }
  142. SDL_FreeSurface(first);
  143. return ret;
  144. }
  145. ///*************/
  146. ///Rotates toRot surface by 180 degrees
  147. ///*************/
  148. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  149. {
  150. 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);
  151. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  152. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  153. for(int i=0; i<ret->w; ++i)
  154. {
  155. for(int j=0; j<ret->h; ++j)
  156. {
  157. {
  158. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  159. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  160. {
  161. SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  162. }
  163. else
  164. {
  165. SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  166. }
  167. }
  168. }
  169. }
  170. SDL_FreeSurface(first);
  171. return ret;
  172. }
  173. //converts surface to cursor
  174. SDL_Cursor * CSDL_Ext::SurfaceToCursor(SDL_Surface *image, int hx, int hy)
  175. {
  176. int w, x, y;
  177. Uint8 *data, *mask, *d, *m, r, g, b;
  178. Uint32 color;
  179. SDL_Cursor *cursor;
  180. w = (image->w + 7) / 8;
  181. data = (Uint8 *)alloca(w * image->h * 2);
  182. if (data == NULL)
  183. return NULL;
  184. memset(data, 0, w * image->h * 2);
  185. mask = data + w * image->h;
  186. if (SDL_MUSTLOCK(image))
  187. SDL_LockSurface(image);
  188. for (y = 0; y < image->h; y++)
  189. {
  190. d = data + y * w;
  191. m = mask + y * w;
  192. for (x = 0; x < image->w; x++)
  193. {
  194. color = CSDL_Ext::SDL_GetPixel(image, x, y);
  195. if ((image->flags & SDL_SRCCOLORKEY) == 0 || color != image->format->colorkey)
  196. {
  197. SDL_GetRGB(color, image->format, &r, &g, &b);
  198. color = (r + g + b) / 3;
  199. m[x / 8] |= 128 >> (x & 7);
  200. if (color < 128)
  201. d[x / 8] |= 128 >> (x & 7);
  202. }
  203. }
  204. }
  205. if (SDL_MUSTLOCK(image))
  206. SDL_UnlockSurface(image);
  207. cursor = SDL_CreateCursor(data, mask, w, image->h, hx, hy);
  208. return cursor;
  209. }
  210. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y, bool colorByte)
  211. {
  212. int bpp = surface->format->BytesPerPixel;
  213. /* Here p is the address to the pixel we want to retrieve */
  214. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  215. switch(bpp) {
  216. case 1:
  217. if(colorByte)
  218. {
  219. return colorToUint32(surface->format->palette->colors+(*p));
  220. }
  221. else
  222. return *p;
  223. case 2:
  224. return *(Uint16 *)p;
  225. case 3:
  226. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  227. return p[0] << 16 | p[1] << 8 | p[2];
  228. else
  229. return p[0] | p[1] << 8 | p[2] << 16;
  230. case 4:
  231. return *(Uint32 *)p;
  232. default:
  233. return 0; /* shouldn't happen, but avoids warnings */
  234. }
  235. }
  236. SDL_Surface * CSDL_Ext::alphaTransform(SDL_Surface *src)
  237. {
  238. Uint32 trans = SDL_MapRGBA(src->format, 0, 255, 255, 255);
  239. //SDL_SetColorKey(src, SDL_SRCCOLORKEY, trans);
  240. /*SDL_SetColorKey(src, 0, trans);
  241. src = SDL_ConvertSurface(src, ekran->format, ekran->flags);
  242. for(int i=0; i<src->w; ++i)
  243. {
  244. for(int j=0; j<src->h; ++j)
  245. {
  246. Uint8 cr, cg, cb, ca;
  247. SDL_GetRGBA(SDL_GetPixel(src, i, j), src->format, &cr, &cg, &cb, &ca);
  248. if(cr == 255 && cb == 255)
  249. {
  250. Uint32 aaaa=src->format->Amask;
  251. Uint32 aaab=src->format->Bmask;
  252. Uint32 aaag=src->format->Gmask;
  253. Uint32 aaar=src->format->Rmask;
  254. Uint32 put = cg << 24 | cr << 16 | ca << 8 | cb;
  255. SDL_Rect rrr = genRect(1, 1, i, j);
  256. SDL_FillRect(src, &rrr, put);
  257. }
  258. }
  259. }*/
  260. //SDL_UpdateRect(src, 0, 0, src->w, src->h);
  261. SDL_SetColorKey(src, 0, trans);
  262. src->flags|=SDL_SRCALPHA;
  263. if(src->format->BitsPerPixel == 8)
  264. {
  265. for(int yy=0; yy<src->format->palette->ncolors; ++yy)
  266. {
  267. SDL_Color cur = *(src->format->palette->colors+yy);
  268. if(cur.r == 255 && cur.b == 255)
  269. {
  270. SDL_Color shadow;
  271. shadow.b = shadow.g = shadow.r = 0;
  272. shadow.unused = cur.g + 25; //25 is a scalable constans to make it nicer
  273. SDL_SetColors(src, &shadow, yy, 1);
  274. }
  275. if(cur.g == 255 && cur.b == 255)
  276. {
  277. SDL_Color transp;
  278. transp.b = transp.g = transp.r = 0;
  279. transp.unused = 255;
  280. SDL_SetColors(src, &transp, yy, 1);
  281. }
  282. }
  283. }
  284. SDL_UpdateRect(src, 0, 0, src->w, src->h);
  285. return src;
  286. }
  287. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  288. {
  289. Uint32 ret = 0;
  290. ret+=color->unused;
  291. ret*=256;
  292. ret+=color->b;
  293. ret*=256;
  294. ret+=color->g;
  295. ret*=256;
  296. ret+=color->r;
  297. return ret;
  298. }