2
0

Images.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include "StdInc.h"
  2. #include "Images.h"
  3. #include "MiscWidgets.h"
  4. #include "../gui/CAnimation.h"
  5. #include "../gui/SDL_Pixels.h"
  6. #include "../gui/CGuiHandler.h"
  7. #include "../gui/CCursorHandler.h"
  8. #include "../battle/CBattleInterface.h"
  9. #include "../battle/CBattleInterfaceClasses.h"
  10. #include "../CBitmapHandler.h"
  11. #include "../Graphics.h"
  12. #include "../CGameInfo.h"
  13. #include "../CPlayerInterface.h"
  14. #include "../CMessage.h"
  15. #include "../CMusicHandler.h"
  16. #include "../windows/CAdvmapInterface.h"
  17. #include "../../CCallback.h"
  18. #include "../../lib/CConfigHandler.h"
  19. #include "../../lib/CGeneralTextHandler.h" //for Unicode related stuff
  20. #include "../../lib/CRandomGenerator.h"
  21. /*
  22. * Images.cpp, part of VCMI engine
  23. *
  24. * Authors: listed in file AUTHORS in main folder
  25. *
  26. * License: GNU General Public License v2.0 or later
  27. * Full text of license available in license.txt file, in main folder
  28. *
  29. */
  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 /*= false*/)
  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 /*= false*/)
  63. {
  64. init();
  65. createSimpleRect(r, screenFormat, color);
  66. }
  67. CPicture::CPicture(SDL_Surface *BG, const Rect &SrcRect, int x /*= 0*/, int y /*= 0*/, bool free /*= false*/)
  68. {
  69. needRefresh = false;
  70. srcRect = new Rect(SrcRect);
  71. pos.x += x;
  72. pos.y += y;
  73. pos.w = srcRect->w;
  74. pos.h = srcRect->h;
  75. bg = BG;
  76. freeSurf = free;
  77. }
  78. void CPicture::setSurface(SDL_Surface *to)
  79. {
  80. bg = to;
  81. if (srcRect)
  82. {
  83. pos.w = srcRect->w;
  84. pos.h = srcRect->h;
  85. }
  86. else
  87. {
  88. pos.w = bg->w;
  89. pos.h = bg->h;
  90. }
  91. }
  92. CPicture::~CPicture()
  93. {
  94. if(freeSurf)
  95. SDL_FreeSurface(bg);
  96. delete srcRect;
  97. }
  98. void CPicture::init()
  99. {
  100. needRefresh = false;
  101. srcRect = nullptr;
  102. }
  103. void CPicture::show(SDL_Surface * to)
  104. {
  105. if (needRefresh)
  106. showAll(to);
  107. }
  108. void CPicture::showAll(SDL_Surface * to)
  109. {
  110. if(bg)
  111. {
  112. if(srcRect)
  113. {
  114. SDL_Rect srcRectCpy = *srcRect;
  115. SDL_Rect dstRect = srcRectCpy;
  116. dstRect.x = pos.x;
  117. dstRect.y = pos.y;
  118. CSDL_Ext::blitSurface(bg, &srcRectCpy, to, &dstRect);
  119. }
  120. else
  121. blitAt(bg, pos, to);
  122. }
  123. }
  124. void CPicture::convertToScreenBPP()
  125. {
  126. SDL_Surface *hlp = bg;
  127. bg = SDL_ConvertSurface(hlp,screen->format,0);
  128. CSDL_Ext::setDefaultColorKey(bg);
  129. SDL_FreeSurface(hlp);
  130. }
  131. void CPicture::setAlpha(int value)
  132. {
  133. CSDL_Ext::setAlpha (bg, value);
  134. }
  135. void CPicture::scaleTo(Point size)
  136. {
  137. SDL_Surface * scaled = CSDL_Ext::scaleSurface(bg, size.x, size.y);
  138. if(freeSurf)
  139. SDL_FreeSurface(bg);
  140. setSurface(scaled);
  141. freeSurf = false;
  142. }
  143. void CPicture::createSimpleRect(const Rect &r, bool screenFormat, ui32 color)
  144. {
  145. pos += r;
  146. pos.w = r.w;
  147. pos.h = r.h;
  148. if(screenFormat)
  149. bg = CSDL_Ext::newSurface(r.w, r.h);
  150. else
  151. bg = SDL_CreateRGBSurface(SDL_SWSURFACE, r.w, r.h, 8, 0, 0, 0, 0);
  152. SDL_FillRect(bg, nullptr, color);
  153. freeSurf = true;
  154. }
  155. void CPicture::colorizeAndConvert(PlayerColor player)
  156. {
  157. assert(bg);
  158. colorize(player);
  159. convertToScreenBPP();
  160. }
  161. void CPicture::colorize(PlayerColor player)
  162. {
  163. assert(bg);
  164. graphics->blueToPlayersAdv(bg, player);
  165. }
  166. CFilledTexture::CFilledTexture(std::string imageName, Rect position):
  167. CIntObject(0, position.topLeft()),
  168. texture(BitmapHandler::loadBitmap(imageName))
  169. {
  170. pos.w = position.w;
  171. pos.h = position.h;
  172. }
  173. CFilledTexture::~CFilledTexture()
  174. {
  175. SDL_FreeSurface(texture);
  176. }
  177. void CFilledTexture::showAll(SDL_Surface *to)
  178. {
  179. CSDL_Ext::CClipRectGuard guard(to, pos);
  180. CSDL_Ext::fillTexture(to, texture);
  181. }
  182. CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  183. frame(Frame),
  184. group(Group),
  185. player(-1),
  186. flags(Flags)
  187. {
  188. pos.x += x;
  189. pos.y += y;
  190. anim = new CAnimation(name);
  191. init();
  192. }
  193. CAnimImage::CAnimImage(CAnimation *Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
  194. anim(Anim),
  195. frame(Frame),
  196. group(Group),
  197. player(-1),
  198. flags(Flags)
  199. {
  200. pos.x += x;
  201. pos.y += y;
  202. init();
  203. }
  204. size_t CAnimImage::size()
  205. {
  206. return anim->size(group);
  207. }
  208. void CAnimImage::init()
  209. {
  210. anim->load(frame, group);
  211. if (flags & CShowableAnim::BASE)
  212. anim->load(0,group);
  213. IImage *img = anim->getImage(frame, group);
  214. if (img)
  215. {
  216. pos.w = img->width();
  217. pos.h = img->height();
  218. }
  219. }
  220. CAnimImage::~CAnimImage()
  221. {
  222. anim->unload(frame, group);
  223. if (flags & CShowableAnim::BASE)
  224. anim->unload(0,group);
  225. delete anim;
  226. }
  227. void CAnimImage::showAll(SDL_Surface * to)
  228. {
  229. IImage *img;
  230. if ( flags & CShowableAnim::BASE && frame != 0)
  231. if ((img = anim->getImage(0, group)))
  232. img->draw(to, pos.x, pos.y);
  233. if ((img = anim->getImage(frame, group)))
  234. img->draw(to, pos.x, pos.y);
  235. }
  236. void CAnimImage::setFrame(size_t Frame, size_t Group)
  237. {
  238. if (frame == Frame && group==Group)
  239. return;
  240. if (anim->size(Group) > Frame)
  241. {
  242. anim->load(Frame, Group);
  243. anim->unload(frame, group);
  244. frame = Frame;
  245. group = Group;
  246. IImage *img = anim->getImage(frame, group);
  247. if (img)
  248. {
  249. if (flags & CShowableAnim::PLAYER_COLORED)
  250. img->playerColored(player);
  251. pos.w = img->width();
  252. pos.h = img->height();
  253. }
  254. }
  255. else
  256. logGlobal->errorStream() << "Error: accessing unavailable frame " << Group << ":" << Frame << " in CAnimation!";
  257. }
  258. void CAnimImage::playerColored(PlayerColor currPlayer)
  259. {
  260. player = currPlayer;
  261. flags |= CShowableAnim::PLAYER_COLORED;
  262. anim->getImage(frame, group)->playerColored(player);
  263. if (flags & CShowableAnim::BASE)
  264. anim->getImage(0, group)->playerColored(player);
  265. }
  266. CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
  267. anim(new CAnimation(name, Flags & USE_RLE)),
  268. group(Group),
  269. frame(0),
  270. first(0),
  271. frameDelay(Delay),
  272. value(0),
  273. flags(Flags),
  274. xOffset(0),
  275. yOffset(0),
  276. alpha(255)
  277. {
  278. anim->loadGroup(group);
  279. last = anim->size(group);
  280. pos.w = anim->getImage(0, group)->width();
  281. pos.h = anim->getImage(0, group)->height();
  282. pos.x+= x;
  283. pos.y+= y;
  284. }
  285. CShowableAnim::~CShowableAnim()
  286. {
  287. anim->unloadGroup(group);
  288. delete anim;
  289. }
  290. void CShowableAnim::setAlpha(ui32 alphaValue)
  291. {
  292. alpha = std::min<ui32>(alphaValue, 255);
  293. }
  294. bool CShowableAnim::set(size_t Group, size_t from, size_t to)
  295. {
  296. size_t max = anim->size(Group);
  297. if (to < max)
  298. max = to;
  299. if (max < from || max == 0)
  300. return false;
  301. anim->load(Group);
  302. anim->unload(group);
  303. group = Group;
  304. frame = first = from;
  305. last = max;
  306. value = 0;
  307. return true;
  308. }
  309. bool CShowableAnim::set(size_t Group)
  310. {
  311. if (anim->size(Group)== 0)
  312. return false;
  313. if (group != Group)
  314. {
  315. anim->loadGroup(Group);
  316. anim->unloadGroup(group);
  317. first = 0;
  318. group = Group;
  319. last = anim->size(Group);
  320. }
  321. frame = value = 0;
  322. return true;
  323. }
  324. void CShowableAnim::reset()
  325. {
  326. value = 0;
  327. frame = first;
  328. if (callback)
  329. callback();
  330. }
  331. void CShowableAnim::clipRect(int posX, int posY, int width, int height)
  332. {
  333. xOffset = posX;
  334. yOffset = posY;
  335. pos.w = width;
  336. pos.h = height;
  337. }
  338. void CShowableAnim::show(SDL_Surface * to)
  339. {
  340. if ( flags & BASE )// && frame != first) // FIXME: results in graphical glytch in Fortress, upgraded hydra's dwelling
  341. blitImage(first, group, to);
  342. blitImage(frame, group, to);
  343. if ((flags & PLAY_ONCE) && frame + 1 == last)
  344. return;
  345. if ( ++value == frameDelay )
  346. {
  347. value = 0;
  348. if ( ++frame >= last)
  349. reset();
  350. }
  351. }
  352. void CShowableAnim::showAll(SDL_Surface * to)
  353. {
  354. if ( flags & BASE )// && frame != first)
  355. blitImage(first, group, to);
  356. blitImage(frame, group, to);
  357. }
  358. void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
  359. {
  360. assert(to);
  361. Rect src( xOffset, yOffset, pos.w, pos.h);
  362. IImage * img = anim->getImage(frame, group);
  363. if (img)
  364. img->draw(to, pos.x-xOffset, pos.y-yOffset, &src, alpha);
  365. }
  366. void CShowableAnim::rotate(bool on, bool vertical)
  367. {
  368. ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
  369. if (on)
  370. flags |= flag;
  371. else
  372. flags &= ~flag;
  373. }
  374. CCreatureAnim::CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags, EAnimType type):
  375. CShowableAnim(x,y,name,flags,4,type)
  376. {
  377. xOffset = picPos.x;
  378. yOffset = picPos.y;
  379. if (picPos.w)
  380. pos.w = picPos.w;
  381. if (picPos.h)
  382. pos.h = picPos.h;
  383. };
  384. void CCreatureAnim::loopPreview(bool warMachine)
  385. {
  386. std::vector<EAnimType> available;
  387. static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
  388. static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
  389. auto & previewList = warMachine ? machPreviewList : creaPreviewList;
  390. for (auto & elem : previewList)
  391. if (anim->size(elem))
  392. available.push_back(elem);
  393. size_t rnd = CRandomGenerator::getDefault().nextInt(available.size() * 2 - 1);
  394. if (rnd >= available.size())
  395. {
  396. EAnimType type;
  397. if ( anim->size(MOVING) == 0 )//no moving animation present
  398. type = HOLDING;
  399. else
  400. type = MOVING;
  401. //display this anim for ~1 second (time is random, but it looks good)
  402. for (size_t i=0; i< 12/anim->size(type) + 1; i++)
  403. addLast(type);
  404. }
  405. else
  406. addLast(available[rnd]);
  407. }
  408. void CCreatureAnim::addLast(EAnimType newType)
  409. {
  410. if (type != MOVING && newType == MOVING)//starting moving - play init sequence
  411. {
  412. queue.push( MOVE_START );
  413. }
  414. else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
  415. {
  416. queue.push( MOVE_END );
  417. }
  418. if (newType == TURN_L || newType == TURN_R)
  419. queue.push(newType);
  420. queue.push(newType);
  421. }
  422. void CCreatureAnim::reset()
  423. {
  424. //if we are in the middle of rotation - set flag
  425. if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
  426. rotate(true);
  427. if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
  428. rotate(false);
  429. while (!queue.empty())
  430. {
  431. EAnimType at = queue.front();
  432. queue.pop();
  433. if (set(at))
  434. return;
  435. }
  436. if (callback)
  437. callback();
  438. while (!queue.empty())
  439. {
  440. EAnimType at = queue.front();
  441. queue.pop();
  442. if (set(at))
  443. return;
  444. }
  445. set(HOLDING);
  446. }
  447. void CCreatureAnim::startPreview(bool warMachine)
  448. {
  449. callback = std::bind(&CCreatureAnim::loopPreview, this, warMachine);
  450. }
  451. void CCreatureAnim::clearAndSet(EAnimType type)
  452. {
  453. while (!queue.empty())
  454. queue.pop();
  455. set(type);
  456. }