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 if (delay < 1) // delay must be at least 1
248 sync_frame += start_frame * delay;
250 if (mode & ANIM_LOOP) // looping animation
252 frame = (sync_frame % (delay * num_frames)) / delay;
254 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
256 frame = sync_frame / delay;
258 if (frame > num_frames - 1)
259 frame = num_frames - 1;
261 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
263 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
265 frame = (sync_frame % (delay * max_anim_frames)) / delay;
266 frame = (frame < num_frames ? frame : max_anim_frames - frame);
268 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
270 int max_anim_frames = 2 * num_frames;
272 frame = (sync_frame % (delay * max_anim_frames)) / delay;
273 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
275 else if (mode & ANIM_RANDOM) // play frames in random order
277 // note: expect different frames for the same delay cycle!
279 if (gfx.anim_random_frame < 0)
280 frame = GetSimpleRandom(num_frames);
282 frame = gfx.anim_random_frame % num_frames;
284 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
286 frame = sync_frame % num_frames;
289 if (mode & ANIM_REVERSE) // use reverse animation direction
290 frame = num_frames - frame - 1;
296 // ============================================================================
297 // global animation functions
298 // ============================================================================
300 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
302 struct GraphicInfo *c = &anim->control_info;
303 int last_anim_random_frame = gfx.anim_random_frame;
306 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
308 part_nr = getAnimationFrame(anim->num_parts, 1,
309 c->anim_mode, c->anim_start_frame,
312 gfx.anim_random_frame = last_anim_random_frame;
317 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
319 const struct GlobalAnimPartControlInfo *o1 =
320 (struct GlobalAnimPartControlInfo *)obj1;
321 const struct GlobalAnimPartControlInfo *o2 =
322 (struct GlobalAnimPartControlInfo *)obj2;
325 if (o1->control_info.draw_order != o2->control_info.draw_order)
326 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
328 compare_result = o1->nr - o2->nr;
330 return compare_result;
333 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
335 const struct GlobalAnimMainControlInfo *o1 =
336 (struct GlobalAnimMainControlInfo *)obj1;
337 const struct GlobalAnimMainControlInfo *o2 =
338 (struct GlobalAnimMainControlInfo *)obj2;
341 if (o1->control_info.draw_order != o2->control_info.draw_order)
342 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
344 compare_result = o1->nr - o2->nr;
346 return compare_result;
349 static void InitToonControls(void)
351 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
352 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
353 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
354 int mode_nr, anim_nr, part_nr;
355 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
356 int num_toons = MAX_NUM_TOONS;
359 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
360 num_toons = global.num_toons;
362 mode_nr = mode_nr_toons;
363 anim_nr = ctrl->num_anims;
366 anim->mode_nr = mode_nr;
367 anim->control_info = graphic_info[control];
370 anim->num_parts_all = 0;
371 anim->part_counter = 0;
372 anim->active_part_nr = 0;
374 anim->has_base = FALSE;
376 anim->last_x = POS_OFFSCREEN;
377 anim->last_y = POS_OFFSCREEN;
379 anim->init_delay_counter = 0;
381 anim->state = ANIM_STATE_INACTIVE;
385 for (i = 0; i < num_toons; i++)
387 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
388 int sound = SND_UNDEFINED;
389 int music = MUS_UNDEFINED;
390 int graphic = IMG_TOON_1 + i;
391 int control = graphic;
394 part->anim_nr = anim_nr;
395 part->mode_nr = mode_nr;
397 part->is_base = FALSE;
401 part->graphic = graphic;
403 part->graphic_info = graphic_info[graphic];
404 part->control_info = graphic_info[control];
406 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
408 part->control_info.init_delay_fixed = 0;
409 part->control_info.init_delay_random = 150;
411 part->control_info.x = ARG_UNDEFINED_VALUE;
412 part->control_info.y = ARG_UNDEFINED_VALUE;
414 part->initial_anim_sync_frame = 0;
416 part->step_delay = 0;
417 part->step_delay_value = graphic_info[control].step_delay;
419 part->state = ANIM_STATE_INACTIVE;
420 part->last_anim_status = -1;
423 anim->num_parts_all++;
431 static void InitGlobalAnimControls(void)
434 int mode_nr, anim_nr, part_nr;
435 int sound, music, graphic, control;
439 for (m = 0; m < NUM_GAME_MODES; m++)
443 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
450 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
452 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
453 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
455 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
457 // if no base animation parameters defined, use default values
458 if (control == IMG_UNDEFINED)
459 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
462 anim->mode_nr = mode_nr;
463 anim->control_info = graphic_info[control];
466 anim->num_parts_all = 0;
467 anim->part_counter = 0;
468 anim->active_part_nr = 0;
470 anim->has_base = FALSE;
472 anim->last_x = POS_OFFSCREEN;
473 anim->last_y = POS_OFFSCREEN;
475 anim->init_delay_counter = 0;
477 anim->state = ANIM_STATE_INACTIVE;
481 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
483 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
485 sound = global_anim_info[a].sound[p][m];
486 music = global_anim_info[a].music[p][m];
487 graphic = global_anim_info[a].graphic[p][m];
488 control = global_anim_info[ctrl_id].graphic[p][m];
490 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
491 control == IMG_UNDEFINED)
495 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
496 m, a, p, mode_nr, anim_nr, part_nr, control);
500 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
501 m, a, p, mode_nr, anim_nr, part_nr, sound);
505 part->old_anim_nr = a;
508 part->anim_nr = anim_nr;
509 part->mode_nr = mode_nr;
513 part->graphic = graphic;
515 part->graphic_info = graphic_info[graphic];
516 part->control_info = graphic_info[control];
518 part->initial_anim_sync_frame = 0;
520 part->step_delay = 0;
521 part->step_delay_value = graphic_info[control].step_delay;
523 part->state = ANIM_STATE_INACTIVE;
524 part->last_anim_status = -1;
526 anim->num_parts_all++;
528 if (p < GLOBAL_ANIM_ID_PART_BASE)
530 part->is_base = FALSE;
537 part->is_base = TRUE;
540 anim->has_base = TRUE;
543 // apply special settings for pointer-style animations
544 if (part->control_info.class == get_hash_from_key("pointer"))
546 // force animation to be on top (must set anim and part control)
547 if (anim->control_info.draw_order == 0)
548 anim->control_info.draw_order = 1000000;
549 if (part->control_info.draw_order == 0)
550 part->control_info.draw_order = 1000000;
552 // force animation to pass-through clicks (must set part control)
553 if (part->control_info.style == STYLE_DEFAULT)
554 part->control_info.style |= STYLE_PASSTHROUGH;
558 if (anim->num_parts > 0 || anim->has_base)
568 // sort all animations according to draw_order and animation number
569 for (m = 0; m < NUM_GAME_MODES; m++)
571 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
573 // sort all main animations for this game mode
574 qsort(ctrl->anim, ctrl->num_anims,
575 sizeof(struct GlobalAnimMainControlInfo),
576 compareGlobalAnimMainControlInfo);
578 for (a = 0; a < ctrl->num_anims; a++)
580 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
582 // sort all animation parts for this main animation
583 qsort(anim->part, anim->num_parts,
584 sizeof(struct GlobalAnimPartControlInfo),
585 compareGlobalAnimPartControlInfo);
589 for (i = 0; i < NUM_GAME_MODES; i++)
590 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
591 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
592 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
593 game_mode_anim_classes_list[i].class;
595 for (i = 0; i < NUM_ANIM_CLASSES; i++)
596 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
597 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
598 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
599 anim_class_game_modes_list[i].game_mode;
601 anim_status_last_before_fading = GAME_MODE_LOADING;
602 anim_status_last = GAME_MODE_LOADING;
603 anim_classes_last = ANIM_CLASS_NONE;
606 void InitGlobalAnimations(void)
608 InitGlobalAnimControls();
611 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
613 Bitmap *fade_bitmap =
614 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
615 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
616 int game_mode_anim_action[NUM_GAME_MODES];
622 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
623 drawing_target == DRAW_TO_SCREEN)
626 // always start with reliable default values (no animation actions)
627 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
628 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
630 if (global.anim_status != anim_status_last)
632 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
633 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
634 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
637 if (drawing_target == DRAW_TO_FADE_TARGET)
640 // special case: changing from/to this screen is done without fading
641 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
642 anim_status_last == GAME_MODE_PSEUDO_TYPENAME)
645 // ---------- part 1 ------------------------------------------------------
646 // start or stop global animations by change of game mode
647 // (special handling of animations for "current screen" and "all screens")
649 if (global.anim_status_next != anim_status_last_before_fading)
651 // stop animations for last screen before fading to new screen
652 game_mode_anim_action[anim_status_last] = ANIM_STOP;
654 // start animations for current screen after fading to new screen
655 game_mode_anim_action[global.anim_status] = ANIM_START;
658 // start animations for all screens after loading new artwork set
659 if (anim_status_last == GAME_MODE_LOADING)
660 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
662 // ---------- part 2 ------------------------------------------------------
663 // start or stop global animations by change of animation class
664 // (generic handling of animations for "class of screens")
666 for (i = 0; i < NUM_ANIM_CLASSES; i++)
668 int anim_class_check = (1 << i);
669 int anim_class_game_mode = anim_class_game_modes[i];
670 int anim_class_last = anim_classes_last & anim_class_check;
671 int anim_class_next = anim_classes_next & anim_class_check;
673 // stop animations for changed screen class before fading to new screen
674 if (before_fading && anim_class_last && !anim_class_next)
675 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
677 // start animations for changed screen class after fading to new screen
678 if (after_fading && !anim_class_last && anim_class_next)
679 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
682 if (drawing_target == DRAW_TO_SCREEN)
686 anim_classes_last = anim_classes_next;
687 anim_status_last_before_fading = global.anim_status;
690 anim_status_last = global.anim_status;
692 // start or stop animations determined to be started or stopped above
693 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
694 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
695 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
697 else if (drawing_target == DRAW_TO_FADE_TARGET)
699 drawing_to_fading_buffer = TRUE;
701 // start animations determined to be (temporary) started above
702 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
703 if (game_mode_anim_action[mode_nr] == ANIM_START)
704 HandleGlobalAnim(ANIM_START, mode_nr);
708 if (global.anim_status == GAME_MODE_LOADING)
711 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
713 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
716 // when preparing source fading buffer, only draw animations to be stopped
717 if (drawing_target == DRAW_TO_FADE_SOURCE &&
718 game_mode_anim_action[mode_nr] != ANIM_STOP)
721 // when preparing target fading buffer, only draw animations to be started
722 if (drawing_target == DRAW_TO_FADE_TARGET &&
723 game_mode_anim_action[mode_nr] != ANIM_START)
727 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
728 mode_nr != game_status)
732 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
734 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
735 struct GraphicInfo *c = &anim->control_info;
736 int part_first, part_last;
739 if (!(anim->state & ANIM_STATE_RUNNING))
742 part_first = part_last = anim->active_part_nr;
744 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
746 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
749 part_last = num_parts - 1;
752 for (part_nr = part_first; part_nr <= part_last; part_nr++)
754 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
755 struct GraphicInfo *g = &part->graphic_info;
758 int width = g->width;
759 int height = g->height;
766 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
767 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
768 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
769 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
771 if (!(part->state & ANIM_STATE_RUNNING))
774 if (part->drawing_stage != drawing_stage)
783 else if (part->x > part->viewport_width - g->width)
784 width -= (part->x - (part->viewport_width - g->width));
792 else if (part->y > part->viewport_height - g->height)
793 height -= (part->y - (part->viewport_height - g->height));
795 if (width <= 0 || height <= 0)
798 dst_x += part->viewport_x;
799 dst_y += part->viewport_y;
801 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
802 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
803 g->anim_mode, g->anim_start_frame,
806 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
812 if (drawing_target == DRAW_TO_SCREEN)
813 blit_screen(src_bitmap, src_x, src_y, width, height,
816 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
822 if (drawing_target == DRAW_TO_FADE_TARGET)
824 // stop animations determined to be (temporary) started above
825 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
826 if (game_mode_anim_action[mode_nr] == ANIM_START)
827 HandleGlobalAnim(ANIM_STOP, mode_nr);
829 drawing_to_fading_buffer = FALSE;
833 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
835 int last_cursor_mode_override = gfx.cursor_mode_override;
837 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
839 ResetGlobalAnim_Clickable();
841 gfx.cursor_mode_override = CURSOR_UNDEFINED;
844 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
846 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
848 ResetGlobalAnim_Clicked();
851 if (gfx.cursor_mode_override != last_cursor_mode_override)
852 SetMouseCursor(gfx.cursor_mode);
855 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
861 boolean changed = FALSE;
863 if (part->last_anim_status == global.anim_status &&
864 part->control_info.class != get_hash_from_key("pointer"))
867 part->last_anim_status = global.anim_status;
869 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
871 if (part->control_info.class == get_hash_from_key("window") ||
872 part->control_info.class == get_hash_from_key("border"))
876 viewport_width = WIN_XSIZE;
877 viewport_height = WIN_YSIZE;
879 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
881 else if (part->control_info.class == get_hash_from_key("pointer"))
883 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
884 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
886 // prevent displaying off-screen custom mouse cursor in upper left corner
887 if (gfx.mouse_x == POS_OFFSCREEN &&
888 gfx.mouse_y == POS_OFFSCREEN)
889 mx = my = POS_OFFSCREEN;
891 viewport_x = mx - part->control_info.x;
892 viewport_y = my - part->control_info.y;
893 viewport_width = part->graphic_info.width;
894 viewport_height = part->graphic_info.height;
896 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
898 // do not use global animation mouse pointer when reloading artwork
899 if (global.anim_status != GAME_MODE_LOADING)
900 gfx.cursor_mode_override = CURSOR_NONE;
902 else if (part->control_info.class == get_hash_from_key("door_1"))
906 viewport_width = DXSIZE;
907 viewport_height = DYSIZE;
909 else if (part->control_info.class == get_hash_from_key("door_2"))
911 if (part->mode_nr == GAME_MODE_EDITOR)
915 viewport_width = EXSIZE;
916 viewport_height = EYSIZE;
922 viewport_width = VXSIZE;
923 viewport_height = VYSIZE;
926 else // default: "playfield"
928 viewport_x = REAL_SX;
929 viewport_y = REAL_SY;
930 viewport_width = FULL_SXSIZE;
931 viewport_height = FULL_SYSIZE;
934 if (viewport_x != part->viewport_x ||
935 viewport_y != part->viewport_y ||
936 viewport_width != part->viewport_width ||
937 viewport_height != part->viewport_height)
939 part->viewport_x = viewport_x;
940 part->viewport_y = viewport_y;
941 part->viewport_width = viewport_width;
942 part->viewport_height = viewport_height;
944 if (part->control_info.class != get_hash_from_key("pointer"))
951 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
953 int sound = part->sound;
955 if (sound == SND_UNDEFINED)
958 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
959 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
962 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
963 if (IS_LOOP_SOUND(sound))
964 PlaySoundLoop(sound);
969 printf("::: PLAY SOUND %d.%d.%d: %d\n",
970 part->anim_nr, part->nr, part->mode_nr, sound);
974 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
976 int sound = part->sound;
978 if (sound == SND_UNDEFINED)
984 printf("::: STOP SOUND %d.%d.%d: %d\n",
985 part->anim_nr, part->nr, part->mode_nr, sound);
989 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
991 int music = part->music;
993 if (music == MUS_UNDEFINED)
996 if (!setup.sound_music)
999 if (IS_LOOP_MUSIC(music))
1000 PlayMusicLoop(music);
1005 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
1006 part->anim_nr, part->nr, part->mode_nr, music);
1010 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1012 int music = part->music;
1014 if (music == MUS_UNDEFINED)
1020 printf("::: STOP MUSIC %d.%d.%d: %d\n",
1021 part->anim_nr, part->nr, part->mode_nr, music);
1025 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1027 // when drawing animations to fading buffer, do not play sounds or music
1028 if (drawing_to_fading_buffer)
1031 PlayGlobalAnimSound(part);
1032 PlayGlobalAnimMusic(part);
1035 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1037 StopGlobalAnimSound(part);
1038 StopGlobalAnimMusic(part);
1041 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1043 // when drawing animations to fading buffer, do not play sounds
1044 if (drawing_to_fading_buffer)
1047 // loop sounds only expire when playing
1048 if (game_status != GAME_MODE_PLAYING)
1051 // check if any sound is defined for this animation part
1052 if (part->sound == SND_UNDEFINED)
1055 // normal (non-loop) sounds do not expire when playing
1056 if (!IS_LOOP_SOUND(part->sound))
1059 // prevent expiring loop sounds when playing
1060 PlayGlobalAnimSound(part);
1063 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1065 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1067 if (mask & ANIM_EVENT_ANY)
1068 return (anim_event & ANIM_EVENT_ANY);
1069 else if (mask & ANIM_EVENT_SELF)
1070 return (anim_event & ANIM_EVENT_SELF);
1071 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1072 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1074 return (anim_event == mask ||
1075 anim_event == mask_anim_only);
1078 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1080 struct GraphicInfo *c = &part->control_info;
1081 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1082 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1085 for (i = 0; i < num_init_events; i++)
1087 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1089 if (checkGlobalAnimEvent(init_event, mask))
1093 for (i = 0; i < num_anim_events; i++)
1095 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1097 if (checkGlobalAnimEvent(anim_event, mask))
1104 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1105 int mx, int my, boolean clicked)
1107 struct GraphicInfo *g = &part->graphic_info;
1108 int part_x = part->viewport_x + part->x;
1109 int part_y = part->viewport_y + part->y;
1110 int part_width = g->width;
1111 int part_height = g->height;
1113 // check if mouse click was detected at all
1117 // check if mouse click is inside the animation part's viewport
1118 if (mx < part->viewport_x ||
1119 mx >= part->viewport_x + part->viewport_width ||
1120 my < part->viewport_y ||
1121 my >= part->viewport_y + part->viewport_height)
1124 // check if mouse click is inside the animation part's graphic
1126 mx >= part_x + part_width ||
1128 my >= part_y + part_height)
1134 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1136 return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1139 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1140 boolean *click_consumed,
1141 boolean *any_event_action,
1142 int event_value, char *info_text)
1144 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1146 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1147 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1148 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1151 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1155 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1157 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1160 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1162 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1164 if (!(part2->state & ANIM_STATE_RUNNING))
1167 if (isClickablePart(part2, mask))
1169 part2->triggered = TRUE;
1170 *click_consumed |= clickConsumed(part); // click was on "part"!
1172 #if DEBUG_ANIM_EVENTS
1173 printf("::: => %d.%d TRIGGERED BY %s OF %d.%d\n",
1174 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1175 part->old_anim_nr + 1, part->old_nr + 1);
1178 printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
1179 part2->control_info.anim_event_action);
1182 // after executing event action, ignore any further actions
1183 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1184 *any_event_action = TRUE;
1188 struct GraphicInfo *c = &part2->control_info;
1190 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1191 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1193 if (isClickablePart(part2, mask))
1194 printf(" <--- TRIGGERED BY %d.%d",
1203 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1204 int delay_type, char *info_text)
1206 #if DEBUG_ANIM_DELAY
1207 printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
1210 DoGlobalAnim_DelayAction(part, delay_type);
1213 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1214 int event_value, char *info_text)
1216 #if DEBUG_ANIM_EVENTS
1217 printf("::: %d.%d %s\n", part->old_anim_nr + 1, part->old_nr + 1, info_text);
1220 boolean click_consumed = FALSE;
1221 boolean any_event_action = FALSE;
1223 // check if this event is defined to trigger other animations
1224 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1225 event_value, info_text);
1228 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1231 if (handle_click && !part->clicked)
1234 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1235 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1236 struct GraphicInfo *g = &part->graphic_info;
1237 struct GraphicInfo *c = &part->control_info;
1238 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1240 if (viewport_changed)
1241 state |= ANIM_STATE_RESTART;
1243 if (state & ANIM_STATE_RESTART)
1245 // when drawing animations to fading buffer, only start fixed animations
1246 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1247 c->y == ARG_UNDEFINED_VALUE))
1248 return ANIM_STATE_INACTIVE;
1250 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1252 part->init_delay_counter =
1253 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1255 part->anim_delay_counter =
1256 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1258 part->post_delay_counter = 0;
1260 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1261 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1263 part->initial_anim_sync_frame =
1264 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1266 if (c->direction & MV_HORIZONTAL)
1268 int pos_bottom = part->viewport_height - g->height;
1270 if (c->position == POS_TOP)
1272 else if (c->position == POS_UPPER)
1273 part->y = GetSimpleRandom(pos_bottom / 2);
1274 else if (c->position == POS_MIDDLE)
1275 part->y = pos_bottom / 2;
1276 else if (c->position == POS_LOWER)
1277 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1278 else if (c->position == POS_BOTTOM)
1279 part->y = pos_bottom;
1281 part->y = GetSimpleRandom(pos_bottom);
1283 if (c->direction == MV_RIGHT)
1285 part->step_xoffset = c->step_offset;
1286 part->x = -g->width + part->step_xoffset;
1290 part->step_xoffset = -c->step_offset;
1291 part->x = part->viewport_width + part->step_xoffset;
1294 part->step_yoffset = 0;
1296 else if (c->direction & MV_VERTICAL)
1298 int pos_right = part->viewport_width - g->width;
1300 if (c->position == POS_LEFT)
1302 else if (c->position == POS_RIGHT)
1303 part->x = pos_right;
1305 part->x = GetSimpleRandom(pos_right);
1307 if (c->direction == MV_DOWN)
1309 part->step_yoffset = c->step_offset;
1310 part->y = -g->height + part->step_yoffset;
1314 part->step_yoffset = -c->step_offset;
1315 part->y = part->viewport_height + part->step_yoffset;
1318 part->step_xoffset = 0;
1325 part->step_xoffset = 0;
1326 part->step_yoffset = 0;
1329 if (part->control_info.class != get_hash_from_key("pointer"))
1331 if (c->x != ARG_UNDEFINED_VALUE)
1333 if (c->y != ARG_UNDEFINED_VALUE)
1337 if (c->position == POS_LAST &&
1338 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1339 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1341 part->x = anim->last_x;
1342 part->y = anim->last_y;
1345 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1346 part->step_xoffset = c->step_xoffset;
1347 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1348 part->step_yoffset = c->step_yoffset;
1350 if (part->init_delay_counter == 0 &&
1351 !part->init_event_state)
1353 PlayGlobalAnimSoundAndMusic(part);
1355 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1356 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1360 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1364 if (part->clicked &&
1365 part->init_event_state)
1367 if (part->initial_anim_sync_frame > 0)
1368 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1370 part->init_delay_counter = 1;
1371 part->init_event_state = FALSE;
1373 part->clicked = FALSE;
1376 if (part->clicked &&
1377 part->anim_event_state)
1379 part->anim_delay_counter = 1;
1380 part->anim_event_state = FALSE;
1382 part->clicked = FALSE;
1385 if (part->init_delay_counter > 0)
1387 part->init_delay_counter--;
1389 if (part->init_delay_counter == 0)
1391 part->init_event_state = FALSE;
1393 PlayGlobalAnimSoundAndMusic(part);
1395 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1396 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1399 return ANIM_STATE_WAITING;
1402 if (part->init_event_state)
1403 return ANIM_STATE_WAITING;
1405 // animation part is now running/visible and therefore clickable
1406 part->clickable = TRUE;
1408 // check if moving animation has left the visible screen area
1409 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1410 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1411 (part->y <= -g->height && part->step_yoffset <= 0) ||
1412 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1414 // do not wait for "anim" events for off-screen animations
1415 part->anim_event_state = FALSE;
1417 // do not stop animation before "anim" or "post" counter are finished
1418 if (part->anim_delay_counter == 0 &&
1419 part->post_delay_counter == 0)
1421 StopGlobalAnimSoundAndMusic(part);
1423 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1425 part->post_delay_counter =
1426 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1428 if (part->post_delay_counter > 0)
1429 return ANIM_STATE_RUNNING;
1431 // drawing last frame not needed here -- animation not visible anymore
1432 return ANIM_STATE_RESTART;
1436 if (part->anim_delay_counter > 0)
1438 part->anim_delay_counter--;
1440 if (part->anim_delay_counter == 0)
1442 part->anim_event_state = FALSE;
1444 StopGlobalAnimSoundAndMusic(part);
1446 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1447 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1449 part->post_delay_counter =
1450 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1452 if (part->post_delay_counter > 0)
1453 return ANIM_STATE_RUNNING;
1455 // additional state "RUNNING" required to not skip drawing last frame
1456 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1460 if (part->post_delay_counter > 0)
1462 part->post_delay_counter--;
1464 if (part->post_delay_counter == 0)
1466 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1467 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1469 return ANIM_STATE_RESTART;
1472 return ANIM_STATE_WAITING;
1475 // special case to prevent expiring loop sounds when playing
1476 PlayGlobalAnimSoundIfLoop(part);
1478 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1480 return ANIM_STATE_RUNNING;
1484 static unsigned int last_counter = -1;
1485 unsigned int counter = Counter();
1487 printf("::: NEXT ANIM PART [%d, %d]\n",
1488 anim_sync_frame, counter - last_counter);
1490 last_counter = counter;
1494 part->x += part->step_xoffset;
1495 part->y += part->step_yoffset;
1497 anim->last_x = part->x;
1498 anim->last_y = part->y;
1500 return ANIM_STATE_RUNNING;
1503 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1506 struct GlobalAnimPartControlInfo *part;
1507 struct GraphicInfo *c = &anim->control_info;
1508 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1509 int state, active_part_nr;
1513 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1514 anim->mode_nr, anim->nr, anim->num_parts);
1515 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1519 printf("::: %s(%d): %d, %d, %d [%d]\n",
1520 (action == ANIM_START ? "ANIM_START" :
1521 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1522 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1524 anim->state & ANIM_STATE_RESTART,
1525 anim->state & ANIM_STATE_WAITING,
1526 anim->state & ANIM_STATE_RUNNING,
1533 anim->state = anim->last_state = ANIM_STATE_RESTART;
1534 anim->active_part_nr = anim->last_active_part_nr = 0;
1535 anim->part_counter = 0;
1540 if (anim->state == ANIM_STATE_INACTIVE)
1543 anim->state = anim->last_state;
1544 anim->active_part_nr = anim->last_active_part_nr;
1549 anim->state = ANIM_STATE_INACTIVE;
1551 for (i = 0; i < num_parts; i++)
1552 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1560 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1563 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1564 anim->mode_nr, anim->nr, num_parts);
1567 for (i = 0; i < num_parts; i++)
1569 part = &anim->part[i];
1574 anim->state = ANIM_STATE_RUNNING;
1575 part->state = ANIM_STATE_RESTART;
1580 if (part->state == ANIM_STATE_INACTIVE)
1586 part->state = ANIM_STATE_INACTIVE;
1594 part->state = HandleGlobalAnim_Part(part, part->state);
1596 // when animation mode is "once", stop after animation was played once
1597 if (c->anim_mode & ANIM_ONCE &&
1598 part->state & ANIM_STATE_RESTART)
1599 part->state = ANIM_STATE_INACTIVE;
1602 anim->last_state = anim->state;
1603 anim->last_active_part_nr = anim->active_part_nr;
1608 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1609 anim->active_part_nr = getGlobalAnimationPart(anim);
1611 part = &anim->part[anim->active_part_nr];
1613 // first set all animation parts to "inactive", ...
1614 for (i = 0; i < num_parts; i++)
1615 anim->part[i].state = ANIM_STATE_INACTIVE;
1617 // ... then set current animation parts to "running"
1618 part->state = ANIM_STATE_RUNNING;
1620 anim->state = HandleGlobalAnim_Part(part, anim->state);
1622 if (anim->state & ANIM_STATE_RESTART)
1623 anim->part_counter++;
1625 // when animation mode is "once", stop after all animations were played once
1626 if (c->anim_mode & ANIM_ONCE &&
1627 anim->part_counter == anim->num_parts)
1628 anim->state = ANIM_STATE_INACTIVE;
1630 state = anim->state;
1631 active_part_nr = anim->active_part_nr;
1633 // while the animation parts are pausing (waiting or inactive), play the base
1634 // (main) animation; this corresponds to the "boring player animation" logic
1635 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1638 if (anim->state == ANIM_STATE_WAITING ||
1639 anim->state == ANIM_STATE_INACTIVE)
1641 anim->active_part_nr = anim->num_parts; // part nr of base animation
1642 part = &anim->part[anim->active_part_nr];
1644 if (anim->state != anim->last_state)
1645 part->state = ANIM_STATE_RESTART;
1647 anim->state = ANIM_STATE_RUNNING;
1648 part->state = HandleGlobalAnim_Part(part, part->state);
1652 anim->last_state = state;
1653 anim->last_active_part_nr = active_part_nr;
1656 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1661 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1662 ctrl->nr, ctrl->num_anims);
1665 for (i = 0; i < ctrl->num_anims; i++)
1666 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1669 static void HandleGlobalAnim(int action, int game_mode)
1672 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1675 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1678 static void DoAnimationExt(void)
1683 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1686 // global animations now synchronized with frame delay of screen update
1689 for (i = 0; i < NUM_GAME_MODES; i++)
1690 HandleGlobalAnim(ANIM_CONTINUE, i);
1693 // force screen redraw in next frame to continue drawing global animations
1694 redraw_mask = REDRAW_ALL;
1698 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1702 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1703 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1704 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1705 ANIM_DELAY_ACTION_NONE);
1707 if (delay_action == ANIM_DELAY_ACTION_NONE)
1710 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1713 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1715 int event_action = (part->init_event_state ?
1716 part->control_info.init_event_action :
1717 part->control_info.anim_event_action);
1719 if (event_action == ANIM_EVENT_ACTION_NONE)
1722 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1724 // check if further actions are allowed to be executed
1725 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1731 static void InitGlobalAnim_Clickable(void)
1735 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1737 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1740 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1742 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1745 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1747 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1749 if (part->triggered)
1750 part->clicked = TRUE;
1752 part->triggered = FALSE;
1753 part->clickable = FALSE;
1759 #define ANIM_CLICKED_RESET 0
1760 #define ANIM_CLICKED_PRESSED 1
1761 #define ANIM_CLICKED_RELEASED 2
1763 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1765 boolean click_consumed = FALSE;
1766 boolean anything_clicked = FALSE;
1767 boolean any_part_clicked = FALSE;
1768 boolean any_event_action = FALSE;
1772 // check game modes in reverse draw order (to stop when clicked)
1773 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1775 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1778 // check animations in reverse draw order (to stop when clicked)
1779 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1781 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1784 // check animation parts in reverse draw order (to stop when clicked)
1785 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1787 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1789 if (clicked_event == ANIM_CLICKED_RESET)
1791 part->clicked = FALSE;
1796 if (!part->clickable)
1799 if (!(part->state & ANIM_STATE_RUNNING))
1802 // always handle "any" click events (clicking anywhere on screen) ...
1803 if (clicked_event == ANIM_CLICKED_PRESSED &&
1804 isClickablePart(part, ANIM_EVENT_ANY))
1806 #if DEBUG_ANIM_EVENTS
1807 printf("::: => %d.%d TRIGGERED BY ANY\n",
1808 part->old_anim_nr + 1, part->old_nr + 1);
1811 anything_clicked = part->clicked = TRUE;
1812 click_consumed |= clickConsumed(part);
1815 // always handle "unclick:any" events (releasing anywhere on screen) ...
1816 if (clicked_event == ANIM_CLICKED_RELEASED &&
1817 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1819 #if DEBUG_ANIM_EVENTS
1820 printf("::: => %d.%d TRIGGERED BY UNCLICK:ANY\n",
1821 part->old_anim_nr + 1, part->old_nr + 1);
1824 anything_clicked = part->clicked = TRUE;
1825 click_consumed |= clickConsumed(part);
1828 // ... but only handle the first (topmost) clickable animation
1829 if (any_part_clicked)
1832 if (clicked_event == ANIM_CLICKED_PRESSED &&
1833 isClickedPart(part, mx, my, TRUE))
1836 printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
1837 part->control_info.anim_event_action);
1840 // after executing event action, ignore any further actions
1841 if (!any_event_action && DoGlobalAnim_EventAction(part))
1842 any_event_action = TRUE;
1844 // determine if mouse clicks should be blocked from other animations
1845 any_part_clicked |= clickConsumed(part);
1847 if (isClickablePart(part, ANIM_EVENT_SELF))
1849 #if DEBUG_ANIM_EVENTS
1850 printf("::: => %d.%d TRIGGERED BY SELF\n",
1851 part->old_anim_nr + 1, part->old_nr + 1);
1854 anything_clicked = part->clicked = TRUE;
1855 click_consumed |= clickConsumed(part);
1858 // check if this click is defined to trigger other animations
1859 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1860 ANIM_EVENT_CLICK, "CLICK");
1866 if (anything_clicked)
1868 handle_click = TRUE;
1870 for (i = 0; i < NUM_GAME_MODES; i++)
1871 HandleGlobalAnim(ANIM_CONTINUE, i);
1873 handle_click = FALSE;
1875 // prevent ignoring release event if processed within same game frame
1876 StopProcessingEvents();
1879 return (click_consumed || any_event_action);
1882 static void ResetGlobalAnim_Clickable(void)
1884 InitGlobalAnim_Clickable();
1887 static void ResetGlobalAnim_Clicked(void)
1889 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1892 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1894 static boolean click_consumed = FALSE;
1895 static int last_button = 0;
1896 boolean press_event;
1897 boolean release_event;
1898 boolean click_consumed_current = click_consumed;
1900 if (button != 0 && force_click)
1903 // check if button state has changed since last invocation
1904 press_event = (button != 0 && last_button == 0);
1905 release_event = (button == 0 && last_button != 0);
1906 last_button = button;
1910 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1911 click_consumed_current = click_consumed;
1916 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
1917 click_consumed = FALSE;
1920 return click_consumed_current;