Images.cpp 12 KB

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