Images.cpp 13 KB

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