Images.cpp 12 KB

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