Images.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  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 "../battle/BattleInterface.h"
  18. #include "../battle/BattleInterfaceClasses.h"
  19. #include "../CGameInfo.h"
  20. #include "../CPlayerInterface.h"
  21. #include "../CMusicHandler.h"
  22. #include "../../CCallback.h"
  23. #include "../../lib/CConfigHandler.h"
  24. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  25. #include "../../lib/CRandomGenerator.h"
  26. CPicture::CPicture(std::shared_ptr<IImage> image, const Point & position)
  27. : bg(image)
  28. , visible(true)
  29. , needRefresh(false)
  30. {
  31. pos += position;
  32. pos.w = bg->width();
  33. pos.h = bg->height();
  34. }
  35. CPicture::CPicture( const std::string &bmpname, int x, int y )
  36. : CPicture(bmpname, Point(x,y))
  37. {}
  38. CPicture::CPicture( const std::string &bmpname )
  39. : CPicture(bmpname, Point(0,0))
  40. {}
  41. CPicture::CPicture( const std::string &bmpname, const Point & position )
  42. : bg(IImage::createFromFile(bmpname))
  43. , visible(true)
  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(std::shared_ptr<IImage> image, const Rect &SrcRect, int x, int y)
  60. : CPicture(image, Point(x,y))
  61. {
  62. srcRect = SrcRect;
  63. pos.w = srcRect->w;
  64. pos.h = srcRect->h;
  65. }
  66. void CPicture::show(SDL_Surface * to)
  67. {
  68. if (visible && needRefresh)
  69. showAll(to);
  70. }
  71. void CPicture::showAll(SDL_Surface * to)
  72. {
  73. if(bg && visible)
  74. bg->draw(to, pos.x, pos.y, srcRect.get_ptr());
  75. }
  76. void CPicture::setAlpha(int value)
  77. {
  78. bg->setAlpha(value);
  79. }
  80. void CPicture::scaleTo(Point size)
  81. {
  82. bg = bg->scaleFast(size);
  83. pos.w = bg->width();
  84. pos.h = bg->height();
  85. }
  86. void CPicture::colorize(PlayerColor player)
  87. {
  88. bg->playerColored(player);
  89. }
  90. CFilledTexture::CFilledTexture(std::string imageName, Rect position):
  91. CIntObject(0, position.topLeft()),
  92. texture(IImage::createFromFile(imageName))
  93. {
  94. pos.w = position.w;
  95. pos.h = position.h;
  96. }
  97. void CFilledTexture::showAll(SDL_Surface *to)
  98. {
  99. CSDL_Ext::CClipRectGuard guard(to, pos);
  100. for (int y=pos.top(); y < pos.bottom(); y+= texture->height())
  101. {
  102. for (int x=pos.left(); x < pos.right(); x+=texture->width())
  103. texture->draw(to, x, y);
  104. }
  105. }
  106. CAnimImage::CAnimImage(const std::string & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  107. frame(Frame),
  108. group(Group),
  109. player(-1),
  110. flags(Flags)
  111. {
  112. pos.x += x;
  113. pos.y += y;
  114. anim = std::make_shared<CAnimation>(name);
  115. init();
  116. }
  117. CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  118. anim(Anim),
  119. frame(Frame),
  120. group(Group),
  121. player(-1),
  122. flags(Flags)
  123. {
  124. pos.x += x;
  125. pos.y += y;
  126. init();
  127. }
  128. CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
  129. anim(Anim),
  130. frame(Frame),
  131. group(Group),
  132. player(-1),
  133. flags(Flags),
  134. scaledSize(targetPos.w, targetPos.h)
  135. {
  136. pos.x += targetPos.x;
  137. pos.y += targetPos.y;
  138. init();
  139. }
  140. size_t CAnimImage::size()
  141. {
  142. return anim->size(group);
  143. }
  144. bool CAnimImage::isScaled() const
  145. {
  146. return (scaledSize.x != 0);
  147. }
  148. void CAnimImage::setSizeFromImage(const IImage &img)
  149. {
  150. if (isScaled())
  151. {
  152. // At the time of writing this, IImage had no method to scale to different aspect ratio
  153. // Therefore, have to ignore the target height and preserve original aspect ratio
  154. pos.w = scaledSize.x;
  155. pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
  156. }
  157. else
  158. {
  159. pos.w = img.width();
  160. pos.h = img.height();
  161. }
  162. }
  163. void CAnimImage::init()
  164. {
  165. visible = true;
  166. anim->load(frame, group);
  167. if (flags & CShowableAnim::BASE)
  168. anim->load(0,group);
  169. auto img = anim->getImage(frame, group);
  170. if (img)
  171. setSizeFromImage(*img);
  172. }
  173. CAnimImage::~CAnimImage()
  174. {
  175. }
  176. void CAnimImage::showAll(SDL_Surface * to)
  177. {
  178. if(!visible)
  179. return;
  180. std::vector<size_t> frames = {frame};
  181. if((flags & CShowableAnim::BASE) && frame != 0)
  182. {
  183. frames.insert(frames.begin(), 0);
  184. }
  185. for(auto targetFrame : frames)
  186. {
  187. if(auto img = anim->getImage(targetFrame, group))
  188. {
  189. if(isScaled())
  190. {
  191. auto scaled = img->scaleFast(scaledSize);
  192. scaled->draw(to, pos.x, pos.y);
  193. }
  194. else
  195. img->draw(to, pos.x, pos.y);
  196. }
  197. }
  198. }
  199. void CAnimImage::setFrame(size_t Frame, size_t Group)
  200. {
  201. if (frame == Frame && group==Group)
  202. return;
  203. if (anim->size(Group) > Frame)
  204. {
  205. anim->load(Frame, Group);
  206. frame = Frame;
  207. group = Group;
  208. if(auto img = anim->getImage(frame, group))
  209. {
  210. if (flags & CShowableAnim::PLAYER_COLORED)
  211. img->playerColored(player);
  212. setSizeFromImage(*img);
  213. }
  214. }
  215. else
  216. logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
  217. }
  218. void CAnimImage::playerColored(PlayerColor currPlayer)
  219. {
  220. player = currPlayer;
  221. flags |= CShowableAnim::PLAYER_COLORED;
  222. anim->getImage(frame, group)->playerColored(player);
  223. if (flags & CShowableAnim::BASE)
  224. anim->getImage(0, group)->playerColored(player);
  225. }
  226. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 frameTime, size_t Group, uint8_t alpha):
  227. anim(std::make_shared<CAnimation>(name)),
  228. group(Group),
  229. frame(0),
  230. first(0),
  231. frameTimeTotal(frameTime),
  232. frameTimePassed(0),
  233. flags(Flags),
  234. xOffset(0),
  235. yOffset(0),
  236. alpha(alpha)
  237. {
  238. anim->loadGroup(group);
  239. last = anim->size(group);
  240. pos.w = anim->getImage(0, group)->width();
  241. pos.h = anim->getImage(0, group)->height();
  242. pos.x+= x;
  243. pos.y+= y;
  244. }
  245. CShowableAnim::~CShowableAnim()
  246. {
  247. anim->unloadGroup(group);
  248. }
  249. void CShowableAnim::setAlpha(ui32 alphaValue)
  250. {
  251. alpha = std::min<ui32>(alphaValue, 255);
  252. }
  253. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  254. {
  255. size_t max = anim->size(Group);
  256. if (to < max)
  257. max = to;
  258. if (max < from || max == 0)
  259. return false;
  260. anim->unloadGroup(group);
  261. anim->loadGroup(Group);
  262. group = Group;
  263. frame = first = from;
  264. last = max;
  265. frameTimePassed = 0;
  266. return true;
  267. }
  268. bool CShowableAnim::set(size_t Group)
  269. {
  270. if (anim->size(Group)== 0)
  271. return false;
  272. if (group != Group)
  273. {
  274. anim->unloadGroup(group);
  275. anim->loadGroup(Group);
  276. first = 0;
  277. group = Group;
  278. last = anim->size(Group);
  279. }
  280. frame = 0;
  281. frameTimePassed = 0;
  282. return true;
  283. }
  284. void CShowableAnim::reset()
  285. {
  286. frame = first;
  287. if (callback)
  288. callback();
  289. }
  290. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  291. {
  292. xOffset = posX;
  293. yOffset = posY;
  294. pos.w = width;
  295. pos.h = height;
  296. }
  297. void CShowableAnim::show(SDL_Surface * to)
  298. {
  299. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  300. blitImage(first, group, to);
  301. blitImage(frame, group, to);
  302. if ((flags & PLAY_ONCE) && frame + 1 == last)
  303. return;
  304. frameTimePassed += GH.mainFPSmng->getElapsedMilliseconds();
  305. if(frameTimePassed >= frameTimeTotal)
  306. {
  307. frameTimePassed -= frameTimeTotal;
  308. if ( ++frame >= last)
  309. reset();
  310. }
  311. }
  312. void CShowableAnim::showAll(SDL_Surface * to)
  313. {
  314. if ( flags & BASE )// && frame != first)
  315. blitImage(first, group, to);
  316. blitImage(frame, group, to);
  317. }
  318. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  319. {
  320. assert(to);
  321. Rect src( xOffset, yOffset, pos.w, pos.h);
  322. auto img = anim->getImage(frame, group);
  323. if(img)
  324. {
  325. img->setAlpha(alpha);
  326. img->draw(to, pos.x, pos.y, &src);
  327. }
  328. }
  329. void CShowableAnim::rotate(bool on, bool vertical)
  330. {
  331. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  332. if (on)
  333. flags |= flag;
  334. else
  335. flags &= ~flag;
  336. }
  337. void CShowableAnim::setDuration(int durationMs)
  338. {
  339. frameTimeTotal = durationMs/(last - first);
  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. }