Images.cpp 12 KB

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