Images.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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/CBattleInterface.h"
  18. #include "../battle/CBattleInterfaceClasses.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;
  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. size_t CAnimImage::size()
  201. {
  202. return anim->size(group);
  203. }
  204. void CAnimImage::init()
  205. {
  206. visible = true;
  207. anim->load(frame, group);
  208. if (flags & CShowableAnim::BASE)
  209. anim->load(0,group);
  210. auto img = anim->getImage(frame, group);
  211. if (img)
  212. {
  213. pos.w = img->width();
  214. pos.h = img->height();
  215. }
  216. }
  217. CAnimImage::~CAnimImage()
  218. {
  219. }
  220. void CAnimImage::showAll(SDL_Surface * to)
  221. {
  222. if(!visible)
  223. return;
  224. if(flags & CShowableAnim::BASE && frame != 0)
  225. if(auto img = anim->getImage(0, group))
  226. img->draw(to, pos.x, pos.y);
  227. if(auto img = anim->getImage(frame, group))
  228. img->draw(to, pos.x, pos.y);
  229. }
  230. void CAnimImage::setFrame(size_t Frame, size_t Group)
  231. {
  232. if (frame == Frame && group==Group)
  233. return;
  234. if (anim->size(Group) > Frame)
  235. {
  236. anim->load(Frame, Group);
  237. frame = Frame;
  238. group = Group;
  239. if(auto img = anim->getImage(frame, group))
  240. {
  241. if (flags & CShowableAnim::PLAYER_COLORED)
  242. img->playerColored(player);
  243. pos.w = img->width();
  244. pos.h = img->height();
  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. flags |= CShowableAnim::PLAYER_COLORED;
  254. anim->getImage(frame, group)->playerColored(player);
  255. if (flags & CShowableAnim::BASE)
  256. anim->getImage(0, group)->playerColored(player);
  257. }
  258. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
  259. anim(std::make_shared<CAnimation>(name)),
  260. group(Group),
  261. frame(0),
  262. first(0),
  263. frameDelay(Delay),
  264. value(0),
  265. flags(Flags),
  266. xOffset(0),
  267. yOffset(0),
  268. alpha(255)
  269. {
  270. anim->loadGroup(group);
  271. last = anim->size(group);
  272. pos.w = anim->getImage(0, group)->width();
  273. pos.h = anim->getImage(0, group)->height();
  274. pos.x+= x;
  275. pos.y+= y;
  276. }
  277. CShowableAnim::~CShowableAnim()
  278. {
  279. anim->unloadGroup(group);
  280. }
  281. void CShowableAnim::setAlpha(ui32 alphaValue)
  282. {
  283. alpha = std::min<ui32>(alphaValue, 255);
  284. }
  285. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  286. {
  287. size_t max = anim->size(Group);
  288. if (to < max)
  289. max = to;
  290. if (max < from || max == 0)
  291. return false;
  292. anim->unloadGroup(group);
  293. anim->loadGroup(Group);
  294. group = Group;
  295. frame = first = from;
  296. last = max;
  297. value = 0;
  298. return true;
  299. }
  300. bool CShowableAnim::set(size_t Group)
  301. {
  302. if (anim->size(Group)== 0)
  303. return false;
  304. if (group != Group)
  305. {
  306. anim->unloadGroup(group);
  307. anim->loadGroup(Group);
  308. first = 0;
  309. group = Group;
  310. last = anim->size(Group);
  311. }
  312. frame = value = 0;
  313. return true;
  314. }
  315. void CShowableAnim::reset()
  316. {
  317. value = 0;
  318. frame = first;
  319. if (callback)
  320. callback();
  321. }
  322. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  323. {
  324. xOffset = posX;
  325. yOffset = posY;
  326. pos.w = width;
  327. pos.h = height;
  328. }
  329. void CShowableAnim::show(SDL_Surface * to)
  330. {
  331. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  332. blitImage(first, group, to);
  333. blitImage(frame, group, to);
  334. if ((flags & PLAY_ONCE) && frame + 1 == last)
  335. return;
  336. if ( ++value == frameDelay )
  337. {
  338. value = 0;
  339. if ( ++frame >= last)
  340. reset();
  341. }
  342. }
  343. void CShowableAnim::showAll(SDL_Surface * to)
  344. {
  345. if ( flags & BASE )// && frame != first)
  346. blitImage(first, group, to);
  347. blitImage(frame, group, to);
  348. }
  349. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  350. {
  351. assert(to);
  352. Rect src( xOffset, yOffset, pos.w, pos.h);
  353. auto img = anim->getImage(frame, group);
  354. if(img)
  355. img->draw(to, pos.x, pos.y, &src, alpha);
  356. }
  357. void CShowableAnim::rotate(bool on, bool vertical)
  358. {
  359. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  360. if (on)
  361. flags |= flag;
  362. else
  363. flags &= ~flag;
  364. }
  365. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, ui8 flags, EAnimType type):
  366. CShowableAnim(x,y,name,flags,4,type)
  367. {
  368. xOffset = 0;
  369. yOffset = 0;
  370. }
  371. void CCreatureAnim::loopPreview(bool warMachine)
  372. {
  373. std::vector<EAnimType> available;
  374. static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
  375. static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
  376. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  377. for (auto & elem : previewList)
  378. if (anim->size(elem))
  379. available.push_back(elem);
  380. size_t rnd = CRandomGenerator::getDefault().nextInt(available.size() * 2 - 1);
  381. if (rnd >= available.size())
  382. {
  383. EAnimType type;
  384. if ( anim->size(MOVING) == 0 )//no moving animation present
  385. type = HOLDING;
  386. else
  387. type = MOVING;
  388. //display this anim for ~1 second (time is random, but it looks good)
  389. for (size_t i=0; i< 12/anim->size(type) + 1; i++)
  390. addLast(type);
  391. }
  392. else
  393. addLast(available[rnd]);
  394. }
  395. void CCreatureAnim::addLast(EAnimType newType)
  396. {
  397. if (type != MOVING && newType == MOVING)//starting moving - play init sequence
  398. {
  399. queue.push( MOVE_START );
  400. }
  401. else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
  402. {
  403. queue.push( MOVE_END );
  404. }
  405. if (newType == TURN_L || newType == TURN_R)
  406. queue.push(newType);
  407. queue.push(newType);
  408. }
  409. void CCreatureAnim::reset()
  410. {
  411. //if we are in the middle of rotation - set flag
  412. if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
  413. rotate(true);
  414. if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
  415. rotate(false);
  416. while (!queue.empty())
  417. {
  418. EAnimType at = queue.front();
  419. queue.pop();
  420. if (set(at))
  421. return;
  422. }
  423. if (callback)
  424. callback();
  425. while (!queue.empty())
  426. {
  427. EAnimType at = queue.front();
  428. queue.pop();
  429. if (set(at))
  430. return;
  431. }
  432. set(HOLDING);
  433. }
  434. void CCreatureAnim::startPreview(bool warMachine)
  435. {
  436. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  437. }
  438. void CCreatureAnim::clearAndSet(EAnimType type)
  439. {
  440. while (!queue.empty())
  441. queue.pop();
  442. set(type);
  443. }