Images.cpp 13 KB

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