2
0

Images.cpp 13 KB

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