Images.cpp 13 KB

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