Images.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Images.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "Images.h"
  12. #include "MiscWidgets.h"
  13. #include "../gui/CGuiHandler.h"
  14. #include "../render/IImage.h"
  15. #include "../render/IRenderHandler.h"
  16. #include "../render/CAnimation.h"
  17. #include "../render/Canvas.h"
  18. #include "../render/ColorFilter.h"
  19. #include "../render/Colors.h"
  20. #include "../battle/BattleInterface.h"
  21. #include "../battle/BattleInterfaceClasses.h"
  22. #include "../CGameInfo.h"
  23. #include "../CPlayerInterface.h"
  24. #include "../../CCallback.h"
  25. #include "../../lib/CConfigHandler.h"
  26. #include "../../lib/texts/CGeneralTextHandler.h" //for Unicode related stuff
  27. #include "../../lib/CRandomGenerator.h"
  28. static EImageBlitMode getModeForFlags( uint8_t flags)
  29. {
  30. if (flags & CCreatureAnim::CREATURE_MODE)
  31. return EImageBlitMode::WITH_SHADOW_AND_SELECTION;
  32. if (flags & CCreatureAnim::MAP_OBJECT_MODE)
  33. return EImageBlitMode::WITH_SHADOW;
  34. return EImageBlitMode::COLORKEY;
  35. }
  36. CPicture::CPicture(std::shared_ptr<IImage> image, const Point & position)
  37. : bg(image)
  38. , needRefresh(false)
  39. {
  40. pos += position;
  41. pos.w = bg->width();
  42. pos.h = bg->height();
  43. addUsedEvents(SHOW_POPUP);
  44. }
  45. CPicture::CPicture( const ImagePath &bmpname, int x, int y )
  46. : CPicture(bmpname, Point(x,y))
  47. {}
  48. CPicture::CPicture( const ImagePath & bmpname )
  49. : CPicture(bmpname, Point(0,0))
  50. {}
  51. CPicture::CPicture( const ImagePath & bmpname, const Point & position, EImageBlitMode mode )
  52. : bg(GH.renderHandler().loadImage(bmpname, mode))
  53. , needRefresh(false)
  54. {
  55. pos.x += position.x;
  56. pos.y += position.y;
  57. assert(bg);
  58. if(bg)
  59. {
  60. pos.w = bg->width();
  61. pos.h = bg->height();
  62. }
  63. else
  64. {
  65. pos.w = pos.h = 0;
  66. }
  67. addUsedEvents(SHOW_POPUP);
  68. }
  69. CPicture::CPicture( const ImagePath & bmpname, const Point & position )
  70. :CPicture(bmpname, position, EImageBlitMode::COLORKEY)
  71. {}
  72. CPicture::CPicture(const ImagePath & bmpname, const Rect &SrcRect, int x, int y)
  73. : CPicture(bmpname, Point(x,y))
  74. {
  75. srcRect = SrcRect;
  76. pos.w = srcRect->w;
  77. pos.h = srcRect->h;
  78. addUsedEvents(SHOW_POPUP);
  79. }
  80. CPicture::CPicture(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
  81. : CPicture(image, Point(x,y))
  82. {
  83. srcRect = SrcRect;
  84. pos.w = srcRect->w;
  85. pos.h = srcRect->h;
  86. addUsedEvents(SHOW_POPUP);
  87. }
  88. void CPicture::show(Canvas & to)
  89. {
  90. if (needRefresh)
  91. showAll(to);
  92. }
  93. void CPicture::showAll(Canvas & to)
  94. {
  95. if(bg)
  96. {
  97. if (srcRect.has_value())
  98. to.draw(bg, pos.topLeft(), *srcRect);
  99. else
  100. to.draw(bg, pos.topLeft());
  101. }
  102. }
  103. void CPicture::setAlpha(uint8_t value)
  104. {
  105. bg->setAlpha(value);
  106. }
  107. void CPicture::scaleTo(Point size)
  108. {
  109. bg->scaleTo(size, EScalingAlgorithm::BILINEAR);
  110. pos.w = bg->width();
  111. pos.h = bg->height();
  112. }
  113. void CPicture::setPlayerColor(PlayerColor player)
  114. {
  115. bg->playerColored(player);
  116. }
  117. void CPicture::addRClickCallback(const std::function<void()> & callback)
  118. {
  119. rCallback = callback;
  120. }
  121. void CPicture::showPopupWindow(const Point & cursorPosition)
  122. {
  123. if(rCallback)
  124. rCallback();
  125. }
  126. CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position)
  127. : CIntObject(0, position.topLeft())
  128. , texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
  129. {
  130. pos.w = position.w;
  131. pos.h = position.h;
  132. imageArea = Rect(Point(), texture->dimensions());
  133. }
  134. CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position, Rect imageArea)
  135. : CIntObject(0, position.topLeft())
  136. , texture(GH.renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
  137. , imageArea(imageArea)
  138. {
  139. pos.w = position.w;
  140. pos.h = position.h;
  141. }
  142. void CFilledTexture::showAll(Canvas & to)
  143. {
  144. CanvasClipRectGuard guard(to, pos);
  145. for (int y=pos.top(); y < pos.bottom(); y+= imageArea.h)
  146. {
  147. for (int x=pos.left(); x < pos.right(); x+= imageArea.w)
  148. to.draw(texture, Point(x,y), imageArea);
  149. }
  150. }
  151. void FilledTexturePlayerIndexed::setPlayerColor(PlayerColor player)
  152. {
  153. texture->playerColored(player);
  154. }
  155. FilledTexturePlayerColored::FilledTexturePlayerColored(Rect position)
  156. :CFilledTexture(ImagePath::builtin("DiBoxBck"), position)
  157. {
  158. }
  159. void FilledTexturePlayerColored::setPlayerColor(PlayerColor player)
  160. {
  161. ImagePath imagePath = ImagePath::builtin("DialogBoxBackground_" + player.toString() + ".bmp");
  162. texture = GH.renderHandler().loadImage(imagePath, EImageBlitMode::COLORKEY);
  163. }
  164. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  165. frame(Frame),
  166. group(Group),
  167. flags(Flags)
  168. {
  169. pos.x += x;
  170. pos.y += y;
  171. anim = GH.renderHandler().loadAnimation(name, getModeForFlags(Flags));
  172. init();
  173. }
  174. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
  175. anim(GH.renderHandler().loadAnimation(name, getModeForFlags(Flags))),
  176. frame(Frame),
  177. group(Group),
  178. flags(Flags),
  179. scaledSize(targetPos.w, targetPos.h)
  180. {
  181. pos.x += targetPos.x;
  182. pos.y += targetPos.y;
  183. init();
  184. }
  185. size_t CAnimImage::size()
  186. {
  187. return anim->size(group);
  188. }
  189. bool CAnimImage::isScaled() const
  190. {
  191. return (scaledSize.x != 0);
  192. }
  193. void CAnimImage::setSizeFromImage(const IImage &img)
  194. {
  195. if (isScaled())
  196. {
  197. // At the time of writing this, IImage had no method to scale to different aspect ratio
  198. // Therefore, have to ignore the target height and preserve original aspect ratio
  199. pos.w = scaledSize.x;
  200. pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
  201. }
  202. else
  203. {
  204. pos.w = img.width();
  205. pos.h = img.height();
  206. }
  207. }
  208. void CAnimImage::init()
  209. {
  210. visible = true;
  211. auto img = anim->getImage(frame, group);
  212. if (img)
  213. setSizeFromImage(*img);
  214. }
  215. CAnimImage::~CAnimImage()
  216. {
  217. }
  218. void CAnimImage::showAll(Canvas & to)
  219. {
  220. if(!visible)
  221. return;
  222. std::vector<size_t> frames = {frame};
  223. if((flags & CShowableAnim::BASE) && frame != 0)
  224. {
  225. frames.insert(frames.begin(), 0);
  226. }
  227. for(auto targetFrame : frames)
  228. {
  229. if(auto img = anim->getImage(targetFrame, group))
  230. {
  231. if(isScaled())
  232. img->scaleTo(scaledSize, EScalingAlgorithm::BILINEAR);
  233. to.draw(img, pos.topLeft());
  234. }
  235. }
  236. }
  237. void CAnimImage::setAnimationPath(const AnimationPath & name, size_t frame)
  238. {
  239. this->frame = frame;
  240. anim = GH.renderHandler().loadAnimation(name, EImageBlitMode::COLORKEY);
  241. init();
  242. }
  243. void CAnimImage::setScale(Point scale)
  244. {
  245. scaledSize = scale;
  246. }
  247. void CAnimImage::setFrame(size_t Frame, size_t Group)
  248. {
  249. if (frame == Frame && group==Group)
  250. return;
  251. if (anim->size(Group) > Frame)
  252. {
  253. frame = Frame;
  254. group = Group;
  255. if(auto img = anim->getImage(frame, group))
  256. {
  257. if (player.has_value())
  258. img->playerColored(*player);
  259. setSizeFromImage(*img);
  260. }
  261. }
  262. else
  263. logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
  264. }
  265. void CAnimImage::setPlayerColor(PlayerColor currPlayer)
  266. {
  267. player = currPlayer;
  268. anim->getImage(frame, group)->playerColored(*player);
  269. if (flags & CShowableAnim::BASE)
  270. anim->getImage(0, group)->playerColored(*player);
  271. }
  272. bool CAnimImage::isPlayerColored() const
  273. {
  274. return player.has_value();
  275. }
  276. CShowableAnim::CShowableAnim(int x, int y, const AnimationPath & name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
  277. anim(GH.renderHandler().loadAnimation(name, getModeForFlags(Flags))),
  278. group(Group),
  279. frame(0),
  280. first(0),
  281. frameTimeTotal(frameTime),
  282. frameTimePassed(0),
  283. flags(Flags),
  284. xOffset(0),
  285. yOffset(0),
  286. alpha(alpha)
  287. {
  288. last = anim->size(group);
  289. auto image = anim->getImage(0, group);
  290. if (!image)
  291. throw std::runtime_error("Failed to load group " + std::to_string(group) + " of animation file " + name.getOriginalName());
  292. pos.w = image->width();
  293. pos.h = image->height();
  294. pos.x+= x;
  295. pos.y+= y;
  296. addUsedEvents(TIME);
  297. }
  298. void CShowableAnim::setAlpha(ui32 alphaValue)
  299. {
  300. alpha = std::min<ui32>(alphaValue, 255);
  301. }
  302. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  303. {
  304. size_t max = anim->size(Group);
  305. if (to < max)
  306. max = to;
  307. if (max < from || max == 0)
  308. return false;
  309. group = Group;
  310. frame = first = from;
  311. last = max;
  312. frameTimePassed = 0;
  313. return true;
  314. }
  315. bool CShowableAnim::set(size_t Group)
  316. {
  317. if (anim->size(Group)== 0)
  318. return false;
  319. if (group != Group)
  320. {
  321. first = 0;
  322. group = Group;
  323. last = anim->size(Group);
  324. }
  325. frame = 0;
  326. frameTimePassed = 0;
  327. return true;
  328. }
  329. void CShowableAnim::reset()
  330. {
  331. frame = first;
  332. if (callback)
  333. callback();
  334. }
  335. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  336. {
  337. xOffset = posX;
  338. yOffset = posY;
  339. pos.w = width;
  340. pos.h = height;
  341. }
  342. void CShowableAnim::show(Canvas & to)
  343. {
  344. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  345. blitImage(first, group, to);
  346. blitImage(frame, group, to);
  347. }
  348. void CShowableAnim::tick(uint32_t msPassed)
  349. {
  350. if ((flags & PLAY_ONCE) && frame + 1 == last)
  351. return;
  352. frameTimePassed += msPassed;
  353. if(frameTimePassed >= frameTimeTotal)
  354. {
  355. frameTimePassed -= frameTimeTotal;
  356. if ( ++frame >= last)
  357. reset();
  358. }
  359. }
  360. void CShowableAnim::showAll(Canvas & to)
  361. {
  362. if ( flags & BASE )// && frame != first)
  363. blitImage(first, group, to);
  364. blitImage(frame, group, to);
  365. }
  366. void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
  367. {
  368. Rect src( xOffset, yOffset, pos.w, pos.h);
  369. auto img = anim->getImage(frame, group);
  370. if(img)
  371. {
  372. img->setAlpha(alpha);
  373. img->setOverlayColor(Colors::TRANSPARENCY);
  374. to.draw(img, pos.topLeft(), src);
  375. }
  376. }
  377. void CShowableAnim::rotate(bool on, bool vertical)
  378. {
  379. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  380. if (on)
  381. flags |= flag;
  382. else
  383. flags &= ~flag;
  384. }
  385. void CShowableAnim::setDuration(int durationMs)
  386. {
  387. frameTimeTotal = durationMs/(last - first);
  388. }
  389. CCreatureAnim::CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags, ECreatureAnimType type):
  390. CShowableAnim(x, y, name, flags | CREATURE_MODE, 100, size_t(type)) // H3 uses 100 ms per frame, irregardless of battle speed settings
  391. {
  392. xOffset = 0;
  393. yOffset = 0;
  394. }
  395. void CCreatureAnim::loopPreview(bool warMachine)
  396. {
  397. std::vector<ECreatureAnimType> available;
  398. static const ECreatureAnimType creaPreviewList[] = {
  399. ECreatureAnimType::HOLDING,
  400. ECreatureAnimType::HITTED,
  401. ECreatureAnimType::DEFENCE,
  402. ECreatureAnimType::ATTACK_FRONT,
  403. ECreatureAnimType::SPECIAL_FRONT
  404. };
  405. static const ECreatureAnimType machPreviewList[] = {
  406. ECreatureAnimType::HOLDING,
  407. ECreatureAnimType::MOVING,
  408. ECreatureAnimType::SHOOT_UP,
  409. ECreatureAnimType::SHOOT_FRONT,
  410. ECreatureAnimType::SHOOT_DOWN
  411. };
  412. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  413. for (auto & elem : previewList)
  414. if (anim->size(size_t(elem)))
  415. available.push_back(elem);
  416. size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
  417. if (rnd >= available.size())
  418. {
  419. ECreatureAnimType type;
  420. if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
  421. type = ECreatureAnimType::HOLDING;
  422. else
  423. type = ECreatureAnimType::MOVING;
  424. //display this anim for ~1 second (time is random, but it looks good)
  425. for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
  426. addLast(type);
  427. }
  428. else
  429. addLast(available[rnd]);
  430. }
  431. void CCreatureAnim::addLast(ECreatureAnimType newType)
  432. {
  433. auto currType = ECreatureAnimType(group);
  434. if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
  435. {
  436. queue.push( ECreatureAnimType::MOVE_START );
  437. }
  438. else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
  439. {
  440. queue.push( ECreatureAnimType::MOVE_END );
  441. }
  442. if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
  443. queue.push(newType);
  444. queue.push(newType);
  445. }
  446. void CCreatureAnim::reset()
  447. {
  448. //if we are in the middle of rotation - set flag
  449. if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
  450. rotate(true);
  451. if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
  452. rotate(false);
  453. while (!queue.empty())
  454. {
  455. ECreatureAnimType at = queue.front();
  456. queue.pop();
  457. if (set(size_t(at)))
  458. return;
  459. }
  460. if (callback)
  461. callback();
  462. while (!queue.empty())
  463. {
  464. ECreatureAnimType at = queue.front();
  465. queue.pop();
  466. if (set(size_t(at)))
  467. return;
  468. }
  469. set(size_t(ECreatureAnimType::HOLDING));
  470. }
  471. void CCreatureAnim::startPreview(bool warMachine)
  472. {
  473. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  474. }
  475. void CCreatureAnim::clearAndSet(ECreatureAnimType type)
  476. {
  477. while (!queue.empty())
  478. queue.pop();
  479. set(size_t(type));
  480. }