2
0

SDL_Extensions.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. #include "StdInc.h"
  2. #include "SDL_Extensions.h"
  3. #include <SDL_ttf.h>
  4. #include "../CGameInfo.h"
  5. #include "../CMessage.h"
  6. #include "../CDefHandler.h"
  7. #include "../Graphics.h"
  8. /*
  9. * SDL_Extensions.cpp, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. SDL_Color Colors::createColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a /*= 0*/)
  18. {
  19. SDL_Color temp = {r, g, b, a};
  20. return temp;
  21. }
  22. SDL_Surface * CSDL_Ext::newSurface(int w, int h, SDL_Surface * mod) //creates new surface, with flags/format same as in surface given
  23. {
  24. return SDL_CreateRGBSurface(mod->flags,w,h,mod->format->BitsPerPixel,mod->format->Rmask,mod->format->Gmask,mod->format->Bmask,mod->format->Amask);
  25. }
  26. SDL_Surface * CSDL_Ext::copySurface(SDL_Surface * mod) //returns copy of given surface
  27. {
  28. //return SDL_DisplayFormat(mod);
  29. return SDL_ConvertSurface(mod, mod->format, mod->flags);
  30. }
  31. bool isItIn(const SDL_Rect * rect, int x, int y)
  32. {
  33. return (x>rect->x && x<rect->x+rect->w) && (y>rect->y && y<rect->y+rect->h);
  34. }
  35. void blitAt(SDL_Surface * src, int x, int y, SDL_Surface * dst)
  36. {
  37. if(!dst) dst = screen;
  38. SDL_Rect pom = genRect(src->h,src->w,x,y);
  39. CSDL_Ext::blitSurface(src,NULL,dst,&pom);
  40. }
  41. void blitAt(SDL_Surface * src, const SDL_Rect & pos, SDL_Surface * dst)
  42. {
  43. blitAt(src,pos.x,pos.y,dst);
  44. }
  45. SDL_Color genRGB(int r, int g, int b, int a=0)
  46. {
  47. SDL_Color ret;
  48. ret.b=b;
  49. ret.g=g;
  50. ret.r=r;
  51. ret.unused=a;
  52. return ret;
  53. }
  54. void updateRect (SDL_Rect * rect, SDL_Surface * scr)
  55. {
  56. SDL_UpdateRect(scr,rect->x,rect->y,rect->w,rect->h);
  57. }
  58. void printAtMiddleWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  59. {
  60. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  61. std::vector<SDL_Surface*> wesu;
  62. wesu.resize(ws.size());
  63. for (size_t i=0; i < wesu.size(); ++i)
  64. {
  65. wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
  66. }
  67. int tox=0, toy=0;
  68. for (size_t i=0; i < wesu.size(); ++i)
  69. {
  70. toy+=wesu[i]->h;
  71. if (tox < wesu[i]->w)
  72. tox=wesu[i]->w;
  73. }
  74. int evx, evy = y - (toy/2);
  75. for (size_t i=0; i < wesu.size(); ++i)
  76. {
  77. evx = (x - (tox/2)) + ((tox-wesu[i]->w)/2);
  78. blitAt(wesu[i],evx,evy,dst);
  79. evy+=wesu[i]->h;
  80. }
  81. for (size_t i=0; i < wesu.size(); ++i)
  82. SDL_FreeSurface(wesu[i]);
  83. }
  84. void printAtWB(const std::string & text, int x, int y, TTF_Font * font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  85. {
  86. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  87. std::vector<SDL_Surface*> wesu;
  88. wesu.resize(ws.size());
  89. for (size_t i=0; i < wesu.size(); ++i)
  90. wesu[i]=TTF_RenderText_Blended(font,ws[i].c_str(),kolor);
  91. int evy = y;
  92. for (size_t i=0; i < wesu.size(); ++i)
  93. {
  94. blitAt(wesu[i],x,evy,dst);
  95. evy+=wesu[i]->h;
  96. }
  97. for (size_t i=0; i < wesu.size(); ++i)
  98. SDL_FreeSurface(wesu[i]);
  99. }
  100. void CSDL_Ext::printAtWB(const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor, SDL_Surface * dst)
  101. {
  102. if (graphics->fontsTrueType[font])
  103. {
  104. printAtWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
  105. return;
  106. }
  107. const Font *f = graphics->fonts[font];
  108. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  109. int cury = y;
  110. for (size_t i=0; i < ws.size(); ++i)
  111. {
  112. printAt(ws[i], x, cury, font, kolor, dst);
  113. cury += f->height;
  114. }
  115. }
  116. void CSDL_Ext::printAtMiddleWB( const std::string & text, int x, int y, EFonts font, int charpr, SDL_Color kolor/*=Colors::Jasmine*/, SDL_Surface * dst/*=screen*/ )
  117. {
  118. if (graphics->fontsTrueType[font])
  119. {
  120. printAtMiddleWB(text,x, y, graphics->fontsTrueType[font], charpr, kolor, dst);
  121. return;
  122. }
  123. const Font *f = graphics->fonts[font];
  124. std::vector<std::string> ws = CMessage::breakText(text,charpr);
  125. int totalHeight = ws.size() * f->height;
  126. int cury = y - totalHeight/2;
  127. for (size_t i=0; i < ws.size(); ++i)
  128. {
  129. printAt(ws[i], x - f->getWidth(ws[i].c_str())/2, cury, font, kolor, dst);
  130. cury += f->height;
  131. }
  132. }
  133. void printAtMiddle(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
  134. {
  135. if(text.length()==0) return;
  136. SDL_Surface * temp;
  137. switch (quality)
  138. {
  139. case 0:
  140. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  141. break;
  142. case 1:
  143. SDL_Color tem;
  144. tem.b = 0xff-kolor.b;
  145. tem.g = 0xff-kolor.g;
  146. tem.r = 0xff-kolor.r;
  147. tem.unused = 0xff-kolor.unused;
  148. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  149. break;
  150. case 2:
  151. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  152. break;
  153. default:
  154. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  155. break;
  156. }
  157. SDL_Rect dstRect = genRect(temp->h, temp->w, x-(temp->w/2), y-(temp->h/2));
  158. CSDL_Ext::blitSurface(temp, NULL, dst, &dstRect);
  159. SDL_FreeSurface(temp);
  160. }
  161. void CSDL_Ext::printAtMiddle( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::Cornsilk*/, SDL_Surface * dst/*=screen*/ )
  162. {
  163. if (graphics->fontsTrueType[font])
  164. {
  165. printAtMiddle(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  166. return;
  167. }
  168. const Font *f = graphics->fonts[font];
  169. int nx = x - f->getWidth(text.c_str())/2,
  170. ny = y - f->height/2;
  171. printAt(text, nx, ny, font, kolor, dst);
  172. }
  173. void printAt(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2, bool refresh=false)
  174. {
  175. if (text.length()==0)
  176. return;
  177. SDL_Surface * temp;
  178. switch (quality)
  179. {
  180. case 0:
  181. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  182. break;
  183. case 1:
  184. SDL_Color tem;
  185. tem.b = 0xff-kolor.b;
  186. tem.g = 0xff-kolor.g;
  187. tem.r = 0xff-kolor.r;
  188. tem.unused = 0xff-kolor.unused;
  189. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  190. break;
  191. case 2:
  192. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  193. break;
  194. default:
  195. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  196. break;
  197. }
  198. SDL_Rect dstRect = genRect(temp->h,temp->w,x,y);
  199. CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
  200. if(refresh)
  201. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  202. SDL_FreeSurface(temp);
  203. }
  204. void CSDL_Ext::printAt( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::Cornsilk*/, SDL_Surface * dst/*=screen*/ )
  205. {
  206. if(!text.size())
  207. return;
  208. if (graphics->fontsTrueType[font])
  209. {
  210. printAt(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  211. return;
  212. }
  213. assert(dst);
  214. assert(font < Graphics::FONTS_NUMBER);
  215. //assume BGR dst surface, TODO - make it general in a tidy way
  216. assert(dst->format->Rshift > dst->format->Gshift);
  217. assert(dst->format->Gshift > dst->format->Bshift);
  218. const Font *f = graphics->fonts[font];
  219. const Uint8 bpp = dst->format->BytesPerPixel;
  220. Uint8 *px = NULL;
  221. Uint8 *src = NULL;
  222. TColorPutter colorPutter = getPutterFor(dst, false);
  223. //if text is in {} braces, we'll ommit them
  224. const int first = (text[0] == '{' ? 1 : 0);
  225. const int beyondEnd = (text[text.size()-1] == '}' ? text.size()-1 : text.size());
  226. for(int txti = first; txti < beyondEnd; txti++)
  227. {
  228. const ui8 c = text[txti];
  229. x += f->chars[c].unknown1;
  230. for(int i = std::max(0, -y); i < f->height && (y + i) < (dst->h - 1); i++)
  231. {
  232. px = (Uint8*)dst->pixels;
  233. px += (y+i) * dst->pitch + x * bpp;
  234. src = f->chars[c].pixels;
  235. src += i * f->chars[c].width;//if we have reached end of surface in previous line
  236. for(int j = std::max(0, -x); j < f->chars[c].width && (j + x) < (dst->w - 1); j++)
  237. {
  238. switch(*src)
  239. {
  240. case 1: //black "shadow"
  241. memset(px, 0, bpp);
  242. break;
  243. case 255: //text colour
  244. colorPutter(px, kolor.r, kolor.g, kolor.b);
  245. break;
  246. }
  247. src++;
  248. px += bpp;
  249. }
  250. }
  251. x += f->chars[c].width;
  252. x += f->chars[c].unknown2;
  253. }
  254. }
  255. void printTo(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
  256. {
  257. if (text.length()==0)
  258. return;
  259. SDL_Surface * temp;
  260. switch (quality)
  261. {
  262. case 0:
  263. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  264. break;
  265. case 1:
  266. SDL_Color tem;
  267. tem.b = 0xff-kolor.b;
  268. tem.g = 0xff-kolor.g;
  269. tem.r = 0xff-kolor.r;
  270. tem.unused = 0xff-kolor.unused;
  271. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  272. break;
  273. case 2:
  274. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  275. break;
  276. default:
  277. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  278. break;
  279. }
  280. SDL_Rect dstRect = genRect(temp->h,temp->w,x-temp->w,y-temp->h);
  281. CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
  282. SDL_UpdateRect(dst,x-temp->w,y-temp->h,temp->w,temp->h);
  283. SDL_FreeSurface(temp);
  284. }
  285. void CSDL_Ext::printTo( const std::string & text, int x, int y, EFonts font, SDL_Color kolor/*=Colors::Cornsilk*/, SDL_Surface * dst/*=screen*/ )
  286. {
  287. if (graphics->fontsTrueType[font])
  288. {
  289. printTo(text,x, y, graphics->fontsTrueType[font], kolor, dst);
  290. return;
  291. }
  292. const Font *f = graphics->fonts[font];
  293. printAt(text, x - f->getWidth(text.c_str()), y - f->height, font, kolor, dst);
  294. }
  295. void printToWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, ui8 quality=2)
  296. {
  297. if (text.length()==0)
  298. return;
  299. SDL_Surface * temp;
  300. switch (quality)
  301. {
  302. case 0:
  303. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  304. break;
  305. case 1:
  306. SDL_Color tem;
  307. tem.b = 0xff-kolor.b;
  308. tem.g = 0xff-kolor.g;
  309. tem.r = 0xff-kolor.r;
  310. tem.unused = 0xff-kolor.unused;
  311. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  312. break;
  313. case 2:
  314. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  315. break;
  316. default:
  317. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  318. break;
  319. }
  320. SDL_Rect dstRect = genRect(temp->h,temp->w,x-temp->w,y-temp->h);
  321. CSDL_Ext::blitSurface(temp,NULL,dst,&dstRect);
  322. SDL_FreeSurface(temp);
  323. }
  324. // Vertical flip
  325. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
  326. {
  327. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  328. const int bpl = ret->pitch;
  329. const int bpp = ret->format->BytesPerPixel;
  330. for(int i=0; i<ret->h; i++) {
  331. char *src = (char *)toRot->pixels + i*bpl;
  332. char *dst = (char *)ret->pixels + i*bpl;
  333. for(int j=0; j<ret->w; j++) {
  334. for (int k=0; k<bpp; k++) {
  335. dst[j*bpp + k] = src[(ret->w-j-1)*bpp + k];
  336. }
  337. }
  338. }
  339. return ret;
  340. }
  341. // Horizontal flip
  342. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  343. {
  344. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  345. int bpl = ret->pitch;
  346. for(int i=0; i<ret->h; i++) {
  347. memcpy((char *)ret->pixels + i*bpl, (char *)toRot->pixels + (ret->h-i-1)*bpl, bpl);
  348. }
  349. return ret;
  350. };
  351. ///**************/
  352. ///Rotates toRot surface by 90 degrees left
  353. ///**************/
  354. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  355. {
  356. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  357. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  358. for(int i=0; i<ret->w; ++i)
  359. {
  360. for(int j=0; j<ret->h; ++j)
  361. {
  362. {
  363. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  364. /*
  365. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  366. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2]);
  367. #else
  368. */
  369. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0]);
  370. //#endif
  371. }
  372. }
  373. }
  374. return ret;
  375. }
  376. ///*************/
  377. ///Rotates toRot surface by 180 degrees
  378. ///*************/
  379. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  380. {
  381. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  382. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  383. if(ret->format->BytesPerPixel!=1)
  384. {
  385. for(int i=0; i<ret->w; ++i)
  386. {
  387. for(int j=0; j<ret->h; ++j)
  388. {
  389. {
  390. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  391. /*
  392. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  393. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2], 0);
  394. #else
  395. */
  396. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0], 0);
  397. //#endif
  398. }
  399. }
  400. }
  401. }
  402. else
  403. {
  404. for(int i=0; i<ret->w; ++i)
  405. {
  406. for(int j=0; j<ret->h; ++j)
  407. {
  408. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  409. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  410. }
  411. }
  412. }
  413. return ret;
  414. }
  415. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  416. {
  417. int bpp = surface->format->BytesPerPixel;
  418. /* Here p is the address to the pixel we want to retrieve */
  419. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  420. switch(bpp)
  421. {
  422. case 1:
  423. if(colorByte)
  424. {
  425. return colorToUint32(surface->format->palette->colors+(*p));
  426. }
  427. else
  428. return *p;
  429. case 2:
  430. return *(Uint16 *)p;
  431. case 3:
  432. /*
  433. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  434. return p[0] << 16 | p[1] << 8 | p[2];
  435. #else
  436. */
  437. return p[0] | p[1] << 8 | p[2] << 16;
  438. //#endif
  439. case 4:
  440. return *(Uint32 *)p;
  441. default:
  442. return 0; // shouldn't happen, but avoids warnings
  443. }
  444. }
  445. void CSDL_Ext::alphaTransform(SDL_Surface *src)
  446. {
  447. assert(src->format->BitsPerPixel == 8);
  448. SDL_Color colors[] = {{0,0,0,255}, {0,0,0,214}, {0,0,0,164}, {0,0,0,82}, {0,0,0,128},
  449. {255,0,0,0}, {255,0,0,0}, {255,0,0,0}, {0,0,0,192}, {0,0,0,192}};
  450. SDL_SetColors(src, colors, 0, ARRAY_COUNT(colors));
  451. SDL_SetColorKey(src, SDL_SRCCOLORKEY, SDL_MapRGBA(src->format, 0, 0, 0, 255));
  452. }
  453. // <=>
  454. static void prepareOutRect(SDL_Rect *src, SDL_Rect *dst, const SDL_Rect & clip_rect)
  455. {
  456. const int xoffset = std::max(clip_rect.x - dst->x, 0),
  457. yoffset = std::max(clip_rect.y - dst->y, 0);
  458. src->x += xoffset;
  459. src->y += yoffset;
  460. dst->x += xoffset;
  461. dst->y += yoffset;
  462. src->w = dst->w = std::max(0,std::min(dst->w - xoffset, clip_rect.x + clip_rect.w - dst->x));
  463. src->h = dst->h = std::max(0,std::min(dst->h - yoffset, clip_rect.y + clip_rect.h - dst->y));
  464. }
  465. template<int bpp>
  466. void CSDL_Ext::blitWithRotateClip(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect, ui8 rotation)//srcRect is not used, works with 8bpp sources and 24bpp dests
  467. {
  468. static void (*blitWithRotate[])(const SDL_Surface *, const SDL_Rect *, SDL_Surface *, const SDL_Rect *) = {blitWithRotate1<bpp>, blitWithRotate2<bpp>, blitWithRotate3<bpp>};
  469. if(!rotation)
  470. {
  471. CSDL_Ext::blitSurface(src, srcRect, dst, dstRect);
  472. }
  473. else
  474. {
  475. prepareOutRect(srcRect, dstRect, dst->clip_rect);
  476. blitWithRotate[rotation-1](src, srcRect, dst, dstRect);
  477. }
  478. }
  479. template<int bpp>
  480. void CSDL_Ext::blitWithRotateClipVal( SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation )
  481. {
  482. blitWithRotateClip<bpp>(src, &srcRect, dst, &dstRect, rotation);
  483. }
  484. template<int bpp>
  485. void CSDL_Ext::blitWithRotateClipWithAlpha(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect, ui8 rotation)//srcRect is not used, works with 8bpp sources and 24bpp dests
  486. {
  487. static void (*blitWithRotate[])(const SDL_Surface *, const SDL_Rect *, SDL_Surface *, const SDL_Rect *) = {blitWithRotate1WithAlpha<bpp>, blitWithRotate2WithAlpha<bpp>, blitWithRotate3WithAlpha<bpp>};
  488. if(!rotation)
  489. {
  490. blit8bppAlphaTo24bpp(src, srcRect, dst, dstRect);
  491. }
  492. else
  493. {
  494. prepareOutRect(srcRect, dstRect, dst->clip_rect);
  495. blitWithRotate[rotation-1](src, srcRect, dst, dstRect);
  496. }
  497. }
  498. template<int bpp>
  499. void CSDL_Ext::blitWithRotateClipValWithAlpha( SDL_Surface *src,SDL_Rect srcRect, SDL_Surface * dst, SDL_Rect dstRect, ui8 rotation )
  500. {
  501. blitWithRotateClipWithAlpha<bpp>(src, &srcRect, dst, &dstRect, rotation);
  502. }
  503. template<int bpp>
  504. void CSDL_Ext::blitWithRotate1(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24/32 bpp dests
  505. {
  506. Uint8 *sp = getPxPtr(src, src->w - srcRect->w - srcRect->x, srcRect->y);
  507. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  508. const SDL_Color * const colors = src->format->palette->colors;
  509. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  510. {
  511. Uint8 *dp = dporg;
  512. for(int j=dstRect->w; j>0; j--, sp++)
  513. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  514. sp += src->w - dstRect->w;
  515. }
  516. }
  517. template<int bpp>
  518. void CSDL_Ext::blitWithRotate2(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24/32 bpp dests
  519. {
  520. Uint8 *sp = getPxPtr(src, srcRect->x, src->h - srcRect->h - srcRect->y);
  521. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  522. const SDL_Color * const colors = src->format->palette->colors;
  523. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  524. {
  525. Uint8 *dp = dporg;
  526. for(int j=dstRect->w; j>0; j--, sp++)
  527. ColorPutter<bpp, 1>::PutColor(dp, colors[*sp]);
  528. sp += src->w - dstRect->w;
  529. }
  530. }
  531. template<int bpp>
  532. void CSDL_Ext::blitWithRotate3(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24/32 bpp dests
  533. {
  534. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + (src->w - srcRect->w - srcRect->x);
  535. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  536. const SDL_Color * const colors = src->format->palette->colors;
  537. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  538. {
  539. Uint8 *dp = dporg;
  540. for(int j=dstRect->w; j>0; j--, sp++)
  541. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  542. sp += src->w - dstRect->w;
  543. }
  544. }
  545. template<int bpp>
  546. void CSDL_Ext::blitWithRotate1WithAlpha(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24/32 bpp dests
  547. {
  548. Uint8 *sp = (Uint8 *)src->pixels + srcRect->y*src->pitch + (src->w - srcRect->w - srcRect->x);
  549. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  550. const SDL_Color * const colors = src->format->palette->colors;
  551. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  552. {
  553. Uint8 *dp = dporg;
  554. for(int j=dstRect->w; j>0; j--, sp++)
  555. {
  556. if(*sp)
  557. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  558. else
  559. dp -= bpp;
  560. }
  561. sp += src->w - dstRect->w;
  562. }
  563. }
  564. template<int bpp>
  565. void CSDL_Ext::blitWithRotate2WithAlpha(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24/32 bpp dests
  566. {
  567. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + srcRect->x;
  568. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  569. const SDL_Color * const colors = src->format->palette->colors;
  570. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  571. {
  572. Uint8 *dp = dporg;
  573. for(int j=dstRect->w; j>0; j--, sp++)
  574. {
  575. if(*sp)
  576. ColorPutter<bpp, 1>::PutColor(dp, colors[*sp]);
  577. else
  578. dp += bpp;
  579. }
  580. sp += src->w - dstRect->w;
  581. }
  582. }
  583. template<int bpp>
  584. void CSDL_Ext::blitWithRotate3WithAlpha(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24/32 bpp dests
  585. {
  586. Uint8 *sp = (Uint8 *)src->pixels + (src->h - srcRect->h - srcRect->y)*src->pitch + (src->w - srcRect->w - srcRect->x);
  587. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  588. const SDL_Color * const colors = src->format->palette->colors;
  589. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  590. {
  591. Uint8 *dp = dporg;
  592. for(int j=dstRect->w; j>0; j--, sp++)
  593. {
  594. if(*sp)
  595. ColorPutter<bpp, -1>::PutColor(dp, colors[*sp]);
  596. else
  597. dp -= bpp;
  598. }
  599. sp += src->w - dstRect->w;
  600. }
  601. }
  602. template<int bpp>
  603. int CSDL_Ext::blit8bppAlphaTo24bppT(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  604. {
  605. if (src && src->format->BytesPerPixel==1 && dst && (bpp==3 || bpp==4 || bpp==2)) //everything's ok
  606. {
  607. SDL_Rect fulldst;
  608. int srcx, srcy, w, h;
  609. /* Make sure the surfaces aren't locked */
  610. if ( ! src || ! dst )
  611. {
  612. SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  613. return -1;
  614. }
  615. if ( src->locked || dst->locked )
  616. {
  617. SDL_SetError("Surfaces must not be locked during blit");
  618. return -1;
  619. }
  620. /* If the destination rectangle is NULL, use the entire dest surface */
  621. if ( dstRect == NULL )
  622. {
  623. fulldst.x = fulldst.y = 0;
  624. dstRect = &fulldst;
  625. }
  626. /* clip the source rectangle to the source surface */
  627. if(srcRect)
  628. {
  629. int maxw, maxh;
  630. srcx = srcRect->x;
  631. w = srcRect->w;
  632. if(srcx < 0)
  633. {
  634. w += srcx;
  635. dstRect->x -= srcx;
  636. srcx = 0;
  637. }
  638. maxw = src->w - srcx;
  639. if(maxw < w)
  640. w = maxw;
  641. srcy = srcRect->y;
  642. h = srcRect->h;
  643. if(srcy < 0)
  644. {
  645. h += srcy;
  646. dstRect->y -= srcy;
  647. srcy = 0;
  648. }
  649. maxh = src->h - srcy;
  650. if(maxh < h)
  651. h = maxh;
  652. }
  653. else
  654. {
  655. srcx = srcy = 0;
  656. w = src->w;
  657. h = src->h;
  658. }
  659. /* clip the destination rectangle against the clip rectangle */
  660. {
  661. SDL_Rect *clip = &dst->clip_rect;
  662. int dx, dy;
  663. dx = clip->x - dstRect->x;
  664. if(dx > 0)
  665. {
  666. w -= dx;
  667. dstRect->x += dx;
  668. srcx += dx;
  669. }
  670. dx = dstRect->x + w - clip->x - clip->w;
  671. if(dx > 0)
  672. w -= dx;
  673. dy = clip->y - dstRect->y;
  674. if(dy > 0)
  675. {
  676. h -= dy;
  677. dstRect->y += dy;
  678. srcy += dy;
  679. }
  680. dy = dstRect->y + h - clip->y - clip->h;
  681. if(dy > 0)
  682. h -= dy;
  683. }
  684. if(w > 0 && h > 0)
  685. {
  686. dstRect->w = w;
  687. dstRect->h = h;
  688. if(SDL_LockSurface(dst))
  689. return -1; //if we cannot lock the surface
  690. const SDL_Color *colors = src->format->palette->colors;
  691. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  692. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  693. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  694. {
  695. Uint8 *color = colory;
  696. Uint8 *p = py;
  697. for(int x = w; x; x--)
  698. {
  699. const SDL_Color &tbc = colors[*color++]; //color to blit
  700. ColorPutter<bpp, +1>::PutColorAlphaSwitch(p, tbc.r, tbc.g, tbc.b, tbc.unused);
  701. }
  702. }
  703. SDL_UnlockSurface(dst);
  704. }
  705. }
  706. return 0;
  707. }
  708. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  709. {
  710. switch(dst->format->BytesPerPixel)
  711. {
  712. case 2: return blit8bppAlphaTo24bppT<2>(src, srcRect, dst, dstRect);
  713. case 3: return blit8bppAlphaTo24bppT<3>(src, srcRect, dst, dstRect);
  714. case 4: return blit8bppAlphaTo24bppT<4>(src, srcRect, dst, dstRect);
  715. default:
  716. tlog1 << (int)dst->format->BitsPerPixel << " bpp is not supported!!!\n";
  717. return -1;
  718. }
  719. }
  720. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  721. {
  722. Uint32 ret = 0;
  723. ret+=color->unused;
  724. ret<<=8; //*=256
  725. ret+=color->b;
  726. ret<<=8; //*=256
  727. ret+=color->g;
  728. ret<<=8; //*=256
  729. ret+=color->r;
  730. return ret;
  731. }
  732. void CSDL_Ext::update(SDL_Surface * what)
  733. {
  734. if(what)
  735. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  736. }
  737. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color)
  738. {
  739. for(int i = 0; i < w; i++)
  740. {
  741. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+i,y,color.x,color.y,color.z);
  742. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+i,y+h-1,color.x,color.y,color.z);
  743. }
  744. for(int i = 0; i < h; i++)
  745. {
  746. SDL_PutPixelWithoutRefreshIfInSurf(sur,x,y+i,color.x,color.y,color.z);
  747. SDL_PutPixelWithoutRefreshIfInSurf(sur,x+w-1,y+i,color.x,color.y,color.z);
  748. }
  749. }
  750. void CSDL_Ext::drawBorder( SDL_Surface * sur, const SDL_Rect &r, const int3 &color )
  751. {
  752. drawBorder(sur, r.x, r.y, r.w, r.h, color);
  753. }
  754. void CSDL_Ext::drawDashedBorder(SDL_Surface * sur, const Rect &r, const int3 &color)
  755. {
  756. const int y1 = r.y, y2 = r.y + r.h-1;
  757. for (int i=0; i<r.w; i++)
  758. {
  759. const int x = r.x + i;
  760. if (i%4 || (i==0))
  761. {
  762. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y1, color.x, color.y, color.z);
  763. SDL_PutPixelWithoutRefreshIfInSurf(sur, x, y2, color.x, color.y, color.z);
  764. }
  765. }
  766. const int x1 = r.x, x2 = r.x + r.w-1;
  767. for (int i=0; i<r.h; i++)
  768. {
  769. const int y = r.y + i;
  770. if ((i%4) || (i==0))
  771. {
  772. SDL_PutPixelWithoutRefreshIfInSurf(sur, x1, y, color.x, color.y, color.z);
  773. SDL_PutPixelWithoutRefreshIfInSurf(sur, x2, y, color.x, color.y, color.z);
  774. }
  775. }
  776. }
  777. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, ui8 player)
  778. {
  779. if(player==254)
  780. return;
  781. if(sur->format->BitsPerPixel==8)
  782. {
  783. SDL_Color *color = (player == 255
  784. ? graphics->neutralColor
  785. : &graphics->playerColors[player]);
  786. SDL_SetColors(sur, color, 5, 1);
  787. }
  788. else
  789. tlog3 << "Warning, setPlayerColor called on not 8bpp surface!\n";
  790. }
  791. const TColorPutter CSDL_Ext::getPutterFor(SDL_Surface * const &dest, int incrementing)
  792. {
  793. #define CASE_BPP(BytesPerPixel) \
  794. case BytesPerPixel: \
  795. if(incrementing > 0) \
  796. return ColorPutter<BytesPerPixel, 1>::PutColor; \
  797. else if(incrementing == 0) \
  798. return ColorPutter<BytesPerPixel, 0>::PutColor; \
  799. else \
  800. return ColorPutter<BytesPerPixel, -1>::PutColor;\
  801. break;
  802. switch(dest->format->BytesPerPixel)
  803. {
  804. CASE_BPP(2)
  805. CASE_BPP(3)
  806. CASE_BPP(4)
  807. default:
  808. tlog1 << (int)dest->format->BitsPerPixel << "bpp is not supported!\n";
  809. return NULL;
  810. }
  811. }
  812. const TColorPutterAlpha CSDL_Ext::getPutterAlphaFor(SDL_Surface * const &dest, int incrementing)
  813. {
  814. switch(dest->format->BytesPerPixel)
  815. {
  816. CASE_BPP(2)
  817. CASE_BPP(3)
  818. CASE_BPP(4)
  819. default:
  820. tlog1 << (int)dest->format->BitsPerPixel << "bpp is not supported!\n";
  821. return NULL;
  822. }
  823. #undef CASE_BPP
  824. }
  825. Uint8 * CSDL_Ext::getPxPtr(const SDL_Surface * const &srf, const int & x, const int & y)
  826. {
  827. return (Uint8 *)srf->pixels + y * srf->pitch + x * srf->format->BytesPerPixel;
  828. }
  829. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  830. {
  831. for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
  832. {
  833. boost::replace_first(str,"%s",tor[i]);
  834. }
  835. return str;
  836. }
  837. bool CSDL_Ext::isTransparent( SDL_Surface * srf, int x, int y )
  838. {
  839. if (x < 0 || y < 0 || x >= srf->w || y >= srf->h)
  840. return true;
  841. if(srf->format->BytesPerPixel == 1)
  842. {
  843. return ((ui8*)srf->pixels)[x + srf->pitch * y] == 0;
  844. }
  845. else
  846. {
  847. assert(!"isTransparent called with non-8bpp surface!");
  848. }
  849. return false;
  850. }
  851. void CSDL_Ext::VflipSurf(SDL_Surface * surf)
  852. {
  853. char buf[4]; //buffer
  854. int bpp = surf->format->BytesPerPixel;
  855. for (int y=0; y<surf->h; ++y)
  856. {
  857. char * base = (char*)surf->pixels + y * surf->pitch;
  858. for (int x=0; x<surf->w/2; ++x)
  859. {
  860. memcpy(buf, base + x * bpp, bpp);
  861. memcpy(base + x * bpp, base + (surf->w - x - 1) * bpp, bpp);
  862. memcpy(base + (surf->w - x - 1) * bpp, buf, bpp);
  863. }
  864. }
  865. }
  866. void CSDL_Ext::SDL_PutPixelWithoutRefresh(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A /*= 255*/)
  867. {
  868. Uint8 *p = getPxPtr(ekran, x, y);
  869. getPutterFor(ekran, false)(p, R, G, B);
  870. //needed?
  871. if(ekran->format->BytesPerPixel==4)
  872. p[3] = A;
  873. }
  874. void CSDL_Ext::SDL_PutPixelWithoutRefreshIfInSurf(SDL_Surface *ekran, const int & x, const int & y, const Uint8 & R, const Uint8 & G, const Uint8 & B, Uint8 A /*= 255*/)
  875. {
  876. if(x >= 0 && x < ekran->w && y >= 0 && y < ekran->h)
  877. SDL_PutPixelWithoutRefresh(ekran, x, y, R, G, B, A);
  878. }
  879. BlitterWithRotationVal CSDL_Ext::getBlitterWithRotation(SDL_Surface *dest)
  880. {
  881. switch(dest->format->BytesPerPixel)
  882. {
  883. case 2: return blitWithRotateClipVal<2>;
  884. case 3: return blitWithRotateClipVal<3>;
  885. case 4: return blitWithRotateClipVal<4>;
  886. default:
  887. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  888. break;
  889. }
  890. assert(0);
  891. return NULL;
  892. }
  893. BlitterWithRotationVal CSDL_Ext::getBlitterWithRotationAndAlpha(SDL_Surface *dest)
  894. {
  895. switch(dest->format->BytesPerPixel)
  896. {
  897. case 2: return blitWithRotateClipValWithAlpha<2>;
  898. case 3: return blitWithRotateClipValWithAlpha<3>;
  899. case 4: return blitWithRotateClipValWithAlpha<4>;
  900. default:
  901. tlog1 << (int)dest->format->BitsPerPixel << " bpp is not supported!!!\n";
  902. break;
  903. }
  904. assert(0);
  905. return NULL;
  906. }
  907. void CSDL_Ext::applyEffect( SDL_Surface * surf, const SDL_Rect * rect, int mode )
  908. {
  909. switch(mode)
  910. {
  911. case 0: //sepia
  912. {
  913. const int sepiaDepth = 20;
  914. const int sepiaIntensity = 30;
  915. for(int xp = rect->x; xp < rect->x + rect->w; ++xp)
  916. {
  917. for(int yp = rect->y; yp < rect->y + rect->h; ++yp)
  918. {
  919. ui8 * pixels = (ui8*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  920. int b = pixels[0];
  921. int g = pixels[1];
  922. int r = pixels[2];
  923. int gry = (r + g + b) / 3;
  924. r = g = b = gry;
  925. r = r + (sepiaDepth * 2);
  926. g = g + sepiaDepth;
  927. if (r>255) r=255;
  928. if (g>255) g=255;
  929. if (b>255) b=255;
  930. // Darken blue color to increase sepia effect
  931. b -= sepiaIntensity;
  932. // normalize if out of bounds
  933. if (b<0) b=0;
  934. if (b>255) b=255;
  935. pixels[0] = b;
  936. pixels[1] = g;
  937. pixels[2] = r;
  938. }
  939. }
  940. }
  941. break;
  942. case 1: //grayscale
  943. {
  944. for(int xp = rect->x; xp < rect->x + rect->w; ++xp)
  945. {
  946. for(int yp = rect->y; yp < rect->y + rect->h; ++yp)
  947. {
  948. ui8 * pixels = (ui8*)surf->pixels + yp * surf->pitch + xp * surf->format->BytesPerPixel;
  949. int b = pixels[0];
  950. int g = pixels[1];
  951. int r = pixels[2];
  952. int gry = (r + g + b) / 3;
  953. pixels[0] = pixels[1] = pixels[2] = gry;
  954. }
  955. }
  956. }
  957. break;
  958. default:
  959. throw std::runtime_error("Unsuppoerted efftct!");
  960. }
  961. }
  962. void CSDL_Ext::blitSurface( SDL_Surface * src, SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect )
  963. {
  964. if (dst != screen)
  965. {
  966. SDL_BlitSurface(src, srcRect, dst, dstRect);
  967. }
  968. else
  969. {
  970. SDL_Rect betterDst;
  971. if (dstRect)
  972. {
  973. betterDst = *dstRect;
  974. }
  975. else
  976. {
  977. betterDst = Rect(0, 0, dst->w, dst->h);
  978. }
  979. SDL_BlitSurface(src, srcRect, dst, &betterDst);
  980. }
  981. }
  982. void CSDL_Ext::fillRect( SDL_Surface *dst, SDL_Rect *dstrect, Uint32 color )
  983. {
  984. SDL_Rect newRect;
  985. if (dstrect)
  986. {
  987. newRect = *dstrect;
  988. }
  989. else
  990. {
  991. newRect = Rect(0, 0, dst->w, dst->h);
  992. }
  993. SDL_FillRect(dst, &newRect, color);
  994. }
  995. std::string CSDL_Ext::trimToFit(std::string text, int widthLimit, EFonts font)
  996. {
  997. int widthSoFar = 0;
  998. for(auto i = text.begin(); i != text.end(); i++)
  999. {
  1000. widthSoFar += graphics->fonts[font]->getCharWidth(*i);
  1001. if(widthSoFar > widthLimit)
  1002. {
  1003. //remove all characteres past limit
  1004. text.erase(i, text.end());
  1005. break;
  1006. }
  1007. }
  1008. return text;
  1009. }
  1010. SDL_Surface * CSDL_Ext::std32bppSurface = NULL;