SDL_Extensions.cpp 29 KB

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