Images.cpp 12 KB

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