Images.cpp 12 KB

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