Images.cpp 12 KB

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