1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://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 step_delay, step_delay_value;
113 int init_delay_counter;
114 int anim_delay_counter;
115 int post_delay_counter;
117 boolean init_event_state;
118 boolean anim_event_state;
127 int last_anim_status;
130 struct GlobalAnimMainControlInfo
132 struct GlobalAnimPartControlInfo base;
133 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
138 struct GraphicInfo control_info;
140 int num_parts; // number of animation parts, but without base part
141 int num_parts_all; // number of animation parts, including base part
145 boolean has_base; // animation has base/main/default animation part
149 int init_delay_counter;
153 int last_state, last_active_part_nr;
156 struct GlobalAnimControlInfo
158 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
164 struct GameModeAnimClass
168 } game_mode_anim_classes_list[] =
170 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
171 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
172 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
173 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
174 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
175 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
176 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
177 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
178 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
179 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
180 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
181 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
182 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
183 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
184 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
185 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
186 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORES },
187 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORES },
188 { GAME_MODE_EDITOR, ANIM_CLASS_NO_TITLE },
189 { GAME_MODE_PLAYING, ANIM_CLASS_NO_TITLE },
194 struct AnimClassGameMode
198 } anim_class_game_modes_list[] =
200 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
201 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
202 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
203 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
204 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
205 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
206 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
207 { ANIM_CLASS_BIT_NO_TITLE, GAME_MODE_PSEUDO_NO_TITLE },
212 // forward declaration for internal use
213 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
214 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
215 static void HandleGlobalAnim(int, int);
216 static void DoAnimationExt(void);
217 static void ResetGlobalAnim_Clickable(void);
218 static void ResetGlobalAnim_Clicked(void);
220 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
222 static unsigned int anim_sync_frame = 0;
224 static int game_mode_anim_classes[NUM_GAME_MODES];
225 static int anim_class_game_modes[NUM_ANIM_CLASSES];
227 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
228 static int anim_status_last = GAME_MODE_DEFAULT;
229 static int anim_classes_last = ANIM_CLASS_NONE;
231 static boolean drawing_to_fading_buffer = FALSE;
233 static boolean handle_click = FALSE;
236 // ============================================================================
237 // generic animation frame calculation
238 // ============================================================================
240 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
245 sync_frame += start_frame * delay;
247 if (mode & ANIM_LOOP) // looping animation
249 frame = (sync_frame % (delay * num_frames)) / delay;
251 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
253 frame = sync_frame / delay;
255 if (frame > num_frames - 1)
256 frame = num_frames - 1;
258 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
260 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
262 frame = (sync_frame % (delay * max_anim_frames)) / delay;
263 frame = (frame < num_frames ? frame : max_anim_frames - frame);
265 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
267 int max_anim_frames = 2 * num_frames;
269 frame = (sync_frame % (delay * max_anim_frames)) / delay;
270 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
272 else if (mode & ANIM_RANDOM) // play frames in random order
274 // note: expect different frames for the same delay cycle!
276 if (gfx.anim_random_frame < 0)
277 frame = GetSimpleRandom(num_frames);
279 frame = gfx.anim_random_frame % num_frames;
281 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
283 frame = sync_frame % num_frames;
286 if (mode & ANIM_REVERSE) // use reverse animation direction
287 frame = num_frames - frame - 1;
293 // ============================================================================
294 // global animation functions
295 // ============================================================================
297 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
299 struct GraphicInfo *c = &anim->control_info;
300 int last_anim_random_frame = gfx.anim_random_frame;
303 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
305 part_nr = getAnimationFrame(anim->num_parts, 1,
306 c->anim_mode, c->anim_start_frame,
309 gfx.anim_random_frame = last_anim_random_frame;
314 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
316 const struct GlobalAnimPartControlInfo *o1 =
317 (struct GlobalAnimPartControlInfo *)obj1;
318 const struct GlobalAnimPartControlInfo *o2 =
319 (struct GlobalAnimPartControlInfo *)obj2;
322 if (o1->control_info.draw_order != o2->control_info.draw_order)
323 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
325 compare_result = o1->nr - o2->nr;
327 return compare_result;
330 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
332 const struct GlobalAnimMainControlInfo *o1 =
333 (struct GlobalAnimMainControlInfo *)obj1;
334 const struct GlobalAnimMainControlInfo *o2 =
335 (struct GlobalAnimMainControlInfo *)obj2;
338 if (o1->control_info.draw_order != o2->control_info.draw_order)
339 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
341 compare_result = o1->nr - o2->nr;
343 return compare_result;
346 static void InitToonControls(void)
348 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
349 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
350 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
351 int mode_nr, anim_nr, part_nr;
352 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
353 int num_toons = MAX_NUM_TOONS;
356 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
357 num_toons = global.num_toons;
359 mode_nr = mode_nr_toons;
360 anim_nr = ctrl->num_anims;
363 anim->mode_nr = mode_nr;
364 anim->control_info = graphic_info[control];
367 anim->num_parts_all = 0;
368 anim->part_counter = 0;
369 anim->active_part_nr = 0;
371 anim->has_base = FALSE;
373 anim->last_x = POS_OFFSCREEN;
374 anim->last_y = POS_OFFSCREEN;
376 anim->init_delay_counter = 0;
378 anim->state = ANIM_STATE_INACTIVE;
382 for (i = 0; i < num_toons; i++)
384 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
385 int sound = SND_UNDEFINED;
386 int music = MUS_UNDEFINED;
387 int graphic = IMG_TOON_1 + i;
388 int control = graphic;
391 part->anim_nr = anim_nr;
392 part->mode_nr = mode_nr;
394 part->is_base = FALSE;
398 part->graphic = graphic;
400 part->graphic_info = graphic_info[graphic];
401 part->control_info = graphic_info[control];
403 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
405 part->control_info.init_delay_fixed = 0;
406 part->control_info.init_delay_random = 150;
408 part->control_info.x = ARG_UNDEFINED_VALUE;
409 part->control_info.y = ARG_UNDEFINED_VALUE;
411 part->initial_anim_sync_frame = 0;
413 part->step_delay = 0;
414 part->step_delay_value = graphic_info[control].step_delay;
416 part->state = ANIM_STATE_INACTIVE;
417 part->last_anim_status = -1;
420 anim->num_parts_all++;
428 static void InitGlobalAnimControls(void)
431 int mode_nr, anim_nr, part_nr;
432 int sound, music, graphic, control;
436 for (m = 0; m < NUM_GAME_MODES; m++)
440 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
447 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
449 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
450 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
452 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
454 // if no base animation parameters defined, use default values
455 if (control == IMG_UNDEFINED)
456 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
459 anim->mode_nr = mode_nr;
460 anim->control_info = graphic_info[control];
463 anim->num_parts_all = 0;
464 anim->part_counter = 0;
465 anim->active_part_nr = 0;
467 anim->has_base = FALSE;
469 anim->last_x = POS_OFFSCREEN;
470 anim->last_y = POS_OFFSCREEN;
472 anim->init_delay_counter = 0;
474 anim->state = ANIM_STATE_INACTIVE;
478 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
480 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
482 sound = global_anim_info[a].sound[p][m];
483 music = global_anim_info[a].music[p][m];
484 graphic = global_anim_info[a].graphic[p][m];
485 control = global_anim_info[ctrl_id].graphic[p][m];
487 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
488 control == IMG_UNDEFINED)
492 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
493 m, a, p, mode_nr, anim_nr, part_nr, control);
497 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
498 m, a, p, mode_nr, anim_nr, part_nr, sound);
502 part->old_anim_nr = a;
505 part->anim_nr = anim_nr;
506 part->mode_nr = mode_nr;
510 part->graphic = graphic;
512 part->graphic_info = graphic_info[graphic];
513 part->control_info = graphic_info[control];
515 part->initial_anim_sync_frame = 0;
517 part->step_delay = 0;
518 part->step_delay_value = graphic_info[control].step_delay;
520 part->state = ANIM_STATE_INACTIVE;
521 part->last_anim_status = -1;
523 anim->num_parts_all++;
525 if (p < GLOBAL_ANIM_ID_PART_BASE)
527 part->is_base = FALSE;
534 part->is_base = TRUE;
537 anim->has_base = TRUE;
540 // apply special settings for pointer-style animations
541 if (part->control_info.class == get_hash_from_key("pointer"))
543 // force animation to be on top (must set anim and part control)
544 if (anim->control_info.draw_order == 0)
545 anim->control_info.draw_order = 1000000;
546 if (part->control_info.draw_order == 0)
547 part->control_info.draw_order = 1000000;
549 // force animation to pass-through clicks (must set part control)
550 if (part->control_info.style == STYLE_DEFAULT)
551 part->control_info.style |= STYLE_PASSTHROUGH;
555 if (anim->num_parts > 0 || anim->has_base)
565 // sort all animations according to draw_order and animation number
566 for (m = 0; m < NUM_GAME_MODES; m++)
568 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
570 // sort all main animations for this game mode
571 qsort(ctrl->anim, ctrl->num_anims,
572 sizeof(struct GlobalAnimMainControlInfo),
573 compareGlobalAnimMainControlInfo);
575 for (a = 0; a < ctrl->num_anims; a++)
577 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
579 // sort all animation parts for this main animation
580 qsort(anim->part, anim->num_parts,
581 sizeof(struct GlobalAnimPartControlInfo),
582 compareGlobalAnimPartControlInfo);
586 for (i = 0; i < NUM_GAME_MODES; i++)
587 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
588 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
589 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
590 game_mode_anim_classes_list[i].class;
592 for (i = 0; i < NUM_ANIM_CLASSES; i++)
593 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
594 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
595 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
596 anim_class_game_modes_list[i].game_mode;
598 anim_status_last_before_fading = GAME_MODE_LOADING;
599 anim_status_last = GAME_MODE_LOADING;
600 anim_classes_last = ANIM_CLASS_NONE;
603 void InitGlobalAnimations(void)
605 InitGlobalAnimControls();
608 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
610 Bitmap *fade_bitmap =
611 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
612 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
613 int game_mode_anim_action[NUM_GAME_MODES];
619 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
620 drawing_target == DRAW_TO_SCREEN)
623 // always start with reliable default values (no animation actions)
624 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
625 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
627 if (global.anim_status != anim_status_last)
629 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
630 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
631 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
634 if (drawing_target == DRAW_TO_FADE_TARGET)
637 // special case: changing from/to this screen is done without fading
638 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
639 anim_status_last == GAME_MODE_PSEUDO_TYPENAME)
642 // ---------- part 1 ------------------------------------------------------
643 // start or stop global animations by change of game mode
644 // (special handling of animations for "current screen" and "all screens")
646 if (global.anim_status_next != anim_status_last_before_fading)
648 // stop animations for last screen before fading to new screen
649 game_mode_anim_action[anim_status_last] = ANIM_STOP;
651 // start animations for current screen after fading to new screen
652 game_mode_anim_action[global.anim_status] = ANIM_START;
655 // start animations for all screens after loading new artwork set
656 if (anim_status_last == GAME_MODE_LOADING)
657 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
659 // ---------- part 2 ------------------------------------------------------
660 // start or stop global animations by change of animation class
661 // (generic handling of animations for "class of screens")
663 for (i = 0; i < NUM_ANIM_CLASSES; i++)
665 int anim_class_check = (1 << i);
666 int anim_class_game_mode = anim_class_game_modes[i];
667 int anim_class_last = anim_classes_last & anim_class_check;
668 int anim_class_next = anim_classes_next & anim_class_check;
670 // stop animations for changed screen class before fading to new screen
671 if (before_fading && anim_class_last && !anim_class_next)
672 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
674 // start animations for changed screen class after fading to new screen
675 if (after_fading && !anim_class_last && anim_class_next)
676 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
679 if (drawing_target == DRAW_TO_SCREEN)
683 anim_classes_last = anim_classes_next;
684 anim_status_last_before_fading = global.anim_status;
687 anim_status_last = global.anim_status;
689 // start or stop animations determined to be started or stopped above
690 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
691 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
692 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
694 else if (drawing_target == DRAW_TO_FADE_TARGET)
696 drawing_to_fading_buffer = TRUE;
698 // start animations determined to be (temporary) started above
699 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
700 if (game_mode_anim_action[mode_nr] == ANIM_START)
701 HandleGlobalAnim(ANIM_START, mode_nr);
705 if (global.anim_status == GAME_MODE_LOADING)
708 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
710 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
713 // when preparing source fading buffer, only draw animations to be stopped
714 if (drawing_target == DRAW_TO_FADE_SOURCE &&
715 game_mode_anim_action[mode_nr] != ANIM_STOP)
718 // when preparing target fading buffer, only draw animations to be started
719 if (drawing_target == DRAW_TO_FADE_TARGET &&
720 game_mode_anim_action[mode_nr] != ANIM_START)
724 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
725 mode_nr != game_status)
729 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
731 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
732 struct GraphicInfo *c = &anim->control_info;
733 int part_first, part_last;
736 if (!(anim->state & ANIM_STATE_RUNNING))
739 part_first = part_last = anim->active_part_nr;
741 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
743 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
746 part_last = num_parts - 1;
749 for (part_nr = part_first; part_nr <= part_last; part_nr++)
751 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
752 struct GraphicInfo *g = &part->graphic_info;
755 int width = g->width;
756 int height = g->height;
763 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
764 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
765 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
766 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
768 if (!(part->state & ANIM_STATE_RUNNING))
771 if (part->drawing_stage != drawing_stage)
780 else if (part->x > part->viewport_width - g->width)
781 width -= (part->x - (part->viewport_width - g->width));
789 else if (part->y > part->viewport_height - g->height)
790 height -= (part->y - (part->viewport_height - g->height));
792 if (width <= 0 || height <= 0)
795 dst_x += part->viewport_x;
796 dst_y += part->viewport_y;
798 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
799 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
800 g->anim_mode, g->anim_start_frame,
803 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
809 if (drawing_target == DRAW_TO_SCREEN)
810 blit_screen(src_bitmap, src_x, src_y, width, height,
813 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
819 if (drawing_target == DRAW_TO_FADE_TARGET)
821 // stop animations determined to be (temporary) started above
822 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
823 if (game_mode_anim_action[mode_nr] == ANIM_START)
824 HandleGlobalAnim(ANIM_STOP, mode_nr);
826 drawing_to_fading_buffer = FALSE;
830 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
832 int last_cursor_mode_override = gfx.cursor_mode_override;
834 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
836 ResetGlobalAnim_Clickable();
838 gfx.cursor_mode_override = CURSOR_UNDEFINED;
841 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
843 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
845 ResetGlobalAnim_Clicked();
848 if (gfx.cursor_mode_override != last_cursor_mode_override)
849 SetMouseCursor(gfx.cursor_mode);
852 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
858 boolean changed = FALSE;
860 if (part->last_anim_status == global.anim_status &&
861 part->control_info.class != get_hash_from_key("pointer"))
864 part->last_anim_status = global.anim_status;
866 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
868 if (part->control_info.class == get_hash_from_key("window") ||
869 part->control_info.class == get_hash_from_key("border"))
873 viewport_width = WIN_XSIZE;
874 viewport_height = WIN_YSIZE;
876 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
878 else if (part->control_info.class == get_hash_from_key("pointer"))
880 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
881 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
883 // prevent displaying off-screen custom mouse cursor in upper left corner
884 if (gfx.mouse_x == POS_OFFSCREEN &&
885 gfx.mouse_y == POS_OFFSCREEN)
886 mx = my = POS_OFFSCREEN;
888 viewport_x = mx - part->control_info.x;
889 viewport_y = my - part->control_info.y;
890 viewport_width = part->graphic_info.width;
891 viewport_height = part->graphic_info.height;
893 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
895 // do not use global animation mouse pointer when reloading artwork
896 if (global.anim_status != GAME_MODE_LOADING)
897 gfx.cursor_mode_override = CURSOR_NONE;
899 else if (part->control_info.class == get_hash_from_key("door_1"))
903 viewport_width = DXSIZE;
904 viewport_height = DYSIZE;
906 else if (part->control_info.class == get_hash_from_key("door_2"))
908 if (part->mode_nr == GAME_MODE_EDITOR)
912 viewport_width = EXSIZE;
913 viewport_height = EYSIZE;
919 viewport_width = VXSIZE;
920 viewport_height = VYSIZE;
923 else // default: "playfield"
925 viewport_x = REAL_SX;
926 viewport_y = REAL_SY;
927 viewport_width = FULL_SXSIZE;
928 viewport_height = FULL_SYSIZE;
931 if (viewport_x != part->viewport_x ||
932 viewport_y != part->viewport_y ||
933 viewport_width != part->viewport_width ||
934 viewport_height != part->viewport_height)
936 part->viewport_x = viewport_x;
937 part->viewport_y = viewport_y;
938 part->viewport_width = viewport_width;
939 part->viewport_height = viewport_height;
941 if (part->control_info.class != get_hash_from_key("pointer"))
948 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
950 int sound = part->sound;
952 if (sound == SND_UNDEFINED)
955 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
956 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
959 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
960 if (IS_LOOP_SOUND(sound))
961 PlaySoundLoop(sound);
966 printf("::: PLAY SOUND %d.%d.%d: %d\n",
967 part->anim_nr, part->nr, part->mode_nr, sound);
971 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
973 int sound = part->sound;
975 if (sound == SND_UNDEFINED)
981 printf("::: STOP SOUND %d.%d.%d: %d\n",
982 part->anim_nr, part->nr, part->mode_nr, sound);
986 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
988 int music = part->music;
990 if (music == MUS_UNDEFINED)
993 if (!setup.sound_music)
996 if (IS_LOOP_MUSIC(music))
997 PlayMusicLoop(music);
1002 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
1003 part->anim_nr, part->nr, part->mode_nr, music);
1007 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1009 int music = part->music;
1011 if (music == MUS_UNDEFINED)
1017 printf("::: STOP MUSIC %d.%d.%d: %d\n",
1018 part->anim_nr, part->nr, part->mode_nr, music);
1022 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1024 // when drawing animations to fading buffer, do not play sounds or music
1025 if (drawing_to_fading_buffer)
1028 PlayGlobalAnimSound(part);
1029 PlayGlobalAnimMusic(part);
1032 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1034 StopGlobalAnimSound(part);
1035 StopGlobalAnimMusic(part);
1038 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1040 // when drawing animations to fading buffer, do not play sounds
1041 if (drawing_to_fading_buffer)
1044 // loop sounds only expire when playing
1045 if (game_status != GAME_MODE_PLAYING)
1048 // check if any sound is defined for this animation part
1049 if (part->sound == SND_UNDEFINED)
1052 // normal (non-loop) sounds do not expire when playing
1053 if (!IS_LOOP_SOUND(part->sound))
1056 // prevent expiring loop sounds when playing
1057 PlayGlobalAnimSound(part);
1060 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1062 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1064 if (mask & ANIM_EVENT_ANY)
1065 return (anim_event & ANIM_EVENT_ANY);
1066 else if (mask & ANIM_EVENT_SELF)
1067 return (anim_event & ANIM_EVENT_SELF);
1068 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1069 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1071 return (anim_event == mask ||
1072 anim_event == mask_anim_only);
1075 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1077 struct GraphicInfo *c = &part->control_info;
1078 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1079 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1082 for (i = 0; i < num_init_events; i++)
1084 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1086 if (checkGlobalAnimEvent(init_event, mask))
1090 for (i = 0; i < num_anim_events; i++)
1092 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1094 if (checkGlobalAnimEvent(anim_event, mask))
1101 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1102 int mx, int my, boolean clicked)
1104 struct GraphicInfo *g = &part->graphic_info;
1105 int part_x = part->viewport_x + part->x;
1106 int part_y = part->viewport_y + part->y;
1107 int part_width = g->width;
1108 int part_height = g->height;
1110 // check if mouse click was detected at all
1114 // check if mouse click is inside the animation part's viewport
1115 if (mx < part->viewport_x ||
1116 mx >= part->viewport_x + part->viewport_width ||
1117 my < part->viewport_y ||
1118 my >= part->viewport_y + part->viewport_height)
1121 // check if mouse click is inside the animation part's graphic
1123 mx >= part_x + part_width ||
1125 my >= part_y + part_height)
1131 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1133 return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1136 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1137 boolean *click_consumed,
1138 boolean *any_event_action,
1139 int event_value, char *info_text)
1141 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1143 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1144 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1145 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1148 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1152 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1154 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1157 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1159 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1161 if (!(part2->state & ANIM_STATE_RUNNING))
1164 if (isClickablePart(part2, mask))
1166 part2->triggered = TRUE;
1167 *click_consumed |= clickConsumed(part); // click was on "part"!
1169 #if DEBUG_ANIM_EVENTS
1170 printf("::: => %d.%d TRIGGERED BY %s OF %d.%d\n",
1171 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1172 part->old_anim_nr + 1, part->old_nr + 1);
1175 printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
1176 part2->control_info.anim_event_action);
1179 // after executing event action, ignore any further actions
1180 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1181 *any_event_action = TRUE;
1185 struct GraphicInfo *c = &part2->control_info;
1187 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1188 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1190 if (isClickablePart(part2, mask))
1191 printf(" <--- TRIGGERED BY %d.%d",
1200 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1201 int delay_type, char *info_text)
1203 #if DEBUG_ANIM_DELAY
1204 printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
1207 DoGlobalAnim_DelayAction(part, delay_type);
1210 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1211 int event_value, char *info_text)
1213 #if DEBUG_ANIM_EVENTS
1214 printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
1217 boolean click_consumed = FALSE;
1218 boolean any_event_action = FALSE;
1220 // check if this event is defined to trigger other animations
1221 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1222 event_value, info_text);
1225 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1228 if (handle_click && !part->clicked)
1231 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1232 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1233 struct GraphicInfo *g = &part->graphic_info;
1234 struct GraphicInfo *c = &part->control_info;
1235 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1237 if (viewport_changed)
1238 state |= ANIM_STATE_RESTART;
1240 if (state & ANIM_STATE_RESTART)
1242 // when drawing animations to fading buffer, only start fixed animations
1243 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1244 c->y == ARG_UNDEFINED_VALUE))
1245 return ANIM_STATE_INACTIVE;
1247 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1249 part->init_delay_counter =
1250 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1252 part->anim_delay_counter =
1253 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1255 part->post_delay_counter = 0;
1257 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1258 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1260 part->initial_anim_sync_frame =
1261 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1263 if (c->direction & MV_HORIZONTAL)
1265 int pos_bottom = part->viewport_height - g->height;
1267 if (c->position == POS_TOP)
1269 else if (c->position == POS_UPPER)
1270 part->y = GetSimpleRandom(pos_bottom / 2);
1271 else if (c->position == POS_MIDDLE)
1272 part->y = pos_bottom / 2;
1273 else if (c->position == POS_LOWER)
1274 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1275 else if (c->position == POS_BOTTOM)
1276 part->y = pos_bottom;
1278 part->y = GetSimpleRandom(pos_bottom);
1280 if (c->direction == MV_RIGHT)
1282 part->step_xoffset = c->step_offset;
1283 part->x = -g->width + part->step_xoffset;
1287 part->step_xoffset = -c->step_offset;
1288 part->x = part->viewport_width + part->step_xoffset;
1291 part->step_yoffset = 0;
1293 else if (c->direction & MV_VERTICAL)
1295 int pos_right = part->viewport_width - g->width;
1297 if (c->position == POS_LEFT)
1299 else if (c->position == POS_RIGHT)
1300 part->x = pos_right;
1302 part->x = GetSimpleRandom(pos_right);
1304 if (c->direction == MV_DOWN)
1306 part->step_yoffset = c->step_offset;
1307 part->y = -g->height + part->step_yoffset;
1311 part->step_yoffset = -c->step_offset;
1312 part->y = part->viewport_height + part->step_yoffset;
1315 part->step_xoffset = 0;
1322 part->step_xoffset = 0;
1323 part->step_yoffset = 0;
1326 if (part->control_info.class != get_hash_from_key("pointer"))
1328 if (c->x != ARG_UNDEFINED_VALUE)
1330 if (c->y != ARG_UNDEFINED_VALUE)
1334 if (c->position == POS_LAST &&
1335 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1336 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1338 part->x = anim->last_x;
1339 part->y = anim->last_y;
1342 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1343 part->step_xoffset = c->step_xoffset;
1344 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1345 part->step_yoffset = c->step_yoffset;
1347 if (part->init_delay_counter == 0 &&
1348 !part->init_event_state)
1350 PlayGlobalAnimSoundAndMusic(part);
1352 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1353 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1357 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1361 if (part->clicked &&
1362 part->init_event_state)
1364 if (part->initial_anim_sync_frame > 0)
1365 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1367 part->init_delay_counter = 1;
1368 part->init_event_state = FALSE;
1370 part->clicked = FALSE;
1373 if (part->clicked &&
1374 part->anim_event_state)
1376 part->anim_delay_counter = 1;
1377 part->anim_event_state = FALSE;
1379 part->clicked = FALSE;
1382 if (part->init_delay_counter > 0)
1384 part->init_delay_counter--;
1386 if (part->init_delay_counter == 0)
1388 part->init_event_state = FALSE;
1390 PlayGlobalAnimSoundAndMusic(part);
1392 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1393 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1396 return ANIM_STATE_WAITING;
1399 if (part->init_event_state)
1400 return ANIM_STATE_WAITING;
1402 // animation part is now running/visible and therefore clickable
1403 part->clickable = TRUE;
1405 // check if moving animation has left the visible screen area
1406 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1407 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1408 (part->y <= -g->height && part->step_yoffset <= 0) ||
1409 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1411 // do not wait for "anim" events for off-screen animations
1412 part->anim_event_state = FALSE;
1414 // do not stop animation before "anim" or "post" counter are finished
1415 if (part->anim_delay_counter == 0 &&
1416 part->post_delay_counter == 0)
1418 StopGlobalAnimSoundAndMusic(part);
1420 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1422 part->post_delay_counter =
1423 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1425 if (part->post_delay_counter > 0)
1426 return ANIM_STATE_RUNNING;
1428 // drawing last frame not needed here -- animation not visible anymore
1429 return ANIM_STATE_RESTART;
1433 if (part->anim_delay_counter > 0)
1435 part->anim_delay_counter--;
1437 if (part->anim_delay_counter == 0)
1439 part->anim_event_state = FALSE;
1441 StopGlobalAnimSoundAndMusic(part);
1443 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1444 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1446 part->post_delay_counter =
1447 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1449 if (part->post_delay_counter > 0)
1450 return ANIM_STATE_RUNNING;
1452 // additional state "RUNNING" required to not skip drawing last frame
1453 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1457 if (part->post_delay_counter > 0)
1459 part->post_delay_counter--;
1461 if (part->post_delay_counter == 0)
1463 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1464 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1466 return ANIM_STATE_RESTART;
1469 return ANIM_STATE_WAITING;
1472 // special case to prevent expiring loop sounds when playing
1473 PlayGlobalAnimSoundIfLoop(part);
1475 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1477 return ANIM_STATE_RUNNING;
1481 static unsigned int last_counter = -1;
1482 unsigned int counter = Counter();
1484 printf("::: NEXT ANIM PART [%d, %d]\n",
1485 anim_sync_frame, counter - last_counter);
1487 last_counter = counter;
1491 part->x += part->step_xoffset;
1492 part->y += part->step_yoffset;
1494 anim->last_x = part->x;
1495 anim->last_y = part->y;
1497 return ANIM_STATE_RUNNING;
1500 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1503 struct GlobalAnimPartControlInfo *part;
1504 struct GraphicInfo *c = &anim->control_info;
1505 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1506 int state, active_part_nr;
1510 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1511 anim->mode_nr, anim->nr, anim->num_parts);
1512 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1516 printf("::: %s(%d): %d, %d, %d [%d]\n",
1517 (action == ANIM_START ? "ANIM_START" :
1518 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1519 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1521 anim->state & ANIM_STATE_RESTART,
1522 anim->state & ANIM_STATE_WAITING,
1523 anim->state & ANIM_STATE_RUNNING,
1530 anim->state = anim->last_state = ANIM_STATE_RESTART;
1531 anim->active_part_nr = anim->last_active_part_nr = 0;
1532 anim->part_counter = 0;
1537 if (anim->state == ANIM_STATE_INACTIVE)
1540 anim->state = anim->last_state;
1541 anim->active_part_nr = anim->last_active_part_nr;
1546 anim->state = ANIM_STATE_INACTIVE;
1548 for (i = 0; i < num_parts; i++)
1549 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1557 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1560 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1561 anim->mode_nr, anim->nr, num_parts);
1564 for (i = 0; i < num_parts; i++)
1566 part = &anim->part[i];
1571 anim->state = ANIM_STATE_RUNNING;
1572 part->state = ANIM_STATE_RESTART;
1577 if (part->state == ANIM_STATE_INACTIVE)
1583 part->state = ANIM_STATE_INACTIVE;
1591 part->state = HandleGlobalAnim_Part(part, part->state);
1593 // when animation mode is "once", stop after animation was played once
1594 if (c->anim_mode & ANIM_ONCE &&
1595 part->state & ANIM_STATE_RESTART)
1596 part->state = ANIM_STATE_INACTIVE;
1599 anim->last_state = anim->state;
1600 anim->last_active_part_nr = anim->active_part_nr;
1605 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1606 anim->active_part_nr = getGlobalAnimationPart(anim);
1608 part = &anim->part[anim->active_part_nr];
1610 // first set all animation parts to "inactive", ...
1611 for (i = 0; i < num_parts; i++)
1612 anim->part[i].state = ANIM_STATE_INACTIVE;
1614 // ... then set current animation parts to "running"
1615 part->state = ANIM_STATE_RUNNING;
1617 anim->state = HandleGlobalAnim_Part(part, anim->state);
1619 if (anim->state & ANIM_STATE_RESTART)
1620 anim->part_counter++;
1622 // when animation mode is "once", stop after all animations were played once
1623 if (c->anim_mode & ANIM_ONCE &&
1624 anim->part_counter == anim->num_parts)
1625 anim->state = ANIM_STATE_INACTIVE;
1627 state = anim->state;
1628 active_part_nr = anim->active_part_nr;
1630 // while the animation parts are pausing (waiting or inactive), play the base
1631 // (main) animation; this corresponds to the "boring player animation" logic
1632 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1635 if (anim->state == ANIM_STATE_WAITING ||
1636 anim->state == ANIM_STATE_INACTIVE)
1638 anim->active_part_nr = anim->num_parts; // part nr of base animation
1639 part = &anim->part[anim->active_part_nr];
1641 if (anim->state != anim->last_state)
1642 part->state = ANIM_STATE_RESTART;
1644 anim->state = ANIM_STATE_RUNNING;
1645 part->state = HandleGlobalAnim_Part(part, part->state);
1649 anim->last_state = state;
1650 anim->last_active_part_nr = active_part_nr;
1653 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1658 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1659 ctrl->nr, ctrl->num_anims);
1662 for (i = 0; i < ctrl->num_anims; i++)
1663 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1666 static void HandleGlobalAnim(int action, int game_mode)
1669 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1672 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1675 static void DoAnimationExt(void)
1680 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1683 // global animations now synchronized with frame delay of screen update
1686 for (i = 0; i < NUM_GAME_MODES; i++)
1687 HandleGlobalAnim(ANIM_CONTINUE, i);
1690 // force screen redraw in next frame to continue drawing global animations
1691 redraw_mask = REDRAW_ALL;
1695 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1699 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1700 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1701 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1702 ANIM_DELAY_ACTION_NONE);
1704 if (delay_action == ANIM_DELAY_ACTION_NONE)
1707 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1710 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1712 int event_action = (part->init_event_state ?
1713 part->control_info.init_event_action :
1714 part->control_info.anim_event_action);
1716 if (event_action == ANIM_EVENT_ACTION_NONE)
1719 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1721 // check if further actions are allowed to be executed
1722 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1728 static void InitGlobalAnim_Clickable(void)
1732 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1734 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1737 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1739 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1742 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1744 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1746 if (part->triggered)
1747 part->clicked = TRUE;
1749 part->triggered = FALSE;
1750 part->clickable = FALSE;
1756 #define ANIM_CLICKED_RESET 0
1757 #define ANIM_CLICKED_PRESSED 1
1758 #define ANIM_CLICKED_RELEASED 2
1760 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1762 boolean click_consumed = FALSE;
1763 boolean anything_clicked = FALSE;
1764 boolean any_part_clicked = FALSE;
1765 boolean any_event_action = FALSE;
1769 // check game modes in reverse draw order (to stop when clicked)
1770 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1772 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1775 // check animations in reverse draw order (to stop when clicked)
1776 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1778 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1781 // check animation parts in reverse draw order (to stop when clicked)
1782 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1784 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1786 if (clicked_event == ANIM_CLICKED_RESET)
1788 part->clicked = FALSE;
1793 if (!part->clickable)
1796 if (!(part->state & ANIM_STATE_RUNNING))
1799 // always handle "any" click events (clicking anywhere on screen) ...
1800 if (clicked_event == ANIM_CLICKED_PRESSED &&
1801 isClickablePart(part, ANIM_EVENT_ANY))
1803 #if DEBUG_ANIM_EVENTS
1804 printf("::: => %d.%d TRIGGERED BY ANY\n",
1805 part->old_anim_nr + 1, part->old_nr + 1);
1808 anything_clicked = part->clicked = TRUE;
1809 click_consumed |= clickConsumed(part);
1812 // always handle "unclick:any" events (releasing anywhere on screen) ...
1813 if (clicked_event == ANIM_CLICKED_RELEASED &&
1814 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1816 #if DEBUG_ANIM_EVENTS
1817 printf("::: => %d.%d TRIGGERED BY UNCLICK:ANY\n",
1818 part->old_anim_nr + 1, part->old_nr + 1);
1821 anything_clicked = part->clicked = TRUE;
1822 click_consumed |= clickConsumed(part);
1825 // ... but only handle the first (topmost) clickable animation
1826 if (any_part_clicked)
1829 if (clicked_event == ANIM_CLICKED_PRESSED &&
1830 isClickedPart(part, mx, my, TRUE))
1833 printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
1834 part->control_info.anim_event_action);
1837 // after executing event action, ignore any further actions
1838 if (!any_event_action && DoGlobalAnim_EventAction(part))
1839 any_event_action = TRUE;
1841 // determine if mouse clicks should be blocked from other animations
1842 any_part_clicked |= clickConsumed(part);
1844 if (isClickablePart(part, ANIM_EVENT_SELF))
1846 #if DEBUG_ANIM_EVENTS
1847 printf("::: => %d.%d TRIGGERED BY SELF\n",
1848 part->old_anim_nr + 1, part->old_nr + 1);
1851 anything_clicked = part->clicked = TRUE;
1852 click_consumed |= clickConsumed(part);
1855 // check if this click is defined to trigger other animations
1856 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1857 ANIM_EVENT_CLICK, "CLICK");
1863 if (anything_clicked)
1865 handle_click = TRUE;
1867 for (i = 0; i < NUM_GAME_MODES; i++)
1868 HandleGlobalAnim(ANIM_CONTINUE, i);
1870 handle_click = FALSE;
1872 // prevent ignoring release event if processed within same game frame
1873 StopProcessingEvents();
1876 return (click_consumed || any_event_action);
1879 static void ResetGlobalAnim_Clickable(void)
1881 InitGlobalAnim_Clickable();
1884 static void ResetGlobalAnim_Clicked(void)
1886 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1889 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1891 static boolean click_consumed = FALSE;
1892 static int last_button = 0;
1893 boolean press_event;
1894 boolean release_event;
1895 boolean click_consumed_current = click_consumed;
1897 if (button != 0 && force_click)
1900 // check if button state has changed since last invocation
1901 press_event = (button != 0 && last_button == 0);
1902 release_event = (button == 0 && last_button != 0);
1903 last_button = button;
1907 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1908 click_consumed_current = click_consumed;
1913 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
1914 click_consumed = FALSE;
1917 return click_consumed_current;