SDL_Extensions.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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::printAtMiddle(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality, bool refresh)
  115. {
  116. if(text.length()==0) return;
  117. SDL_Surface * temp;
  118. switch (quality)
  119. {
  120. case 0:
  121. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  122. break;
  123. case 1:
  124. SDL_Color tem;
  125. tem.b = 0xff-kolor.b;
  126. tem.g = 0xff-kolor.g;
  127. tem.r = 0xff-kolor.r;
  128. tem.unused = 0xff-kolor.unused;
  129. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  130. break;
  131. case 2:
  132. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  133. break;
  134. default:
  135. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  136. break;
  137. }
  138. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-(temp->w/2),y-(temp->h/2)));
  139. if(refresh)
  140. SDL_UpdateRect(dst,x-(temp->w/2),y-(temp->h/2),temp->w,temp->h);
  141. SDL_FreeSurface(temp);
  142. }
  143. 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)
  144. {
  145. if (text.length()==0)
  146. return;
  147. SDL_Surface * temp;
  148. switch (quality)
  149. {
  150. case 0:
  151. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  152. break;
  153. case 1:
  154. SDL_Color tem;
  155. tem.b = 0xff-kolor.b;
  156. tem.g = 0xff-kolor.g;
  157. tem.r = 0xff-kolor.r;
  158. tem.unused = 0xff-kolor.unused;
  159. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  160. break;
  161. case 2:
  162. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  163. break;
  164. default:
  165. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  166. break;
  167. }
  168. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x,y));
  169. if(refresh)
  170. SDL_UpdateRect(dst,x,y,temp->w,temp->h);
  171. SDL_FreeSurface(temp);
  172. }
  173. void CSDL_Ext::printAtWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  174. {
  175. printAt(text,x,y,font,kolor,dst,quality, true);
  176. }
  177. void CSDL_Ext::printTo(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  178. {
  179. if (text.length()==0)
  180. return;
  181. SDL_Surface * temp;
  182. switch (quality)
  183. {
  184. case 0:
  185. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  186. break;
  187. case 1:
  188. SDL_Color tem;
  189. tem.b = 0xff-kolor.b;
  190. tem.g = 0xff-kolor.g;
  191. tem.r = 0xff-kolor.r;
  192. tem.unused = 0xff-kolor.unused;
  193. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  194. break;
  195. case 2:
  196. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  197. break;
  198. default:
  199. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  200. break;
  201. }
  202. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-temp->w,y-temp->h));
  203. SDL_UpdateRect(dst,x-temp->w,y-temp->h,temp->w,temp->h);
  204. SDL_FreeSurface(temp);
  205. }
  206. void CSDL_Ext::printToWR(const std::string & text, int x, int y, TTF_Font * font, SDL_Color kolor, SDL_Surface * dst, unsigned char quality)
  207. {
  208. if (text.length()==0)
  209. return;
  210. SDL_Surface * temp;
  211. switch (quality)
  212. {
  213. case 0:
  214. temp = TTF_RenderText_Solid(font,text.c_str(),kolor);
  215. break;
  216. case 1:
  217. SDL_Color tem;
  218. tem.b = 0xff-kolor.b;
  219. tem.g = 0xff-kolor.g;
  220. tem.r = 0xff-kolor.r;
  221. tem.unused = 0xff-kolor.unused;
  222. temp = TTF_RenderText_Shaded(font,text.c_str(),kolor,tem);
  223. break;
  224. case 2:
  225. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  226. break;
  227. default:
  228. temp = TTF_RenderText_Blended(font,text.c_str(),kolor);
  229. break;
  230. }
  231. SDL_BlitSurface(temp,NULL,dst,&genRect(temp->h,temp->w,x-temp->w,y-temp->h));
  232. SDL_FreeSurface(temp);
  233. }
  234. 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)
  235. {
  236. Uint8 *p = (Uint8 *)ekran->pixels + y * ekran->pitch + x * ekran->format->BytesPerPixel;
  237. p[0] = B;
  238. p[1] = G;
  239. p[2] = R;
  240. if(ekran->format->BytesPerPixel==4)
  241. p[3] = A;
  242. SDL_UpdateRect(ekran, x, y, 1, 1);
  243. }
  244. // Vertical flip
  245. SDL_Surface * CSDL_Ext::rotate01(SDL_Surface * toRot)
  246. {
  247. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  248. const int bpl = ret->pitch;
  249. const int bpp = ret->format->BytesPerPixel;
  250. for(int i=0; i<ret->h; i++) {
  251. char *src = (char *)toRot->pixels + i*bpl;
  252. char *dst = (char *)ret->pixels + i*bpl;
  253. for(int j=0; j<ret->w; j++) {
  254. for (int k=0; k<bpp; k++) {
  255. dst[j*bpp + k] = src[(ret->w-j-1)*bpp + k];
  256. }
  257. }
  258. }
  259. return ret;
  260. }
  261. // Horizontal flip
  262. SDL_Surface * CSDL_Ext::hFlip(SDL_Surface * toRot)
  263. {
  264. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  265. int bpl = ret->pitch;
  266. for(int i=0; i<ret->h; i++) {
  267. memcpy((char *)ret->pixels + i*bpl, (char *)toRot->pixels + (ret->h-i-1)*bpl, bpl);
  268. }
  269. return ret;
  270. };
  271. ///**************/
  272. ///Rotates toRot surface by 90 degrees left
  273. ///**************/
  274. SDL_Surface * CSDL_Ext::rotate02(SDL_Surface * toRot)
  275. {
  276. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  277. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  278. for(int i=0; i<ret->w; ++i)
  279. {
  280. for(int j=0; j<ret->h; ++j)
  281. {
  282. {
  283. Uint8 *p = (Uint8 *)toRot->pixels + i * toRot->pitch + j * toRot->format->BytesPerPixel;
  284. /*
  285. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  286. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2]);
  287. #else
  288. */
  289. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0]);
  290. //#endif
  291. }
  292. }
  293. }
  294. return ret;
  295. }
  296. ///*************/
  297. ///Rotates toRot surface by 180 degrees
  298. ///*************/
  299. SDL_Surface * CSDL_Ext::rotate03(SDL_Surface * toRot)
  300. {
  301. SDL_Surface * ret = SDL_ConvertSurface(toRot, toRot->format, toRot->flags);
  302. //SDL_SetColorKey(ret, SDL_SRCCOLORKEY, toRot->format->colorkey);
  303. if(ret->format->BytesPerPixel!=1)
  304. {
  305. for(int i=0; i<ret->w; ++i)
  306. {
  307. for(int j=0; j<ret->h; ++j)
  308. {
  309. {
  310. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel+2;
  311. /*
  312. #if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
  313. SDL_PutPixelWithoutRefresh(ret, i, j, p[0], p[1], p[2], 0);
  314. #else
  315. */
  316. SDL_PutPixelWithoutRefresh(ret, i, j, p[2], p[1], p[0], 0);
  317. //#endif
  318. }
  319. }
  320. }
  321. }
  322. else
  323. {
  324. for(int i=0; i<ret->w; ++i)
  325. {
  326. for(int j=0; j<ret->h; ++j)
  327. {
  328. Uint8 *p = (Uint8 *)toRot->pixels + (ret->h - j - 1) * toRot->pitch + (ret->w - i - 1) * toRot->format->BytesPerPixel;
  329. (*((Uint8*)ret->pixels + j*ret->pitch + i*ret->format->BytesPerPixel)) = *p;
  330. }
  331. }
  332. }
  333. return ret;
  334. }
  335. Uint32 CSDL_Ext::SDL_GetPixel(SDL_Surface *surface, const int & x, const int & y, bool colorByte)
  336. {
  337. int bpp = surface->format->BytesPerPixel;
  338. /* Here p is the address to the pixel we want to retrieve */
  339. Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
  340. switch(bpp)
  341. {
  342. case 1:
  343. if(colorByte)
  344. {
  345. return colorToUint32(surface->format->palette->colors+(*p));
  346. }
  347. else
  348. return *p;
  349. case 2:
  350. return *(Uint16 *)p;
  351. case 3:
  352. /*
  353. #if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
  354. return p[0] << 16 | p[1] << 8 | p[2];
  355. #else
  356. */
  357. return p[0] | p[1] << 8 | p[2] << 16;
  358. //#endif
  359. case 4:
  360. return *(Uint32 *)p;
  361. default:
  362. return 0; // shouldn't happen, but avoids warnings
  363. }
  364. }
  365. void CSDL_Ext::alphaTransform(SDL_Surface *src)
  366. {
  367. assert(src->format->BitsPerPixel == 8);
  368. SDL_Color colors[] = {{0,0,0,255}, {0,0,0,214}, {0,0,0,164}, {0,0,0,82}, {0,0,0,128},
  369. {255,0,0,0}, {255,0,0,0}, {255,0,0,0}, {0,0,0,192}, {0,0,0,192}};
  370. SDL_SetColors(src, colors, 0, ARRAY_COUNT(colors));
  371. SDL_SetColorKey(src, SDL_SRCCOLORKEY, SDL_MapRGBA(src->format, 0, 0, 0, 255));
  372. }
  373. // <=>
  374. static void prepareOutRect(SDL_Rect &dst, const SDL_Rect *dstRect, const SDL_Rect *clip_rect)
  375. {
  376. dst.x = std::max(dstRect->x,clip_rect->x);
  377. dst.y = std::max(dstRect->y,clip_rect->y);
  378. dst.w = std::max(0,std::min(dstRect->w - (dst.x - dstRect->x), clip_rect->x + clip_rect->w - dst.x));
  379. dst.h = std::max(0,std::min(dstRect->h - (dst.y - dstRect->y), clip_rect->y + clip_rect->h - dst.y));
  380. }
  381. 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
  382. {
  383. Uint8 *sp = (Uint8 *)src->pixels;
  384. const int bpp = dst->format->BytesPerPixel;
  385. Uint8 *dporg = (Uint8 *)dst->pixels + dstRect->y*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  386. const SDL_Color * const colors = src->format->palette->colors;
  387. for(int i=dstRect->h; i>0; i--, dporg += dst->pitch)
  388. {
  389. Uint8 *dp = dporg;
  390. sp += src->w - dstRect->w;
  391. for(int j=dstRect->w; j>0; j--, sp++)
  392. {
  393. if (bpp == 4)
  394. *(--dp) = 0;
  395. const SDL_Color color = colors[*sp];
  396. *(--dp) = color.r;
  397. *(--dp) = color.g;
  398. *(--dp) = color.b;
  399. }
  400. }
  401. }
  402. 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
  403. {
  404. SDL_Rect realDest;
  405. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  406. blitWithRotate1(src,srcRect,dst,&realDest);
  407. }
  408. 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
  409. {
  410. Uint8 *sp = (Uint8 *)src->pixels;
  411. const int bpp = dst->format->BytesPerPixel;
  412. Uint8 *dporg = (Uint8 *)dst->pixels + (dstRect->y + dstRect->h - 1)*dst->pitch + dstRect->x*bpp;
  413. const SDL_Color * const colors = src->format->palette->colors;
  414. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  415. {
  416. Uint8 *dp = dporg;
  417. for(int j=dstRect->w; j>0; j--, sp++)
  418. {
  419. const SDL_Color color = colors[*sp];
  420. *(dp++) = color.b;
  421. *(dp++) = color.g;
  422. *(dp++) = color.r;
  423. if (bpp == 4)
  424. *(dp++) = 0;
  425. }
  426. sp += src->w - dstRect->w;
  427. }
  428. }
  429. 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
  430. {
  431. SDL_Rect realDest;
  432. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  433. blitWithRotate2(src,srcRect,dst,&realDest);
  434. }
  435. 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
  436. {
  437. Uint8 *sp = (Uint8 *)src->pixels;
  438. const int bpp = dst->format->BytesPerPixel;
  439. Uint8 *dporg = (Uint8 *)dst->pixels +(dstRect->y + dstRect->h - 1)*dst->pitch + (dstRect->x+dstRect->w)*bpp;
  440. const SDL_Color * const colors = src->format->palette->colors;
  441. for(int i=dstRect->h; i>0; i--, dporg -= dst->pitch)
  442. {
  443. Uint8 *dp = dporg;
  444. sp += src->w - dstRect->w;
  445. for(int j=dstRect->w; j>0; j--, sp++)
  446. {
  447. const SDL_Color color = colors[*sp];
  448. if (bpp == 4)
  449. *(--dp) = 0;
  450. *(--dp) = color.r;
  451. *(--dp) = color.g;
  452. *(--dp) = color.b;
  453. }
  454. }
  455. }
  456. 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
  457. {
  458. SDL_Rect realDest;
  459. prepareOutRect(realDest,dstRect,&dst->clip_rect);
  460. blitWithRotate3(src,srcRect,dst,&realDest);
  461. }
  462. int CSDL_Ext::blit8bppAlphaTo24bpp(const SDL_Surface * src, const SDL_Rect * srcRect, SDL_Surface * dst, SDL_Rect * dstRect)
  463. {
  464. const int bpp = dst->format->BytesPerPixel;
  465. if (src && src->format->BytesPerPixel==1 && dst && (bpp==3 || bpp==4)) //everything's ok
  466. {
  467. SDL_Rect fulldst;
  468. int srcx, srcy, w, h;
  469. /* Make sure the surfaces aren't locked */
  470. if ( ! src || ! dst )
  471. {
  472. SDL_SetError("SDL_UpperBlit: passed a NULL surface");
  473. return -1;
  474. }
  475. if ( src->locked || dst->locked )
  476. {
  477. SDL_SetError("Surfaces must not be locked during blit");
  478. return -1;
  479. }
  480. /* If the destination rectangle is NULL, use the entire dest surface */
  481. if ( dstRect == NULL )
  482. {
  483. fulldst.x = fulldst.y = 0;
  484. dstRect = &fulldst;
  485. }
  486. /* clip the source rectangle to the source surface */
  487. if(srcRect)
  488. {
  489. int maxw, maxh;
  490. srcx = srcRect->x;
  491. w = srcRect->w;
  492. if(srcx < 0)
  493. {
  494. w += srcx;
  495. dstRect->x -= srcx;
  496. srcx = 0;
  497. }
  498. maxw = src->w - srcx;
  499. if(maxw < w)
  500. w = maxw;
  501. srcy = srcRect->y;
  502. h = srcRect->h;
  503. if(srcy < 0)
  504. {
  505. h += srcy;
  506. dstRect->y -= srcy;
  507. srcy = 0;
  508. }
  509. maxh = src->h - srcy;
  510. if(maxh < h)
  511. h = maxh;
  512. }
  513. else
  514. {
  515. srcx = srcy = 0;
  516. w = src->w;
  517. h = src->h;
  518. }
  519. /* clip the destination rectangle against the clip rectangle */
  520. {
  521. SDL_Rect *clip = &dst->clip_rect;
  522. int dx, dy;
  523. dx = clip->x - dstRect->x;
  524. if(dx > 0)
  525. {
  526. w -= dx;
  527. dstRect->x += dx;
  528. srcx += dx;
  529. }
  530. dx = dstRect->x + w - clip->x - clip->w;
  531. if(dx > 0)
  532. w -= dx;
  533. dy = clip->y - dstRect->y;
  534. if(dy > 0)
  535. {
  536. h -= dy;
  537. dstRect->y += dy;
  538. srcy += dy;
  539. }
  540. dy = dstRect->y + h - clip->y - clip->h;
  541. if(dy > 0)
  542. h -= dy;
  543. }
  544. if(w > 0 && h > 0)
  545. {
  546. dstRect->w = w;
  547. dstRect->h = h;
  548. if(SDL_LockSurface(dst))
  549. return -1; //if we cannot lock the surface
  550. const SDL_Color *colors = src->format->palette->colors;
  551. Uint8 *colory = (Uint8*)src->pixels + srcy*src->pitch + srcx;
  552. Uint8 *py = (Uint8*)dst->pixels + dstRect->y*dst->pitch + dstRect->x*bpp;
  553. if(dst->format->Rshift==16) //such as screen
  554. {
  555. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  556. {
  557. Uint8 *color = colory;
  558. Uint8 *p = py;
  559. for(int x=w; x; x--, p += bpp)
  560. {
  561. const SDL_Color tbc = colors[*color++]; //color to blit
  562. switch (tbc.unused)
  563. {
  564. case 255:
  565. // ~59% of calls
  566. break;
  567. case 0:
  568. // ~37% of calls
  569. p[0] = tbc.b;
  570. p[1] = tbc.g;
  571. p[2] = tbc.r;
  572. break;
  573. case 128: // optimized
  574. // ~3.5% of calls
  575. p[0] = ((Uint16)tbc.b + (Uint16)p[0]) >> 1;
  576. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  577. p[2] = ((Uint16)tbc.r + (Uint16)p[2]) >> 1;
  578. break;
  579. default:
  580. // ~0.5% of calls
  581. p[0] = ((((Uint32)p[0]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  582. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  583. p[2] = ((((Uint32)p[2]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  584. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  585. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  586. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  587. break;
  588. }
  589. }
  590. }
  591. }
  592. else if(dst->format->Rshift==0) //like in most surfaces
  593. {
  594. for(int y=h; y; y--, colory+=src->pitch, py+=dst->pitch)
  595. {
  596. Uint8 *color = colory;
  597. Uint8 *p = py;
  598. for(int x=w; x; x--, p += bpp)
  599. {
  600. const SDL_Color tbc = colors[*color++]; //color to blit
  601. // According analyze, the values of tbc.unused are fixed,
  602. // and the approximate ratios are as following:
  603. //
  604. // tbc.unused numbers
  605. // 192 2679
  606. // 164 326907
  607. // 82 705590
  608. // 214 1292625
  609. // 128 4842923
  610. // 0 72138078
  611. // 255 77547326
  612. //
  613. // By making use of such characteristic, we may implement a
  614. // very fast algorithm for heroes3 without loose much quality.
  615. switch (tbc.unused)
  616. {
  617. case 255:
  618. break;
  619. case 0:
  620. p[0] = tbc.r;
  621. p[1] = tbc.g;
  622. p[2] = tbc.b;
  623. break;
  624. case 128: // optimized
  625. p[0] = ((Uint16)tbc.r + (Uint16)p[0]) >> 1;
  626. p[1] = ((Uint16)tbc.g + (Uint16)p[1]) >> 1;
  627. p[2] = ((Uint16)tbc.b + (Uint16)p[2]) >> 1;
  628. break;
  629. default:
  630. p[0] = ((((Uint32)p[0]-(Uint32)tbc.r)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.r);
  631. p[1] = ((((Uint32)p[1]-(Uint32)tbc.g)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.g);
  632. p[2] = ((((Uint32)p[2]-(Uint32)tbc.b)*(Uint32)tbc.unused) >> 8 + (Uint32)tbc.b);
  633. //p[0] = ((Uint32)tbc.unused*(Uint32)p[0] + (Uint32)tbc.r*(Uint32)(255-tbc.unused))>>8; //red
  634. //p[1] = ((Uint32)tbc.unused*(Uint32)p[1] + (Uint32)tbc.g*(Uint32)(255-tbc.unused))>>8; //green
  635. //p[2] = ((Uint32)tbc.unused*(Uint32)p[2] + (Uint32)tbc.b*(Uint32)(255-tbc.unused))>>8; //blue
  636. break;
  637. }
  638. }
  639. }
  640. }
  641. SDL_UnlockSurface(dst);
  642. }
  643. }
  644. return 0;
  645. }
  646. Uint32 CSDL_Ext::colorToUint32(const SDL_Color * color)
  647. {
  648. Uint32 ret = 0;
  649. ret+=color->unused;
  650. ret<<=8; //*=256
  651. ret+=color->b;
  652. ret<<=8; //*=256
  653. ret+=color->g;
  654. ret<<=8; //*=256
  655. ret+=color->r;
  656. return ret;
  657. }
  658. void CSDL_Ext::update(SDL_Surface * what)
  659. {
  660. if(what)
  661. SDL_UpdateRect(what, 0, 0, what->w, what->h);
  662. }
  663. void CSDL_Ext::drawBorder(SDL_Surface * sur, int x, int y, int w, int h, int3 color)
  664. {
  665. for(int i=0;i<w;i++)
  666. {
  667. SDL_PutPixelWithoutRefresh(sur,x+i,y,color.x,color.y,color.z);
  668. SDL_PutPixelWithoutRefresh(sur,x+i,y+h-1,color.x,color.y,color.z);
  669. }
  670. for(int i=0; i<h;i++)
  671. {
  672. SDL_PutPixelWithoutRefresh(sur,x,y+i,color.x,color.y,color.z);
  673. SDL_PutPixelWithoutRefresh(sur,x+w-1,y+i,color.x,color.y,color.z);
  674. }
  675. }
  676. void CSDL_Ext::setPlayerColor(SDL_Surface * sur, unsigned char player)
  677. {
  678. if(player==254)
  679. return;
  680. if(sur->format->BitsPerPixel==8)
  681. {
  682. if(player != 255)
  683. *(sur->format->palette->colors+5) = graphics->playerColors[player];
  684. else
  685. *(sur->format->palette->colors+5) = *graphics->neutralColor;
  686. }
  687. }
  688. int readNormalNr (std::istream &in, int bytCon)
  689. {
  690. int ret=0;
  691. int amp=1;
  692. unsigned char byte;
  693. if (in.good())
  694. {
  695. for (int i=0; i<bytCon; i++)
  696. {
  697. in.read((char*)&byte,1);
  698. ret+=byte*amp;
  699. amp<<=8;
  700. }
  701. }
  702. else return -1;
  703. return ret;
  704. }
  705. std::string CSDL_Ext::processStr(std::string str, std::vector<std::string> & tor)
  706. {
  707. for (size_t i=0; (i<tor.size())&&(boost::find_first(str,"%s")); ++i)
  708. {
  709. boost::replace_first(str,"%s",tor[i]);
  710. }
  711. return str;
  712. }
  713. SDL_Surface * CSDL_Ext::std32bppSurface = NULL;