Images.cpp 12 KB

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