Images.cpp 13 KB

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