Images.cpp 12 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 "../CMusicHandler.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 = GH.renderHandler().loadAnimation(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. pos.w = anim->getImage(0, group)->width();
  291. pos.h = anim->getImage(0, group)->height();
  292. pos.x+= x;
  293. pos.y+= y;
  294. addUsedEvents(TIME);
  295. }
  296. CShowableAnim::~CShowableAnim()
  297. {
  298. anim->unloadGroup(group);
  299. }
  300. void CShowableAnim::setAlpha(ui32 alphaValue)
  301. {
  302. alpha = std::min<ui32>(alphaValue, 255);
  303. }
  304. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  305. {
  306. size_t max = anim->size(Group);
  307. if (to < max)
  308. max = to;
  309. if (max < from || max == 0)
  310. return false;
  311. anim->unloadGroup(group);
  312. anim->loadGroup(Group);
  313. group = Group;
  314. frame = first = from;
  315. last = max;
  316. frameTimePassed = 0;
  317. return true;
  318. }
  319. bool CShowableAnim::set(size_t Group)
  320. {
  321. if (anim->size(Group)== 0)
  322. return false;
  323. if (group != Group)
  324. {
  325. anim->unloadGroup(group);
  326. anim->loadGroup(Group);
  327. first = 0;
  328. group = Group;
  329. last = anim->size(Group);
  330. }
  331. frame = 0;
  332. frameTimePassed = 0;
  333. return true;
  334. }
  335. void CShowableAnim::reset()
  336. {
  337. frame = first;
  338. if (callback)
  339. callback();
  340. }
  341. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  342. {
  343. xOffset = posX;
  344. yOffset = posY;
  345. pos.w = width;
  346. pos.h = height;
  347. }
  348. void CShowableAnim::show(Canvas & to)
  349. {
  350. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  351. blitImage(first, group, to);
  352. blitImage(frame, group, to);
  353. }
  354. void CShowableAnim::tick(uint32_t msPassed)
  355. {
  356. if ((flags & PLAY_ONCE) && frame + 1 == last)
  357. return;
  358. frameTimePassed += msPassed;
  359. if(frameTimePassed >= frameTimeTotal)
  360. {
  361. frameTimePassed -= frameTimeTotal;
  362. if ( ++frame >= last)
  363. reset();
  364. }
  365. }
  366. void CShowableAnim::showAll(Canvas & to)
  367. {
  368. if ( flags & BASE )// && frame != first)
  369. blitImage(first, group, to);
  370. blitImage(frame, group, to);
  371. }
  372. void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
  373. {
  374. Rect src( xOffset, yOffset, pos.w, pos.h);
  375. auto img = anim->getImage(frame, group);
  376. if(img)
  377. {
  378. img->setAlpha(alpha);
  379. to.draw(img, pos.topLeft(), src);
  380. }
  381. }
  382. void CShowableAnim::rotate(bool on, bool vertical)
  383. {
  384. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  385. if (on)
  386. flags |= flag;
  387. else
  388. flags &= ~flag;
  389. }
  390. void CShowableAnim::setDuration(int durationMs)
  391. {
  392. frameTimeTotal = durationMs/(last - first);
  393. }
  394. CCreatureAnim::CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags, ECreatureAnimType type):
  395. CShowableAnim(x, y, name, flags, 100, size_t(type)) // H3 uses 100 ms per frame, irregardless of battle speed settings
  396. {
  397. xOffset = 0;
  398. yOffset = 0;
  399. }
  400. void CCreatureAnim::loopPreview(bool warMachine)
  401. {
  402. std::vector<ECreatureAnimType> available;
  403. static const ECreatureAnimType creaPreviewList[] = {
  404. ECreatureAnimType::HOLDING,
  405. ECreatureAnimType::HITTED,
  406. ECreatureAnimType::DEFENCE,
  407. ECreatureAnimType::ATTACK_FRONT,
  408. ECreatureAnimType::SPECIAL_FRONT
  409. };
  410. static const ECreatureAnimType machPreviewList[] = {
  411. ECreatureAnimType::HOLDING,
  412. ECreatureAnimType::MOVING,
  413. ECreatureAnimType::SHOOT_UP,
  414. ECreatureAnimType::SHOOT_FRONT,
  415. ECreatureAnimType::SHOOT_DOWN
  416. };
  417. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  418. for (auto & elem : previewList)
  419. if (anim->size(size_t(elem)))
  420. available.push_back(elem);
  421. size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
  422. if (rnd >= available.size())
  423. {
  424. ECreatureAnimType type;
  425. if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
  426. type = ECreatureAnimType::HOLDING;
  427. else
  428. type = ECreatureAnimType::MOVING;
  429. //display this anim for ~1 second (time is random, but it looks good)
  430. for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
  431. addLast(type);
  432. }
  433. else
  434. addLast(available[rnd]);
  435. }
  436. void CCreatureAnim::addLast(ECreatureAnimType newType)
  437. {
  438. auto currType = ECreatureAnimType(group);
  439. if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
  440. {
  441. queue.push( ECreatureAnimType::MOVE_START );
  442. }
  443. else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
  444. {
  445. queue.push( ECreatureAnimType::MOVE_END );
  446. }
  447. if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
  448. queue.push(newType);
  449. queue.push(newType);
  450. }
  451. void CCreatureAnim::reset()
  452. {
  453. //if we are in the middle of rotation - set flag
  454. if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
  455. rotate(true);
  456. if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
  457. rotate(false);
  458. while (!queue.empty())
  459. {
  460. ECreatureAnimType at = queue.front();
  461. queue.pop();
  462. if (set(size_t(at)))
  463. return;
  464. }
  465. if (callback)
  466. callback();
  467. while (!queue.empty())
  468. {
  469. ECreatureAnimType at = queue.front();
  470. queue.pop();
  471. if (set(size_t(at)))
  472. return;
  473. }
  474. set(size_t(ECreatureAnimType::HOLDING));
  475. }
  476. void CCreatureAnim::startPreview(bool warMachine)
  477. {
  478. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  479. }
  480. void CCreatureAnim::clearAndSet(ECreatureAnimType type)
  481. {
  482. while (!queue.empty())
  483. queue.pop();
  484. set(size_t(type));
  485. }