SDL_Extensions.cpp 37 KB

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