SDL_Extensions.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  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 &dst, const SDL_Rect *dstRect, const SDL_Rect *clip_rect)
  448. {
  449. dst.x = std::max(dstRect->x,clip_rect->x);
  450. dst.y = std::max(dstRect->y,clip_rect->y);
  451. dst.w = std::max(0,std::min(dstRect->w - (dst.x - dstRect->x), clip_rect->x + clip_rect->w - dst.x));
  452. dst.h = std::max(0,std::min(dstRect->h - (dst.y - dstRect->y), clip_rect->y + clip_rect->h - dst.y));
  453. }
  454. 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
  455. {
  456. Uint8 *sp = (Uint8 *)src->pixels;
  457. const int bpp = dst->format->BytesPerPixel;
  458. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  459. const SDL_Color * const colors = src->format->palette->colors;
  460. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  461. {
  462. Uint8 *dp = dporg;
  463. sp += src->w - dstRect->w;
  464. for(int j=dstRect->w; j>0; j--, sp++)
  465. {
  466. if (bpp == 4)
  467. *(--dp) = 0;
  468. const SDL_Color color = colors[*sp];
  469. *(--dp) = color.r;
  470. *(--dp) = color.g;
  471. *(--dp) = color.b;
  472. }
  473. }
  474. }
  475. void CSDL_Ext::blitWithRotate1clip(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  476. {
  477. SDL_Rect realDest;
  478. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  479. blitWithRotate1(src,srcRect,dst,&realDest);
  480. }
  481. 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
  482. {
  483. Uint8 *sp = (Uint8 *)src->pixels;
  484. const int bpp = dst->format->BytesPerPixel;
  485. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  486. const SDL_Color * const colors = src->format->palette->colors;
  487. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  488. {
  489. Uint8 *dp = dporg;
  490. for(int j=dstRect->w; j>0; j--, sp++)
  491. {
  492. const SDL_Color color = colors[*sp];
  493. *(dp++) = color.b;
  494. *(dp++) = color.g;
  495. *(dp++) = color.r;
  496. if (bpp == 4)
  497. *(dp++) = 0;
  498. }
  499. sp += src->w - dstRect->w;
  500. }
  501. }
  502. void CSDL_Ext::blitWithRotate2clip(const SDL_Surface *src, const SDL_Rect * srcRect, SDL_Surface * dst, const SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  503. {
  504. SDL_Rect realDest;
  505. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  506. blitWithRotate2(src,srcRect,dst,&realDest);
  507. }
  508. 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
  509. {
  510. Uint8 *sp = (Uint8 *)src->pixels;
  511. const int bpp = dst->format->BytesPerPixel;
  512. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  513. const SDL_Color * const colors = src->format->palette->colors;
  514. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  515. {
  516. Uint8 *dp = dporg;
  517. sp += src->w - dstRect->w;
  518. for(int j=dstRect->w; j>0; j--, sp++)
  519. {
  520. const SDL_Color color = colors[*sp];
  521. if (bpp == 4)
  522. *(--dp) = 0;
  523. *(--dp) = color.r;
  524. *(--dp) = color.g;
  525. *(--dp) = color.b;
  526. }
  527. }
  528. }
  529. void CSDL_Ext::blitWithRotate3clip(SDL_Surface *src,SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)//srcRect is not used, works with 8bpp sources and 24bpp dests
  530. {
  531. SDL_Rect realDest;
  532. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  533. blitWithRotate3(src,srcRect,dst,&realDest);
  534. }
  535. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  536. {
  537. const int bpp = dst->format->BytesPerPixel;
  538. if (src && src->format->BytesPerPixel==1 && dst && (bpp==3 || bpp==4)) //everything's ok
  539. {
  540. SDL_Rect fulldst;
  541. int srcx, srcy, w, h;
  542. /* Make sure the surfaces aren't locked */
  543. if ( ! src || ! dst )
  544. {
  545. SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  546. return -1;
  547. }
  548. if ( src->locked || dst->locked )
  549. {
  550. SDL_SetError("Surfaces must not be locked during blit");
  551. return -1;
  552. }
  553. /* If the destination rectangle is NULL, use the entire dest surface */
  554. if ( dstRect == NULL )
  555. {
  556. fulldst.x = fulldst.y = 0;
  557. dstRect = &fulldst;
  558. }
  559. /* clip the source rectangle to the source surface */
  560. if(srcRect)
  561. {
  562. int maxw, maxh;
  563. srcx = srcRect->x;
  564. w = srcRect->w;
  565. if(srcx < 0)
  566. {
  567. w += srcx;
  568. dstRect->x -= srcx;
  569. srcx = 0;
  570. }
  571. maxw = src->w - srcx;
  572. if(maxw < w)
  573. w = maxw;
  574. srcy = srcRect->y;
  575. h = srcRect->h;
  576. if(srcy < 0)
  577. {
  578. h += srcy;
  579. dstRect->y -= srcy;
  580. srcy = 0;
  581. }
  582. maxh = src->h - srcy;
  583. if(maxh < h)
  584. h = maxh;
  585. }
  586. else
  587. {
  588. srcx = srcy = 0;
  589. w = src->w;
  590. h = src->h;
  591. }
  592. /* clip the destination rectangle against the clip rectangle */
  593. {
  594. SDL_Rect *clip = &dst->clip_rect;
  595. int dx, dy;
  596. dx = clip->x - dstRect->x;
  597. if(dx > 0)
  598. {
  599. w -= dx;
  600. dstRect->x += dx;
  601. srcx += dx;
  602. }
  603. dx = dstRect->x + w - clip->x - clip->w;
  604. if(dx > 0)
  605. w -= dx;
  606. dy = clip->y - dstRect->y;
  607. if(dy > 0)
  608. {
  609. h -= dy;
  610. dstRect->y += dy;
  611. srcy += dy;
  612. }
  613. dy = dstRect->y + h - clip->y - clip->h;
  614. if(dy > 0)
  615. h -= dy;
  616. }
  617. if(w > 0 && h > 0)
  618. {
  619. dstRect->w = w;
  620. dstRect->h = h;
  621. if(SDL_LockSurface(dst))
  622. return -1; //if we cannot lock the surface
  623. const SDL_Color *colors = src->format->palette->colors;
  624. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  625. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  626. if(dst->format->Rshift==16) //such as screen
  627. {
  628. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  629. {
  630. Uint8 *color = colory;
  631. Uint8 *p = py;
  632. for(int x=w; x; x--, p += bpp)
  633. {
  634. const SDL_Color tbc = colors[*color++]; //color to blit
  635. switch (tbc.unused)
  636. {
  637. case 255:
  638. // ~59% of calls
  639. break;
  640. case 0:
  641. // ~37% of calls
  642. p[0] = tbc.b;
  643. p[1] = tbc.g;
  644. p[2] = tbc.r;
  645. break;
  646. case 128: // optimized
  647. // ~3.5% of calls
  648. p[0] = ((Uint16)tbc.b + (Uint16)p[0]) >> 1;
  649. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  650. p[2] = ((Uint16)tbc.r + (Uint16)p[2]) >> 1;
  651. break;
  652. default:
  653. // ~0.5% of calls
  654. p[0] = ((((Uint32)p[0]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  655. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  656. p[2] = ((((Uint32)p[2]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  657. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  658. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  659. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  660. break;
  661. }
  662. }
  663. }
  664. }
  665. else if(dst->format->Rshift==0) //like in most surfaces
  666. {
  667. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  668. {
  669. Uint8 *color = colory;
  670. Uint8 *p = py;
  671. for(int x=w; x; x--, p += bpp)
  672. {
  673. const SDL_Color tbc = colors[*color++]; //color to blit
  674. // According analyze, the values of tbc.unused are fixed,
  675. // and the approximate ratios are as following:
  676. //
  677. // tbc.unused numbers
  678. // 192 2679
  679. // 164 326907
  680. // 82 705590
  681. // 214 1292625
  682. // 128 4842923
  683. // 0 72138078
  684. // 255 77547326
  685. //
  686. // By making use of such characteristic, we may implement a
  687. // very fast algorithm for heroes3 without loose much quality.
  688. switch (tbc.unused)
  689. {
  690. case 255:
  691. break;
  692. case 0:
  693. p[0] = tbc.r;
  694. p[1] = tbc.g;
  695. p[2] = tbc.b;
  696. break;
  697. case 128: // optimized
  698. p[0] = ((Uint16)tbc.r + (Uint16)p[0]) >> 1;
  699. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  700. p[2] = ((Uint16)tbc.b + (Uint16)p[2]) >> 1;
  701. break;
  702. default:
  703. p[0] = ((((Uint32)p[0]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  704. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  705. p[2] = ((((Uint32)p[2]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  706. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  707. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  708. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  709. break;
  710. }
  711. }
  712. }
  713. }
  714. SDL_UnlockSurface(dst);
  715. }
  716. }
  717. return 0;
  718. }
  719. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  720. {
  721. Uint32 ret = 0;
  722. ret+=color->unused;
  723. ret<<=8; //*=256
  724. ret+=color->b;
  725. ret<<=8; //*=256
  726. ret+=color->g;
  727. ret<<=8; //*=256
  728. ret+=color->r;
  729. return ret;
  730. }
  731. void CSDL_Ext::update(SDL_Surface * what)
  732. {
  733. if(what)
  734. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  735. }
  736. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, const int3 &color)
  737. {
  738. for(int i=0;i<w;i++)
  739. {
  740. SDL_PutPixelWithoutRefresh(sur,x+i,y,color.x,color.y,color.z);
  741. SDL_PutPixelWithoutRefresh(sur,x+i,y+h-1,color.x,color.y,color.z);
  742. }
  743. for(int i=0; i<h;i++)
  744. {
  745. SDL_PutPixelWithoutRefresh(sur,x,y+i,color.x,color.y,color.z);
  746. SDL_PutPixelWithoutRefresh(sur,x+w-1,y+i,color.x,color.y,color.z);
  747. }
  748. }
  749. void CSDL_Ext::drawBorder( SDL_Surface * sur, const SDL_Rect &r, const int3 &color )
  750. {
  751. drawBorder(sur, r.x, r.y, r.w, r.h, color);
  752. }
  753. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, unsigned char player)
  754. {
  755. if(player==254)
  756. return;
  757. if(sur->format->BitsPerPixel==8)
  758. {
  759. if(player != 255)
  760. *(sur->format->palette->colors+5) = graphics->playerColors[player];
  761. else
  762. *(sur->format->palette->colors+5) = *graphics->neutralColor;
  763. }
  764. }
  765. int readNormalNr (std::istream &in, int bytCon)
  766. {
  767. int ret=0;
  768. int amp=1;
  769. unsigned char byte;
  770. if (in.good())
  771. {
  772. for (int i=0; i<bytCon; i++)
  773. {
  774. in.read((char*)&byte,1);
  775. ret+=byte*amp;
  776. amp<<=8;
  777. }
  778. }
  779. else return -1;
  780. return ret;
  781. }
  782. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  783. {
  784. for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
  785. {
  786. boost::replace_first(str,"%s",tor[i]);
  787. }
  788. return str;
  789. }
  790. SDL_Surface * CSDL_Ext::std32bppSurface = NULL;