|
1 | 1 | #include "include/animation.h" |
2 | 2 |
|
3 | 3 | #include "include/framerate.h" |
| 4 | +#include "include/log.h" |
| 5 | +#include "include/datastructures.h" |
| 6 | + |
| 7 | +static struct { |
| 8 | + int mIsPaused; |
| 9 | + |
| 10 | +} gData; |
| 11 | + |
| 12 | +int getDurationInFrames(Duration tDuration){ |
| 13 | + return tDuration * getInverseFramerateFactor(); |
| 14 | +} |
4 | 15 |
|
5 | 16 | int handleDurationAndCheckIfOver(Duration* tNow, Duration tDuration) { |
| 17 | + if(gData.mIsPaused) return 0; |
6 | 18 | (*tNow)++; |
7 | | - if ((*tNow) >= (tDuration * getInverseFramerateFactor())) { |
| 19 | + if ((*tNow) >= getDurationInFrames(tDuration)) { |
8 | 20 | return 1; |
9 | 21 | } |
10 | 22 |
|
11 | 23 | return 0; |
12 | 24 | } |
13 | 25 |
|
| 26 | +AnimationResult animateWithoutLoop(Animation* tAnimation) { |
| 27 | + AnimationResult ret = ANIMATION_CONTINUING; |
| 28 | + if (handleDurationAndCheckIfOver(&tAnimation->mNow, tAnimation->mDuration)) { |
| 29 | + tAnimation->mNow = 0; |
| 30 | + tAnimation->mFrame++; |
| 31 | + if (tAnimation->mFrame >= tAnimation->mFrameAmount) { |
| 32 | + tAnimation->mFrame = tAnimation->mFrameAmount-1; |
| 33 | + tAnimation->mNow = getDurationInFrames(tAnimation->mDuration); |
| 34 | + ret = ANIMATION_OVER; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + return ret; |
| 39 | +} |
| 40 | + |
14 | 41 | void animate(Animation* tAnimation) { |
15 | | - if (handleDurationAndCheckIfOver(&tAnimation->mNow, tAnimation->mDuration)) { |
16 | | - tAnimation->mNow = 0; |
17 | | - tAnimation->mFrame++; |
18 | | - if (tAnimation->mFrame >= tAnimation->mFrameAmount) { |
19 | | - tAnimation->mFrame = 0; |
20 | | - } |
21 | | - } |
| 42 | + AnimationResult ret = animateWithoutLoop(tAnimation); |
| 43 | + if(ret == ANIMATION_OVER){ |
| 44 | + resetAnimation(tAnimation); |
| 45 | + } |
22 | 46 | } |
23 | 47 |
|
| 48 | + |
| 49 | + |
24 | 50 | void resetAnimation(Animation* tAnimation) { |
25 | 51 | tAnimation->mNow = 0; |
26 | 52 | tAnimation->mFrame = 0; |
27 | 53 | } |
| 54 | + |
| 55 | +Animation createEmptyAnimation(){ |
| 56 | + Animation ret; |
| 57 | + ret.mFrame = 0; |
| 58 | + ret.mFrameAmount = 0; |
| 59 | + ret.mNow = 0; |
| 60 | + ret.mDuration = 1000000000; |
| 61 | + return ret; |
| 62 | +} |
| 63 | + |
| 64 | + |
| 65 | +Animation createOneFrameAnimation(){ |
| 66 | + Animation ret = createEmptyAnimation(); |
| 67 | + ret.mFrameAmount = 1; |
| 68 | + return ret; |
| 69 | +} |
| 70 | + |
| 71 | + |
| 72 | +void pauseDurationHandling() { |
| 73 | + gData.mIsPaused = 1; |
| 74 | +} |
| 75 | +void resumeDurationHandling() { |
| 76 | + gData.mIsPaused = 0; |
| 77 | +} |
| 78 | + |
| 79 | +typedef struct AnimationElement_internal { |
| 80 | + |
| 81 | + void* mCaller; |
| 82 | + AnimationPlayerCB mCB; |
| 83 | + |
| 84 | + Position mPosition; |
| 85 | + Rectangle mTexturePosition; |
| 86 | + TextureData* mTextureData; |
| 87 | + Animation mAnimation; |
| 88 | + int mIsLooped; |
| 89 | + |
| 90 | + struct AnimationElement_internal* mPrev; |
| 91 | + struct AnimationElement_internal* mNext; |
| 92 | + |
| 93 | +} AnimationElement; |
| 94 | + |
| 95 | +typedef struct { |
| 96 | + int mSize; |
| 97 | + AnimationElement* mFirst; |
| 98 | + AnimationElement* mLast; |
| 99 | +} AnimationList; |
| 100 | + |
| 101 | +static struct{ |
| 102 | + List mList; |
| 103 | + |
| 104 | +} gAnimationHandler; |
| 105 | + |
| 106 | +void setupAnimationHandler(){ |
| 107 | + if(list_size(&gAnimationHandler.mList) > 0){ |
| 108 | + logWarning("Setting up non-empty animation handler; Cleaning up."); |
| 109 | + shutdownAnimationHandler(); |
| 110 | + } |
| 111 | + |
| 112 | + gAnimationHandler.mList = new_list(); |
| 113 | +} |
| 114 | + |
| 115 | +static int updateAndRemoveCB(void* tCaller, void* tData) { |
| 116 | + (void) tCaller; |
| 117 | + AnimationElement* cur = tData; |
| 118 | + AnimationResult res = animateWithoutLoop(&cur->mAnimation); |
| 119 | + if(res == ANIMATION_OVER) { |
| 120 | + if(cur->mIsLooped) { |
| 121 | + resetAnimation(&cur->mAnimation); |
| 122 | + } else { |
| 123 | + if(cur->mCB != NULL) { |
| 124 | + cur->mCB(cur->mCaller); |
| 125 | + } |
| 126 | + return 1; |
| 127 | + } |
| 128 | + } |
| 129 | + return 0; |
| 130 | +} |
| 131 | + |
| 132 | +void updateAnimationHandler(){ |
| 133 | + list_remove_predicate(&gAnimationHandler.mList, updateAndRemoveCB, NULL); |
| 134 | +} |
| 135 | + |
| 136 | +static void drawAnimationHandlerCB(void* tCaller, void* tData) { |
| 137 | + (void) tCaller; |
| 138 | + AnimationElement* cur = tData; |
| 139 | + int frame = cur->mAnimation.mFrame; |
| 140 | + drawSprite(cur->mTextureData[frame], cur->mPosition, cur->mTexturePosition); |
| 141 | +} |
| 142 | + |
| 143 | +void drawHandledAnimations() { |
| 144 | + list_map(&gAnimationHandler.mList, drawAnimationHandlerCB, NULL); |
| 145 | +} |
| 146 | + |
| 147 | +static void emptyAnimationHandler(){ |
| 148 | + list_empty(&gAnimationHandler.mList); |
| 149 | +} |
| 150 | + |
| 151 | +static int playAnimationInternal(Position tPosition, TextureData* tTextures, Animation tAnimation, Rectangle tTexturePosition, AnimationPlayerCB tOptionalCB, void* tCaller, int tIsLooped){ |
| 152 | + |
| 153 | + AnimationElement* e = malloc(sizeof(AnimationElement)); |
| 154 | + e->mCaller = tCaller; |
| 155 | + e->mCB = tOptionalCB; |
| 156 | + e->mIsLooped = tIsLooped; |
| 157 | + |
| 158 | + e->mPosition = tPosition; |
| 159 | + e->mTexturePosition = tTexturePosition; |
| 160 | + e->mTextureData = tTextures; |
| 161 | + e->mAnimation = tAnimation; |
| 162 | + |
| 163 | + return list_push_front_owned(&gAnimationHandler.mList, (void*)e); |
| 164 | +} |
| 165 | + |
| 166 | + |
| 167 | +int playAnimation(Position tPosition, TextureData* tTextures, Animation tAnimation, Rectangle tTexturePosition, AnimationPlayerCB tOptionalCB, void* tCaller){ |
| 168 | + return playAnimationInternal(tPosition, tTextures, tAnimation, tTexturePosition, tOptionalCB, tCaller, 0); |
| 169 | + |
| 170 | +} |
| 171 | + |
| 172 | +int playAnimationLoop(Position tPosition, TextureData* tTextures, Animation tAnimation, Rectangle tTexturePosition){ |
| 173 | + return playAnimationInternal(tPosition, tTextures, tAnimation, tTexturePosition, NULL, NULL, 1); |
| 174 | +} |
| 175 | + |
| 176 | +void removeHandledAnimation(int tID) { |
| 177 | + list_remove(&gAnimationHandler.mList, tID); |
| 178 | +} |
| 179 | + |
| 180 | +void shutdownAnimationHandler(){ |
| 181 | + emptyAnimationHandler(); |
| 182 | +} |
0 commit comments