Images.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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 Delay, size_t Group, uint8_t alpha):
  231. anim(std::make_shared<CAnimation>(name)),
  232. group(Group),
  233. frame(0),
  234. first(0),
  235. frameDelay(Delay),
  236. value(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. value = 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 = value = 0;
  285. return true;
  286. }
  287. void CShowableAnim::reset()
  288. {
  289. value = 0;
  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. if ( ++value == frameDelay )
  309. {
  310. value = 0;
  311. if ( ++frame >= last)
  312. reset();
  313. }
  314. }
  315. void CShowableAnim::showAll(SDL_Surface * to)
  316. {
  317. if ( flags & BASE )// && frame != first)
  318. blitImage(first, group, to);
  319. blitImage(frame, group, to);
  320. }
  321. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  322. {
  323. assert(to);
  324. Rect src( xOffset, yOffset, pos.w, pos.h);
  325. auto img = anim->getImage(frame, group);
  326. if(img)
  327. {
  328. img->setAlpha(alpha);
  329. img->draw(to, pos.x, pos.y, &src);
  330. }
  331. }
  332. void CShowableAnim::rotate(bool on, bool vertical)
  333. {
  334. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  335. if (on)
  336. flags |= flag;
  337. else
  338. flags &= ~flag;
  339. }
  340. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, ui8 flags, ECreatureAnimType type):
  341. CShowableAnim(x,y,name,flags,4,size_t(type))
  342. {
  343. xOffset = 0;
  344. yOffset = 0;
  345. }
  346. void CCreatureAnim::loopPreview(bool warMachine)
  347. {
  348. std::vector<ECreatureAnimType> available;
  349. static const ECreatureAnimType creaPreviewList[] = {
  350. ECreatureAnimType::HOLDING,
  351. ECreatureAnimType::HITTED,
  352. ECreatureAnimType::DEFENCE,
  353. ECreatureAnimType::ATTACK_FRONT,
  354. ECreatureAnimType::SPECIAL_FRONT
  355. };
  356. static const ECreatureAnimType machPreviewList[] = {
  357. ECreatureAnimType::HOLDING,
  358. ECreatureAnimType::MOVING,
  359. ECreatureAnimType::SHOOT_UP,
  360. ECreatureAnimType::SHOOT_FRONT,
  361. ECreatureAnimType::SHOOT_DOWN
  362. };
  363. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  364. for (auto & elem : previewList)
  365. if (anim->size(size_t(elem)))
  366. available.push_back(elem);
  367. size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
  368. if (rnd >= available.size())
  369. {
  370. ECreatureAnimType type;
  371. if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
  372. type = ECreatureAnimType::HOLDING;
  373. else
  374. type = ECreatureAnimType::MOVING;
  375. //display this anim for ~1 second (time is random, but it looks good)
  376. for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
  377. addLast(type);
  378. }
  379. else
  380. addLast(available[rnd]);
  381. }
  382. void CCreatureAnim::addLast(ECreatureAnimType newType)
  383. {
  384. auto currType = ECreatureAnimType(group);
  385. if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
  386. {
  387. queue.push( ECreatureAnimType::MOVE_START );
  388. }
  389. else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
  390. {
  391. queue.push( ECreatureAnimType::MOVE_END );
  392. }
  393. if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
  394. queue.push(newType);
  395. queue.push(newType);
  396. }
  397. void CCreatureAnim::reset()
  398. {
  399. //if we are in the middle of rotation - set flag
  400. if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
  401. rotate(true);
  402. if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
  403. rotate(false);
  404. while (!queue.empty())
  405. {
  406. ECreatureAnimType at = queue.front();
  407. queue.pop();
  408. if (set(size_t(at)))
  409. return;
  410. }
  411. if (callback)
  412. callback();
  413. while (!queue.empty())
  414. {
  415. ECreatureAnimType at = queue.front();
  416. queue.pop();
  417. if (set(size_t(at)))
  418. return;
  419. }
  420. set(size_t(ECreatureAnimType::HOLDING));
  421. }
  422. void CCreatureAnim::startPreview(bool warMachine)
  423. {
  424. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  425. }
  426. void CCreatureAnim::clearAndSet(ECreatureAnimType type)
  427. {
  428. while (!queue.empty())
  429. queue.pop();
  430. set(size_t(type));
  431. }