SDL_Extensions.cpp 5.8 KB

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