SDL_Extensions.cpp 10 KB

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