SDL_Extensions.cpp 36 KB

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