1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #include "libgame/libgame.h"
22 #define DEBUG_ANIM_DELAY 0
23 #define DEBUG_ANIM_EVENTS 0
26 // values for global toon animation definition
27 #define NUM_GLOBAL_TOON_ANIMS 1
28 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
30 // values for global animation definition (including toons)
31 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
32 NUM_GLOBAL_TOON_ANIMS)
33 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
34 NUM_GLOBAL_TOON_PARTS)
36 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
37 #define ANIM_CLASS_BIT_TITLE 1
38 #define ANIM_CLASS_BIT_MAIN 2
39 #define ANIM_CLASS_BIT_SCORES 3
40 #define ANIM_CLASS_BIT_SUBMENU 4
41 #define ANIM_CLASS_BIT_MENU 5
42 #define ANIM_CLASS_BIT_TOONS 6
43 #define ANIM_CLASS_BIT_NO_TITLE 7
45 #define NUM_ANIM_CLASSES 8
47 #define ANIM_CLASS_NONE 0
48 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
49 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
50 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
51 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
52 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
53 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
54 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
55 #define ANIM_CLASS_NO_TITLE (1 << ANIM_CLASS_BIT_NO_TITLE)
57 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
61 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
66 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
68 ANIM_CLASS_SUBMENU | \
71 // values for global animation states
72 #define ANIM_STATE_INACTIVE 0
73 #define ANIM_STATE_RESTART (1 << 0)
74 #define ANIM_STATE_WAITING (1 << 1)
75 #define ANIM_STATE_RUNNING (1 << 2)
77 // values for global animation control
78 #define ANIM_NO_ACTION 0
80 #define ANIM_CONTINUE 2
84 struct GlobalAnimPartControlInfo
86 int old_nr; // position before mapping animation parts linearly
87 int old_anim_nr; // position before mapping animations linearly
93 boolean is_base; // animation part is base/main/default animation part
99 struct GraphicInfo graphic_info;
100 struct GraphicInfo control_info;
108 int step_xoffset, step_yoffset;
110 unsigned int initial_anim_sync_frame;
111 unsigned int anim_random_frame;
112 unsigned int step_delay, step_delay_value;
114 int init_delay_counter;
115 int anim_delay_counter;
116 int post_delay_counter;
118 boolean init_event_state;
119 boolean anim_event_state;
128 int last_anim_status;
131 struct GlobalAnimMainControlInfo
133 struct GlobalAnimPartControlInfo base;
134 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
139 struct GraphicInfo control_info;
141 int num_parts; // number of animation parts, but without base part
142 int num_parts_all; // number of animation parts, including base part
146 boolean has_base; // animation has base/main/default animation part
150 int init_delay_counter;
154 int last_state, last_active_part_nr;
157 struct GlobalAnimControlInfo
159 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
165 struct GameModeAnimClass
169 } game_mode_anim_classes_list[] =
171 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
172 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
173 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
174 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
175 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
176 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
177 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
178 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
179 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
180 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
181 { GAME_MODE_NAMES, ANIM_CLASS_TOONS_MENU_SUBMENU },
182 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
183 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
184 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
185 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
186 { GAME_MODE_PSEUDO_NAMESONLY, ANIM_CLASS_TOONS_MENU_SUBMENU },
187 { GAME_MODE_PSEUDO_TYPENAMES, ANIM_CLASS_TOONS_MENU_SUBMENU },
188 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
189 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
190 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORES },
191 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORES },
192 { GAME_MODE_EDITOR, ANIM_CLASS_NO_TITLE },
193 { GAME_MODE_PLAYING, ANIM_CLASS_NO_TITLE },
198 struct AnimClassGameMode
202 } anim_class_game_modes_list[] =
204 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
205 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
206 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
207 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
208 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
209 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
210 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
211 { ANIM_CLASS_BIT_NO_TITLE, GAME_MODE_PSEUDO_NO_TITLE },
216 // forward declaration for internal use
217 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
218 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
219 static void HandleGlobalAnim(int, int);
220 static void DoAnimationExt(void);
221 static void ResetGlobalAnim_Clickable(void);
222 static void ResetGlobalAnim_Clicked(void);
224 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
226 static unsigned int anim_sync_frame = 0;
228 static int game_mode_anim_classes[NUM_GAME_MODES];
229 static int anim_class_game_modes[NUM_ANIM_CLASSES];
231 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
232 static int anim_status_last = GAME_MODE_DEFAULT;
233 static int anim_classes_last = ANIM_CLASS_NONE;
235 static boolean drawing_to_fading_buffer = FALSE;
237 static boolean handle_click = FALSE;
240 // ============================================================================
241 // generic animation frame calculation
242 // ============================================================================
244 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
249 if (delay < 1) // delay must be at least 1
252 sync_frame += start_frame * delay;
254 if (mode & ANIM_LOOP) // looping animation
256 frame = (sync_frame % (delay * num_frames)) / delay;
258 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
260 frame = sync_frame / delay;
262 if (frame > num_frames - 1)
263 frame = num_frames - 1;
265 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
267 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
269 frame = (sync_frame % (delay * max_anim_frames)) / delay;
270 frame = (frame < num_frames ? frame : max_anim_frames - frame);
272 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
274 int max_anim_frames = 2 * num_frames;
276 frame = (sync_frame % (delay * max_anim_frames)) / delay;
277 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
279 else if (mode & ANIM_RANDOM) // play frames in random order
281 // note: expect different frames for the same delay cycle!
283 if (gfx.anim_random_frame < 0)
284 frame = GetSimpleRandom(num_frames);
286 frame = gfx.anim_random_frame % num_frames;
288 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
290 frame = sync_frame % num_frames;
293 if (mode & ANIM_REVERSE) // use reverse animation direction
294 frame = num_frames - frame - 1;
300 // ============================================================================
301 // global animation functions
302 // ============================================================================
304 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
306 struct GraphicInfo *c = &anim->control_info;
307 int last_anim_random_frame = gfx.anim_random_frame;
310 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
312 part_nr = getAnimationFrame(anim->num_parts, 1,
313 c->anim_mode, c->anim_start_frame,
316 gfx.anim_random_frame = last_anim_random_frame;
321 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
323 const struct GlobalAnimPartControlInfo *o1 =
324 (struct GlobalAnimPartControlInfo *)obj1;
325 const struct GlobalAnimPartControlInfo *o2 =
326 (struct GlobalAnimPartControlInfo *)obj2;
329 if (o1->control_info.draw_order != o2->control_info.draw_order)
330 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
332 compare_result = o1->nr - o2->nr;
334 return compare_result;
337 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
339 const struct GlobalAnimMainControlInfo *o1 =
340 (struct GlobalAnimMainControlInfo *)obj1;
341 const struct GlobalAnimMainControlInfo *o2 =
342 (struct GlobalAnimMainControlInfo *)obj2;
345 if (o1->control_info.draw_order != o2->control_info.draw_order)
346 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
348 compare_result = o1->nr - o2->nr;
350 return compare_result;
353 static void InitToonControls(void)
355 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
356 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
357 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
358 int mode_nr, anim_nr, part_nr;
359 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
360 int num_toons = MAX_NUM_TOONS;
363 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
364 num_toons = global.num_toons;
366 mode_nr = mode_nr_toons;
367 anim_nr = ctrl->num_anims;
370 anim->mode_nr = mode_nr;
371 anim->control_info = graphic_info[control];
374 anim->num_parts_all = 0;
375 anim->part_counter = 0;
376 anim->active_part_nr = 0;
378 anim->has_base = FALSE;
380 anim->last_x = POS_OFFSCREEN;
381 anim->last_y = POS_OFFSCREEN;
383 anim->init_delay_counter = 0;
385 anim->state = ANIM_STATE_INACTIVE;
389 for (i = 0; i < num_toons; i++)
391 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
392 int sound = SND_UNDEFINED;
393 int music = MUS_UNDEFINED;
394 int graphic = IMG_TOON_1 + i;
395 int control = graphic;
398 part->anim_nr = anim_nr;
399 part->mode_nr = mode_nr;
401 part->is_base = FALSE;
405 part->graphic = graphic;
407 part->graphic_info = graphic_info[graphic];
408 part->control_info = graphic_info[control];
410 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
412 part->control_info.init_delay_fixed = 0;
413 part->control_info.init_delay_random = 150;
415 part->control_info.x = ARG_UNDEFINED_VALUE;
416 part->control_info.y = ARG_UNDEFINED_VALUE;
418 part->initial_anim_sync_frame = 0;
419 part->anim_random_frame = -1;
421 part->step_delay = 0;
422 part->step_delay_value = graphic_info[control].step_delay;
424 part->state = ANIM_STATE_INACTIVE;
425 part->last_anim_status = -1;
428 anim->num_parts_all++;
436 static void InitGlobalAnimControls(void)
439 int mode_nr, anim_nr, part_nr;
440 int sound, music, graphic, control;
444 for (m = 0; m < NUM_GAME_MODES; m++)
448 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
455 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
457 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
458 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
460 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
462 // if no base animation parameters defined, use default values
463 if (control == IMG_UNDEFINED)
464 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
467 anim->mode_nr = mode_nr;
468 anim->control_info = graphic_info[control];
471 anim->num_parts_all = 0;
472 anim->part_counter = 0;
473 anim->active_part_nr = 0;
475 anim->has_base = FALSE;
477 anim->last_x = POS_OFFSCREEN;
478 anim->last_y = POS_OFFSCREEN;
480 anim->init_delay_counter = 0;
482 anim->state = ANIM_STATE_INACTIVE;
486 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
488 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
490 sound = global_anim_info[a].sound[p][m];
491 music = global_anim_info[a].music[p][m];
492 graphic = global_anim_info[a].graphic[p][m];
493 control = global_anim_info[ctrl_id].graphic[p][m];
495 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
496 control == IMG_UNDEFINED)
500 Debug("anim:InitGlobalAnimControls",
501 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
502 m, a, p, mode_nr, anim_nr, part_nr, control);
506 Debug("anim:InitGlobalAnimControls",
507 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
508 m, a, p, mode_nr, anim_nr, part_nr, sound);
512 part->old_anim_nr = a;
515 part->anim_nr = anim_nr;
516 part->mode_nr = mode_nr;
520 part->graphic = graphic;
522 part->graphic_info = graphic_info[graphic];
523 part->control_info = graphic_info[control];
525 part->initial_anim_sync_frame = 0;
526 part->anim_random_frame = -1;
528 part->step_delay = 0;
529 part->step_delay_value = graphic_info[control].step_delay;
531 part->state = ANIM_STATE_INACTIVE;
532 part->last_anim_status = -1;
534 anim->num_parts_all++;
536 if (p < GLOBAL_ANIM_ID_PART_BASE)
538 part->is_base = FALSE;
545 part->is_base = TRUE;
548 anim->has_base = TRUE;
551 // apply special settings for pointer-style animations
552 if (part->control_info.class == get_hash_from_key("pointer"))
554 // force animation to be on top (must set anim and part control)
555 if (anim->control_info.draw_order == 0)
556 anim->control_info.draw_order = 1000000;
557 if (part->control_info.draw_order == 0)
558 part->control_info.draw_order = 1000000;
560 // force animation to pass-through clicks (must set part control)
561 if (part->control_info.style == STYLE_DEFAULT)
562 part->control_info.style |= STYLE_PASSTHROUGH;
566 if (anim->num_parts > 0 || anim->has_base)
576 // sort all animations according to draw_order and animation number
577 for (m = 0; m < NUM_GAME_MODES; m++)
579 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
581 // sort all main animations for this game mode
582 qsort(ctrl->anim, ctrl->num_anims,
583 sizeof(struct GlobalAnimMainControlInfo),
584 compareGlobalAnimMainControlInfo);
586 for (a = 0; a < ctrl->num_anims; a++)
588 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
590 // sort all animation parts for this main animation
591 qsort(anim->part, anim->num_parts,
592 sizeof(struct GlobalAnimPartControlInfo),
593 compareGlobalAnimPartControlInfo);
597 for (i = 0; i < NUM_GAME_MODES; i++)
598 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
599 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
600 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
601 game_mode_anim_classes_list[i].class;
603 for (i = 0; i < NUM_ANIM_CLASSES; i++)
604 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
605 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
606 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
607 anim_class_game_modes_list[i].game_mode;
609 anim_status_last_before_fading = GAME_MODE_LOADING;
610 anim_status_last = GAME_MODE_LOADING;
611 anim_classes_last = ANIM_CLASS_NONE;
614 void InitGlobalAnimations(void)
616 InitGlobalAnimControls();
619 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
621 Bitmap *fade_bitmap =
622 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
623 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
624 int game_mode_anim_action[NUM_GAME_MODES];
630 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
631 drawing_target == DRAW_TO_SCREEN)
634 // always start with reliable default values (no animation actions)
635 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
636 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
638 if (global.anim_status != anim_status_last)
640 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
641 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
642 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
645 if (drawing_target == DRAW_TO_FADE_TARGET)
648 // special case: changing from/to these screens is done without fading
649 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
650 global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
651 anim_status_last == GAME_MODE_PSEUDO_TYPENAME ||
652 anim_status_last == GAME_MODE_PSEUDO_TYPENAMES)
655 // ---------- part 1 ------------------------------------------------------
656 // start or stop global animations by change of game mode
657 // (special handling of animations for "current screen" and "all screens")
659 if (global.anim_status_next != anim_status_last_before_fading)
661 // stop animations for last screen before fading to new screen
662 game_mode_anim_action[anim_status_last] = ANIM_STOP;
664 // start animations for current screen after fading to new screen
665 game_mode_anim_action[global.anim_status] = ANIM_START;
668 // start animations for all screens after loading new artwork set
669 if (anim_status_last == GAME_MODE_LOADING)
670 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
672 // ---------- part 2 ------------------------------------------------------
673 // start or stop global animations by change of animation class
674 // (generic handling of animations for "class of screens")
676 for (i = 0; i < NUM_ANIM_CLASSES; i++)
678 int anim_class_check = (1 << i);
679 int anim_class_game_mode = anim_class_game_modes[i];
680 int anim_class_last = anim_classes_last & anim_class_check;
681 int anim_class_next = anim_classes_next & anim_class_check;
683 // stop animations for changed screen class before fading to new screen
684 if (before_fading && anim_class_last && !anim_class_next)
685 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
687 // start animations for changed screen class after fading to new screen
688 if (after_fading && !anim_class_last && anim_class_next)
689 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
692 if (drawing_target == DRAW_TO_SCREEN)
696 anim_classes_last = anim_classes_next;
697 anim_status_last_before_fading = global.anim_status;
700 anim_status_last = global.anim_status;
702 // start or stop animations determined to be started or stopped above
703 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
704 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
705 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
707 else if (drawing_target == DRAW_TO_FADE_TARGET)
709 drawing_to_fading_buffer = TRUE;
711 // start animations determined to be (temporary) started above
712 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
713 if (game_mode_anim_action[mode_nr] == ANIM_START)
714 HandleGlobalAnim(ANIM_START, mode_nr);
718 if (global.anim_status == GAME_MODE_LOADING)
721 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
723 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
726 // when preparing source fading buffer, only draw animations to be stopped
727 if (drawing_target == DRAW_TO_FADE_SOURCE &&
728 game_mode_anim_action[mode_nr] != ANIM_STOP)
731 // when preparing target fading buffer, only draw animations to be started
732 if (drawing_target == DRAW_TO_FADE_TARGET &&
733 game_mode_anim_action[mode_nr] != ANIM_START)
737 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
738 mode_nr != game_status)
742 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
744 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
745 struct GraphicInfo *c = &anim->control_info;
746 int part_first, part_last;
749 if (!(anim->state & ANIM_STATE_RUNNING))
752 part_first = part_last = anim->active_part_nr;
754 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
756 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
759 part_last = num_parts - 1;
762 for (part_nr = part_first; part_nr <= part_last; part_nr++)
764 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
765 struct GraphicInfo *g = &part->graphic_info;
768 int width = g->width;
769 int height = g->height;
776 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
777 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
778 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
779 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
780 int last_anim_random_frame = gfx.anim_random_frame;
782 if (!(part->state & ANIM_STATE_RUNNING))
785 if (part->drawing_stage != drawing_stage)
794 else if (part->x > part->viewport_width - g->width)
795 width -= (part->x - (part->viewport_width - g->width));
803 else if (part->y > part->viewport_height - g->height)
804 height -= (part->y - (part->viewport_height - g->height));
806 if (width <= 0 || height <= 0)
809 dst_x += part->viewport_x;
810 dst_y += part->viewport_y;
812 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
814 // re-initialize random animation frame after animation delay
815 if (g->anim_mode == ANIM_RANDOM &&
816 sync_frame % g->anim_delay == 0 &&
818 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
820 gfx.anim_random_frame = part->anim_random_frame;
822 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
823 g->anim_mode, g->anim_start_frame,
826 gfx.anim_random_frame = last_anim_random_frame;
828 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
834 if (drawing_target == DRAW_TO_SCREEN)
835 blit_screen(src_bitmap, src_x, src_y, width, height,
838 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
844 if (drawing_target == DRAW_TO_FADE_TARGET)
846 // stop animations determined to be (temporary) started above
847 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
848 if (game_mode_anim_action[mode_nr] == ANIM_START)
849 HandleGlobalAnim(ANIM_STOP, mode_nr);
851 drawing_to_fading_buffer = FALSE;
855 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
857 int last_cursor_mode_override = gfx.cursor_mode_override;
859 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
861 ResetGlobalAnim_Clickable();
863 gfx.cursor_mode_override = CURSOR_UNDEFINED;
866 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
868 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
870 ResetGlobalAnim_Clicked();
873 DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
875 if (gfx.cursor_mode_override != last_cursor_mode_override)
876 SetMouseCursor(gfx.cursor_mode);
879 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
885 boolean changed = FALSE;
887 if (part->last_anim_status == global.anim_status &&
888 part->control_info.class != get_hash_from_key("pointer"))
891 part->last_anim_status = global.anim_status;
893 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
895 if (part->control_info.class == get_hash_from_key("window") ||
896 part->control_info.class == get_hash_from_key("border"))
900 viewport_width = WIN_XSIZE;
901 viewport_height = WIN_YSIZE;
903 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
905 else if (part->control_info.class == get_hash_from_key("pointer"))
907 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
908 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
910 // prevent displaying off-screen custom mouse cursor in upper left corner
911 if (gfx.mouse_x == POS_OFFSCREEN &&
912 gfx.mouse_y == POS_OFFSCREEN)
913 mx = my = POS_OFFSCREEN;
915 viewport_x = mx - part->control_info.x;
916 viewport_y = my - part->control_info.y;
917 viewport_width = part->graphic_info.width;
918 viewport_height = part->graphic_info.height;
920 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
922 // do not use global animation mouse pointer when reloading artwork
923 if (global.anim_status != GAME_MODE_LOADING)
924 gfx.cursor_mode_override = CURSOR_NONE;
926 else if (part->control_info.class == get_hash_from_key("door_1"))
930 viewport_width = DXSIZE;
931 viewport_height = DYSIZE;
933 else if (part->control_info.class == get_hash_from_key("door_2"))
935 if (part->mode_nr == GAME_MODE_EDITOR)
939 viewport_width = EXSIZE;
940 viewport_height = EYSIZE;
946 viewport_width = VXSIZE;
947 viewport_height = VYSIZE;
950 else // default: "playfield"
952 viewport_x = REAL_SX;
953 viewport_y = REAL_SY;
954 viewport_width = FULL_SXSIZE;
955 viewport_height = FULL_SYSIZE;
958 if (viewport_x != part->viewport_x ||
959 viewport_y != part->viewport_y ||
960 viewport_width != part->viewport_width ||
961 viewport_height != part->viewport_height)
963 part->viewport_x = viewport_x;
964 part->viewport_y = viewport_y;
965 part->viewport_width = viewport_width;
966 part->viewport_height = viewport_height;
968 if (part->control_info.class != get_hash_from_key("pointer"))
975 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
977 int sound = part->sound;
979 if (sound == SND_UNDEFINED)
982 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
983 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
986 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
987 if (IS_LOOP_SOUND(sound))
988 PlaySoundLoop(sound);
993 Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
994 part->anim_nr, part->nr, part->mode_nr, sound);
998 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1000 int sound = part->sound;
1002 if (sound == SND_UNDEFINED)
1008 Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1009 part->anim_nr, part->nr, part->mode_nr, sound);
1013 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1015 int music = part->music;
1017 if (music == MUS_UNDEFINED)
1020 if (!setup.sound_music)
1023 if (IS_LOOP_MUSIC(music))
1024 PlayMusicLoop(music);
1029 Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1030 part->anim_nr, part->nr, part->mode_nr, music);
1034 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1036 int music = part->music;
1038 if (music == MUS_UNDEFINED)
1044 Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1045 part->anim_nr, part->nr, part->mode_nr, music);
1049 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1051 // when drawing animations to fading buffer, do not play sounds or music
1052 if (drawing_to_fading_buffer)
1055 PlayGlobalAnimSound(part);
1056 PlayGlobalAnimMusic(part);
1059 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1061 StopGlobalAnimSound(part);
1062 StopGlobalAnimMusic(part);
1065 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1067 // when drawing animations to fading buffer, do not play sounds
1068 if (drawing_to_fading_buffer)
1071 // loop sounds only expire when playing
1072 if (game_status != GAME_MODE_PLAYING)
1075 // check if any sound is defined for this animation part
1076 if (part->sound == SND_UNDEFINED)
1079 // normal (non-loop) sounds do not expire when playing
1080 if (!IS_LOOP_SOUND(part->sound))
1083 // prevent expiring loop sounds when playing
1084 PlayGlobalAnimSound(part);
1087 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1089 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1091 if (mask & ANIM_EVENT_ANY)
1092 return (anim_event & ANIM_EVENT_ANY);
1093 else if (mask & ANIM_EVENT_SELF)
1094 return (anim_event & ANIM_EVENT_SELF);
1095 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1096 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1098 return (anim_event == mask ||
1099 anim_event == mask_anim_only);
1102 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1104 struct GraphicInfo *c = &part->control_info;
1105 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1106 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1109 for (i = 0; i < num_init_events; i++)
1111 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1113 if (checkGlobalAnimEvent(init_event, mask))
1117 for (i = 0; i < num_anim_events; i++)
1119 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1121 if (checkGlobalAnimEvent(anim_event, mask))
1128 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1129 int mx, int my, boolean clicked)
1131 struct GraphicInfo *g = &part->graphic_info;
1132 int part_x = part->viewport_x + part->x;
1133 int part_y = part->viewport_y + part->y;
1134 int part_width = g->width;
1135 int part_height = g->height;
1137 // check if mouse click was detected at all
1141 // check if mouse click is inside the animation part's viewport
1142 if (mx < part->viewport_x ||
1143 mx >= part->viewport_x + part->viewport_width ||
1144 my < part->viewport_y ||
1145 my >= part->viewport_y + part->viewport_height)
1148 // check if mouse click is inside the animation part's graphic
1150 mx >= part_x + part_width ||
1152 my >= part_y + part_height)
1158 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1160 return (part->control_info.style & STYLE_BLOCK ? TRUE : FALSE);
1163 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1165 return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1168 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1169 boolean *click_consumed,
1170 boolean *any_event_action,
1171 int event_value, char *info_text)
1173 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1175 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1176 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1177 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1180 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1184 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1186 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1189 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1191 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1193 if (!(part2->state & ANIM_STATE_RUNNING))
1196 if (isClickablePart(part2, mask))
1198 part2->triggered = TRUE;
1199 *click_consumed |= clickConsumed(part); // click was on "part"!
1201 #if DEBUG_ANIM_EVENTS
1202 Debug("anim:InitGlobalAnim_Triggered",
1203 "%d.%d TRIGGERED BY %s OF %d.%d",
1204 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1205 part->old_anim_nr + 1, part->old_nr + 1);
1208 Debug("anim:InitGlobalAnim_Triggered",
1209 "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1210 part2->control_info.anim_event_action);
1213 // after executing event action, ignore any further actions
1214 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1215 *any_event_action = TRUE;
1219 struct GraphicInfo *c = &part2->control_info;
1221 if (isClickablePart(part2, mask))
1222 Debug("anim:InitGlobalAnim_Triggered",
1223 "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1224 anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1227 Debug("anim:InitGlobalAnim_Triggered",
1228 "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1229 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1235 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1236 int delay_type, char *info_text)
1238 #if DEBUG_ANIM_DELAY
1239 Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1240 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1243 DoGlobalAnim_DelayAction(part, delay_type);
1246 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1247 int event_value, char *info_text)
1249 #if DEBUG_ANIM_EVENTS
1250 Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1251 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1254 boolean click_consumed = FALSE;
1255 boolean any_event_action = FALSE;
1257 // check if this event is defined to trigger other animations
1258 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1259 event_value, info_text);
1262 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1265 if (handle_click && !part->clicked)
1268 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1269 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1270 struct GraphicInfo *g = &part->graphic_info;
1271 struct GraphicInfo *c = &part->control_info;
1272 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1274 if (viewport_changed)
1275 state |= ANIM_STATE_RESTART;
1277 if (state & ANIM_STATE_RESTART)
1279 // when drawing animations to fading buffer, only start fixed animations
1280 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1281 c->y == ARG_UNDEFINED_VALUE))
1282 return ANIM_STATE_INACTIVE;
1284 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1286 part->init_delay_counter =
1287 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1289 part->anim_delay_counter =
1290 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1292 part->post_delay_counter = 0;
1294 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1295 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1297 part->initial_anim_sync_frame =
1298 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1300 // do not re-initialize random animation frame after fade-in
1301 if (part->anim_random_frame == -1)
1302 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1304 if (c->direction & MV_HORIZONTAL)
1306 int pos_bottom = part->viewport_height - g->height;
1308 if (c->position == POS_TOP)
1310 else if (c->position == POS_UPPER)
1311 part->y = GetSimpleRandom(pos_bottom / 2);
1312 else if (c->position == POS_MIDDLE)
1313 part->y = pos_bottom / 2;
1314 else if (c->position == POS_LOWER)
1315 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1316 else if (c->position == POS_BOTTOM)
1317 part->y = pos_bottom;
1319 part->y = GetSimpleRandom(pos_bottom);
1321 if (c->direction == MV_RIGHT)
1323 part->step_xoffset = c->step_offset;
1324 part->x = -g->width + part->step_xoffset;
1328 part->step_xoffset = -c->step_offset;
1329 part->x = part->viewport_width + part->step_xoffset;
1332 part->step_yoffset = 0;
1334 else if (c->direction & MV_VERTICAL)
1336 int pos_right = part->viewport_width - g->width;
1338 if (c->position == POS_LEFT)
1340 else if (c->position == POS_RIGHT)
1341 part->x = pos_right;
1343 part->x = GetSimpleRandom(pos_right);
1345 if (c->direction == MV_DOWN)
1347 part->step_yoffset = c->step_offset;
1348 part->y = -g->height + part->step_yoffset;
1352 part->step_yoffset = -c->step_offset;
1353 part->y = part->viewport_height + part->step_yoffset;
1356 part->step_xoffset = 0;
1363 part->step_xoffset = 0;
1364 part->step_yoffset = 0;
1367 if (part->control_info.class != get_hash_from_key("pointer"))
1369 if (c->x != ARG_UNDEFINED_VALUE)
1371 if (c->y != ARG_UNDEFINED_VALUE)
1375 if (c->position == POS_LAST &&
1376 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1377 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1379 part->x = anim->last_x;
1380 part->y = anim->last_y;
1383 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1384 part->step_xoffset = c->step_xoffset;
1385 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1386 part->step_yoffset = c->step_yoffset;
1388 if (part->init_delay_counter == 0 &&
1389 !part->init_event_state)
1391 PlayGlobalAnimSoundAndMusic(part);
1393 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1394 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1398 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1402 if (part->clicked &&
1403 part->init_event_state)
1405 if (part->initial_anim_sync_frame > 0)
1406 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1408 part->init_delay_counter = 1;
1409 part->init_event_state = FALSE;
1411 part->clicked = FALSE;
1414 if (part->clicked &&
1415 part->anim_event_state)
1417 part->anim_delay_counter = 1;
1418 part->anim_event_state = FALSE;
1420 part->clicked = FALSE;
1423 if (part->init_delay_counter > 0)
1425 part->init_delay_counter--;
1427 if (part->init_delay_counter == 0)
1429 part->init_event_state = FALSE;
1431 PlayGlobalAnimSoundAndMusic(part);
1433 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1434 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1437 return ANIM_STATE_WAITING;
1440 if (part->init_event_state)
1441 return ANIM_STATE_WAITING;
1443 // animation part is now running/visible and therefore clickable
1444 part->clickable = TRUE;
1446 // check if moving animation has left the visible screen area
1447 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1448 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1449 (part->y <= -g->height && part->step_yoffset <= 0) ||
1450 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1452 // do not wait for "anim" events for off-screen animations
1453 part->anim_event_state = FALSE;
1455 // do not stop animation before "anim" or "post" counter are finished
1456 if (part->anim_delay_counter == 0 &&
1457 part->post_delay_counter == 0)
1459 StopGlobalAnimSoundAndMusic(part);
1461 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1463 part->post_delay_counter =
1464 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1466 if (part->post_delay_counter > 0)
1467 return ANIM_STATE_RUNNING;
1469 // drawing last frame not needed here -- animation not visible anymore
1470 return ANIM_STATE_RESTART;
1474 if (part->anim_delay_counter > 0)
1476 part->anim_delay_counter--;
1478 if (part->anim_delay_counter == 0)
1480 part->anim_event_state = FALSE;
1482 StopGlobalAnimSoundAndMusic(part);
1484 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1485 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1487 part->post_delay_counter =
1488 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1490 if (part->post_delay_counter > 0)
1491 return ANIM_STATE_RUNNING;
1493 // additional state "RUNNING" required to not skip drawing last frame
1494 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1498 if (part->post_delay_counter > 0)
1500 part->post_delay_counter--;
1502 if (part->post_delay_counter == 0)
1504 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1505 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1507 return ANIM_STATE_RESTART;
1510 return ANIM_STATE_WAITING;
1513 // special case to prevent expiring loop sounds when playing
1514 PlayGlobalAnimSoundIfLoop(part);
1516 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1518 return ANIM_STATE_RUNNING;
1522 static unsigned int last_counter = -1;
1523 unsigned int counter = Counter();
1525 Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1526 anim_sync_frame, counter - last_counter);
1528 last_counter = counter;
1532 part->x += part->step_xoffset;
1533 part->y += part->step_yoffset;
1535 anim->last_x = part->x;
1536 anim->last_y = part->y;
1538 return ANIM_STATE_RUNNING;
1541 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1544 struct GlobalAnimPartControlInfo *part;
1545 struct GraphicInfo *c = &anim->control_info;
1546 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1547 int state, active_part_nr;
1551 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1552 anim->mode_nr, anim->nr, anim->num_parts);
1553 Debug("anim:HandleGlobalAnim_Main",
1554 "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1558 Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1559 (action == ANIM_START ? "ANIM_START" :
1560 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1561 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1563 anim->state & ANIM_STATE_RESTART,
1564 anim->state & ANIM_STATE_WAITING,
1565 anim->state & ANIM_STATE_RUNNING,
1572 anim->state = anim->last_state = ANIM_STATE_RESTART;
1573 anim->active_part_nr = anim->last_active_part_nr = 0;
1574 anim->part_counter = 0;
1579 if (anim->state == ANIM_STATE_INACTIVE)
1582 anim->state = anim->last_state;
1583 anim->active_part_nr = anim->last_active_part_nr;
1588 anim->state = ANIM_STATE_INACTIVE;
1590 for (i = 0; i < num_parts; i++)
1591 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1599 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1602 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1603 anim->mode_nr, anim->nr, num_parts);
1606 for (i = 0; i < num_parts; i++)
1608 part = &anim->part[i];
1613 anim->state = ANIM_STATE_RUNNING;
1614 part->state = ANIM_STATE_RESTART;
1619 if (part->state == ANIM_STATE_INACTIVE)
1625 part->state = ANIM_STATE_INACTIVE;
1633 part->state = HandleGlobalAnim_Part(part, part->state);
1635 // when animation mode is "once", stop after animation was played once
1636 if (c->anim_mode & ANIM_ONCE &&
1637 part->state & ANIM_STATE_RESTART)
1638 part->state = ANIM_STATE_INACTIVE;
1641 anim->last_state = anim->state;
1642 anim->last_active_part_nr = anim->active_part_nr;
1647 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1648 anim->active_part_nr = getGlobalAnimationPart(anim);
1650 part = &anim->part[anim->active_part_nr];
1652 // first set all animation parts to "inactive", ...
1653 for (i = 0; i < num_parts; i++)
1654 anim->part[i].state = ANIM_STATE_INACTIVE;
1656 // ... then set current animation parts to "running"
1657 part->state = ANIM_STATE_RUNNING;
1659 anim->state = HandleGlobalAnim_Part(part, anim->state);
1661 if (anim->state & ANIM_STATE_RESTART)
1662 anim->part_counter++;
1664 // when animation mode is "once", stop after all animations were played once
1665 if (c->anim_mode & ANIM_ONCE &&
1666 anim->part_counter == anim->num_parts)
1667 anim->state = ANIM_STATE_INACTIVE;
1669 state = anim->state;
1670 active_part_nr = anim->active_part_nr;
1672 // while the animation parts are pausing (waiting or inactive), play the base
1673 // (main) animation; this corresponds to the "boring player animation" logic
1674 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1677 if (anim->state == ANIM_STATE_WAITING ||
1678 anim->state == ANIM_STATE_INACTIVE)
1680 anim->active_part_nr = anim->num_parts; // part nr of base animation
1681 part = &anim->part[anim->active_part_nr];
1683 if (anim->state != anim->last_state)
1684 part->state = ANIM_STATE_RESTART;
1686 anim->state = ANIM_STATE_RUNNING;
1687 part->state = HandleGlobalAnim_Part(part, part->state);
1691 anim->last_state = state;
1692 anim->last_active_part_nr = active_part_nr;
1695 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1700 Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1703 for (i = 0; i < ctrl->num_anims; i++)
1704 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1707 static void HandleGlobalAnim(int action, int game_mode)
1710 Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1713 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1716 static void DoAnimationExt(void)
1721 Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1724 // global animations now synchronized with frame delay of screen update
1727 for (i = 0; i < NUM_GAME_MODES; i++)
1728 HandleGlobalAnim(ANIM_CONTINUE, i);
1731 // force screen redraw in next frame to continue drawing global animations
1732 redraw_mask = REDRAW_ALL;
1736 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1740 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1741 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1742 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1743 ANIM_DELAY_ACTION_NONE);
1745 if (delay_action == ANIM_DELAY_ACTION_NONE)
1748 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1751 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1753 int event_action = (part->init_event_state ?
1754 part->control_info.init_event_action :
1755 part->control_info.anim_event_action);
1757 if (event_action == ANIM_EVENT_ACTION_NONE)
1760 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1762 // check if further actions are allowed to be executed
1763 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1769 static void InitGlobalAnim_Clickable(void)
1773 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1775 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1778 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1780 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1783 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1785 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1787 if (part->triggered)
1788 part->clicked = TRUE;
1790 part->triggered = FALSE;
1791 part->clickable = FALSE;
1797 #define ANIM_CLICKED_RESET 0
1798 #define ANIM_CLICKED_PRESSED 1
1799 #define ANIM_CLICKED_RELEASED 2
1801 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1803 boolean click_consumed = FALSE;
1804 boolean anything_clicked = FALSE;
1805 boolean any_part_clicked = FALSE;
1806 boolean any_event_action = FALSE;
1810 // check game modes in reverse draw order (to stop when clicked)
1811 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1813 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1816 // check animations in reverse draw order (to stop when clicked)
1817 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1819 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1822 // check animation parts in reverse draw order (to stop when clicked)
1823 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1825 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1827 if (clicked_event == ANIM_CLICKED_RESET)
1829 part->clicked = FALSE;
1834 if (!part->clickable)
1837 if (!(part->state & ANIM_STATE_RUNNING))
1840 // always handle "any" click events (clicking anywhere on screen) ...
1841 if (clicked_event == ANIM_CLICKED_PRESSED &&
1842 isClickablePart(part, ANIM_EVENT_ANY))
1844 #if DEBUG_ANIM_EVENTS
1845 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
1846 part->old_anim_nr + 1, part->old_nr + 1);
1849 anything_clicked = part->clicked = TRUE;
1850 click_consumed |= clickConsumed(part);
1853 // always handle "unclick:any" events (releasing anywhere on screen) ...
1854 if (clicked_event == ANIM_CLICKED_RELEASED &&
1855 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1857 #if DEBUG_ANIM_EVENTS
1858 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
1859 part->old_anim_nr + 1, part->old_nr + 1);
1862 anything_clicked = part->clicked = TRUE;
1863 click_consumed |= clickConsumed(part);
1866 // ... but only handle the first (topmost) clickable animation
1867 if (any_part_clicked)
1870 if (clicked_event == ANIM_CLICKED_PRESSED &&
1871 isClickedPart(part, mx, my, TRUE))
1874 Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
1875 anim_nr, part_nr, part->control_info.anim_event_action);
1878 // after executing event action, ignore any further actions
1879 if (!any_event_action && DoGlobalAnim_EventAction(part))
1880 any_event_action = TRUE;
1882 // determine if mouse clicks should be blocked from other animations
1883 any_part_clicked |= clickConsumed(part);
1885 if (isClickablePart(part, ANIM_EVENT_SELF))
1887 #if DEBUG_ANIM_EVENTS
1888 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
1889 part->old_anim_nr + 1, part->old_nr + 1);
1892 anything_clicked = part->clicked = TRUE;
1893 click_consumed |= clickConsumed(part);
1896 // determine if mouse clicks should be blocked by this animation
1897 click_consumed |= clickBlocked(part);
1899 // check if this click is defined to trigger other animations
1900 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1901 ANIM_EVENT_CLICK, "CLICK");
1907 if (anything_clicked)
1909 handle_click = TRUE;
1911 for (i = 0; i < NUM_GAME_MODES; i++)
1912 HandleGlobalAnim(ANIM_CONTINUE, i);
1914 handle_click = FALSE;
1916 // prevent ignoring release event if processed within same game frame
1917 StopProcessingEvents();
1920 return (click_consumed || any_event_action);
1923 static void ResetGlobalAnim_Clickable(void)
1925 InitGlobalAnim_Clickable();
1928 static void ResetGlobalAnim_Clicked(void)
1930 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1933 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1935 static boolean click_consumed = FALSE;
1936 static int last_button = 0;
1937 boolean press_event;
1938 boolean release_event;
1939 boolean click_consumed_current = click_consumed;
1941 if (button != 0 && force_click)
1944 // check if button state has changed since last invocation
1945 press_event = (button != 0 && last_button == 0);
1946 release_event = (button == 0 && last_button != 0);
1947 last_button = button;
1951 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1952 click_consumed_current = click_consumed;
1957 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
1958 click_consumed = FALSE;
1961 return click_consumed_current;