SDL_Extensions.cpp 11 KB

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