Images.cpp 12 KB

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