SDL_Extensions.cpp 28 KB

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