Images.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  163. // anim(Anim),
  164. // frame(Frame),
  165. // group(Group),
  166. // flags(Flags)
  167. //{
  168. // pos.x += x;
  169. // pos.y += y;
  170. // init();
  171. //}
  172. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
  173. anim(GH.renderHandler().loadAnimation(name, EImageBlitMode::COLORKEY)),
  174. frame(Frame),
  175. group(Group),
  176. flags(Flags),
  177. scaledSize(targetPos.w, targetPos.h)
  178. {
  179. pos.x += targetPos.x;
  180. pos.y += targetPos.y;
  181. init();
  182. }
  183. size_t CAnimImage::size()
  184. {
  185. return anim->size(group);
  186. }
  187. bool CAnimImage::isScaled() const
  188. {
  189. return (scaledSize.x != 0);
  190. }
  191. void CAnimImage::setSizeFromImage(const IImage &img)
  192. {
  193. if (isScaled())
  194. {
  195. // At the time of writing this, IImage had no method to scale to different aspect ratio
  196. // Therefore, have to ignore the target height and preserve original aspect ratio
  197. pos.w = scaledSize.x;
  198. pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
  199. }
  200. else
  201. {
  202. pos.w = img.width();
  203. pos.h = img.height();
  204. }
  205. }
  206. void CAnimImage::init()
  207. {
  208. visible = true;
  209. auto img = anim->getImage(frame, group);
  210. if (img)
  211. setSizeFromImage(*img);
  212. }
  213. CAnimImage::~CAnimImage()
  214. {
  215. }
  216. void CAnimImage::showAll(Canvas & to)
  217. {
  218. if(!visible)
  219. return;
  220. std::vector<size_t> frames = {frame};
  221. if((flags & CShowableAnim::BASE) && frame != 0)
  222. {
  223. frames.insert(frames.begin(), 0);
  224. }
  225. for(auto targetFrame : frames)
  226. {
  227. if(auto img = anim->getImage(targetFrame, group))
  228. {
  229. if(isScaled())
  230. img->scaleFast(scaledSize);
  231. to.draw(img, pos.topLeft());
  232. }
  233. }
  234. }
  235. void CAnimImage::setAnimationPath(const AnimationPath & name, size_t frame)
  236. {
  237. this->frame = frame;
  238. anim = GH.renderHandler().loadAnimation(name, EImageBlitMode::COLORKEY);
  239. init();
  240. }
  241. void CAnimImage::setScale(Point scale)
  242. {
  243. scaledSize = scale;
  244. }
  245. void CAnimImage::setFrame(size_t Frame, size_t Group)
  246. {
  247. if (frame == Frame && group==Group)
  248. return;
  249. if (anim->size(Group) > Frame)
  250. {
  251. frame = Frame;
  252. group = Group;
  253. if(auto img = anim->getImage(frame, group))
  254. {
  255. if (player.has_value())
  256. img->playerColored(*player);
  257. setSizeFromImage(*img);
  258. }
  259. }
  260. else
  261. logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
  262. }
  263. void CAnimImage::setPlayerColor(PlayerColor currPlayer)
  264. {
  265. player = currPlayer;
  266. anim->getImage(frame, group)->playerColored(*player);
  267. if (flags & CShowableAnim::BASE)
  268. anim->getImage(0, group)->playerColored(*player);
  269. }
  270. bool CAnimImage::isPlayerColored() const
  271. {
  272. return player.has_value();
  273. }
  274. CShowableAnim::CShowableAnim(int x, int y, const AnimationPath & name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
  275. anim(GH.renderHandler().loadAnimation(name, (Flags & PALETTE_ALPHA) ? EImageBlitMode::ALPHA : EImageBlitMode::COLORKEY)),
  276. group(Group),
  277. frame(0),
  278. first(0),
  279. frameTimeTotal(frameTime),
  280. frameTimePassed(0),
  281. flags(Flags),
  282. xOffset(0),
  283. yOffset(0),
  284. alpha(alpha)
  285. {
  286. last = anim->size(group);
  287. auto image = anim->getImage(0, group);
  288. if (!image)
  289. throw std::runtime_error("Failed to load group " + std::to_string(group) + " of animation file " + name.getOriginalName());
  290. pos.w = image->width();
  291. pos.h = image->height();
  292. pos.x+= x;
  293. pos.y+= y;
  294. addUsedEvents(TIME);
  295. }
  296. void CShowableAnim::setAlpha(ui32 alphaValue)
  297. {
  298. alpha = std::min<ui32>(alphaValue, 255);
  299. }
  300. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  301. {
  302. size_t max = anim->size(Group);
  303. if (to < max)
  304. max = to;
  305. if (max < from || max == 0)
  306. return false;
  307. group = Group;
  308. frame = first = from;
  309. last = max;
  310. frameTimePassed = 0;
  311. return true;
  312. }
  313. bool CShowableAnim::set(size_t Group)
  314. {
  315. if (anim->size(Group)== 0)
  316. return false;
  317. if (group != Group)
  318. {
  319. first = 0;
  320. group = Group;
  321. last = anim->size(Group);
  322. }
  323. frame = 0;
  324. frameTimePassed = 0;
  325. return true;
  326. }
  327. void CShowableAnim::reset()
  328. {
  329. frame = first;
  330. if (callback)
  331. callback();
  332. }
  333. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  334. {
  335. xOffset = posX;
  336. yOffset = posY;
  337. pos.w = width;
  338. pos.h = height;
  339. }
  340. void CShowableAnim::show(Canvas & to)
  341. {
  342. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  343. blitImage(first, group, to);
  344. blitImage(frame, group, to);
  345. }
  346. void CShowableAnim::tick(uint32_t msPassed)
  347. {
  348. if ((flags & PLAY_ONCE) && frame + 1 == last)
  349. return;
  350. frameTimePassed += msPassed;
  351. if(frameTimePassed >= frameTimeTotal)
  352. {
  353. frameTimePassed -= frameTimeTotal;
  354. if ( ++frame >= last)
  355. reset();
  356. }
  357. }
  358. void CShowableAnim::showAll(Canvas & to)
  359. {
  360. if ( flags & BASE )// && frame != first)
  361. blitImage(first, group, to);
  362. blitImage(frame, group, to);
  363. }
  364. void CShowableAnim::blitImage(size_t frame, size_t group, Canvas & to)
  365. {
  366. Rect src( xOffset, yOffset, pos.w, pos.h);
  367. auto img = anim->getImage(frame, group);
  368. if(img)
  369. {
  370. img->setAlpha(alpha);
  371. to.draw(img, pos.topLeft(), src);
  372. }
  373. }
  374. void CShowableAnim::rotate(bool on, bool vertical)
  375. {
  376. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  377. if (on)
  378. flags |= flag;
  379. else
  380. flags &= ~flag;
  381. }
  382. void CShowableAnim::setDuration(int durationMs)
  383. {
  384. frameTimeTotal = durationMs/(last - first);
  385. }
  386. CCreatureAnim::CCreatureAnim(int x, int y, const AnimationPath & name, ui8 flags, ECreatureAnimType type):
  387. CShowableAnim(x, y, name, flags | PALETTE_ALPHA, 100, size_t(type)) // H3 uses 100 ms per frame, irregardless of battle speed settings
  388. {
  389. xOffset = 0;
  390. yOffset = 0;
  391. }
  392. void CCreatureAnim::loopPreview(bool warMachine)
  393. {
  394. std::vector<ECreatureAnimType> available;
  395. static const ECreatureAnimType creaPreviewList[] = {
  396. ECreatureAnimType::HOLDING,
  397. ECreatureAnimType::HITTED,
  398. ECreatureAnimType::DEFENCE,
  399. ECreatureAnimType::ATTACK_FRONT,
  400. ECreatureAnimType::SPECIAL_FRONT
  401. };
  402. static const ECreatureAnimType machPreviewList[] = {
  403. ECreatureAnimType::HOLDING,
  404. ECreatureAnimType::MOVING,
  405. ECreatureAnimType::SHOOT_UP,
  406. ECreatureAnimType::SHOOT_FRONT,
  407. ECreatureAnimType::SHOOT_DOWN
  408. };
  409. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  410. for (auto & elem : previewList)
  411. if (anim->size(size_t(elem)))
  412. available.push_back(elem);
  413. size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
  414. if (rnd >= available.size())
  415. {
  416. ECreatureAnimType type;
  417. if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
  418. type = ECreatureAnimType::HOLDING;
  419. else
  420. type = ECreatureAnimType::MOVING;
  421. //display this anim for ~1 second (time is random, but it looks good)
  422. for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
  423. addLast(type);
  424. }
  425. else
  426. addLast(available[rnd]);
  427. }
  428. void CCreatureAnim::addLast(ECreatureAnimType newType)
  429. {
  430. auto currType = ECreatureAnimType(group);
  431. if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
  432. {
  433. queue.push( ECreatureAnimType::MOVE_START );
  434. }
  435. else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
  436. {
  437. queue.push( ECreatureAnimType::MOVE_END );
  438. }
  439. if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
  440. queue.push(newType);
  441. queue.push(newType);
  442. }
  443. void CCreatureAnim::reset()
  444. {
  445. //if we are in the middle of rotation - set flag
  446. if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
  447. rotate(true);
  448. if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
  449. rotate(false);
  450. while (!queue.empty())
  451. {
  452. ECreatureAnimType at = queue.front();
  453. queue.pop();
  454. if (set(size_t(at)))
  455. return;
  456. }
  457. if (callback)
  458. callback();
  459. while (!queue.empty())
  460. {
  461. ECreatureAnimType at = queue.front();
  462. queue.pop();
  463. if (set(size_t(at)))
  464. return;
  465. }
  466. set(size_t(ECreatureAnimType::HOLDING));
  467. }
  468. void CCreatureAnim::startPreview(bool warMachine)
  469. {
  470. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  471. }
  472. void CCreatureAnim::clearAndSet(ECreatureAnimType type)
  473. {
  474. while (!queue.empty())
  475. queue.pop();
  476. set(size_t(type));
  477. }