Images.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 Delay, size_t Group, uint8_t alpha):
  294. anim(std::make_shared<CAnimation>(name)),
  295. group(Group),
  296. frame(0),
  297. first(0),
  298. frameDelay(Delay),
  299. value(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. value = 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 = value = 0;
  348. return true;
  349. }
  350. void CShowableAnim::reset()
  351. {
  352. value = 0;
  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. if ( ++value == frameDelay )
  372. {
  373. value = 0;
  374. if ( ++frame >= last)
  375. reset();
  376. }
  377. }
  378. void CShowableAnim::showAll(SDL_Surface * to)
  379. {
  380. if ( flags & BASE )// && frame != first)
  381. blitImage(first, group, to);
  382. blitImage(frame, group, to);
  383. }
  384. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  385. {
  386. assert(to);
  387. Rect src( xOffset, yOffset, pos.w, pos.h);
  388. auto img = anim->getImage(frame, group);
  389. if(img)
  390. {
  391. const ColorFilter alphaFilter = ColorFilter::genAlphaShifter(vstd::lerp(0.0f, 1.0f, alpha/255.0f));
  392. img->adjustPalette(alphaFilter);
  393. img->draw(to, pos.x, pos.y, &src);
  394. }
  395. }
  396. void CShowableAnim::rotate(bool on, bool vertical)
  397. {
  398. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  399. if (on)
  400. flags |= flag;
  401. else
  402. flags &= ~flag;
  403. }
  404. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, ui8 flags, ECreatureAnimType type):
  405. CShowableAnim(x,y,name,flags,4,size_t(type))
  406. {
  407. xOffset = 0;
  408. yOffset = 0;
  409. }
  410. void CCreatureAnim::loopPreview(bool warMachine)
  411. {
  412. std::vector<ECreatureAnimType> available;
  413. static const ECreatureAnimType creaPreviewList[] = {
  414. ECreatureAnimType::HOLDING,
  415. ECreatureAnimType::HITTED,
  416. ECreatureAnimType::DEFENCE,
  417. ECreatureAnimType::ATTACK_FRONT,
  418. ECreatureAnimType::SPECIAL_FRONT
  419. };
  420. static const ECreatureAnimType machPreviewList[] = {
  421. ECreatureAnimType::HOLDING,
  422. ECreatureAnimType::MOVING,
  423. ECreatureAnimType::SHOOT_UP,
  424. ECreatureAnimType::SHOOT_FRONT,
  425. ECreatureAnimType::SHOOT_DOWN
  426. };
  427. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  428. for (auto & elem : previewList)
  429. if (anim->size(size_t(elem)))
  430. available.push_back(elem);
  431. size_t rnd = CRandomGenerator::getDefault().nextInt((int)available.size() * 2 - 1);
  432. if (rnd >= available.size())
  433. {
  434. ECreatureAnimType type;
  435. if ( anim->size(size_t(ECreatureAnimType::MOVING)) == 0 )//no moving animation present
  436. type = ECreatureAnimType::HOLDING;
  437. else
  438. type = ECreatureAnimType::MOVING;
  439. //display this anim for ~1 second (time is random, but it looks good)
  440. for (size_t i=0; i< 12/anim->size(size_t(type)) + 1; i++)
  441. addLast(type);
  442. }
  443. else
  444. addLast(available[rnd]);
  445. }
  446. void CCreatureAnim::addLast(ECreatureAnimType newType)
  447. {
  448. auto currType = ECreatureAnimType(group);
  449. if (currType != ECreatureAnimType::MOVING && newType == ECreatureAnimType::MOVING)//starting moving - play init sequence
  450. {
  451. queue.push( ECreatureAnimType::MOVE_START );
  452. }
  453. else if (currType == ECreatureAnimType::MOVING && newType != ECreatureAnimType::MOVING )//previous anim was moving - finish it
  454. {
  455. queue.push( ECreatureAnimType::MOVE_END );
  456. }
  457. if (newType == ECreatureAnimType::TURN_L || newType == ECreatureAnimType::TURN_R)
  458. queue.push(newType);
  459. queue.push(newType);
  460. }
  461. void CCreatureAnim::reset()
  462. {
  463. //if we are in the middle of rotation - set flag
  464. if (group == size_t(ECreatureAnimType::TURN_L) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_L)
  465. rotate(true);
  466. if (group == size_t(ECreatureAnimType::TURN_R) && !queue.empty() && queue.front() == ECreatureAnimType::TURN_R)
  467. rotate(false);
  468. while (!queue.empty())
  469. {
  470. ECreatureAnimType at = queue.front();
  471. queue.pop();
  472. if (set(size_t(at)))
  473. return;
  474. }
  475. if (callback)
  476. callback();
  477. while (!queue.empty())
  478. {
  479. ECreatureAnimType at = queue.front();
  480. queue.pop();
  481. if (set(size_t(at)))
  482. return;
  483. }
  484. set(size_t(ECreatureAnimType::HOLDING));
  485. }
  486. void CCreatureAnim::startPreview(bool warMachine)
  487. {
  488. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  489. }
  490. void CCreatureAnim::clearAndSet(ECreatureAnimType type)
  491. {
  492. while (!queue.empty())
  493. queue.pop();
  494. set(size_t(type));
  495. }