Images.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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 "../../lib/CConfigHandler.h"
  24. #include "../../lib/texts/CGeneralTextHandler.h" //for Unicode related stuff
  25. #include "../../lib/CRandomGenerator.h"
  26. static EImageBlitMode getModeForFlags( uint8_t flags)
  27. {
  28. if (flags & CCreatureAnim::CREATURE_MODE)
  29. return EImageBlitMode::WITH_SHADOW_AND_SELECTION;
  30. if (flags & CCreatureAnim::MAP_OBJECT_MODE)
  31. return EImageBlitMode::WITH_SHADOW;
  32. return EImageBlitMode::COLORKEY;
  33. }
  34. CPicture::CPicture(std::shared_ptr<IImage> image, const Point & position)
  35. : bg(image)
  36. , needRefresh(false)
  37. {
  38. pos += position;
  39. pos.w = bg->width();
  40. pos.h = bg->height();
  41. addUsedEvents(SHOW_POPUP);
  42. }
  43. CPicture::CPicture( const ImagePath &bmpname, int x, int y )
  44. : CPicture(bmpname, Point(x,y))
  45. {}
  46. CPicture::CPicture( const ImagePath & bmpname )
  47. : CPicture(bmpname, Point(0,0))
  48. {}
  49. CPicture::CPicture( const ImagePath & bmpname, const Point & position, EImageBlitMode mode )
  50. : bg(ENGINE->renderHandler().loadImage(bmpname, mode))
  51. , needRefresh(false)
  52. {
  53. pos.x += position.x;
  54. pos.y += position.y;
  55. assert(bg);
  56. if(bg)
  57. {
  58. pos.w = bg->width();
  59. pos.h = bg->height();
  60. }
  61. else
  62. {
  63. pos.w = pos.h = 0;
  64. }
  65. addUsedEvents(SHOW_POPUP);
  66. }
  67. CPicture::CPicture( const ImagePath & bmpname, const Point & position )
  68. :CPicture(bmpname, position, EImageBlitMode::COLORKEY)
  69. {}
  70. CPicture::CPicture(const ImagePath & bmpname, const Rect &SrcRect, int x, int y)
  71. : CPicture(bmpname, Point(x,y))
  72. {
  73. srcRect = SrcRect;
  74. pos.w = srcRect->w;
  75. pos.h = srcRect->h;
  76. addUsedEvents(SHOW_POPUP);
  77. }
  78. CPicture::CPicture(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
  79. : CPicture(image, Point(x,y))
  80. {
  81. srcRect = SrcRect;
  82. pos.w = srcRect->w;
  83. pos.h = srcRect->h;
  84. addUsedEvents(SHOW_POPUP);
  85. }
  86. void CPicture::show(Canvas & to)
  87. {
  88. if (needRefresh)
  89. showAll(to);
  90. }
  91. void CPicture::showAll(Canvas & to)
  92. {
  93. if(bg)
  94. {
  95. if (srcRect.has_value())
  96. to.draw(bg, pos.topLeft(), *srcRect);
  97. else
  98. to.draw(bg, pos.topLeft());
  99. }
  100. }
  101. void CPicture::setAlpha(uint8_t value)
  102. {
  103. bg->setAlpha(value);
  104. }
  105. void CPicture::scaleTo(Point size)
  106. {
  107. bg->scaleTo(size, EScalingAlgorithm::BILINEAR);
  108. pos.w = bg->width();
  109. pos.h = bg->height();
  110. }
  111. void CPicture::setPlayerColor(PlayerColor player)
  112. {
  113. bg->playerColored(player);
  114. }
  115. void CPicture::addRClickCallback(const std::function<void()> & callback)
  116. {
  117. rCallback = callback;
  118. }
  119. void CPicture::showPopupWindow(const Point & cursorPosition)
  120. {
  121. if(rCallback)
  122. rCallback();
  123. }
  124. CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position)
  125. : CIntObject(0, position.topLeft())
  126. , texture(ENGINE->renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
  127. {
  128. pos.w = position.w;
  129. pos.h = position.h;
  130. imageArea = Rect(Point(), texture->dimensions());
  131. }
  132. CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position, Rect imageArea)
  133. : CIntObject(0, position.topLeft())
  134. , texture(ENGINE->renderHandler().loadImage(imageName, EImageBlitMode::COLORKEY))
  135. , imageArea(imageArea)
  136. {
  137. pos.w = position.w;
  138. pos.h = position.h;
  139. }
  140. void CFilledTexture::showAll(Canvas & to)
  141. {
  142. CanvasClipRectGuard guard(to, pos);
  143. for (int y=pos.top(); y < pos.bottom(); y+= imageArea.h)
  144. {
  145. for (int x=pos.left(); x < pos.right(); x+= imageArea.w)
  146. to.draw(texture, Point(x,y), imageArea);
  147. }
  148. }
  149. void FilledTexturePlayerIndexed::setPlayerColor(PlayerColor player)
  150. {
  151. texture->playerColored(player);
  152. }
  153. FilledTexturePlayerColored::FilledTexturePlayerColored(Rect position)
  154. :CFilledTexture(ImagePath::builtin("DiBoxBck"), position)
  155. {
  156. }
  157. void FilledTexturePlayerColored::setPlayerColor(PlayerColor player)
  158. {
  159. ImagePath imagePath = ImagePath::builtin("DialogBoxBackground_" + player.toString() + ".bmp");
  160. texture = ENGINE->renderHandler().loadImage(imagePath, EImageBlitMode::COLORKEY);
  161. }
  162. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  163. frame(Frame),
  164. group(Group),
  165. flags(Flags)
  166. {
  167. pos.x += x;
  168. pos.y += y;
  169. anim = ENGINE->renderHandler().loadAnimation(name, getModeForFlags(Flags));
  170. init();
  171. }
  172. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
  173. anim(ENGINE->renderHandler().loadAnimation(name, getModeForFlags(Flags))),
  174. frame(Frame),
  175. group(Group),
  176. flags(Flags),
  177. scaledSize(targetPos.w, targetPos.h)
  178. {
  179. pos.x += targetPos.x;
  180. pos.y += targetPos.y;
  181. init();
  182. }
  183. size_t CAnimImage::size()
  184. {
  185. return anim->size(group);
  186. }
  187. bool CAnimImage::isScaled() const
  188. {
  189. return (scaledSize.x != 0);
  190. }
  191. void CAnimImage::setSizeFromImage(const IImage &img)
  192. {
  193. if (isScaled())
  194. {
  195. // At the time of writing this, IImage had no method to scale to different aspect ratio
  196. // Therefore, have to ignore the target height and preserve original aspect ratio
  197. pos.w = scaledSize.x;
  198. pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
  199. }
  200. else
  201. {
  202. pos.w = img.width();
  203. pos.h = img.height();
  204. }
  205. }
  206. void CAnimImage::init()
  207. {
  208. visible = true;
  209. auto img = anim->getImage(frame, group);
  210. if (img)
  211. setSizeFromImage(*img);
  212. }
  213. CAnimImage::~CAnimImage()
  214. {
  215. }
  216. void CAnimImage::showAll(Canvas & to)
  217. {
  218. if(!visible)
  219. return;
  220. std::vector<size_t> frames = {frame};
  221. if((flags & CShowableAnim::BASE) && frame != 0)
  222. {
  223. frames.insert(frames.begin(), 0);
  224. }
  225. for(auto targetFrame : frames)
  226. {
  227. if(auto img = anim->getImage(targetFrame, group))
  228. {
  229. if(isScaled())
  230. img->scaleTo(scaledSize, EScalingAlgorithm::BILINEAR);
  231. to.draw(img, pos.topLeft());
  232. }
  233. }
  234. }
  235. void CAnimImage::setAnimationPath(const AnimationPath & name, size_t frame)
  236. {
  237. this->frame = frame;
  238. anim = ENGINE->renderHandler().loadAnimation(name, EImageBlitMode::COLORKEY);
  239. init();
  240. }
  241. void CAnimImage::setScale(Point scale)
  242. {
  243. scaledSize = scale;
  244. }
  245. void CAnimImage::setFrame(size_t Frame, size_t Group)
  246. {
  247. if (frame == Frame && group==Group)
  248. return;
  249. if (anim->size(Group) > Frame)
  250. {
  251. frame = Frame;
  252. group = Group;
  253. if(auto img = anim->getImage(frame, group))
  254. {
  255. if (player.has_value())
  256. img->playerColored(*player);
  257. setSizeFromImage(*img);
  258. }
  259. }
  260. else
  261. logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
  262. }
  263. void CAnimImage::setPlayerColor(PlayerColor currPlayer)
  264. {
  265. player = currPlayer;
  266. anim->getImage(frame, group)->playerColored(*player);
  267. if (flags & CShowableAnim::BASE)
  268. anim->getImage(0, group)->playerColored(*player);
  269. }
  270. bool CAnimImage::isPlayerColored() const
  271. {
  272. return player.has_value();
  273. }
  274. CShowableAnim::CShowableAnim(int x, int y, const AnimationPath & name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
  275. anim(ENGINE->renderHandler().loadAnimation(name, getModeForFlags(Flags))),
  276. group(Group),
  277. frame(0),
  278. first(0),
  279. frameTimeTotal(frameTime),
  280. frameTimePassed(0),
  281. flags(Flags),
  282. xOffset(0),
  283. yOffset(0),
  284. alpha(alpha)
  285. {
  286. last = anim->size(group);
  287. auto image = anim->getImage(0, group);
  288. if (!image)
  289. throw std::runtime_error("Failed to load group " + std::to_string(group) + " of animation file " + name.getOriginalName());
  290. pos.w = image->width();
  291. pos.h = image->height();
  292. pos.x+= x;
  293. pos.y+= y;
  294. addUsedEvents(TIME);
  295. }
  296. void CShowableAnim::setAlpha(ui32 alphaValue)
  297. {
  298. alpha = std::min<ui32>(alphaValue, 255);
  299. }
  300. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  301. {
  302. size_t max = anim->size(Group);
  303. if (to < max)
  304. max = to;
  305. if (max < from || max == 0)
  306. return false;
  307. group = Group;
  308. frame = first = from;
  309. last = max;
  310. frameTimePassed = 0;
  311. return true;
  312. }
  313. bool CShowableAnim::set(size_t Group)
  314. {
  315. if (anim->size(Group)== 0)
  316. return false;
  317. if (group != Group)
  318. {
  319. first = 0;
  320. group = Group;
  321. last = anim->size(Group);
  322. }
  323. frame = 0;
  324. frameTimePassed = 0;
  325. return true;
  326. }
  327. void CShowableAnim::reset()
  328. {
  329. frame = first;
  330. if (callback)
  331. callback();
  332. }
  333. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  334. {
  335. xOffset = posX;
  336. yOffset = posY;
  337. pos.w = width;
  338. pos.h = height;
  339. }
  340. void CShowableAnim::show(Canvas & to)
  341. {
  342. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  343. blitImage(first, group, to);
  344. blitImage(frame, group, to);
  345. }
  346. void CShowableAnim::tick(uint32_t msPassed)
  347. {
  348. if ((flags & PLAY_ONCE) && frame + 1 == last)
  349. return;
  350. frameTimePassed += msPassed;
  351. if(frameTimePassed >= frameTimeTotal)
  352. {
  353. frameTimePassed -= frameTimeTotal;
  354. if ( ++frame >= last)
  355. reset();
  356. }
  357. }
  358. void CShowableAnim::showAll(Canvas & to)
  359. {
  360. if ( flags & BASE )// && frame != first)
  361. blitImage(first, group, to);
  362. blitImage(frame, group, to);
  363. }
  364. void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
  365. {
  366. Rect src( xOffset, yOffset, pos.w, pos.h);
  367. auto img = anim->getImage(frame, group);
  368. if(img)
  369. {
  370. img->setAlpha(alpha);
  371. if (getModeForFlags(flags) == EImageBlitMode::WITH_SHADOW_AND_SELECTION)
  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. }