Images.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  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/CCursorHandler.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( SDL_Surface *BG, int x, int y, bool Free )
  31. {
  32. init();
  33. bg = BG;
  34. freeSurf = Free;
  35. pos.x += x;
  36. pos.y += y;
  37. pos.w = BG->w;
  38. pos.h = BG->h;
  39. }
  40. CPicture::CPicture( const std::string &bmpname, int x, int y )
  41. {
  42. init();
  43. bg = BitmapHandler::loadBitmap(bmpname);
  44. freeSurf = true;
  45. pos.x += x;
  46. pos.y += y;
  47. if(bg)
  48. {
  49. pos.w = bg->w;
  50. pos.h = bg->h;
  51. }
  52. else
  53. {
  54. pos.w = pos.h = 0;
  55. }
  56. }
  57. CPicture::CPicture(const Rect &r, const SDL_Color &color, bool screenFormat)
  58. {
  59. init();
  60. createSimpleRect(r, screenFormat, SDL_MapRGB(bg->format, color.r, color.g,color.b));
  61. }
  62. CPicture::CPicture(const Rect &r, ui32 color, bool screenFormat)
  63. {
  64. init();
  65. createSimpleRect(r, screenFormat, color);
  66. }
  67. CPicture::CPicture(SDL_Surface * BG, const Rect &SrcRect, int x, int y, bool free)
  68. {
  69. visible = true;
  70. needRefresh = false;
  71. srcRect = new Rect(SrcRect);
  72. pos.x += x;
  73. pos.y += y;
  74. pos.w = srcRect->w;
  75. pos.h = srcRect->h;
  76. bg = BG;
  77. freeSurf = free;
  78. }
  79. void CPicture::setSurface(SDL_Surface *to)
  80. {
  81. bg = to;
  82. if (srcRect)
  83. {
  84. pos.w = srcRect->w;
  85. pos.h = srcRect->h;
  86. }
  87. else
  88. {
  89. pos.w = bg->w;
  90. pos.h = bg->h;
  91. }
  92. }
  93. CPicture::~CPicture()
  94. {
  95. if(freeSurf)
  96. SDL_FreeSurface(bg);
  97. delete srcRect;
  98. }
  99. void CPicture::init()
  100. {
  101. visible = true;
  102. needRefresh = false;
  103. srcRect = nullptr;
  104. }
  105. void CPicture::show(SDL_Surface * to)
  106. {
  107. if (visible && needRefresh)
  108. showAll(to);
  109. }
  110. void CPicture::showAll(SDL_Surface * to)
  111. {
  112. if(bg && visible)
  113. {
  114. if(srcRect)
  115. {
  116. SDL_Rect srcRectCpy = *srcRect;
  117. SDL_Rect dstRect = srcRectCpy;
  118. dstRect.x = pos.x;
  119. dstRect.y = pos.y;
  120. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  121. }
  122. else
  123. blitAt(bg, pos, to);
  124. }
  125. }
  126. void CPicture::convertToScreenBPP()
  127. {
  128. SDL_Surface *hlp = bg;
  129. bg = SDL_ConvertSurface(hlp,screen->format,0);
  130. CSDL_Ext::setDefaultColorKey(bg);
  131. SDL_FreeSurface(hlp);
  132. }
  133. void CPicture::setAlpha(int value)
  134. {
  135. CSDL_Ext::setAlpha (bg, value);
  136. }
  137. void CPicture::scaleTo(Point size)
  138. {
  139. SDL_Surface * scaled = CSDL_Ext::scaleSurface(bg, size.x, size.y);
  140. if(freeSurf)
  141. SDL_FreeSurface(bg);
  142. setSurface(scaled);
  143. freeSurf = false;
  144. }
  145. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  146. {
  147. pos += r.topLeft();
  148. pos.w = r.w;
  149. pos.h = r.h;
  150. if(screenFormat)
  151. bg = CSDL_Ext::newSurface(r.w, r.h);
  152. else
  153. bg = SDL_CreateRGBSurface(0, r.w, r.h, 8, 0, 0, 0, 0);
  154. SDL_FillRect(bg, nullptr, color);
  155. freeSurf = true;
  156. }
  157. void CPicture::colorize(PlayerColor player)
  158. {
  159. assert(bg);
  160. graphics->blueToPlayersAdv(bg, player);
  161. }
  162. CFilledTexture::CFilledTexture(std::string imageName, Rect position):
  163. CIntObject(0, position.topLeft()),
  164. texture(BitmapHandler::loadBitmap(imageName))
  165. {
  166. pos.w = position.w;
  167. pos.h = position.h;
  168. }
  169. CFilledTexture::~CFilledTexture()
  170. {
  171. SDL_FreeSurface(texture);
  172. }
  173. void CFilledTexture::showAll(SDL_Surface *to)
  174. {
  175. CSDL_Ext::CClipRectGuard guard(to, pos);
  176. CSDL_Ext::fillTexture(to, texture);
  177. }
  178. CAnimImage::CAnimImage(const std::string & name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  179. frame(Frame),
  180. group(Group),
  181. player(-1),
  182. flags(Flags)
  183. {
  184. pos.x += x;
  185. pos.y += y;
  186. anim = std::make_shared<CAnimation>(name);
  187. init();
  188. }
  189. CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  190. anim(Anim),
  191. frame(Frame),
  192. group(Group),
  193. player(-1),
  194. flags(Flags)
  195. {
  196. pos.x += x;
  197. pos.y += y;
  198. init();
  199. }
  200. CAnimImage::CAnimImage(std::shared_ptr<CAnimation> Anim, size_t Frame, Rect targetPos, size_t Group, ui8 Flags):
  201. anim(Anim),
  202. frame(Frame),
  203. group(Group),
  204. player(-1),
  205. flags(Flags),
  206. scaledSize(targetPos.w, targetPos.h)
  207. {
  208. pos.x += targetPos.x;
  209. pos.y += targetPos.y;
  210. init();
  211. }
  212. size_t CAnimImage::size()
  213. {
  214. return anim->size(group);
  215. }
  216. bool CAnimImage::isScaled() const
  217. {
  218. return (scaledSize.x != 0);
  219. }
  220. void CAnimImage::setSizeFromImage(const IImage &img)
  221. {
  222. if (isScaled())
  223. {
  224. // At the time of writing this, IImage had no method to scale to different aspect ratio
  225. // Therefore, have to ignore the target height and preserve original aspect ratio
  226. pos.w = scaledSize.x;
  227. pos.h = roundf(float(scaledSize.x) * img.height() / img.width());
  228. }
  229. else
  230. {
  231. pos.w = img.width();
  232. pos.h = img.height();
  233. }
  234. }
  235. void CAnimImage::init()
  236. {
  237. visible = true;
  238. anim->load(frame, group);
  239. if (flags & CShowableAnim::BASE)
  240. anim->load(0,group);
  241. auto img = anim->getImage(frame, group);
  242. if (img)
  243. setSizeFromImage(*img);
  244. }
  245. CAnimImage::~CAnimImage()
  246. {
  247. }
  248. void CAnimImage::showAll(SDL_Surface * to)
  249. {
  250. if(!visible)
  251. return;
  252. std::vector<size_t> frames = {frame};
  253. if((flags & CShowableAnim::BASE) && frame != 0)
  254. {
  255. frames.insert(frames.begin(), 0);
  256. }
  257. for(auto targetFrame : frames)
  258. {
  259. if(auto img = anim->getImage(targetFrame, group))
  260. {
  261. if(isScaled())
  262. {
  263. auto scaled = img->scaleFast(float(scaledSize.x) / img->width());
  264. scaled->draw(to, pos.x, pos.y);
  265. }
  266. else
  267. img->draw(to, pos.x, pos.y);
  268. }
  269. }
  270. }
  271. void CAnimImage::setFrame(size_t Frame, size_t Group)
  272. {
  273. if (frame == Frame && group==Group)
  274. return;
  275. if (anim->size(Group) > Frame)
  276. {
  277. anim->load(Frame, Group);
  278. frame = Frame;
  279. group = Group;
  280. if(auto img = anim->getImage(frame, group))
  281. {
  282. if (flags & CShowableAnim::PLAYER_COLORED)
  283. img->playerColored(player);
  284. setSizeFromImage(*img);
  285. }
  286. }
  287. else
  288. logGlobal->error("Error: accessing unavailable frame %d:%d in CAnimation!", Group, Frame);
  289. }
  290. void CAnimImage::playerColored(PlayerColor currPlayer)
  291. {
  292. player = currPlayer;
  293. flags |= CShowableAnim::PLAYER_COLORED;
  294. anim->getImage(frame, group)->playerColored(player);
  295. if (flags & CShowableAnim::BASE)
  296. anim->getImage(0, group)->playerColored(player);
  297. }
  298. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
  299. anim(std::make_shared<CAnimation>(name)),
  300. group(Group),
  301. frame(0),
  302. first(0),
  303. frameDelay(Delay),
  304. value(0),
  305. flags(Flags),
  306. xOffset(0),
  307. yOffset(0),
  308. alpha(255)
  309. {
  310. anim->loadGroup(group);
  311. last = anim->size(group);
  312. pos.w = anim->getImage(0, group)->width();
  313. pos.h = anim->getImage(0, group)->height();
  314. pos.x+= x;
  315. pos.y+= y;
  316. }
  317. CShowableAnim::~CShowableAnim()
  318. {
  319. anim->unloadGroup(group);
  320. }
  321. void CShowableAnim::setAlpha(ui32 alphaValue)
  322. {
  323. alpha = std::min<ui32>(alphaValue, 255);
  324. }
  325. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  326. {
  327. size_t max = anim->size(Group);
  328. if (to < max)
  329. max = to;
  330. if (max < from || max == 0)
  331. return false;
  332. anim->unloadGroup(group);
  333. anim->loadGroup(Group);
  334. group = Group;
  335. frame = first = from;
  336. last = max;
  337. value = 0;
  338. return true;
  339. }
  340. bool CShowableAnim::set(size_t Group)
  341. {
  342. if (anim->size(Group)== 0)
  343. return false;
  344. if (group != Group)
  345. {
  346. anim->unloadGroup(group);
  347. anim->loadGroup(Group);
  348. first = 0;
  349. group = Group;
  350. last = anim->size(Group);
  351. }
  352. frame = value = 0;
  353. return true;
  354. }
  355. void CShowableAnim::reset()
  356. {
  357. value = 0;
  358. frame = first;
  359. if (callback)
  360. callback();
  361. }
  362. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  363. {
  364. xOffset = posX;
  365. yOffset = posY;
  366. pos.w = width;
  367. pos.h = height;
  368. }
  369. void CShowableAnim::show(SDL_Surface * to)
  370. {
  371. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  372. blitImage(first, group, to);
  373. blitImage(frame, group, to);
  374. if ((flags & PLAY_ONCE) && frame + 1 == last)
  375. return;
  376. if ( ++value == frameDelay )
  377. {
  378. value = 0;
  379. if ( ++frame >= last)
  380. reset();
  381. }
  382. }
  383. void CShowableAnim::showAll(SDL_Surface * to)
  384. {
  385. if ( flags & BASE )// && frame != first)
  386. blitImage(first, group, to);
  387. blitImage(frame, group, to);
  388. }
  389. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  390. {
  391. assert(to);
  392. Rect src( xOffset, yOffset, pos.w, pos.h);
  393. auto img = anim->getImage(frame, group);
  394. if(img)
  395. img->draw(to, pos.x, pos.y, &src, alpha);
  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,4,size_t(type))
  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. }