1 // ============================================================================
2 // Artsoft Retro-Game Library
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
16 /* values for toon animation */
18 #define ANIM_CONTINUE 1
22 static struct ToonScreenInfo screen_info;
25 /* ========================================================================= */
26 /* generic animation frame calculation */
27 /* ========================================================================= */
29 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
34 sync_frame += start_frame * delay;
36 if (mode & ANIM_LOOP) /* looping animation */
38 frame = (sync_frame % (delay * num_frames)) / delay;
40 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
42 frame = sync_frame / delay;
44 if (frame > num_frames - 1)
45 frame = num_frames - 1;
47 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
49 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
51 frame = (sync_frame % (delay * max_anim_frames)) / delay;
52 frame = (frame < num_frames ? frame : max_anim_frames - frame);
54 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
56 int max_anim_frames = 2 * num_frames;
58 frame = (sync_frame % (delay * max_anim_frames)) / delay;
59 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
61 else if (mode & ANIM_RANDOM) /* play frames in random order */
63 /* note: expect different frames for the same delay cycle! */
65 if (gfx.anim_random_frame < 0)
66 frame = GetSimpleRandom(num_frames);
68 frame = gfx.anim_random_frame % num_frames;
70 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
72 frame = sync_frame % num_frames;
75 if (mode & ANIM_REVERSE) /* use reverse animation direction */
76 frame = num_frames - frame - 1;
82 /* ========================================================================= */
83 /* toon animation functions */
84 /* ========================================================================= */
86 static int get_toon_direction(char *direction_string_raw)
88 char *direction_string = getStringToLower(direction_string_raw);
89 int direction = (strEqual(direction_string, "left") ? MV_LEFT :
90 strEqual(direction_string, "right") ? MV_RIGHT :
91 strEqual(direction_string, "up") ? MV_UP :
92 strEqual(direction_string, "down") ? MV_DOWN :
95 free(direction_string);
100 void InitToonScreen(Bitmap *save_buffer,
101 void (*update_function)(void),
102 void (*prepare_backbuffer_function)(void),
103 boolean (*redraw_needed_function)(void),
104 struct ToonInfo *toons, int num_toons,
105 int startx, int starty,
106 int width, int height,
107 int frame_delay_value)
109 screen_info.save_buffer = save_buffer;
110 screen_info.update_function = update_function;
111 screen_info.prepare_backbuffer_function = prepare_backbuffer_function;
112 screen_info.redraw_needed_function = redraw_needed_function;
113 screen_info.toons = toons;
114 screen_info.num_toons = num_toons;
115 screen_info.startx = startx;
116 screen_info.starty = starty;
117 screen_info.width = width;
118 screen_info.height = height;
119 screen_info.frame_delay_value = frame_delay_value;
122 void DrawAnim(Bitmap *toon_bitmap, int src_x, int src_y, int width, int height,
123 int dest_x, int dest_y, int pad_x, int pad_y)
125 int pad_dest_x = dest_x - pad_x;
126 int pad_dest_y = dest_y - pad_y;
127 int pad_width = width + 2 * pad_x;
128 int pad_height = height + 2 * pad_y;
132 /* correct values to avoid off-screen blitting (start position) */
133 if (pad_dest_x < screen_info.startx)
135 pad_width -= (screen_info.startx - pad_dest_x);
136 pad_dest_x = screen_info.startx;
138 if (pad_dest_y < screen_info.starty)
140 pad_height -= (screen_info.starty - pad_dest_y);
141 pad_dest_y = screen_info.starty;
144 /* correct values to avoid off-screen blitting (blit size) */
145 if (pad_width > screen_info.width)
146 pad_width = screen_info.width;
147 if (pad_height > screen_info.height)
148 pad_height = screen_info.height;
150 /* special method to avoid flickering interference with BackToFront() */
151 BlitBitmap(backbuffer, screen_info.save_buffer, pad_dest_x, pad_dest_y,
152 pad_width, pad_height, buffer_x, buffer_y);
153 BlitBitmapMasked(toon_bitmap, backbuffer, src_x, src_y, width, height,
155 BlitBitmap(backbuffer, window, pad_dest_x, pad_dest_y, pad_width, pad_height,
156 pad_dest_x, pad_dest_y);
158 screen_info.update_function();
160 BlitBitmap(screen_info.save_buffer, backbuffer, buffer_x, buffer_y,
161 pad_width, pad_height, pad_dest_x, pad_dest_y);
163 /* prevent immediate redraw of restored toon area in backbuffer */
164 redraw_mask = REDRAW_NONE;
167 boolean AnimateToon(int toon_nr, boolean restart)
169 static unsigned int animation_frame_counter = 0;
170 static int pos_x = 0, pos_y = 0;
171 static int delta_x = 0, delta_y = 0;
172 static int frame = 0;
173 static boolean horiz_move, vert_move;
174 static unsigned int anim_delay = 0;
175 static unsigned int anim_delay_value = 0;
176 static int width,height;
177 static int pad_x,pad_y;
178 static int cut_x,cut_y;
179 static int src_x, src_y;
180 static int dest_x, dest_y;
181 struct ToonInfo *anim = &screen_info.toons[toon_nr];
182 Bitmap *anim_bitmap = screen_info.toons[toon_nr].bitmap;
183 int direction = get_toon_direction(anim->direction);
187 horiz_move = (direction & (MV_LEFT | MV_RIGHT));
188 vert_move = (direction & (MV_UP | MV_DOWN));
189 anim_delay_value = anim->step_delay * screen_info.frame_delay_value;
191 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
192 anim->anim_mode, anim->anim_start_frame,
193 animation_frame_counter++);
197 int pos_bottom = screen_info.height - anim->height;
199 if (strEqual(anim->position, "top"))
201 else if (strEqual(anim->position, "bottom"))
203 else if (strEqual(anim->position, "upper"))
204 pos_y = GetSimpleRandom(pos_bottom / 2);
205 else if (strEqual(anim->position, "lower"))
206 pos_y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
208 pos_y = GetSimpleRandom(pos_bottom);
210 if (direction == MV_RIGHT)
212 delta_x = anim->step_offset;
213 pos_x = -anim->width + delta_x;
217 delta_x = -anim->step_offset;
218 pos_x = screen_info.width + delta_x;
225 int pos_right = screen_info.width - anim->width;
227 if (strEqual(anim->position, "left"))
229 else if (strEqual(anim->position, "right"))
232 pos_x = GetSimpleRandom(pos_right);
234 if (direction == MV_DOWN)
236 delta_y = anim->step_offset;
237 pos_y = -anim->height + delta_y;
241 delta_y = -anim->step_offset;
242 pos_y = screen_info.height + delta_y;
249 if (pos_x <= -anim->width - anim->step_offset ||
250 pos_x >= screen_info.width + anim->step_offset ||
251 pos_y <= -anim->height - anim->step_offset ||
252 pos_y >= screen_info.height + anim->step_offset)
255 if (!DelayReached(&anim_delay, anim_delay_value))
257 if (screen_info.redraw_needed_function() && !restart)
258 DrawAnim(anim_bitmap,
259 src_x + cut_x, src_y + cut_y,
261 screen_info.startx + dest_x,
262 screen_info.starty + dest_y,
268 if (pos_x < -anim->width)
269 pos_x = -anim->width;
270 else if (pos_x > screen_info.width)
271 pos_x = screen_info.width;
272 if (pos_y < -anim->height)
273 pos_y = -anim->height;
274 else if (pos_y > screen_info.height)
275 pos_y = screen_info.height;
277 pad_x = (horiz_move ? anim->step_offset : 0);
278 pad_y = (vert_move ? anim->step_offset : 0);
279 src_x = anim->src_x + frame * anim->width;
285 height = anim->height;
293 else if (pos_x > screen_info.width - anim->width)
294 width -= (pos_x - (screen_info.width - anim->width));
302 else if (pos_y > screen_info.height - anim->height)
303 height -= (pos_y - (screen_info.height - anim->height));
305 DrawAnim(anim_bitmap,
306 src_x + cut_x, src_y + cut_y,
308 screen_info.startx + dest_x,
309 screen_info.starty + dest_y,
315 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
316 anim->anim_mode, anim->anim_start_frame,
317 animation_frame_counter++);
322 void HandleAnimation(int mode)
324 static unsigned int animstart_delay = -1;
325 static unsigned int animstart_delay_value = 0;
326 static boolean anim_running = FALSE;
327 static boolean anim_restart = TRUE;
328 static boolean reset_delay = TRUE;
329 static int toon_nr = 0;
331 if (!setup.toons || screen_info.num_toons == 0)
334 /* this may happen after reloading graphics and redefining "num_toons" */
335 if (toon_nr >= screen_info.num_toons)
341 screen_info.prepare_backbuffer_function();
358 redraw_mask |= REDRAW_FIELD;
360 screen_info.update_function();
362 anim_running = FALSE;
373 animstart_delay = Counter();
374 animstart_delay_value = GetSimpleRandom(3000);
380 if (!DelayReached(&animstart_delay, animstart_delay_value))
383 toon_nr = GetSimpleRandom(screen_info.num_toons);
386 anim_restart = reset_delay = AnimateToon(toon_nr, anim_restart);
391 HandleAnimation(ANIM_START);
396 HandleAnimation(ANIM_STOP);
401 HandleAnimation(ANIM_CONTINUE);