2
0

CFadeAnimation.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * CAnimation.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 "CAnimation.h"
  12. #include "SDL_Extensions.h"
  13. #include "ColorFilter.h"
  14. #include "../CBitmapHandler.h"
  15. #include "../Graphics.h"
  16. #include "../../lib/filesystem/Filesystem.h"
  17. #include "../../lib/filesystem/ISimpleResourceLoader.h"
  18. #include "../../lib/JsonNode.h"
  19. #include "../../lib/CRandomGenerator.h"
  20. #include "../../lib/vcmi_endian.h"
  21. #include <SDL_surface.h>
  22. float CFadeAnimation::initialCounter() const
  23. {
  24. if (fadingMode == EMode::OUT)
  25. return 1.0f;
  26. return 0.0f;
  27. }
  28. void CFadeAnimation::update()
  29. {
  30. if (!fading)
  31. return;
  32. if (fadingMode == EMode::OUT)
  33. fadingCounter -= delta;
  34. else
  35. fadingCounter += delta;
  36. if (isFinished())
  37. {
  38. fading = false;
  39. if (shouldFreeSurface)
  40. {
  41. SDL_FreeSurface(fadingSurface);
  42. fadingSurface = nullptr;
  43. }
  44. }
  45. }
  46. bool CFadeAnimation::isFinished() const
  47. {
  48. if (fadingMode == EMode::OUT)
  49. return fadingCounter <= 0.0f;
  50. return fadingCounter >= 1.0f;
  51. }
  52. CFadeAnimation::CFadeAnimation()
  53. : delta(0), fadingSurface(nullptr), fading(false), fadingCounter(0), shouldFreeSurface(false),
  54. fadingMode(EMode::NONE)
  55. {
  56. }
  57. CFadeAnimation::~CFadeAnimation()
  58. {
  59. if (fadingSurface && shouldFreeSurface)
  60. SDL_FreeSurface(fadingSurface);
  61. }
  62. void CFadeAnimation::init(EMode mode, SDL_Surface * sourceSurface, bool freeSurfaceAtEnd, float animDelta)
  63. {
  64. if (fading)
  65. {
  66. // in that case, immediately finish the previous fade
  67. // (alternatively, we could just return here to ignore the new fade request until this one finished (but we'd need to free the passed bitmap to avoid leaks))
  68. logGlobal->warn("Tried to init fading animation that is already running.");
  69. if (fadingSurface && shouldFreeSurface)
  70. SDL_FreeSurface(fadingSurface);
  71. }
  72. if (animDelta <= 0.0f)
  73. {
  74. logGlobal->warn("Fade anim: delta should be positive; %f given.", animDelta);
  75. animDelta = DEFAULT_DELTA;
  76. }
  77. if (sourceSurface)
  78. fadingSurface = sourceSurface;
  79. delta = animDelta;
  80. fadingMode = mode;
  81. fadingCounter = initialCounter();
  82. fading = true;
  83. shouldFreeSurface = freeSurfaceAtEnd;
  84. }
  85. void CFadeAnimation::draw(SDL_Surface * targetSurface, const Point &targetPoint)
  86. {
  87. if (!fading || !fadingSurface || fadingMode == EMode::NONE)
  88. {
  89. fading = false;
  90. return;
  91. }
  92. CSDL_Ext::setAlpha(fadingSurface, (int)(fadingCounter * 255));
  93. CSDL_Ext::blitSurface(fadingSurface, targetSurface, targetPoint); //FIXME
  94. CSDL_Ext::setAlpha(fadingSurface, 255);
  95. }