Images.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  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. {
  302. pos.w = image->width();
  303. pos.h = image->height();
  304. }
  305. else
  306. {
  307. logGlobal->error("Failed to load group %d of animation file %s", group, name.getOriginalName());
  308. }
  309. pos.x+= x;
  310. pos.y+= y;
  311. addUsedEvents(TIME);
  312. }
  313. void CShowableAnim::setAlpha(ui32 alphaValue)
  314. {
  315. alpha = std::min<ui32>(alphaValue, 255);
  316. }
  317. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  318. {
  319. size_t max = anim->size(Group);
  320. if (to < max)
  321. max = to;
  322. if (max < from || max == 0)
  323. return false;
  324. group = Group;
  325. frame = first = from;
  326. last = max;
  327. frameTimePassed = 0;
  328. return true;
  329. }
  330. bool CShowableAnim::set(size_t Group)
  331. {
  332. if (anim->size(Group)== 0)
  333. return false;
  334. if (group != Group)
  335. {
  336. first = 0;
  337. group = Group;
  338. last = anim->size(Group);
  339. }
  340. frame = 0;
  341. frameTimePassed = 0;
  342. return true;
  343. }
  344. void CShowableAnim::reset()
  345. {
  346. frame = first;
  347. if (callback)
  348. callback();
  349. }
  350. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  351. {
  352. xOffset = posX;
  353. yOffset = posY;
  354. pos.w = width;
  355. pos.h = height;
  356. }
  357. void CShowableAnim::show(Canvas & to)
  358. {
  359. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  360. blitImage(first, group, to);
  361. blitImage(frame, group, to);
  362. }
  363. void CShowableAnim::tick(uint32_t msPassed)
  364. {
  365. if ((flags & PLAY_ONCE) && frame + 1 == last)
  366. return;
  367. frameTimePassed += msPassed;
  368. if(frameTimePassed >= frameTimeTotal)
  369. {
  370. frameTimePassed -= frameTimeTotal;
  371. if ( ++frame >= last)
  372. reset();
  373. }
  374. }
  375. void CShowableAnim::showAll(Canvas & to)
  376. {
  377. if ( flags & BASE )// && frame != first)
  378. blitImage(first, group, to);
  379. blitImage(frame, group, to);
  380. }
  381. void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
  382. {
  383. Rect src( xOffset, yOffset, pos.w, pos.h);
  384. auto img = anim->getImage(frame, group);
  385. if(img)
  386. {
  387. img->setAlpha(alpha);
  388. if (getModeForFlags(flags) == EImageBlitMode::WITH_SHADOW_AND_SELECTION)
  389. img->setOverlayColor(Colors::TRANSPARENCY);
  390. to.draw(img, pos.topLeft(), src);
  391. }
  392. }
  393. void CShowableAnim::rotate(bool on, bool vertical)
  394. {
  395. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  396. if (on)
  397. flags |= flag;
  398. else
  399. flags &= ~flag;
  400. }
  401. void CShowableAnim::setDuration(int durationMs)
  402. {
  403. frameTimeTotal = durationMs/(last - first);
  404. }
  405. CCreatureAnim::CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags, ECreatureAnimType type):
  406. CShowableAnim(x, y, name, flags | CREATURE_MODE, 100, size_t(type)) // H3 uses 100 ms per frame, irregardless of battle speed settings
  407. {
  408. xOffset = 0;
  409. yOffset = 0;
  410. }
  411. void CCreatureAnim::loopPreview(bool warMachine)
  412. {
  413. std::vector<ECreatureAnimType> available;
  414. static const ECreatureAnimType creaPreviewList[] = {
  415. ECreatureAnimType::HOLDING,
  416. ECreatureAnimType::HITTED,
  417. ECreatureAnimType::DEFENCE,
  418. ECreatureAnimType::ATTACK_FRONT,
  419. ECreatureAnimType::SPECIAL_FRONT
  420. };
  421. static const ECreatureAnimType machPreviewList[] = {
  422. ECreatureAnimType::HOLDING,
  423. ECreatureAnimType::MOVING,
  424. ECreatureAnimType::SHOOT_UP,
  425. ECreatureAnimType::SHOOT_FRONT,
  426. ECreatureAnimType::SHOOT_DOWN
  427. };
  428. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  429. for (auto & elem : previewList)
  430. if (anim->size(size_t(elem)))
  431. available.push_back(elem);
  432. size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
  433. if (rnd >= available.size())
  434. {
  435. ECreatureAnimType type;
  436. if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
  437. type = ECreatureAnimType::HOLDING;
  438. else
  439. type = ECreatureAnimType::MOVING;
  440. //display this anim for ~1 second (time is random, but it looks good)
  441. for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
  442. addLast(type);
  443. }
  444. else
  445. addLast(available[rnd]);
  446. }
  447. void CCreatureAnim::addLast(ECreatureAnimType newType)
  448. {
  449. auto currType = ECreatureAnimType(group);
  450. if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
  451. {
  452. queue.push( ECreatureAnimType::MOVE_START );
  453. }
  454. else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
  455. {
  456. queue.push( ECreatureAnimType::MOVE_END );
  457. }
  458. if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
  459. queue.push(newType);
  460. queue.push(newType);
  461. }
  462. void CCreatureAnim::reset()
  463. {
  464. //if we are in the middle of rotation - set flag
  465. if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
  466. rotate(true);
  467. if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
  468. rotate(false);
  469. while (!queue.empty())
  470. {
  471. ECreatureAnimType at = queue.front();
  472. queue.pop();
  473. if (set(size_t(at)))
  474. return;
  475. }
  476. if (callback)
  477. callback();
  478. while (!queue.empty())
  479. {
  480. ECreatureAnimType at = queue.front();
  481. queue.pop();
  482. if (set(size_t(at)))
  483. return;
  484. }
  485. set(size_t(ECreatureAnimType::HOLDING));
  486. }
  487. void CCreatureAnim::startPreview(bool warMachine)
  488. {
  489. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  490. }
  491. void CCreatureAnim::clearAndSet(ECreatureAnimType type)
  492. {
  493. while (!queue.empty())
  494. queue.pop();
  495. set(size_t(type));
  496. }