SDL_Extensions.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include "stdafx.h"
  2. #include "SDL_Extensions.h"
  3. #include "SDL_TTF.h"
  4. extern SDL_Surface * ekran;
  5. extern SDL_Color tytulowy, tlo, zwykly ;
  6. bool isItIn(const SDL_Rect * rect, int x, int y)
  7. {
  8. if ((x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h))
  9. return true;
  10. else return false;
  11. }
  12. SDL_Rect genRect(int hh, int ww, int xx, int yy)
  13. {
  14. SDL_Rect ret;
  15. ret.h=hh;
  16. ret.w=ww;
  17. ret.x=xx;
  18. ret.y=yy;
  19. return ret;
  20. }
  21. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst=ekran)
  22. {
  23. SDL_BlitSurface(src,NULL,dst,&genRect(src->h,src->w,x,y));
  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 printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=ekran)
  39. {
  40. SDL_Surface * temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  41. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x,y));
  42. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  43. SDL_FreeSurface(temp);
  44. }
  45. void CSDL_Ext::SDL_PutPixel(SDL_Surface *ekran, int x, int y, Uint8 R, Uint8 G, Uint8 B, int myC)
  46. {
  47. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel-myC;
  48. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  49. {
  50. p[0] = R;
  51. p[1] = G;
  52. p[2] = B;
  53. }
  54. else
  55. {
  56. p[0] = B;
  57. p[1] = G;
  58. p[2] = R;
  59. }
  60. SDL_UpdateRect(ekran, x, y, 1, 1);
  61. }
  62. ///**************/
  63. ///Reverses the toRot surface by the vertical axis
  64. ///**************/
  65. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
  66. {
  67. 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);
  68. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  69. for(int i=0; i<ret->w; ++i)
  70. {
  71. for(int j=0; j<ret->h; ++j)
  72. {
  73. {
  74. Uint8 *p = (Uint8 *)toRot->pixels + j * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  75. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  76. {
  77. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  78. }
  79. else
  80. {
  81. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  82. }
  83. }
  84. }
  85. }
  86. SDL_FreeSurface(first);
  87. return ret;
  88. }
  89. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  90. {
  91. 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);
  92. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  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 + (ret->h - j -1) * toRot->pitch + i * toRot->format->BytesPerPixel-2;
  99. int k=2;
  100. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  101. {
  102. CSDL_Ext::SDL_PutPixel(ret, i, j, p[0], p[1], p[2], k);
  103. }
  104. else
  105. {
  106. CSDL_Ext::SDL_PutPixel(ret, i, j, p[2], p[1], p[0], k);
  107. }
  108. }
  109. }
  110. }
  111. SDL_FreeSurface(first);
  112. return ret;
  113. };
  114. ///**************/
  115. ///Rotates toRot surface by 90 degrees left
  116. ///**************/
  117. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  118. {
  119. 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);
  120. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  121. for(int i=0; i<ret->w; ++i)
  122. {
  123. for(int j=0; j<ret->h; ++j)
  124. {
  125. {
  126. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  127. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  128. {
  129. SDL_PutPixel(ret, i, j, p[0], p[1], p[2]);
  130. }
  131. else
  132. {
  133. SDL_PutPixel(ret, i, j, p[2], p[1], p[0]);
  134. }
  135. }
  136. }
  137. }
  138. SDL_FreeSurface(first);
  139. return ret;
  140. }
  141. ///*************/
  142. ///Rotates toRot surface by 180 degrees
  143. ///*************/
  144. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  145. {
  146. 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);
  147. SDL_Surface * ret = SDL_ConvertSurface(first, toRot->format, toRot->flags);
  148. for(int i=0; i<ret->w; ++i)
  149. {
  150. for(int j=0; j<ret->h; ++j)
  151. {
  152. {
  153. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  154. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  155. {
  156. SDL_PutPixel(ret, i, j, p[0], p[1], p[2], 2);
  157. }
  158. else
  159. {
  160. SDL_PutPixel(ret, i, j, p[2], p[1], p[0], 2);
  161. }
  162. }
  163. }
  164. }
  165. SDL_FreeSurface(first);
  166. return ret;
  167. }
  168. //converts surface to cursor
  169. SDL_Cursor * CSDL_Ext::SurfaceToCursor(SDL_Surface *image, int hx, int hy)
  170. {
  171. int w, x, y;
  172. Uint8 *data, *mask, *d, *m, r, g, b;
  173. Uint32 color;
  174. SDL_Cursor *cursor;
  175. w = (image->w + 7) / 8;
  176. data = (Uint8 *)alloca(w * image->h * 2);
  177. if (data == NULL)
  178. return NULL;
  179. memset(data, 0, w * image->h * 2);
  180. mask = data + w * image->h;
  181. if (SDL_MUSTLOCK(image))
  182. SDL_LockSurface(image);
  183. for (y = 0; y < image->h; y++)
  184. {
  185. d = data + y * w;
  186. m = mask + y * w;
  187. for (x = 0; x < image->w; x++)
  188. {
  189. color = CSDL_Ext::SDL_GetPixel(image, x, y);
  190. if ((image->flags & SDL_SRCCOLORKEY) == 0 || color != image->format->colorkey)
  191. {
  192. SDL_GetRGB(color, image->format, &r, &g, &b);
  193. color = (r + g + b) / 3;
  194. m[x / 8] |= 128 >> (x & 7);
  195. if (color < 128)
  196. d[x / 8] |= 128 >> (x & 7);
  197. }
  198. }
  199. }
  200. if (SDL_MUSTLOCK(image))
  201. SDL_UnlockSurface(image);
  202. cursor = SDL_CreateCursor(data, mask, w, image->h, hx, hy);
  203. return cursor;
  204. }
  205. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, int x, int y)
  206. {
  207. int bpp = surface->format->BytesPerPixel;
  208. /* Here p is the address to the pixel we want to retrieve */
  209. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  210. switch(bpp) {
  211. case 1:
  212. return *p;
  213. case 2:
  214. return *(Uint16 *)p;
  215. case 3:
  216. if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  217. return p[0] << 16 | p[1] << 8 | p[2];
  218. else
  219. return p[0] | p[1] << 8 | p[2] << 16;
  220. case 4:
  221. return *(Uint32 *)p;
  222. default:
  223. return 0; /* shouldn't happen, but avoids warnings */
  224. }
  225. }