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;
234 // ============================================================================
235 // generic animation frame calculation
236 // ============================================================================
238 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
243 sync_frame += start_frame * delay;
245 if (mode & ANIM_LOOP) // looping animation
247 frame = (sync_frame % (delay * num_frames)) / delay;
249 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
251 frame = sync_frame / delay;
253 if (frame > num_frames - 1)
254 frame = num_frames - 1;
256 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
258 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
260 frame = (sync_frame % (delay * max_anim_frames)) / delay;
261 frame = (frame < num_frames ? frame : max_anim_frames - frame);
263 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
265 int max_anim_frames = 2 * num_frames;
267 frame = (sync_frame % (delay * max_anim_frames)) / delay;
268 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
270 else if (mode & ANIM_RANDOM) // play frames in random order
272 // note: expect different frames for the same delay cycle!
274 if (gfx.anim_random_frame < 0)
275 frame = GetSimpleRandom(num_frames);
277 frame = gfx.anim_random_frame % num_frames;
279 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
281 frame = sync_frame % num_frames;
284 if (mode & ANIM_REVERSE) // use reverse animation direction
285 frame = num_frames - frame - 1;
291 // ============================================================================
292 // global animation functions
293 // ============================================================================
295 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
297 struct GraphicInfo *c = &anim->control_info;
298 int last_anim_random_frame = gfx.anim_random_frame;
301 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
303 part_nr = getAnimationFrame(anim->num_parts, 1,
304 c->anim_mode, c->anim_start_frame,
307 gfx.anim_random_frame = last_anim_random_frame;
312 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
314 const struct GlobalAnimPartControlInfo *o1 =
315 (struct GlobalAnimPartControlInfo *)obj1;
316 const struct GlobalAnimPartControlInfo *o2 =
317 (struct GlobalAnimPartControlInfo *)obj2;
320 if (o1->control_info.draw_order != o2->control_info.draw_order)
321 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
323 compare_result = o1->nr - o2->nr;
325 return compare_result;
328 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
330 const struct GlobalAnimMainControlInfo *o1 =
331 (struct GlobalAnimMainControlInfo *)obj1;
332 const struct GlobalAnimMainControlInfo *o2 =
333 (struct GlobalAnimMainControlInfo *)obj2;
336 if (o1->control_info.draw_order != o2->control_info.draw_order)
337 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
339 compare_result = o1->nr - o2->nr;
341 return compare_result;
344 static void InitToonControls(void)
346 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
347 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
348 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
349 int mode_nr, anim_nr, part_nr;
350 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
351 int num_toons = MAX_NUM_TOONS;
354 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
355 num_toons = global.num_toons;
357 mode_nr = mode_nr_toons;
358 anim_nr = ctrl->num_anims;
361 anim->mode_nr = mode_nr;
362 anim->control_info = graphic_info[control];
365 anim->num_parts_all = 0;
366 anim->part_counter = 0;
367 anim->active_part_nr = 0;
369 anim->has_base = FALSE;
371 anim->last_x = POS_OFFSCREEN;
372 anim->last_y = POS_OFFSCREEN;
374 anim->init_delay_counter = 0;
376 anim->state = ANIM_STATE_INACTIVE;
380 for (i = 0; i < num_toons; i++)
382 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
383 int sound = SND_UNDEFINED;
384 int music = MUS_UNDEFINED;
385 int graphic = IMG_TOON_1 + i;
386 int control = graphic;
389 part->anim_nr = anim_nr;
390 part->mode_nr = mode_nr;
392 part->is_base = FALSE;
396 part->graphic = graphic;
398 part->graphic_info = graphic_info[graphic];
399 part->control_info = graphic_info[control];
401 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
403 part->control_info.init_delay_fixed = 0;
404 part->control_info.init_delay_random = 150;
406 part->control_info.x = ARG_UNDEFINED_VALUE;
407 part->control_info.y = ARG_UNDEFINED_VALUE;
409 part->initial_anim_sync_frame = 0;
411 part->step_delay = 0;
412 part->step_delay_value = graphic_info[control].step_delay;
414 part->state = ANIM_STATE_INACTIVE;
415 part->last_anim_status = -1;
418 anim->num_parts_all++;
426 static void InitGlobalAnimControls(void)
429 int mode_nr, anim_nr, part_nr;
430 int sound, music, graphic, control;
434 for (m = 0; m < NUM_GAME_MODES; m++)
438 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
445 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
447 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
448 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
450 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
452 // if no base animation parameters defined, use default values
453 if (control == IMG_UNDEFINED)
454 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
457 anim->mode_nr = mode_nr;
458 anim->control_info = graphic_info[control];
461 anim->num_parts_all = 0;
462 anim->part_counter = 0;
463 anim->active_part_nr = 0;
465 anim->has_base = FALSE;
467 anim->last_x = POS_OFFSCREEN;
468 anim->last_y = POS_OFFSCREEN;
470 anim->init_delay_counter = 0;
472 anim->state = ANIM_STATE_INACTIVE;
476 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
478 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
480 sound = global_anim_info[a].sound[p][m];
481 music = global_anim_info[a].music[p][m];
482 graphic = global_anim_info[a].graphic[p][m];
483 control = global_anim_info[ctrl_id].graphic[p][m];
485 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
486 control == IMG_UNDEFINED)
490 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
491 m, a, p, mode_nr, anim_nr, part_nr, control);
495 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
496 m, a, p, mode_nr, anim_nr, part_nr, sound);
500 part->old_anim_nr = a;
503 part->anim_nr = anim_nr;
504 part->mode_nr = mode_nr;
508 part->graphic = graphic;
510 part->graphic_info = graphic_info[graphic];
511 part->control_info = graphic_info[control];
513 part->initial_anim_sync_frame = 0;
515 part->step_delay = 0;
516 part->step_delay_value = graphic_info[control].step_delay;
518 part->state = ANIM_STATE_INACTIVE;
519 part->last_anim_status = -1;
521 anim->num_parts_all++;
523 if (p < GLOBAL_ANIM_ID_PART_BASE)
525 part->is_base = FALSE;
532 part->is_base = TRUE;
535 anim->has_base = TRUE;
539 if (anim->num_parts > 0 || anim->has_base)
549 // sort all animations according to draw_order and animation number
550 for (m = 0; m < NUM_GAME_MODES; m++)
552 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
554 // sort all main animations for this game mode
555 qsort(ctrl->anim, ctrl->num_anims,
556 sizeof(struct GlobalAnimMainControlInfo),
557 compareGlobalAnimMainControlInfo);
559 for (a = 0; a < ctrl->num_anims; a++)
561 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
563 // sort all animation parts for this main animation
564 qsort(anim->part, anim->num_parts,
565 sizeof(struct GlobalAnimPartControlInfo),
566 compareGlobalAnimPartControlInfo);
570 for (i = 0; i < NUM_GAME_MODES; i++)
571 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
572 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
573 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
574 game_mode_anim_classes_list[i].class;
576 for (i = 0; i < NUM_ANIM_CLASSES; i++)
577 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
578 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
579 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
580 anim_class_game_modes_list[i].game_mode;
582 anim_status_last_before_fading = GAME_MODE_LOADING;
583 anim_status_last = GAME_MODE_LOADING;
584 anim_classes_last = ANIM_CLASS_NONE;
587 void InitGlobalAnimations(void)
589 InitGlobalAnimControls();
592 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
594 Bitmap *fade_bitmap =
595 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
596 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
597 int game_mode_anim_action[NUM_GAME_MODES];
603 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
604 drawing_target == DRAW_TO_SCREEN)
607 // always start with reliable default values (no animation actions)
608 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
609 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
611 if (global.anim_status != anim_status_last)
613 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
614 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
615 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
618 if (drawing_target == DRAW_TO_FADE_TARGET)
621 // special case: changing from/to this screen is done without fading
622 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
623 anim_status_last == GAME_MODE_PSEUDO_TYPENAME)
626 // ---------- part 1 ------------------------------------------------------
627 // start or stop global animations by change of game mode
628 // (special handling of animations for "current screen" and "all screens")
630 if (global.anim_status_next != anim_status_last_before_fading)
632 // stop animations for last screen before fading to new screen
633 game_mode_anim_action[anim_status_last] = ANIM_STOP;
635 // start animations for current screen after fading to new screen
636 game_mode_anim_action[global.anim_status] = ANIM_START;
639 // start animations for all screens after loading new artwork set
640 if (anim_status_last == GAME_MODE_LOADING)
641 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
643 // ---------- part 2 ------------------------------------------------------
644 // start or stop global animations by change of animation class
645 // (generic handling of animations for "class of screens")
647 for (i = 0; i < NUM_ANIM_CLASSES; i++)
649 int anim_class_check = (1 << i);
650 int anim_class_game_mode = anim_class_game_modes[i];
651 int anim_class_last = anim_classes_last & anim_class_check;
652 int anim_class_next = anim_classes_next & anim_class_check;
654 // stop animations for changed screen class before fading to new screen
655 if (before_fading && anim_class_last && !anim_class_next)
656 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
658 // start animations for changed screen class after fading to new screen
659 if (after_fading && !anim_class_last && anim_class_next)
660 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
663 if (drawing_target == DRAW_TO_SCREEN)
667 anim_classes_last = anim_classes_next;
668 anim_status_last_before_fading = global.anim_status;
671 anim_status_last = global.anim_status;
673 // start or stop animations determined to be started or stopped above
674 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
675 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
676 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
678 else if (drawing_target == DRAW_TO_FADE_TARGET)
680 drawing_to_fading_buffer = TRUE;
682 // start animations determined to be (temporary) started above
683 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
684 if (game_mode_anim_action[mode_nr] == ANIM_START)
685 HandleGlobalAnim(ANIM_START, mode_nr);
689 if (global.anim_status == GAME_MODE_LOADING)
692 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
694 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
697 // when preparing source fading buffer, only draw animations to be stopped
698 if (drawing_target == DRAW_TO_FADE_SOURCE &&
699 game_mode_anim_action[mode_nr] != ANIM_STOP)
702 // when preparing target fading buffer, only draw animations to be started
703 if (drawing_target == DRAW_TO_FADE_TARGET &&
704 game_mode_anim_action[mode_nr] != ANIM_START)
708 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
709 mode_nr != game_status)
713 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
715 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
716 struct GraphicInfo *c = &anim->control_info;
717 int part_first, part_last;
720 if (!(anim->state & ANIM_STATE_RUNNING))
723 part_first = part_last = anim->active_part_nr;
725 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
727 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
730 part_last = num_parts - 1;
733 for (part_nr = part_first; part_nr <= part_last; part_nr++)
735 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
736 struct GraphicInfo *g = &part->graphic_info;
739 int width = g->width;
740 int height = g->height;
747 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
748 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
749 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
750 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
752 if (!(part->state & ANIM_STATE_RUNNING))
755 if (part->drawing_stage != drawing_stage)
764 else if (part->x > part->viewport_width - g->width)
765 width -= (part->x - (part->viewport_width - g->width));
773 else if (part->y > part->viewport_height - g->height)
774 height -= (part->y - (part->viewport_height - g->height));
776 if (width <= 0 || height <= 0)
779 dst_x += part->viewport_x;
780 dst_y += part->viewport_y;
782 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
783 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
784 g->anim_mode, g->anim_start_frame,
787 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
793 if (drawing_target == DRAW_TO_SCREEN)
794 blit_screen(src_bitmap, src_x, src_y, width, height,
797 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
803 if (drawing_target == DRAW_TO_FADE_TARGET)
805 // stop animations determined to be (temporary) started above
806 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
807 if (game_mode_anim_action[mode_nr] == ANIM_START)
808 HandleGlobalAnim(ANIM_STOP, mode_nr);
810 drawing_to_fading_buffer = FALSE;
814 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
816 int last_cursor_mode_override = gfx.cursor_mode_override;
818 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
820 ResetGlobalAnim_Clickable();
822 gfx.cursor_mode_override = CURSOR_UNDEFINED;
825 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
827 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
829 ResetGlobalAnim_Clicked();
832 if (gfx.cursor_mode_override != last_cursor_mode_override)
833 SetMouseCursor(gfx.cursor_mode);
836 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
842 boolean changed = FALSE;
844 if (part->last_anim_status == global.anim_status &&
845 part->control_info.class != get_hash_from_key("pointer"))
848 part->last_anim_status = global.anim_status;
850 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
852 if (part->control_info.class == get_hash_from_key("window") ||
853 part->control_info.class == get_hash_from_key("border"))
857 viewport_width = WIN_XSIZE;
858 viewport_height = WIN_YSIZE;
860 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
862 else if (part->control_info.class == get_hash_from_key("pointer"))
864 viewport_x = gfx.mouse_x + part->control_info.x;
865 viewport_y = gfx.mouse_y + part->control_info.y;
866 viewport_width = part->graphic_info.width;
867 viewport_height = part->graphic_info.height;
869 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
871 gfx.cursor_mode_override = CURSOR_NONE;
873 else if (part->control_info.class == get_hash_from_key("door_1"))
877 viewport_width = DXSIZE;
878 viewport_height = DYSIZE;
880 else if (part->control_info.class == get_hash_from_key("door_2"))
882 if (part->mode_nr == GAME_MODE_EDITOR)
886 viewport_width = EXSIZE;
887 viewport_height = EYSIZE;
893 viewport_width = VXSIZE;
894 viewport_height = VYSIZE;
897 else // default: "playfield"
899 viewport_x = REAL_SX;
900 viewport_y = REAL_SY;
901 viewport_width = FULL_SXSIZE;
902 viewport_height = FULL_SYSIZE;
905 if (viewport_x != part->viewport_x ||
906 viewport_y != part->viewport_y ||
907 viewport_width != part->viewport_width ||
908 viewport_height != part->viewport_height)
910 part->viewport_x = viewport_x;
911 part->viewport_y = viewport_y;
912 part->viewport_width = viewport_width;
913 part->viewport_height = viewport_height;
921 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
923 int sound = part->sound;
925 if (sound == SND_UNDEFINED)
928 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
929 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
932 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
933 if (IS_LOOP_SOUND(sound))
934 PlaySoundLoop(sound);
939 printf("::: PLAY SOUND %d.%d.%d: %d\n",
940 part->anim_nr, part->nr, part->mode_nr, sound);
944 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
946 int sound = part->sound;
948 if (sound == SND_UNDEFINED)
954 printf("::: STOP SOUND %d.%d.%d: %d\n",
955 part->anim_nr, part->nr, part->mode_nr, sound);
959 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
961 int music = part->music;
963 if (music == MUS_UNDEFINED)
966 if (!setup.sound_music)
969 if (IS_LOOP_MUSIC(music))
970 PlayMusicLoop(music);
975 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
976 part->anim_nr, part->nr, part->mode_nr, music);
980 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
982 int music = part->music;
984 if (music == MUS_UNDEFINED)
990 printf("::: STOP MUSIC %d.%d.%d: %d\n",
991 part->anim_nr, part->nr, part->mode_nr, music);
995 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
997 // when drawing animations to fading buffer, do not play sounds or music
998 if (drawing_to_fading_buffer)
1001 PlayGlobalAnimSound(part);
1002 PlayGlobalAnimMusic(part);
1005 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1007 StopGlobalAnimSound(part);
1008 StopGlobalAnimMusic(part);
1011 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1013 // when drawing animations to fading buffer, do not play sounds
1014 if (drawing_to_fading_buffer)
1017 // loop sounds only expire when playing
1018 if (game_status != GAME_MODE_PLAYING)
1021 // check if any sound is defined for this animation part
1022 if (part->sound == SND_UNDEFINED)
1025 // normal (non-loop) sounds do not expire when playing
1026 if (!IS_LOOP_SOUND(part->sound))
1029 // prevent expiring loop sounds when playing
1030 PlayGlobalAnimSound(part);
1033 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1035 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1037 if (mask & ANIM_EVENT_ANY)
1038 return (anim_event & ANIM_EVENT_ANY);
1039 else if (mask & ANIM_EVENT_SELF)
1040 return (anim_event & ANIM_EVENT_SELF);
1041 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1042 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1044 return (anim_event == mask ||
1045 anim_event == mask_anim_only);
1048 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1050 struct GraphicInfo *c = &part->control_info;
1051 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1052 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1055 for (i = 0; i < num_init_events; i++)
1057 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1059 if (checkGlobalAnimEvent(init_event, mask))
1063 for (i = 0; i < num_anim_events; i++)
1065 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1067 if (checkGlobalAnimEvent(anim_event, mask))
1074 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1075 int mx, int my, boolean clicked)
1077 struct GraphicInfo *g = &part->graphic_info;
1078 int part_x = part->viewport_x + part->x;
1079 int part_y = part->viewport_y + part->y;
1080 int part_width = g->width;
1081 int part_height = g->height;
1083 // check if mouse click was detected at all
1087 // check if mouse click is inside the animation part's viewport
1088 if (mx < part->viewport_x ||
1089 mx >= part->viewport_x + part->viewport_width ||
1090 my < part->viewport_y ||
1091 my >= part->viewport_y + part->viewport_height)
1094 // check if mouse click is inside the animation part's graphic
1096 mx >= part_x + part_width ||
1098 my >= part_y + part_height)
1104 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1106 return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1109 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1110 boolean *anything_clicked,
1111 boolean *any_event_action,
1112 int event_value, char *info_text)
1114 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1116 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1117 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1118 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1121 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1125 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1127 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1130 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1132 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1134 if (part2->state != ANIM_STATE_RUNNING)
1137 if (isClickablePart(part2, mask))
1139 part2->triggered = TRUE;
1140 *anything_clicked = clickConsumed(part); // click was on "part"!
1142 #if DEBUG_ANIM_EVENTS
1143 printf("::: => %d.%d TRIGGERED BY %s OF %d.%d\n",
1144 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1145 part->old_anim_nr + 1, part->old_nr + 1);
1148 printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
1149 part2->control_info.anim_event_action);
1152 // after executing event action, ignore any further actions
1153 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1154 *any_event_action = TRUE;
1158 struct GraphicInfo *c = &part2->control_info;
1160 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1161 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1163 if (isClickablePart(part2, mask))
1164 printf(" <--- TRIGGERED BY %d.%d",
1173 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1174 int delay_type, char *info_text)
1176 #if DEBUG_ANIM_DELAY
1177 printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
1180 DoGlobalAnim_DelayAction(part, delay_type);
1183 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1184 int event_value, char *info_text)
1186 #if DEBUG_ANIM_EVENTS
1187 printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
1190 boolean anything_clicked = FALSE;
1191 boolean any_event_action = FALSE;
1193 // check if this event is defined to trigger other animations
1194 InitGlobalAnim_Triggered(part, &anything_clicked, &any_event_action,
1195 event_value, info_text);
1198 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1201 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1202 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1203 struct GraphicInfo *g = &part->graphic_info;
1204 struct GraphicInfo *c = &part->control_info;
1205 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1207 if (viewport_changed)
1208 state |= ANIM_STATE_RESTART;
1210 if (state & ANIM_STATE_RESTART)
1212 // when drawing animations to fading buffer, only start fixed animations
1213 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1214 c->y == ARG_UNDEFINED_VALUE))
1215 return ANIM_STATE_INACTIVE;
1217 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1219 part->init_delay_counter =
1220 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1222 part->anim_delay_counter =
1223 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1225 part->post_delay_counter = 0;
1227 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1228 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1230 part->initial_anim_sync_frame =
1231 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1233 if (c->direction & MV_HORIZONTAL)
1235 int pos_bottom = part->viewport_height - g->height;
1237 if (c->position == POS_TOP)
1239 else if (c->position == POS_UPPER)
1240 part->y = GetSimpleRandom(pos_bottom / 2);
1241 else if (c->position == POS_MIDDLE)
1242 part->y = pos_bottom / 2;
1243 else if (c->position == POS_LOWER)
1244 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1245 else if (c->position == POS_BOTTOM)
1246 part->y = pos_bottom;
1248 part->y = GetSimpleRandom(pos_bottom);
1250 if (c->direction == MV_RIGHT)
1252 part->step_xoffset = c->step_offset;
1253 part->x = -g->width + part->step_xoffset;
1257 part->step_xoffset = -c->step_offset;
1258 part->x = part->viewport_width + part->step_xoffset;
1261 part->step_yoffset = 0;
1263 else if (c->direction & MV_VERTICAL)
1265 int pos_right = part->viewport_width - g->width;
1267 if (c->position == POS_LEFT)
1269 else if (c->position == POS_RIGHT)
1270 part->x = pos_right;
1272 part->x = GetSimpleRandom(pos_right);
1274 if (c->direction == MV_DOWN)
1276 part->step_yoffset = c->step_offset;
1277 part->y = -g->height + part->step_yoffset;
1281 part->step_yoffset = -c->step_offset;
1282 part->y = part->viewport_height + part->step_yoffset;
1285 part->step_xoffset = 0;
1292 part->step_xoffset = 0;
1293 part->step_yoffset = 0;
1296 if (part->control_info.class != get_hash_from_key("pointer"))
1298 if (c->x != ARG_UNDEFINED_VALUE)
1300 if (c->y != ARG_UNDEFINED_VALUE)
1304 if (c->position == POS_LAST &&
1305 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1306 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1308 part->x = anim->last_x;
1309 part->y = anim->last_y;
1312 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1313 part->step_xoffset = c->step_xoffset;
1314 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1315 part->step_yoffset = c->step_yoffset;
1317 if (part->init_delay_counter == 0 &&
1318 !part->init_event_state)
1320 PlayGlobalAnimSoundAndMusic(part);
1322 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1323 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1327 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1331 if (part->clicked &&
1332 part->init_event_state)
1334 if (part->initial_anim_sync_frame > 0)
1335 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1337 part->init_delay_counter = 1;
1338 part->init_event_state = FALSE;
1340 part->clicked = FALSE;
1343 if (part->clicked &&
1344 part->anim_event_state)
1346 part->anim_delay_counter = 1;
1347 part->anim_event_state = FALSE;
1349 part->clicked = FALSE;
1352 if (part->init_delay_counter > 0)
1354 part->init_delay_counter--;
1356 if (part->init_delay_counter == 0)
1358 part->init_event_state = FALSE;
1360 PlayGlobalAnimSoundAndMusic(part);
1362 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1363 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1366 return ANIM_STATE_WAITING;
1369 if (part->init_event_state)
1370 return ANIM_STATE_WAITING;
1372 // animation part is now running/visible and therefore clickable
1373 part->clickable = TRUE;
1375 // check if moving animation has left the visible screen area
1376 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1377 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1378 (part->y <= -g->height && part->step_yoffset <= 0) ||
1379 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1381 // do not wait for "anim" events for off-screen animations
1382 part->anim_event_state = FALSE;
1384 // do not stop animation before "anim" or "post" counter are finished
1385 if (part->anim_delay_counter == 0 &&
1386 part->post_delay_counter == 0)
1388 StopGlobalAnimSoundAndMusic(part);
1390 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1392 part->post_delay_counter =
1393 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1395 if (part->post_delay_counter > 0)
1396 return ANIM_STATE_RUNNING;
1398 // drawing last frame not needed here -- animation not visible anymore
1399 return ANIM_STATE_RESTART;
1403 if (part->anim_delay_counter > 0)
1405 part->anim_delay_counter--;
1407 if (part->anim_delay_counter == 0)
1409 part->anim_event_state = FALSE;
1411 StopGlobalAnimSoundAndMusic(part);
1413 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1414 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1416 part->post_delay_counter =
1417 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1419 if (part->post_delay_counter > 0)
1420 return ANIM_STATE_RUNNING;
1422 // additional state "RUNNING" required to not skip drawing last frame
1423 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1427 if (part->post_delay_counter > 0)
1429 part->post_delay_counter--;
1431 if (part->post_delay_counter == 0)
1433 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1434 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1436 return ANIM_STATE_RESTART;
1439 return ANIM_STATE_WAITING;
1442 // special case to prevent expiring loop sounds when playing
1443 PlayGlobalAnimSoundIfLoop(part);
1445 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1447 return ANIM_STATE_RUNNING;
1451 static unsigned int last_counter = -1;
1452 unsigned int counter = Counter();
1454 printf("::: NEXT ANIM PART [%d, %d]\n",
1455 anim_sync_frame, counter - last_counter);
1457 last_counter = counter;
1461 part->x += part->step_xoffset;
1462 part->y += part->step_yoffset;
1464 anim->last_x = part->x;
1465 anim->last_y = part->y;
1467 return ANIM_STATE_RUNNING;
1470 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1473 struct GlobalAnimPartControlInfo *part;
1474 struct GraphicInfo *c = &anim->control_info;
1475 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1476 int state, active_part_nr;
1480 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1481 anim->mode_nr, anim->nr, anim->num_parts);
1482 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1486 printf("::: %s(%d): %d, %d, %d [%d]\n",
1487 (action == ANIM_START ? "ANIM_START" :
1488 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1489 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1491 anim->state & ANIM_STATE_RESTART,
1492 anim->state & ANIM_STATE_WAITING,
1493 anim->state & ANIM_STATE_RUNNING,
1500 anim->state = anim->last_state = ANIM_STATE_RESTART;
1501 anim->active_part_nr = anim->last_active_part_nr = 0;
1502 anim->part_counter = 0;
1507 if (anim->state == ANIM_STATE_INACTIVE)
1510 anim->state = anim->last_state;
1511 anim->active_part_nr = anim->last_active_part_nr;
1516 anim->state = ANIM_STATE_INACTIVE;
1518 for (i = 0; i < num_parts; i++)
1519 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1527 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1530 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1531 anim->mode_nr, anim->nr, num_parts);
1534 for (i = 0; i < num_parts; i++)
1536 part = &anim->part[i];
1541 anim->state = ANIM_STATE_RUNNING;
1542 part->state = ANIM_STATE_RESTART;
1547 if (part->state == ANIM_STATE_INACTIVE)
1553 part->state = ANIM_STATE_INACTIVE;
1561 part->state = HandleGlobalAnim_Part(part, part->state);
1563 // when animation mode is "once", stop after animation was played once
1564 if (c->anim_mode & ANIM_ONCE &&
1565 part->state & ANIM_STATE_RESTART)
1566 part->state = ANIM_STATE_INACTIVE;
1569 anim->last_state = anim->state;
1570 anim->last_active_part_nr = anim->active_part_nr;
1575 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1576 anim->active_part_nr = getGlobalAnimationPart(anim);
1578 part = &anim->part[anim->active_part_nr];
1580 // first set all animation parts to "inactive", ...
1581 for (i = 0; i < num_parts; i++)
1582 anim->part[i].state = ANIM_STATE_INACTIVE;
1584 // ... then set current animation parts to "running"
1585 part->state = ANIM_STATE_RUNNING;
1587 anim->state = HandleGlobalAnim_Part(part, anim->state);
1589 if (anim->state & ANIM_STATE_RESTART)
1590 anim->part_counter++;
1592 // when animation mode is "once", stop after all animations were played once
1593 if (c->anim_mode & ANIM_ONCE &&
1594 anim->part_counter == anim->num_parts)
1595 anim->state = ANIM_STATE_INACTIVE;
1597 state = anim->state;
1598 active_part_nr = anim->active_part_nr;
1600 // while the animation parts are pausing (waiting or inactive), play the base
1601 // (main) animation; this corresponds to the "boring player animation" logic
1602 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1605 if (anim->state == ANIM_STATE_WAITING ||
1606 anim->state == ANIM_STATE_INACTIVE)
1608 anim->active_part_nr = anim->num_parts; // part nr of base animation
1609 part = &anim->part[anim->active_part_nr];
1611 if (anim->state != anim->last_state)
1612 part->state = ANIM_STATE_RESTART;
1614 anim->state = ANIM_STATE_RUNNING;
1615 part->state = HandleGlobalAnim_Part(part, part->state);
1619 anim->last_state = state;
1620 anim->last_active_part_nr = active_part_nr;
1623 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1628 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1629 ctrl->nr, ctrl->num_anims);
1632 for (i = 0; i < ctrl->num_anims; i++)
1633 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1636 static void HandleGlobalAnim(int action, int game_mode)
1639 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1642 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1645 static void DoAnimationExt(void)
1650 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1653 // global animations now synchronized with frame delay of screen update
1656 for (i = 0; i < NUM_GAME_MODES; i++)
1657 HandleGlobalAnim(ANIM_CONTINUE, i);
1660 // force screen redraw in next frame to continue drawing global animations
1661 redraw_mask = REDRAW_ALL;
1665 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1669 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1670 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1671 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1672 ANIM_DELAY_ACTION_NONE);
1674 if (delay_action == ANIM_DELAY_ACTION_NONE)
1677 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1680 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1682 int event_action = (part->init_event_state ?
1683 part->control_info.init_event_action :
1684 part->control_info.anim_event_action);
1686 if (event_action == ANIM_EVENT_ACTION_NONE)
1689 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1691 // check if further actions are allowed to be executed
1692 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1698 static void InitGlobalAnim_Clickable(void)
1702 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1704 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1707 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1709 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1712 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1714 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1716 if (part->triggered)
1717 part->clicked = TRUE;
1719 part->triggered = FALSE;
1720 part->clickable = FALSE;
1726 #define ANIM_CLICKED_RESET 0
1727 #define ANIM_CLICKED_PRESSED 1
1728 #define ANIM_CLICKED_RELEASED 2
1730 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1732 boolean anything_clicked = FALSE;
1733 boolean any_part_clicked = FALSE;
1734 boolean any_event_action = FALSE;
1737 // check game modes in reverse draw order (to stop when clicked)
1738 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1740 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1743 // check animations in reverse draw order (to stop when clicked)
1744 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1746 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1749 // check animation parts in reverse draw order (to stop when clicked)
1750 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1752 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1754 if (clicked_event == ANIM_CLICKED_RESET)
1756 part->clicked = FALSE;
1761 if (!part->clickable)
1764 if (part->state != ANIM_STATE_RUNNING)
1767 // always handle "any" click events (clicking anywhere on screen) ...
1768 if (clicked_event == ANIM_CLICKED_PRESSED &&
1769 isClickablePart(part, ANIM_EVENT_ANY))
1771 #if DEBUG_ANIM_EVENTS
1772 printf("::: => %d.%d TRIGGERED BY ANY\n",
1773 part->old_anim_nr + 1, part->old_nr + 1);
1776 part->clicked = TRUE;
1777 anything_clicked = clickConsumed(part);
1780 // always handle "unclick:any" events (releasing anywhere on screen) ...
1781 if (clicked_event == ANIM_CLICKED_RELEASED &&
1782 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1784 #if DEBUG_ANIM_EVENTS
1785 printf("::: => %d.%d TRIGGERED BY UNCLICK:ANY\n",
1786 part->old_anim_nr + 1, part->old_nr + 1);
1789 part->clicked = TRUE;
1790 anything_clicked = clickConsumed(part);
1793 // ... but only handle the first (topmost) clickable animation
1794 if (any_part_clicked)
1797 if (clicked_event == ANIM_CLICKED_PRESSED &&
1798 isClickedPart(part, mx, my, TRUE))
1801 printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
1802 part->control_info.anim_event_action);
1805 // after executing event action, ignore any further actions
1806 if (!any_event_action && DoGlobalAnim_EventAction(part))
1807 any_event_action = TRUE;
1809 // determine if mouse clicks should be blocked from other animations
1810 any_part_clicked = clickConsumed(part);
1812 if (isClickablePart(part, ANIM_EVENT_SELF))
1814 #if DEBUG_ANIM_EVENTS
1815 printf("::: => %d.%d TRIGGERED BY SELF\n",
1816 part->old_anim_nr + 1, part->old_nr + 1);
1819 part->clicked = TRUE;
1820 anything_clicked = clickConsumed(part);
1823 // check if this click is defined to trigger other animations
1824 InitGlobalAnim_Triggered(part, &anything_clicked, &any_event_action,
1825 ANIM_EVENT_CLICK, "CLICK");
1831 return (anything_clicked || any_event_action);
1834 static void ResetGlobalAnim_Clickable(void)
1836 InitGlobalAnim_Clickable();
1839 static void ResetGlobalAnim_Clicked(void)
1841 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1844 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1846 static boolean click_consumed = FALSE;
1847 static int last_button = 0;
1848 boolean press_event;
1849 boolean release_event;
1850 boolean click_consumed_current = click_consumed;
1852 if (button != 0 && force_click)
1855 // check if button state has changed since last invocation
1856 press_event = (button != 0 && last_button == 0);
1857 release_event = (button == 0 && last_button != 0);
1858 last_button = button;
1862 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1863 click_consumed_current = click_consumed;
1868 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
1869 click_consumed = FALSE;
1872 return click_consumed_current;