PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ZE_ZAnimation.cpp

https://github.com/jamesturk/zengine
C++ | 152 lines | 124 code | 18 blank | 10 comment | 16 complexity | 18d5ab7393ec3bebbec2506d9fbd8dca MD5 | raw file
  1. /*******************************************************************************
  2. This file is Part of the ZEngine Library for 2D game development.
  3. Copyright (C) 2002-2004 James Turk
  4. Licensed under a BSD-style license.
  5. The maintainer of this library is James Turk (james@conceptofzero.net)
  6. and the home of this Library is http://www.zengine.sourceforge.net
  7. *******************************************************************************/
  8. #include "ZE_ZAnimation.h"
  9. namespace ZE
  10. {
  11. ZAnimation::ZAnimation() :
  12. rEngine(ZEngine::GetInstance()),
  13. rAnimImages(NULL),
  14. rCurFrame(0),rNumFrames(0),rFrameStep(0),
  15. rFrameDelay(0),rNextFrameTime(0),
  16. rAnimType(ZANIM_NONE), rBackwards(false)
  17. {
  18. }
  19. ZAnimation::ZAnimation(ZImage *images, int numFrames, Uint32 frameDelay, ZAnimType type, bool backwards)
  20. {
  21. rEngine = ZEngine::GetInstance();
  22. Create(images,numFrames,frameDelay,type,backwards);
  23. }
  24. ZAnimation::~ZAnimation()
  25. {
  26. }
  27. void ZAnimation::Create(ZImage *images, int numFrames, Uint32 frameDelay, ZAnimType type, bool backwards)
  28. {
  29. if(!images)
  30. rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::Create with NULL images parameter.");
  31. rAnimImages = images;
  32. rNumFrames = numFrames;
  33. rFrameDelay = frameDelay;
  34. rAnimType = type;
  35. rBackwards = backwards;
  36. Reset();
  37. }
  38. void ZAnimation::SetAnimImages(ZImage *images, int numFrames)
  39. {
  40. if(!images)
  41. rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::SetAnimImages with NULL images parameter.");
  42. rAnimImages = images;
  43. rNumFrames = numFrames;
  44. }
  45. void ZAnimation::SetFrameDelay(Uint32 frameDelay)
  46. {
  47. rFrameDelay = frameDelay;
  48. }
  49. void ZAnimation::SetAnimType(ZAnimType type, bool backwards)
  50. {
  51. rAnimType = type;
  52. rBackwards = backwards;
  53. }
  54. void ZAnimation::Reset()
  55. {
  56. rFrameStep = 0;
  57. rCurFrame = rBackwards ? rNumFrames-1 : 0;
  58. }
  59. void ZAnimation::Start()
  60. {
  61. if(rAnimImages)
  62. {
  63. if(rAnimType != ZANIM_NONE)
  64. rFrameStep = rBackwards ? -1 : 1;
  65. else
  66. rFrameStep = 0;
  67. rNextFrameTime = rEngine->GetTime()+rFrameDelay;
  68. }
  69. else
  70. {
  71. rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::Start with no images loaded.");
  72. }
  73. }
  74. void ZAnimation::Pause()
  75. {
  76. rFrameStep = 0;
  77. }
  78. void ZAnimation::SetFrame(int frame)
  79. {
  80. if(frame >= 0 && frame < rNumFrames)
  81. rCurFrame = frame;
  82. else if(frame < 0 && frame >= -rNumFrames)
  83. SetFrame(rNumFrames+frame);
  84. else
  85. {
  86. rEngine->ReportError(ZERR_WARNING,"Attempt to set frame to %d in ZAnimation::SetFrame, valid range is %d to %d.",frame,-rNumFrames,rNumFrames-1);
  87. }
  88. }
  89. void ZAnimation::Update()
  90. {
  91. //if not paused/stopped and time to update has come
  92. if(rFrameStep && rEngine->GetTime() >= rNextFrameTime)
  93. {
  94. rCurFrame += rFrameStep;
  95. if(rCurFrame < 0 || rCurFrame >= rNumFrames)
  96. {
  97. switch(rAnimType)
  98. {
  99. case ZANIM_ONCE:
  100. Pause();
  101. rCurFrame = rBackwards ? 0 : rNumFrames-1;
  102. break;
  103. case ZANIM_LOOP:
  104. rCurFrame = rBackwards ? rNumFrames-1 : 0;
  105. break;
  106. case ZANIM_REVERSE:
  107. rBackwards = !rBackwards;
  108. rCurFrame = rBackwards ? rNumFrames-2 : 1; //second to last or second frame
  109. rFrameStep *= -1;
  110. break;
  111. case ZANIM_NONE:
  112. default:
  113. Reset();
  114. rEngine->ReportError(ZERR_ERROR,"Unknown error: Invalid Enum [%d] in ZAnimation::Update",static_cast<int>(rAnimType));
  115. break;
  116. }
  117. }
  118. rNextFrameTime = rEngine->GetTime()+rFrameDelay;
  119. }
  120. }
  121. void ZAnimation::Draw(float x, float y) const
  122. {
  123. if(rAnimImages)
  124. rAnimImages[rCurFrame].Draw(x,y);
  125. else
  126. rEngine->ReportError(ZERR_WARNING,"Called ZAnimation::Draw with no images loaded.");
  127. }
  128. bool ZAnimation::IsRunning() const
  129. {
  130. return rFrameStep != 0;
  131. }
  132. }