SDL_Extensions.cpp 35 KB

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