Images.cpp 11 KB

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