Images.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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(SDL_SWSURFACE, 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(new CAnimation(name, Flags & USE_RLE)),
  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. delete anim;
  281. }
  282. void CShowableAnim::setAlpha(ui32 alphaValue)
  283. {
  284. alpha = std::min<ui32>(alphaValue, 255);
  285. }
  286. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  287. {
  288. size_t max = anim->size(Group);
  289. if (to < max)
  290. max = to;
  291. if (max < from || max == 0)
  292. return false;
  293. anim->unloadGroup(group);
  294. anim->loadGroup(Group);
  295. group = Group;
  296. frame = first = from;
  297. last = max;
  298. value = 0;
  299. return true;
  300. }
  301. bool CShowableAnim::set(size_t Group)
  302. {
  303. if (anim->size(Group)== 0)
  304. return false;
  305. if (group != Group)
  306. {
  307. anim->unloadGroup(group);
  308. anim->loadGroup(Group);
  309. first = 0;
  310. group = Group;
  311. last = anim->size(Group);
  312. }
  313. frame = value = 0;
  314. return true;
  315. }
  316. void CShowableAnim::reset()
  317. {
  318. value = 0;
  319. frame = first;
  320. if (callback)
  321. callback();
  322. }
  323. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  324. {
  325. xOffset = posX;
  326. yOffset = posY;
  327. pos.w = width;
  328. pos.h = height;
  329. }
  330. void CShowableAnim::show(SDL_Surface * to)
  331. {
  332. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  333. blitImage(first, group, to);
  334. blitImage(frame, group, to);
  335. if ((flags & PLAY_ONCE) && frame + 1 == last)
  336. return;
  337. if ( ++value == frameDelay )
  338. {
  339. value = 0;
  340. if ( ++frame >= last)
  341. reset();
  342. }
  343. }
  344. void CShowableAnim::showAll(SDL_Surface * to)
  345. {
  346. if ( flags & BASE )// && frame != first)
  347. blitImage(first, group, to);
  348. blitImage(frame, group, to);
  349. }
  350. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  351. {
  352. assert(to);
  353. Rect src( xOffset, yOffset, pos.w, pos.h);
  354. auto img = anim->getImage(frame, group);
  355. if (img)
  356. img->draw(to, pos.x-xOffset, pos.y-yOffset, &src, alpha);
  357. }
  358. void CShowableAnim::rotate(bool on, bool vertical)
  359. {
  360. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  361. if (on)
  362. flags |= flag;
  363. else
  364. flags &= ~flag;
  365. }
  366. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags, EAnimType type):
  367. CShowableAnim(x,y,name,flags,4,type)
  368. {
  369. xOffset = picPos.x;
  370. yOffset = picPos.y;
  371. if (picPos.w)
  372. pos.w = picPos.w;
  373. if (picPos.h)
  374. pos.h = picPos.h;
  375. }
  376. void CCreatureAnim::loopPreview(bool warMachine)
  377. {
  378. std::vector<EAnimType> available;
  379. static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
  380. static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
  381. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  382. for (auto & elem : previewList)
  383. if (anim->size(elem))
  384. available.push_back(elem);
  385. size_t rnd = CRandomGenerator::getDefault().nextInt(available.size() * 2 - 1);
  386. if (rnd >= available.size())
  387. {
  388. EAnimType type;
  389. if ( anim->size(MOVING) == 0 )//no moving animation present
  390. type = HOLDING;
  391. else
  392. type = MOVING;
  393. //display this anim for ~1 second (time is random, but it looks good)
  394. for (size_t i=0; i< 12/anim->size(type) + 1; i++)
  395. addLast(type);
  396. }
  397. else
  398. addLast(available[rnd]);
  399. }
  400. void CCreatureAnim::addLast(EAnimType newType)
  401. {
  402. if (type != MOVING && newType == MOVING)//starting moving - play init sequence
  403. {
  404. queue.push( MOVE_START );
  405. }
  406. else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
  407. {
  408. queue.push( MOVE_END );
  409. }
  410. if (newType == TURN_L || newType == TURN_R)
  411. queue.push(newType);
  412. queue.push(newType);
  413. }
  414. void CCreatureAnim::reset()
  415. {
  416. //if we are in the middle of rotation - set flag
  417. if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
  418. rotate(true);
  419. if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
  420. rotate(false);
  421. while (!queue.empty())
  422. {
  423. EAnimType at = queue.front();
  424. queue.pop();
  425. if (set(at))
  426. return;
  427. }
  428. if (callback)
  429. callback();
  430. while (!queue.empty())
  431. {
  432. EAnimType at = queue.front();
  433. queue.pop();
  434. if (set(at))
  435. return;
  436. }
  437. set(HOLDING);
  438. }
  439. void CCreatureAnim::startPreview(bool warMachine)
  440. {
  441. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  442. }
  443. void CCreatureAnim::clearAndSet(EAnimType type)
  444. {
  445. while (!queue.empty())
  446. queue.pop();
  447. set(type);
  448. }