Images.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #include "StdInc.h"
  2. #include "Images.h"
  3. #include "MiscWidgets.h"
  4. #include "../gui/CAnimation.h"
  5. #include "../gui/SDL_Pixels.h"
  6. #include "../gui/SDL_Extensions.h"
  7. #include "../gui/CGuiHandler.h"
  8. #include "../gui/CCursorHandler.h"
  9. #include "../battle/CBattleInterface.h"
  10. #include "../battle/CBattleInterfaceClasses.h"
  11. #include "../CBitmapHandler.h"
  12. #include "../Graphics.h"
  13. #include "../CGameInfo.h"
  14. #include "../CPlayerInterface.h"
  15. #include "../CMessage.h"
  16. #include "../CMusicHandler.h"
  17. #include "../windows/CAdvmapInterface.h"
  18. #include "../../CCallback.h"
  19. #include "../../lib/CConfigHandler.h"
  20. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  21. #include "../../lib/CRandomGenerator.h"
  22. /*
  23. * Images.cpp, part of VCMI engine
  24. *
  25. * Authors: listed in file AUTHORS in main folder
  26. *
  27. * License: GNU General Public License v2.0 or later
  28. * Full text of license available in license.txt file, in main folder
  29. *
  30. */
  31. CPicture::CPicture( SDL_Surface *BG, int x, int y, bool Free )
  32. {
  33. init();
  34. bg = BG;
  35. freeSurf = Free;
  36. pos.x += x;
  37. pos.y += y;
  38. pos.w = BG->w;
  39. pos.h = BG->h;
  40. }
  41. CPicture::CPicture( const std::string &bmpname, int x, int y )
  42. {
  43. init();
  44. bg = BitmapHandler::loadBitmap(bmpname);
  45. freeSurf = true;;
  46. pos.x += x;
  47. pos.y += y;
  48. if(bg)
  49. {
  50. pos.w = bg->w;
  51. pos.h = bg->h;
  52. }
  53. else
  54. {
  55. pos.w = pos.h = 0;
  56. }
  57. }
  58. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat /*= false*/)
  59. {
  60. init();
  61. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  62. }
  63. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat /*= false*/)
  64. {
  65. init();
  66. createSimpleRect(r, screenFormat, color);
  67. }
  68. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  69. {
  70. needRefresh = false;
  71. srcRect = new Rect(SrcRect);
  72. pos.x += x;
  73. pos.y += y;
  74. pos.w = srcRect->w;
  75. pos.h = srcRect->h;
  76. bg = BG;
  77. freeSurf = free;
  78. }
  79. void CPicture::setSurface(SDL_Surface *to)
  80. {
  81. bg = to;
  82. if (srcRect)
  83. {
  84. pos.w = srcRect->w;
  85. pos.h = srcRect->h;
  86. }
  87. else
  88. {
  89. pos.w = bg->w;
  90. pos.h = bg->h;
  91. }
  92. }
  93. CPicture::~CPicture()
  94. {
  95. if(freeSurf)
  96. SDL_FreeSurface(bg);
  97. delete srcRect;
  98. }
  99. void CPicture::init()
  100. {
  101. needRefresh = false;
  102. srcRect = nullptr;
  103. }
  104. void CPicture::show(SDL_Surface * to)
  105. {
  106. if (needRefresh)
  107. showAll(to);
  108. }
  109. void CPicture::showAll(SDL_Surface * to)
  110. {
  111. if(bg)
  112. {
  113. if(srcRect)
  114. {
  115. SDL_Rect srcRectCpy = *srcRect;
  116. SDL_Rect dstRect = srcRectCpy;
  117. dstRect.x = pos.x;
  118. dstRect.y = pos.y;
  119. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  120. }
  121. else
  122. blitAt(bg, pos, to);
  123. }
  124. }
  125. void CPicture::convertToScreenBPP()
  126. {
  127. SDL_Surface *hlp = bg;
  128. bg = SDL_ConvertSurface(hlp,screen->format,0);
  129. CSDL_Ext::setDefaultColorKey(bg);
  130. SDL_FreeSurface(hlp);
  131. }
  132. void CPicture::setAlpha(int value)
  133. {
  134. CSDL_Ext::setAlpha (bg, value);
  135. }
  136. void CPicture::scaleTo(Point size)
  137. {
  138. SDL_Surface * scaled = CSDL_Ext::scaleSurface(bg, size.x, size.y);
  139. if(freeSurf)
  140. SDL_FreeSurface(bg);
  141. setSurface(scaled);
  142. freeSurf = false;
  143. }
  144. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  145. {
  146. pos += r;
  147. pos.w = r.w;
  148. pos.h = r.h;
  149. if(screenFormat)
  150. bg = CSDL_Ext::newSurface(r.w, r.h);
  151. else
  152. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  153. SDL_FillRect(bg, nullptr, color);
  154. freeSurf = true;
  155. }
  156. void CPicture::colorizeAndConvert(PlayerColor player)
  157. {
  158. assert(bg);
  159. colorize(player);
  160. convertToScreenBPP();
  161. }
  162. void CPicture::colorize(PlayerColor player)
  163. {
  164. assert(bg);
  165. graphics->blueToPlayersAdv(bg, player);
  166. }
  167. CFilledTexture::CFilledTexture(std::string imageName, Rect position):
  168. CIntObject(0, position.topLeft()),
  169. texture(BitmapHandler::loadBitmap(imageName))
  170. {
  171. pos.w = position.w;
  172. pos.h = position.h;
  173. }
  174. CFilledTexture::~CFilledTexture()
  175. {
  176. SDL_FreeSurface(texture);
  177. }
  178. void CFilledTexture::showAll(SDL_Surface *to)
  179. {
  180. CSDL_Ext::CClipRectGuard guard(to, pos);
  181. CSDL_Ext::fillTexture(to, texture);
  182. }
  183. CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  184. frame(Frame),
  185. group(Group),
  186. player(-1),
  187. flags(Flags)
  188. {
  189. pos.x += x;
  190. pos.y += y;
  191. anim = new CAnimation(name);
  192. init();
  193. }
  194. CAnimImage::CAnimImage(CAnimation *Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  195. anim(Anim),
  196. frame(Frame),
  197. group(Group),
  198. player(-1),
  199. flags(Flags)
  200. {
  201. pos.x += x;
  202. pos.y += y;
  203. init();
  204. }
  205. size_t CAnimImage::size()
  206. {
  207. return anim->size(group);
  208. }
  209. void CAnimImage::init()
  210. {
  211. anim->load(frame, group);
  212. if (flags & CShowableAnim::BASE)
  213. anim->load(0,group);
  214. IImage *img = anim->getImage(frame, group);
  215. if (img)
  216. {
  217. pos.w = img->width();
  218. pos.h = img->height();
  219. }
  220. }
  221. CAnimImage::~CAnimImage()
  222. {
  223. anim->unload(frame, group);
  224. if (flags & CShowableAnim::BASE)
  225. anim->unload(0,group);
  226. delete anim;
  227. }
  228. void CAnimImage::showAll(SDL_Surface * to)
  229. {
  230. IImage *img;
  231. if ( flags & CShowableAnim::BASE && frame != 0)
  232. if ((img = anim->getImage(0, group)))
  233. img->draw(to, pos.x, pos.y);
  234. if ((img = anim->getImage(frame, group)))
  235. img->draw(to, pos.x, pos.y);
  236. }
  237. void CAnimImage::setFrame(size_t Frame, size_t Group)
  238. {
  239. if (frame == Frame && group==Group)
  240. return;
  241. if (anim->size(Group) > Frame)
  242. {
  243. anim->load(Frame, Group);
  244. anim->unload(frame, group);
  245. frame = Frame;
  246. group = Group;
  247. IImage *img = anim->getImage(frame, group);
  248. if (img)
  249. {
  250. if (flags & CShowableAnim::PLAYER_COLORED)
  251. img->playerColored(player);
  252. pos.w = img->width();
  253. pos.h = img->height();
  254. }
  255. }
  256. else
  257. logGlobal->errorStream() << "Error: accessing unavailable frame " << Group << ":" << Frame << " in CAnimation!";
  258. }
  259. void CAnimImage::playerColored(PlayerColor currPlayer)
  260. {
  261. player = currPlayer;
  262. flags |= CShowableAnim::PLAYER_COLORED;
  263. anim->getImage(frame, group)->playerColored(player);
  264. if (flags & CShowableAnim::BASE)
  265. anim->getImage(0, group)->playerColored(player);
  266. }
  267. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
  268. anim(new CAnimation(name, Flags & USE_RLE)),
  269. group(Group),
  270. frame(0),
  271. first(0),
  272. frameDelay(Delay),
  273. value(0),
  274. flags(Flags),
  275. xOffset(0),
  276. yOffset(0),
  277. alpha(255)
  278. {
  279. anim->loadGroup(group);
  280. last = anim->size(group);
  281. pos.w = anim->getImage(0, group)->width();
  282. pos.h = anim->getImage(0, group)->height();
  283. pos.x+= x;
  284. pos.y+= y;
  285. }
  286. CShowableAnim::~CShowableAnim()
  287. {
  288. anim->unloadGroup(group);
  289. delete anim;
  290. }
  291. void CShowableAnim::setAlpha(ui32 alphaValue)
  292. {
  293. alpha = std::min<ui32>(alphaValue, 255);
  294. }
  295. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  296. {
  297. size_t max = anim->size(Group);
  298. if (to < max)
  299. max = to;
  300. if (max < from || max == 0)
  301. return false;
  302. anim->load(Group);
  303. anim->unload(group);
  304. group = Group;
  305. frame = first = from;
  306. last = max;
  307. value = 0;
  308. return true;
  309. }
  310. bool CShowableAnim::set(size_t Group)
  311. {
  312. if (anim->size(Group)== 0)
  313. return false;
  314. if (group != Group)
  315. {
  316. anim->loadGroup(Group);
  317. anim->unloadGroup(group);
  318. first = 0;
  319. group = Group;
  320. last = anim->size(Group);
  321. }
  322. frame = value = 0;
  323. return true;
  324. }
  325. void CShowableAnim::reset()
  326. {
  327. value = 0;
  328. frame = first;
  329. if (callback)
  330. callback();
  331. }
  332. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  333. {
  334. xOffset = posX;
  335. yOffset = posY;
  336. pos.w = width;
  337. pos.h = height;
  338. }
  339. void CShowableAnim::show(SDL_Surface * to)
  340. {
  341. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  342. blitImage(first, group, to);
  343. blitImage(frame, group, to);
  344. if ((flags & PLAY_ONCE) && frame + 1 == last)
  345. return;
  346. if ( ++value == frameDelay )
  347. {
  348. value = 0;
  349. if ( ++frame >= last)
  350. reset();
  351. }
  352. }
  353. void CShowableAnim::showAll(SDL_Surface * to)
  354. {
  355. if ( flags & BASE )// && frame != first)
  356. blitImage(first, group, to);
  357. blitImage(frame, group, to);
  358. }
  359. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  360. {
  361. assert(to);
  362. Rect src( xOffset, yOffset, pos.w, pos.h);
  363. IImage * img = anim->getImage(frame, group);
  364. if (img)
  365. img->draw(to, pos.x-xOffset, pos.y-yOffset, &src, alpha);
  366. }
  367. void CShowableAnim::rotate(bool on, bool vertical)
  368. {
  369. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  370. if (on)
  371. flags |= flag;
  372. else
  373. flags &= ~flag;
  374. }
  375. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags, EAnimType type):
  376. CShowableAnim(x,y,name,flags,4,type)
  377. {
  378. xOffset = picPos.x;
  379. yOffset = picPos.y;
  380. if (picPos.w)
  381. pos.w = picPos.w;
  382. if (picPos.h)
  383. pos.h = picPos.h;
  384. };
  385. void CCreatureAnim::loopPreview(bool warMachine)
  386. {
  387. std::vector<EAnimType> available;
  388. static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
  389. static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
  390. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  391. for (auto & elem : previewList)
  392. if (anim->size(elem))
  393. available.push_back(elem);
  394. size_t rnd = CRandomGenerator::getDefault().nextInt(available.size() * 2 - 1);
  395. if (rnd >= available.size())
  396. {
  397. EAnimType type;
  398. if ( anim->size(MOVING) == 0 )//no moving animation present
  399. type = HOLDING;
  400. else
  401. type = MOVING;
  402. //display this anim for ~1 second (time is random, but it looks good)
  403. for (size_t i=0; i< 12/anim->size(type) + 1; i++)
  404. addLast(type);
  405. }
  406. else
  407. addLast(available[rnd]);
  408. }
  409. void CCreatureAnim::addLast(EAnimType newType)
  410. {
  411. if (type != MOVING && newType == MOVING)//starting moving - play init sequence
  412. {
  413. queue.push( MOVE_START );
  414. }
  415. else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
  416. {
  417. queue.push( MOVE_END );
  418. }
  419. if (newType == TURN_L || newType == TURN_R)
  420. queue.push(newType);
  421. queue.push(newType);
  422. }
  423. void CCreatureAnim::reset()
  424. {
  425. //if we are in the middle of rotation - set flag
  426. if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
  427. rotate(true);
  428. if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
  429. rotate(false);
  430. while (!queue.empty())
  431. {
  432. EAnimType at = queue.front();
  433. queue.pop();
  434. if (set(at))
  435. return;
  436. }
  437. if (callback)
  438. callback();
  439. while (!queue.empty())
  440. {
  441. EAnimType at = queue.front();
  442. queue.pop();
  443. if (set(at))
  444. return;
  445. }
  446. set(HOLDING);
  447. }
  448. void CCreatureAnim::startPreview(bool warMachine)
  449. {
  450. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  451. }
  452. void CCreatureAnim::clearAndSet(EAnimType type)
  453. {
  454. while (!queue.empty())
  455. queue.pop();
  456. set(type);
  457. }