Images.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. , visible(true)
  32. , needRefresh(false)
  33. {
  34. pos += position;
  35. pos.w = bg->width();
  36. pos.h = bg->height();
  37. }
  38. CPicture::CPicture( const ImagePath &bmpname, int x, int y )
  39. : CPicture(bmpname, Point(x,y))
  40. {}
  41. CPicture::CPicture( const ImagePath & bmpname )
  42. : CPicture(bmpname, Point(0,0))
  43. {}
  44. CPicture::CPicture( const ImagePath & bmpname, const Point & position )
  45. : bg(GH.renderHandler().loadImage(bmpname))
  46. , visible(true)
  47. , needRefresh(false)
  48. {
  49. pos.x += position.x;
  50. pos.y += position.y;
  51. assert(bg);
  52. if(bg)
  53. {
  54. pos.w = bg->width();
  55. pos.h = bg->height();
  56. }
  57. else
  58. {
  59. pos.w = pos.h = 0;
  60. }
  61. }
  62. CPicture::CPicture(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
  63. : CPicture(image, Point(x,y))
  64. {
  65. srcRect = SrcRect;
  66. pos.w = srcRect->w;
  67. pos.h = srcRect->h;
  68. }
  69. void CPicture::show(Canvas & to)
  70. {
  71. if (visible && needRefresh)
  72. showAll(to);
  73. }
  74. void CPicture::showAll(Canvas & to)
  75. {
  76. if(bg && visible)
  77. {
  78. if (srcRect.has_value())
  79. to.draw(bg, pos.topLeft(), *srcRect);
  80. else
  81. to.draw(bg, pos.topLeft());
  82. }
  83. }
  84. void CPicture::setAlpha(int value)
  85. {
  86. bg->setAlpha(value);
  87. }
  88. void CPicture::scaleTo(Point size)
  89. {
  90. bg = bg->scaleFast(size);
  91. pos.w = bg->width();
  92. pos.h = bg->height();
  93. }
  94. void CPicture::colorize(PlayerColor player)
  95. {
  96. bg->playerColored(player);
  97. }
  98. CFilledTexture::CFilledTexture(const ImagePath & imageName, Rect position):
  99. CIntObject(0, position.topLeft()),
  100. texture(GH.renderHandler().loadImage(imageName))
  101. {
  102. pos.w = position.w;
  103. pos.h = position.h;
  104. imageArea = Rect(Point(), texture->dimensions());
  105. }
  106. CFilledTexture::CFilledTexture(std::shared_ptr<IImage> image, Rect position, Rect imageArea)
  107. : CIntObject(0, position.topLeft())
  108. , texture(image)
  109. , imageArea(imageArea)
  110. {
  111. pos.w = position.w;
  112. pos.h = position.h;
  113. }
  114. void CFilledTexture::showAll(Canvas & to)
  115. {
  116. CSDL_Ext::CClipRectGuard guard(to.getInternalSurface(), pos);
  117. for (int y=pos.top(); y < pos.bottom(); y+= imageArea.h)
  118. {
  119. for (int x=pos.left(); x < pos.right(); x+= imageArea.w)
  120. to.draw(texture, Point(x,y), imageArea);
  121. }
  122. }
  123. FilledTexturePlayerColored::FilledTexturePlayerColored(const ImagePath & imageName, Rect position)
  124. : CFilledTexture(imageName, position)
  125. {
  126. }
  127. void FilledTexturePlayerColored::playerColored(PlayerColor player)
  128. {
  129. // Color transform to make color of brown DIBOX.PCX texture match color of specified player
  130. std::array<ColorFilter, PlayerColor::PLAYER_LIMIT_I> filters = {
  131. ColorFilter::genRangeShifter( 0.25, 0, 0, 1.25, 0.00, 0.00 ), // red
  132. ColorFilter::genRangeShifter( 0, 0, 0, 0.45, 1.20, 4.50 ), // blue
  133. ColorFilter::genRangeShifter( 0.40, 0.27, 0.23, 1.10, 1.20, 1.15 ), // tan
  134. ColorFilter::genRangeShifter( -0.27, 0.10, -0.27, 0.70, 1.70, 0.70 ), // green
  135. ColorFilter::genRangeShifter( 0.47, 0.17, -0.27, 1.60, 1.20, 0.70 ), // orange
  136. ColorFilter::genRangeShifter( 0.12, -0.1, 0.25, 1.15, 1.20, 2.20 ), // purple
  137. ColorFilter::genRangeShifter( -0.13, 0.23, 0.23, 0.90, 1.20, 2.20 ), // teal
  138. ColorFilter::genRangeShifter( 0.44, 0.15, 0.25, 1.00, 1.00, 1.75 ) // pink
  139. };
  140. assert(player.isValidPlayer());
  141. if (!player.isValidPlayer())
  142. {
  143. logGlobal->error("Unable to colorize to invalid player color %d!", static_cast<int>(player.getNum()));
  144. return;
  145. }
  146. texture->adjustPalette(filters[player.getNum()], 0);
  147. }
  148. CAnimImage::CAnimImage(const AnimationPath & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  149. frame(Frame),
  150. group(Group),
  151. flags(Flags)
  152. {
  153. pos.x += x;
  154. pos.y += y;
  155. anim = GH.renderHandler().loadAnimation(name);
  156. init();
  157. }
  158. CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  159. anim(Anim),
  160. frame(Frame),
  161. group(Group),
  162. flags(Flags)
  163. {
  164. pos.x += x;
  165. pos.y += y;
  166. init();
  167. }
  168. CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
  169. anim(Anim),
  170. frame(Frame),
  171. group(Group),
  172. flags(Flags),
  173. scaledSize(targetPos.w, targetPos.h)
  174. {
  175. pos.x += targetPos.x;
  176. pos.y += targetPos.y;
  177. init();
  178. }
  179. size_t CAnimImage::size()
  180. {
  181. return anim->size(group);
  182. }
  183. bool CAnimImage::isScaled() const
  184. {
  185. return (scaledSize.x != 0);
  186. }
  187. void CAnimImage::setSizeFromImage(const IImage &img)
  188. {
  189. if (isScaled())
  190. {
  191. // At the time of writing this, IImage had no method to scale to different aspect ratio
  192. // Therefore, have to ignore the target height and preserve original aspect ratio
  193. pos.w = scaledSize.x;
  194. pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
  195. }
  196. else
  197. {
  198. pos.w = img.width();
  199. pos.h = img.height();
  200. }
  201. }
  202. void CAnimImage::init()
  203. {
  204. visible = true;
  205. anim->load(frame, group);
  206. if (flags & CShowableAnim::BASE)
  207. anim->load(0,group);
  208. auto img = anim->getImage(frame, group);
  209. if (img)
  210. setSizeFromImage(*img);
  211. }
  212. CAnimImage::~CAnimImage()
  213. {
  214. }
  215. void CAnimImage::showAll(Canvas & to)
  216. {
  217. if(!visible)
  218. return;
  219. std::vector<size_t> frames = {frame};
  220. if((flags & CShowableAnim::BASE) && frame != 0)
  221. {
  222. frames.insert(frames.begin(), 0);
  223. }
  224. for(auto targetFrame : frames)
  225. {
  226. if(auto img = anim->getImage(targetFrame, group))
  227. {
  228. if(isScaled())
  229. {
  230. auto scaled = img->scaleFast(scaledSize);
  231. to.draw(scaled, pos.topLeft());
  232. }
  233. else
  234. to.draw(img, pos.topLeft());
  235. }
  236. }
  237. }
  238. void CAnimImage::setFrame(size_t Frame, size_t Group)
  239. {
  240. if (frame == Frame && group==Group)
  241. return;
  242. if (anim->size(Group) > Frame)
  243. {
  244. anim->load(Frame, Group);
  245. frame = Frame;
  246. group = Group;
  247. if(auto img = anim->getImage(frame, group))
  248. {
  249. if (player.has_value())
  250. img->playerColored(*player);
  251. setSizeFromImage(*img);
  252. }
  253. }
  254. else
  255. logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
  256. }
  257. void CAnimImage::playerColored(PlayerColor currPlayer)
  258. {
  259. player = currPlayer;
  260. anim->getImage(frame, group)->playerColored(*player);
  261. if (flags & CShowableAnim::BASE)
  262. anim->getImage(0, group)->playerColored(*player);
  263. }
  264. bool CAnimImage::isPlayerColored() const
  265. {
  266. return player.has_value();
  267. }
  268. CShowableAnim::CShowableAnim(int x, int y, const AnimationPath & name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
  269. anim(GH.renderHandler().loadAnimation(name)),
  270. group(Group),
  271. frame(0),
  272. first(0),
  273. frameTimeTotal(frameTime),
  274. frameTimePassed(0),
  275. flags(Flags),
  276. xOffset(0),
  277. yOffset(0),
  278. alpha(alpha)
  279. {
  280. anim->loadGroup(group);
  281. last = anim->size(group);
  282. pos.w = anim->getImage(0, group)->width();
  283. pos.h = anim->getImage(0, group)->height();
  284. pos.x+= x;
  285. pos.y+= y;
  286. addUsedEvents(TIME);
  287. }
  288. CShowableAnim::~CShowableAnim()
  289. {
  290. anim->unloadGroup(group);
  291. }
  292. void CShowableAnim::setAlpha(ui32 alphaValue)
  293. {
  294. alpha = std::min<ui32>(alphaValue, 255);
  295. }
  296. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  297. {
  298. size_t max = anim->size(Group);
  299. if (to < max)
  300. max = to;
  301. if (max < from || max == 0)
  302. return false;
  303. anim->unloadGroup(group);
  304. anim->loadGroup(Group);
  305. group = Group;
  306. frame = first = from;
  307. last = max;
  308. frameTimePassed = 0;
  309. return true;
  310. }
  311. bool CShowableAnim::set(size_t Group)
  312. {
  313. if (anim->size(Group)== 0)
  314. return false;
  315. if (group != Group)
  316. {
  317. anim->unloadGroup(group);
  318. anim->loadGroup(Group);
  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, 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. }