Debian Codegraph @

terps/plus/animations.c

raw symbols SCIP
1 //
2 // animations.c
3 // Part of Plus, an interpreter for Scott Adams Graphic Adventures Plus
4 //
5 // Created by Petter Sjölund on 2022-03-24.
6 //
7
8 #include <stdlib.h>
9 #include <string.h>
10
11 #include "common.h"
12 #include "definitions.h"
13 #include "glk.h"
14 #include "graphics.h"
15
16 #include "animations.h"
17
18 #define ANIMATION_RATE 200
19 #define CANNON_RATE 300
20 #define MAX_ANIM_FRAMES 32
21
22 int AnimationRunning = 0;
23 int AnimationBackground = 0;
24 int LastAnimationBackground = 0;
25
26 int AnimationRoom = -1;
27 glui32 AnimTimerRate = 0;
28
29 static int AnimationStage = 0;
30 static int StopNext = 0;
31 static int ImgTail = 0;
32 static int FramesTail = 0;
33
34 static char *AnimationFilenames[MAX_ANIM_FRAMES];
35 static int AnimationFrames[MAX_ANIM_FRAMES];
36
37 /*
38 The "fire cannon at blob" animation in Fantastic Four is actually two
39 animations sequences, one after another, which conflicts with our
40 animation system, so we use a hack to stitch them together, basically
41 adding the new frames to the end of the buffer rather than clearing it
42 and starting over.
43 */
44
45 static int PostCannonAnimationSeam = 0;
46 static int CannonAnimationPause = 0;
47
48 /*
49 The Atari ST version of Spider-Man only uses a single picture with color cycling for the "shooting web" animation, so we use a hack to ignore attempts to draw the other frames and instead wait exactly one color cycle.
50 */
51 int STWebAnimation = 0;
52 int STWebAnimationFinished = 1;
53
54 void AddImageToBuffer(char *basename)
55 {
56 if (ImgTail >= MAX_ANIM_FRAMES - 1)
57 return;
58 size_t len = strlen(basename) + 1;
59 if (AnimationFilenames[ImgTail] != NULL)
60 free(AnimationFilenames[ImgTail]);
61 AnimationFilenames[ImgTail] = MemAlloc(len);
62 debug_print("AddImageToBuffer: Setting AnimationFilenames[%d] to %s\n", ImgTail, basename);
63 memcpy(AnimationFilenames[ImgTail++], basename, len);
64 AnimationFilenames[ImgTail] = NULL;
65 }
66
67 void AddFrameToBuffer(int frameidx)
68 {
69 if (FramesTail >= MAX_ANIM_FRAMES - 1)
70 return;
71 debug_print("AddFrameToBuffer: Setting AnimationFrames[%d] to %d\n", FramesTail, frameidx);
72 AnimationFrames[FramesTail++] = frameidx;
73 AnimationFrames[FramesTail] = -1;
74 }
75
76 void AddRoomImage(int image)
77 {
78 char *shortname = ShortNameFromType('R', image);
79 AddImageToBuffer(shortname);
80 free(shortname);
81 STWebAnimation = 0;
82 }
83
84 void AddItemImage(int image)
85 {
86 char *shortname = ShortNameFromType('B', image);
87 AddImageToBuffer(shortname);
88 free(shortname);
89 STWebAnimation = 0;
90 }
91
92 void AddSpecialImage(int image)
93 {
94
95 if (CurrentSys == SYS_ST && CurrentGame == SPIDERMAN) {
96 if (image == 14) {
97 image = 18;
98 STWebAnimation = 1;
99 STWebAnimationFinished = 1;
100 } else if (image > 14 && image < 19) {
101 return;
102 } else {
103 STWebAnimation = 0;
104 }
105 }
106
107 char *shortname = ShortNameFromType('S', image);
108 AddImageToBuffer(shortname);
109 free(shortname);
110 }
111
112 void SetAnimationTimer(glui32 milliseconds)
113 {
114 debug_print("SetAnimationTimer %d\n", milliseconds);
115 AnimTimerRate = milliseconds;
116 if (milliseconds == 0 && ColorCyclingRunning)
117 return;
118 if (TimerRate == 0 || TimerRate > milliseconds) {
119 debug_print("Actually setting animation timer\n");
120 SetTimer(milliseconds);
121 }
122 }
123
124 void Animate(int frame)
125 {
126 if (Images[0].Filename == NULL)
127 return;
128
129 int cannonanimation = (CurrentGame == FANTASTIC4 && MyLoc == 5);
130
131 if (PostCannonAnimationSeam) {
132 AddFrameToBuffer(10 + frame);
133 return;
134 }
135
136 if (STWebAnimation && frame > 0)
137 return;
138
139 AddFrameToBuffer(frame);
140 if (!AnimationRunning) {
141
142 if (!AnimationBackground)
143 LastAnimationBackground = 0;
144
145 if (cannonanimation)
146 SetAnimationTimer(CANNON_RATE);
147 else
148 SetAnimationTimer(ANIMATION_RATE);
149
150 AnimationRunning = 1;
151 AnimationStage = 0;
152 AnimationRoom = MyLoc;
153 }
154
155 if (cannonanimation && frame == 9)
156 PostCannonAnimationSeam = 1;
157 }
158
159 void ClearAnimationBuffer(void)
160 {
161
162 if (PostCannonAnimationSeam)
163 return;
164
165 for (int i = 0; i < MAX_ANIM_FRAMES && AnimationFilenames[i] != NULL; i++) {
166 if (AnimationFilenames[i] != NULL)
167 free(AnimationFilenames[i]);
168 AnimationFilenames[i] = NULL;
169 }
170 ImgTail = 0;
171 FramesTail = 0;
172 }
173
174 void ClearFrames(void)
175 {
176 for (int i = 0; i < FramesTail; i++) {
177 AnimationFrames[i] = -1;
178 }
179 FramesTail = 0;
180 }
181
182 void InitAnimationBuffer(void)
183 {
184 for (int i = 0; i < MAX_ANIM_FRAMES; i++) {
185 AnimationFilenames[i] = NULL;
186 AnimationFrames[i] = -1;
187 }
188 }
189
190 void StopAnimation(void)
191 {
192 SetAnimationTimer(0);
193 debug_print("StopAnimation: stopped timer\n");
194 AnimationStage = 0;
195 AnimationRunning = 0;
196 PostCannonAnimationSeam = 0;
197 AnimationRoom = -1;
198 StopNext = 0;
199 }
200
201 void DrawApple2ImageFromVideoMem(void);
202 void DrawBlack(void);
203
204 void UpdateAnimation(void) // Draw animation frame
205 {
206 if (CannonAnimationPause) {
207 SetAnimationTimer(CANNON_RATE);
208 CannonAnimationPause = 0;
209 }
210
211 if (STWebAnimation) {
212 if (!STWebAnimationFinished) {
213 if (AnimTimerRate != 25)
214 SetAnimationTimer(25);
215 return;
216 } else {
217 STWebAnimationFinished = 0;
218 }
219 }
220
221 if (StopNext) {
222 StopNext = 0;
223 if (AnimationFrames[AnimationStage] == -1)
224 ClearFrames();
225 StopAnimation();
226
227 if (CurrentGame == BANZAI && MyLoc == 29 && IsSet(6)) {
228 DrawBlack();
229 } else if (!showing_inventory) {
230 glk_window_clear(Graphics);
231 DrawCurrentRoom();
232 if (CurrentGame == SPIDERMAN && MyLoc == 5) {
233 DrawItemImage(23);
234 if (CurrentSys == SYS_APPLE2)
235 DrawApple2ImageFromVideoMem();
236 }
237 }
238
239 return;
240 }
241
242 if (AnimationStage >= MAX_ANIM_FRAMES - 1 || AnimationRoom != MyLoc || !IsSet(GRAPHICSBIT)) {
243 StopNext = 1;
244 return;
245 }
246
247 if (AnimationBackground) {
248 char buf[5];
249 snprintf(buf, sizeof buf, "S0%02d", AnimationBackground);
250 LastAnimationBackground = AnimationBackground;
251 AnimationBackground = 0;
252 DrawImageWithName(buf);
253 return;
254 }
255
256 if (AnimationFrames[AnimationStage] != -1) {
257 debug_print("UpdateAnimation: Drawing AnimationFrames[%d] (%d)\n", AnimationStage, AnimationFrames[AnimationStage]);
258 if (!DrawImageWithName(AnimationFilenames[AnimationFrames[AnimationStage++]]))
259 StopNext = 1;
260 else if (CurrentSys == SYS_APPLE2)
261 DrawApple2ImageFromVideoMem();
262
263 if (PostCannonAnimationSeam && AnimationStage == 10) {
264 CannonAnimationPause = 1;
265 SetAnimationTimer(2000);
266 }
267
268 if (STWebAnimation) {
269 StopNext = 1;
270 }
271
272 } else {
273 debug_print("StopNext\n");
274 SetAnimationTimer(2000);
275 StopNext = 1;
276 }
277 }