1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #include "libgame/libgame.h"
22 #define DEBUG_ANIM_DELAY 0
23 #define DEBUG_ANIM_EVENTS 0
26 // values for global toon animation definition
27 #define NUM_GLOBAL_TOON_ANIMS 1
28 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
30 // values for global animation definition (including toons)
31 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
32 NUM_GLOBAL_TOON_ANIMS)
33 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
34 NUM_GLOBAL_TOON_PARTS)
36 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
37 #define ANIM_CLASS_BIT_TITLE 1
38 #define ANIM_CLASS_BIT_MAIN 2
39 #define ANIM_CLASS_BIT_NAMES 3
40 #define ANIM_CLASS_BIT_SCORES 4
41 #define ANIM_CLASS_BIT_SCORESONLY 5
42 #define ANIM_CLASS_BIT_SUBMENU 6
43 #define ANIM_CLASS_BIT_MENU 7
44 #define ANIM_CLASS_BIT_TOONS 8
45 #define ANIM_CLASS_BIT_NO_TITLE 9
47 #define NUM_ANIM_CLASSES 10
49 #define ANIM_CLASS_NONE 0
50 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
51 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
52 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
53 #define ANIM_CLASS_NAMES (1 << ANIM_CLASS_BIT_NAMES)
54 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
55 #define ANIM_CLASS_SCORESONLY (1 << ANIM_CLASS_BIT_SCORESONLY)
56 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
57 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
58 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
59 #define ANIM_CLASS_NO_TITLE (1 << ANIM_CLASS_BIT_NO_TITLE)
61 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
65 #define ANIM_CLASS_TOONS_SCORESONLY (ANIM_CLASS_TOONS | \
67 ANIM_CLASS_SCORESONLY | \
70 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
75 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
77 ANIM_CLASS_SUBMENU | \
80 #define ANIM_CLASS_TOONS_MENU_SUBMENU_2 (ANIM_CLASS_TOONS | \
82 ANIM_CLASS_SUBMENU | \
86 // values for global animation states
87 #define ANIM_STATE_INACTIVE 0
88 #define ANIM_STATE_RESTART (1 << 0)
89 #define ANIM_STATE_WAITING (1 << 1)
90 #define ANIM_STATE_RUNNING (1 << 2)
92 // values for global animation control
93 #define ANIM_NO_ACTION 0
95 #define ANIM_CONTINUE 2
99 struct GlobalAnimPartControlInfo
101 int old_nr; // position before mapping animation parts linearly
102 int old_anim_nr; // position before mapping animations linearly
108 boolean is_base; // animation part is base/main/default animation part
114 struct GraphicInfo graphic_info;
115 struct GraphicInfo control_info;
123 int step_xoffset, step_yoffset;
125 unsigned int initial_anim_sync_frame;
126 unsigned int anim_random_frame;
128 DelayCounter step_delay;
130 int init_delay_counter;
131 int anim_delay_counter;
132 int post_delay_counter;
134 boolean init_event_state;
135 boolean anim_event_state;
144 int last_anim_status;
147 struct GlobalAnimMainControlInfo
149 struct GlobalAnimPartControlInfo base;
150 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
155 struct GraphicInfo control_info;
157 int num_parts; // number of animation parts, but without base part
158 int num_parts_all; // number of animation parts, including base part
162 boolean has_base; // animation has base/main/default animation part
166 int init_delay_counter;
170 int last_state, last_active_part_nr;
173 struct GlobalAnimControlInfo
175 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
181 struct GameModeAnimClass
185 } game_mode_anim_classes_list[] =
187 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
188 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
189 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
190 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
191 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
192 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
193 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
194 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
195 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
196 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
197 { GAME_MODE_NAMES, ANIM_CLASS_TOONS_MENU_SUBMENU },
198 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
199 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
200 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
201 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
202 { GAME_MODE_PSEUDO_NAMESONLY, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
203 { GAME_MODE_PSEUDO_TYPENAMES, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
204 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
205 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
206 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORESONLY },
207 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORESONLY },
208 { GAME_MODE_SCOREINFO, ANIM_CLASS_TOONS_SCORES },
209 { GAME_MODE_EDITOR, ANIM_CLASS_NO_TITLE },
210 { GAME_MODE_PLAYING, ANIM_CLASS_NO_TITLE },
215 struct AnimClassGameMode
219 } anim_class_game_modes_list[] =
221 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
222 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
223 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
224 { ANIM_CLASS_BIT_NAMES, GAME_MODE_NAMES },
225 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
226 { ANIM_CLASS_BIT_SCORESONLY, GAME_MODE_PSEUDO_SCORESONLY },
227 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
228 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
229 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
230 { ANIM_CLASS_BIT_NO_TITLE, GAME_MODE_PSEUDO_NO_TITLE },
235 // forward declaration for internal use
236 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
237 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
238 static void HandleGlobalAnim(int, int);
239 static void DoAnimationExt(void);
240 static void ResetGlobalAnim_Clickable(void);
241 static void ResetGlobalAnim_Clicked(void);
243 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
245 static unsigned int anim_sync_frame = 0;
247 static int game_mode_anim_classes[NUM_GAME_MODES];
248 static int anim_class_game_modes[NUM_ANIM_CLASSES];
250 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
251 static int anim_status_last = GAME_MODE_DEFAULT;
252 static int anim_classes_last = ANIM_CLASS_NONE;
254 static boolean drawing_to_fading_buffer = FALSE;
256 static boolean handle_click = FALSE;
259 // ============================================================================
260 // generic animation frame calculation
261 // ============================================================================
263 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
268 if (delay < 1) // delay must be at least 1
271 sync_frame += start_frame * delay;
273 if (mode & ANIM_LOOP) // looping animation
275 frame = (sync_frame % (delay * num_frames)) / delay;
277 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
279 frame = sync_frame / delay;
281 if (frame > num_frames - 1)
282 frame = num_frames - 1;
284 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
286 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
288 frame = (sync_frame % (delay * max_anim_frames)) / delay;
289 frame = (frame < num_frames ? frame : max_anim_frames - frame);
291 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
293 int max_anim_frames = 2 * num_frames;
295 frame = (sync_frame % (delay * max_anim_frames)) / delay;
296 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
298 else if (mode & ANIM_RANDOM) // play frames in random order
300 // note: expect different frames for the same delay cycle!
302 if (gfx.anim_random_frame < 0)
303 frame = GetSimpleRandom(num_frames);
305 frame = gfx.anim_random_frame % num_frames;
307 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
309 frame = sync_frame % num_frames;
312 if (mode & ANIM_REVERSE) // use reverse animation direction
313 frame = num_frames - frame - 1;
319 // ============================================================================
320 // global animation functions
321 // ============================================================================
323 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
325 struct GraphicInfo *c = &anim->control_info;
326 int last_anim_random_frame = gfx.anim_random_frame;
329 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
331 part_nr = getAnimationFrame(anim->num_parts, 1,
332 c->anim_mode, c->anim_start_frame,
335 gfx.anim_random_frame = last_anim_random_frame;
340 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
342 const struct GlobalAnimPartControlInfo *o1 =
343 (struct GlobalAnimPartControlInfo *)obj1;
344 const struct GlobalAnimPartControlInfo *o2 =
345 (struct GlobalAnimPartControlInfo *)obj2;
348 if (o1->control_info.draw_order != o2->control_info.draw_order)
349 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
351 compare_result = o1->nr - o2->nr;
353 return compare_result;
356 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
358 const struct GlobalAnimMainControlInfo *o1 =
359 (struct GlobalAnimMainControlInfo *)obj1;
360 const struct GlobalAnimMainControlInfo *o2 =
361 (struct GlobalAnimMainControlInfo *)obj2;
364 if (o1->control_info.draw_order != o2->control_info.draw_order)
365 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
367 compare_result = o1->nr - o2->nr;
369 return compare_result;
372 static void InitToonControls(void)
374 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
375 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
376 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
377 int mode_nr, anim_nr, part_nr;
378 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
379 int num_toons = MAX_NUM_TOONS;
382 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
383 num_toons = global.num_toons;
385 mode_nr = mode_nr_toons;
386 anim_nr = ctrl->num_anims;
389 anim->mode_nr = mode_nr;
390 anim->control_info = graphic_info[control];
393 anim->num_parts_all = 0;
394 anim->part_counter = 0;
395 anim->active_part_nr = 0;
397 anim->has_base = FALSE;
399 anim->last_x = POS_OFFSCREEN;
400 anim->last_y = POS_OFFSCREEN;
402 anim->init_delay_counter = 0;
404 anim->state = ANIM_STATE_INACTIVE;
408 for (i = 0; i < num_toons; i++)
410 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
411 int sound = SND_UNDEFINED;
412 int music = MUS_UNDEFINED;
413 int graphic = IMG_TOON_1 + i;
414 int control = graphic;
417 part->anim_nr = anim_nr;
418 part->mode_nr = mode_nr;
420 part->is_base = FALSE;
424 part->graphic = graphic;
426 part->graphic_info = graphic_info[graphic];
427 part->control_info = graphic_info[control];
429 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
431 part->control_info.init_delay_fixed = 0;
432 part->control_info.init_delay_random = 150;
434 part->control_info.x = ARG_UNDEFINED_VALUE;
435 part->control_info.y = ARG_UNDEFINED_VALUE;
437 part->initial_anim_sync_frame = 0;
438 part->anim_random_frame = -1;
440 part->step_delay.count = 0;
441 part->step_delay.value = graphic_info[control].step_delay;
443 part->state = ANIM_STATE_INACTIVE;
444 part->last_anim_status = -1;
447 anim->num_parts_all++;
455 static void InitGlobalAnimControls(void)
458 int mode_nr, anim_nr, part_nr;
459 int sound, music, graphic, control;
463 for (m = 0; m < NUM_GAME_MODES; m++)
467 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
474 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
476 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
477 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
479 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
481 // if no base animation parameters defined, use default values
482 if (control == IMG_UNDEFINED)
483 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
486 anim->mode_nr = mode_nr;
487 anim->control_info = graphic_info[control];
490 anim->num_parts_all = 0;
491 anim->part_counter = 0;
492 anim->active_part_nr = 0;
494 anim->has_base = FALSE;
496 anim->last_x = POS_OFFSCREEN;
497 anim->last_y = POS_OFFSCREEN;
499 anim->init_delay_counter = 0;
501 anim->state = ANIM_STATE_INACTIVE;
505 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
507 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
509 sound = global_anim_info[a].sound[p][m];
510 music = global_anim_info[a].music[p][m];
511 graphic = global_anim_info[a].graphic[p][m];
512 control = global_anim_info[ctrl_id].graphic[p][m];
514 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
515 control == IMG_UNDEFINED)
519 Debug("anim:InitGlobalAnimControls",
520 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
521 m, a, p, mode_nr, anim_nr, part_nr, control);
525 Debug("anim:InitGlobalAnimControls",
526 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
527 m, a, p, mode_nr, anim_nr, part_nr, sound);
531 part->old_anim_nr = a;
534 part->anim_nr = anim_nr;
535 part->mode_nr = mode_nr;
539 part->graphic = graphic;
541 part->graphic_info = graphic_info[graphic];
542 part->control_info = graphic_info[control];
544 part->initial_anim_sync_frame = 0;
545 part->anim_random_frame = -1;
547 part->step_delay.count = 0;
548 part->step_delay.value = graphic_info[control].step_delay;
550 part->state = ANIM_STATE_INACTIVE;
551 part->last_anim_status = -1;
553 anim->num_parts_all++;
555 if (p < GLOBAL_ANIM_ID_PART_BASE)
557 part->is_base = FALSE;
564 part->is_base = TRUE;
567 anim->has_base = TRUE;
570 // apply special settings for pointer-style animations
571 if (part->control_info.class == get_hash_from_key("pointer"))
573 // force animation to be on top (must set anim and part control)
574 if (anim->control_info.draw_order == 0)
575 anim->control_info.draw_order = 1000000;
576 if (part->control_info.draw_order == 0)
577 part->control_info.draw_order = 1000000;
579 // force animation to pass-through clicks (must set part control)
580 if (part->control_info.style == STYLE_DEFAULT)
581 part->control_info.style |= STYLE_PASSTHROUGH;
585 if (anim->num_parts > 0 || anim->has_base)
595 // sort all animations according to draw_order and animation number
596 for (m = 0; m < NUM_GAME_MODES; m++)
598 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
600 // sort all main animations for this game mode
601 qsort(ctrl->anim, ctrl->num_anims,
602 sizeof(struct GlobalAnimMainControlInfo),
603 compareGlobalAnimMainControlInfo);
605 for (a = 0; a < ctrl->num_anims; a++)
607 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
609 // sort all animation parts for this main animation
610 qsort(anim->part, anim->num_parts,
611 sizeof(struct GlobalAnimPartControlInfo),
612 compareGlobalAnimPartControlInfo);
616 for (i = 0; i < NUM_GAME_MODES; i++)
617 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
618 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
619 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
620 game_mode_anim_classes_list[i].class;
622 for (i = 0; i < NUM_ANIM_CLASSES; i++)
623 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
624 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
625 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
626 anim_class_game_modes_list[i].game_mode;
628 anim_status_last_before_fading = GAME_MODE_LOADING;
629 anim_status_last = GAME_MODE_LOADING;
630 anim_classes_last = ANIM_CLASS_NONE;
633 void InitGlobalAnimations(void)
635 InitGlobalAnimControls();
638 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
640 Bitmap *fade_bitmap =
641 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
642 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
643 int game_mode_anim_action[NUM_GAME_MODES];
649 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
650 drawing_target == DRAW_TO_SCREEN)
653 // always start with reliable default values (no animation actions)
654 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
655 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
657 if (global.anim_status != anim_status_last)
659 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
660 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
661 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
664 if (drawing_target == DRAW_TO_FADE_TARGET)
667 // special case: changing from/to these screens is done without fading
668 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
669 global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
670 anim_status_last == GAME_MODE_PSEUDO_TYPENAME ||
671 anim_status_last == GAME_MODE_PSEUDO_TYPENAMES)
674 // ---------- part 1 ------------------------------------------------------
675 // start or stop global animations by change of game mode
676 // (special handling of animations for "current screen" and "all screens")
678 if (global.anim_status_next != anim_status_last_before_fading)
680 // stop animations for last screen before fading to new screen
681 game_mode_anim_action[anim_status_last] = ANIM_STOP;
683 // start animations for current screen after fading to new screen
684 game_mode_anim_action[global.anim_status] = ANIM_START;
687 // start animations for all screens after loading new artwork set
688 if (anim_status_last == GAME_MODE_LOADING)
689 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
691 // ---------- part 2 ------------------------------------------------------
692 // start or stop global animations by change of animation class
693 // (generic handling of animations for "class of screens")
695 for (i = 0; i < NUM_ANIM_CLASSES; i++)
697 int anim_class_check = (1 << i);
698 int anim_class_game_mode = anim_class_game_modes[i];
699 int anim_class_last = anim_classes_last & anim_class_check;
700 int anim_class_next = anim_classes_next & anim_class_check;
702 // stop animations for changed screen class before fading to new screen
703 if (before_fading && anim_class_last && !anim_class_next)
704 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
706 // start animations for changed screen class after fading to new screen
707 if (after_fading && !anim_class_last && anim_class_next)
708 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
711 if (drawing_target == DRAW_TO_SCREEN)
715 anim_classes_last = anim_classes_next;
716 anim_status_last_before_fading = global.anim_status;
719 anim_status_last = global.anim_status;
721 // start or stop animations determined to be started or stopped above
722 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
723 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
724 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
726 else if (drawing_target == DRAW_TO_FADE_TARGET)
728 drawing_to_fading_buffer = TRUE;
730 // start animations determined to be (temporary) started above
731 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
732 if (game_mode_anim_action[mode_nr] == ANIM_START)
733 HandleGlobalAnim(ANIM_START, mode_nr);
737 if (global.anim_status == GAME_MODE_LOADING)
740 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
742 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
745 // when preparing source fading buffer, only draw animations to be stopped
746 if (drawing_target == DRAW_TO_FADE_SOURCE &&
747 game_mode_anim_action[mode_nr] != ANIM_STOP)
750 // when preparing target fading buffer, only draw animations to be started
751 if (drawing_target == DRAW_TO_FADE_TARGET &&
752 game_mode_anim_action[mode_nr] != ANIM_START)
756 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
757 mode_nr != game_status)
761 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
763 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
764 struct GraphicInfo *c = &anim->control_info;
765 int part_first, part_last;
768 if (!(anim->state & ANIM_STATE_RUNNING))
771 part_first = part_last = anim->active_part_nr;
773 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
775 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
778 part_last = num_parts - 1;
781 for (part_nr = part_first; part_nr <= part_last; part_nr++)
783 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
784 struct GraphicInfo *g = &part->graphic_info;
787 int width = g->width;
788 int height = g->height;
795 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
796 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
797 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
798 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
799 int last_anim_random_frame = gfx.anim_random_frame;
801 if (!(part->state & ANIM_STATE_RUNNING))
804 if (part->drawing_stage != drawing_stage)
813 else if (part->x > part->viewport_width - g->width)
814 width -= (part->x - (part->viewport_width - g->width));
822 else if (part->y > part->viewport_height - g->height)
823 height -= (part->y - (part->viewport_height - g->height));
825 if (width <= 0 || height <= 0)
828 dst_x += part->viewport_x;
829 dst_y += part->viewport_y;
831 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
833 // re-initialize random animation frame after animation delay
834 if (g->anim_mode == ANIM_RANDOM &&
835 sync_frame % g->anim_delay == 0 &&
837 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
839 gfx.anim_random_frame = part->anim_random_frame;
841 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
842 g->anim_mode, g->anim_start_frame,
845 gfx.anim_random_frame = last_anim_random_frame;
847 getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
853 if (drawing_target == DRAW_TO_SCREEN)
854 blit_screen(src_bitmap, src_x, src_y, width, height,
857 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
863 if (drawing_target == DRAW_TO_FADE_TARGET)
865 // stop animations determined to be (temporary) started above
866 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
867 if (game_mode_anim_action[mode_nr] == ANIM_START)
868 HandleGlobalAnim(ANIM_STOP, mode_nr);
870 drawing_to_fading_buffer = FALSE;
874 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
876 int last_cursor_mode_override = gfx.cursor_mode_override;
878 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
880 ResetGlobalAnim_Clickable();
882 gfx.cursor_mode_override = CURSOR_UNDEFINED;
885 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
887 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
889 ResetGlobalAnim_Clicked();
892 DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
894 if (gfx.cursor_mode_override != last_cursor_mode_override)
895 SetMouseCursor(gfx.cursor_mode);
898 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
904 boolean changed = FALSE;
906 if (part->last_anim_status == global.anim_status &&
907 part->control_info.class != get_hash_from_key("pointer"))
910 part->last_anim_status = global.anim_status;
912 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
914 if (part->control_info.class == get_hash_from_key("window") ||
915 part->control_info.class == get_hash_from_key("border"))
919 viewport_width = WIN_XSIZE;
920 viewport_height = WIN_YSIZE;
922 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
924 else if (part->control_info.class == get_hash_from_key("pointer"))
926 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
927 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
929 // prevent displaying off-screen custom mouse cursor in upper left corner
930 if (gfx.mouse_x == POS_OFFSCREEN &&
931 gfx.mouse_y == POS_OFFSCREEN)
932 mx = my = POS_OFFSCREEN;
934 viewport_x = mx - part->control_info.x;
935 viewport_y = my - part->control_info.y;
936 viewport_width = part->graphic_info.width;
937 viewport_height = part->graphic_info.height;
939 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
941 // do not use global animation mouse pointer when reloading artwork
942 if (global.anim_status != GAME_MODE_LOADING)
943 gfx.cursor_mode_override = CURSOR_NONE;
945 else if (part->control_info.class == get_hash_from_key("door_1"))
949 viewport_width = DXSIZE;
950 viewport_height = DYSIZE;
952 else if (part->control_info.class == get_hash_from_key("door_2"))
954 if (part->mode_nr == GAME_MODE_EDITOR)
958 viewport_width = EXSIZE;
959 viewport_height = EYSIZE;
965 viewport_width = VXSIZE;
966 viewport_height = VYSIZE;
969 else // default: "playfield"
971 viewport_x = REAL_SX;
972 viewport_y = REAL_SY;
973 viewport_width = FULL_SXSIZE;
974 viewport_height = FULL_SYSIZE;
977 if (viewport_x != part->viewport_x ||
978 viewport_y != part->viewport_y ||
979 viewport_width != part->viewport_width ||
980 viewport_height != part->viewport_height)
982 part->viewport_x = viewport_x;
983 part->viewport_y = viewport_y;
984 part->viewport_width = viewport_width;
985 part->viewport_height = viewport_height;
987 if (part->control_info.class != get_hash_from_key("pointer"))
994 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
996 int sound = part->sound;
998 if (sound == SND_UNDEFINED)
1001 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1002 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1005 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1006 if (IS_LOOP_SOUND(sound))
1007 PlaySoundLoop(sound);
1012 Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1013 part->anim_nr, part->nr, part->mode_nr, sound);
1017 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1019 int sound = part->sound;
1021 if (sound == SND_UNDEFINED)
1027 Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1028 part->anim_nr, part->nr, part->mode_nr, sound);
1032 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1034 int music = part->music;
1036 if (music == MUS_UNDEFINED)
1039 if (!setup.sound_music)
1042 if (IS_LOOP_MUSIC(music))
1043 PlayMusicLoop(music);
1048 Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1049 part->anim_nr, part->nr, part->mode_nr, music);
1053 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1055 int music = part->music;
1057 if (music == MUS_UNDEFINED)
1063 Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1064 part->anim_nr, part->nr, part->mode_nr, music);
1068 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1070 // when drawing animations to fading buffer, do not play sounds or music
1071 if (drawing_to_fading_buffer)
1074 PlayGlobalAnimSound(part);
1075 PlayGlobalAnimMusic(part);
1078 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1080 StopGlobalAnimSound(part);
1081 StopGlobalAnimMusic(part);
1084 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1086 // when drawing animations to fading buffer, do not play sounds
1087 if (drawing_to_fading_buffer)
1090 // loop sounds only expire when playing
1091 if (game_status != GAME_MODE_PLAYING)
1094 // check if any sound is defined for this animation part
1095 if (part->sound == SND_UNDEFINED)
1098 // normal (non-loop) sounds do not expire when playing
1099 if (!IS_LOOP_SOUND(part->sound))
1102 // prevent expiring loop sounds when playing
1103 PlayGlobalAnimSound(part);
1106 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1108 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1110 if (mask & ANIM_EVENT_ANY)
1111 return (anim_event & ANIM_EVENT_ANY);
1112 else if (mask & ANIM_EVENT_SELF)
1113 return (anim_event & ANIM_EVENT_SELF);
1114 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1115 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1117 return (anim_event == mask ||
1118 anim_event == mask_anim_only);
1121 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1123 struct GraphicInfo *c = &part->control_info;
1124 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1125 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1128 for (i = 0; i < num_init_events; i++)
1130 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1132 if (checkGlobalAnimEvent(init_event, mask))
1136 for (i = 0; i < num_anim_events; i++)
1138 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1140 if (checkGlobalAnimEvent(anim_event, mask))
1147 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1148 int mx, int my, boolean clicked)
1150 struct GraphicInfo *g = &part->graphic_info;
1151 int part_x = part->viewport_x + part->x;
1152 int part_y = part->viewport_y + part->y;
1153 int part_width = g->width;
1154 int part_height = g->height;
1156 // check if mouse click was detected at all
1160 // check if mouse click is inside the animation part's viewport
1161 if (mx < part->viewport_x ||
1162 mx >= part->viewport_x + part->viewport_width ||
1163 my < part->viewport_y ||
1164 my >= part->viewport_y + part->viewport_height)
1167 // check if mouse click is inside the animation part's graphic
1169 mx >= part_x + part_width ||
1171 my >= part_y + part_height)
1177 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1179 return (part->control_info.style & STYLE_BLOCK ? TRUE : FALSE);
1182 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1184 return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1187 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1188 boolean *click_consumed,
1189 boolean *any_event_action,
1190 int event_value, char *info_text)
1192 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1194 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1195 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1196 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1199 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1203 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1205 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1208 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1210 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1212 if (!(part2->state & ANIM_STATE_RUNNING))
1215 if (isClickablePart(part2, mask))
1217 part2->triggered = TRUE;
1218 *click_consumed |= clickConsumed(part); // click was on "part"!
1220 #if DEBUG_ANIM_EVENTS
1221 Debug("anim:InitGlobalAnim_Triggered",
1222 "%d.%d TRIGGERED BY %s OF %d.%d",
1223 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1224 part->old_anim_nr + 1, part->old_nr + 1);
1227 Debug("anim:InitGlobalAnim_Triggered",
1228 "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1229 part2->control_info.anim_event_action);
1232 // after executing event action, ignore any further actions
1233 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1234 *any_event_action = TRUE;
1238 struct GraphicInfo *c = &part2->control_info;
1240 if (isClickablePart(part2, mask))
1241 Debug("anim:InitGlobalAnim_Triggered",
1242 "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1243 anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1246 Debug("anim:InitGlobalAnim_Triggered",
1247 "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1248 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1254 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1255 int delay_type, char *info_text)
1257 #if DEBUG_ANIM_DELAY
1258 Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1259 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1262 DoGlobalAnim_DelayAction(part, delay_type);
1265 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1266 int event_value, char *info_text)
1268 #if DEBUG_ANIM_EVENTS
1269 Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1270 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1273 boolean click_consumed = FALSE;
1274 boolean any_event_action = FALSE;
1276 // check if this event is defined to trigger other animations
1277 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1278 event_value, info_text);
1281 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1284 if (handle_click && !part->clicked)
1287 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1288 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1289 struct GraphicInfo *g = &part->graphic_info;
1290 struct GraphicInfo *c = &part->control_info;
1291 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1293 if (viewport_changed)
1294 state |= ANIM_STATE_RESTART;
1296 if (state & ANIM_STATE_RESTART)
1298 // when drawing animations to fading buffer, only start fixed animations
1299 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1300 c->y == ARG_UNDEFINED_VALUE))
1301 return ANIM_STATE_INACTIVE;
1303 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1305 part->init_delay_counter =
1306 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1308 part->anim_delay_counter =
1309 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1311 part->post_delay_counter = 0;
1313 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1314 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1316 part->initial_anim_sync_frame =
1317 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1319 // do not re-initialize random animation frame after fade-in
1320 if (part->anim_random_frame == -1)
1321 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1323 if (c->direction & MV_HORIZONTAL)
1325 int pos_bottom = part->viewport_height - g->height;
1327 if (c->position == POS_TOP)
1329 else if (c->position == POS_UPPER)
1330 part->y = GetSimpleRandom(pos_bottom / 2);
1331 else if (c->position == POS_MIDDLE)
1332 part->y = pos_bottom / 2;
1333 else if (c->position == POS_LOWER)
1334 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1335 else if (c->position == POS_BOTTOM)
1336 part->y = pos_bottom;
1338 part->y = GetSimpleRandom(pos_bottom);
1340 if (c->direction == MV_RIGHT)
1342 part->step_xoffset = c->step_offset;
1343 part->x = -g->width + part->step_xoffset;
1347 part->step_xoffset = -c->step_offset;
1348 part->x = part->viewport_width + part->step_xoffset;
1351 part->step_yoffset = 0;
1353 else if (c->direction & MV_VERTICAL)
1355 int pos_right = part->viewport_width - g->width;
1357 if (c->position == POS_LEFT)
1359 else if (c->position == POS_RIGHT)
1360 part->x = pos_right;
1362 part->x = GetSimpleRandom(pos_right);
1364 if (c->direction == MV_DOWN)
1366 part->step_yoffset = c->step_offset;
1367 part->y = -g->height + part->step_yoffset;
1371 part->step_yoffset = -c->step_offset;
1372 part->y = part->viewport_height + part->step_yoffset;
1375 part->step_xoffset = 0;
1382 part->step_xoffset = 0;
1383 part->step_yoffset = 0;
1386 if (part->control_info.class != get_hash_from_key("pointer"))
1388 if (c->x != ARG_UNDEFINED_VALUE)
1390 if (c->y != ARG_UNDEFINED_VALUE)
1394 if (c->position == POS_LAST &&
1395 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1396 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1398 part->x = anim->last_x;
1399 part->y = anim->last_y;
1402 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1403 part->step_xoffset = c->step_xoffset;
1404 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1405 part->step_yoffset = c->step_yoffset;
1407 if (part->init_delay_counter == 0 &&
1408 !part->init_event_state)
1410 PlayGlobalAnimSoundAndMusic(part);
1412 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1413 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1417 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1421 if (part->clicked &&
1422 part->init_event_state)
1424 if (part->initial_anim_sync_frame > 0)
1425 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1427 part->init_delay_counter = 1;
1428 part->init_event_state = FALSE;
1430 part->clicked = FALSE;
1433 if (part->clicked &&
1434 part->anim_event_state)
1436 part->anim_delay_counter = 1;
1437 part->anim_event_state = FALSE;
1439 part->clicked = FALSE;
1442 if (part->init_delay_counter > 0)
1444 part->init_delay_counter--;
1446 if (part->init_delay_counter == 0)
1448 part->init_event_state = FALSE;
1450 PlayGlobalAnimSoundAndMusic(part);
1452 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1453 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1456 return ANIM_STATE_WAITING;
1459 if (part->init_event_state)
1460 return ANIM_STATE_WAITING;
1462 // animation part is now running/visible and therefore clickable
1463 part->clickable = TRUE;
1465 // check if moving animation has left the visible screen area
1466 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1467 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1468 (part->y <= -g->height && part->step_yoffset <= 0) ||
1469 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1471 // do not wait for "anim" events for off-screen animations
1472 part->anim_event_state = FALSE;
1474 // do not stop animation before "anim" or "post" counter are finished
1475 if (part->anim_delay_counter == 0 &&
1476 part->post_delay_counter == 0)
1478 StopGlobalAnimSoundAndMusic(part);
1480 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1482 part->post_delay_counter =
1483 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1485 if (part->post_delay_counter > 0)
1486 return ANIM_STATE_RUNNING;
1488 // drawing last frame not needed here -- animation not visible anymore
1489 return ANIM_STATE_RESTART;
1493 if (part->anim_delay_counter > 0)
1495 part->anim_delay_counter--;
1497 if (part->anim_delay_counter == 0)
1499 part->anim_event_state = FALSE;
1501 StopGlobalAnimSoundAndMusic(part);
1503 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1504 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1506 part->post_delay_counter =
1507 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1509 if (part->post_delay_counter > 0)
1510 return ANIM_STATE_RUNNING;
1512 // additional state "RUNNING" required to not skip drawing last frame
1513 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1517 if (part->post_delay_counter > 0)
1519 part->post_delay_counter--;
1521 if (part->post_delay_counter == 0)
1523 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1524 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1526 return ANIM_STATE_RESTART;
1529 return ANIM_STATE_WAITING;
1532 // special case to prevent expiring loop sounds when playing
1533 PlayGlobalAnimSoundIfLoop(part);
1535 if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1536 return ANIM_STATE_RUNNING;
1540 static unsigned int last_counter = -1;
1541 unsigned int counter = Counter();
1543 Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1544 anim_sync_frame, counter - last_counter);
1546 last_counter = counter;
1550 part->x += part->step_xoffset;
1551 part->y += part->step_yoffset;
1553 anim->last_x = part->x;
1554 anim->last_y = part->y;
1556 return ANIM_STATE_RUNNING;
1559 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1562 struct GlobalAnimPartControlInfo *part;
1563 struct GraphicInfo *c = &anim->control_info;
1564 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1565 int state, active_part_nr;
1569 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1570 anim->mode_nr, anim->nr, anim->num_parts);
1571 Debug("anim:HandleGlobalAnim_Main",
1572 "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1576 Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1577 (action == ANIM_START ? "ANIM_START" :
1578 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1579 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1581 anim->state & ANIM_STATE_RESTART,
1582 anim->state & ANIM_STATE_WAITING,
1583 anim->state & ANIM_STATE_RUNNING,
1590 anim->state = anim->last_state = ANIM_STATE_RESTART;
1591 anim->active_part_nr = anim->last_active_part_nr = 0;
1592 anim->part_counter = 0;
1597 if (anim->state == ANIM_STATE_INACTIVE)
1600 anim->state = anim->last_state;
1601 anim->active_part_nr = anim->last_active_part_nr;
1606 anim->state = ANIM_STATE_INACTIVE;
1608 for (i = 0; i < num_parts; i++)
1609 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1617 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1620 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1621 anim->mode_nr, anim->nr, num_parts);
1624 for (i = 0; i < num_parts; i++)
1626 part = &anim->part[i];
1631 anim->state = ANIM_STATE_RUNNING;
1632 part->state = ANIM_STATE_RESTART;
1637 if (part->state == ANIM_STATE_INACTIVE)
1643 part->state = ANIM_STATE_INACTIVE;
1651 part->state = HandleGlobalAnim_Part(part, part->state);
1653 // when animation mode is "once", stop after animation was played once
1654 if (c->anim_mode & ANIM_ONCE &&
1655 part->state & ANIM_STATE_RESTART)
1656 part->state = ANIM_STATE_INACTIVE;
1659 anim->last_state = anim->state;
1660 anim->last_active_part_nr = anim->active_part_nr;
1665 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1666 anim->active_part_nr = getGlobalAnimationPart(anim);
1668 part = &anim->part[anim->active_part_nr];
1670 // first set all animation parts to "inactive", ...
1671 for (i = 0; i < num_parts; i++)
1672 anim->part[i].state = ANIM_STATE_INACTIVE;
1674 // ... then set current animation parts to "running"
1675 part->state = ANIM_STATE_RUNNING;
1677 anim->state = HandleGlobalAnim_Part(part, anim->state);
1679 if (anim->state & ANIM_STATE_RESTART)
1680 anim->part_counter++;
1682 // when animation mode is "once", stop after all animations were played once
1683 if (c->anim_mode & ANIM_ONCE &&
1684 anim->part_counter == anim->num_parts)
1685 anim->state = ANIM_STATE_INACTIVE;
1687 state = anim->state;
1688 active_part_nr = anim->active_part_nr;
1690 // while the animation parts are pausing (waiting or inactive), play the base
1691 // (main) animation; this corresponds to the "boring player animation" logic
1692 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1695 if (anim->state == ANIM_STATE_WAITING ||
1696 anim->state == ANIM_STATE_INACTIVE)
1698 anim->active_part_nr = anim->num_parts; // part nr of base animation
1699 part = &anim->part[anim->active_part_nr];
1701 if (anim->state != anim->last_state)
1702 part->state = ANIM_STATE_RESTART;
1704 anim->state = ANIM_STATE_RUNNING;
1705 part->state = HandleGlobalAnim_Part(part, part->state);
1709 anim->last_state = state;
1710 anim->last_active_part_nr = active_part_nr;
1713 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1718 Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1721 for (i = 0; i < ctrl->num_anims; i++)
1722 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1725 static void HandleGlobalAnim(int action, int game_mode)
1728 Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1731 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1734 static void DoAnimationExt(void)
1739 Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1742 // global animations now synchronized with frame delay of screen update
1745 for (i = 0; i < NUM_GAME_MODES; i++)
1746 HandleGlobalAnim(ANIM_CONTINUE, i);
1749 // force screen redraw in next frame to continue drawing global animations
1750 redraw_mask = REDRAW_ALL;
1754 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1758 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1759 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1760 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1761 ANIM_DELAY_ACTION_NONE);
1763 if (delay_action == ANIM_DELAY_ACTION_NONE)
1766 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1769 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1771 int event_action = (part->init_event_state ?
1772 part->control_info.init_event_action :
1773 part->control_info.anim_event_action);
1775 if (event_action == ANIM_EVENT_ACTION_NONE)
1778 if (event_action < MAX_IMAGE_FILES)
1779 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1781 OpenURLFromHash(anim_url_hash, event_action);
1783 // check if further actions are allowed to be executed
1784 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1790 static void InitGlobalAnim_Clickable(void)
1794 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1796 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1799 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1801 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1804 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1806 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1808 if (part->triggered)
1809 part->clicked = TRUE;
1811 part->triggered = FALSE;
1812 part->clickable = FALSE;
1818 #define ANIM_CLICKED_RESET 0
1819 #define ANIM_CLICKED_PRESSED 1
1820 #define ANIM_CLICKED_RELEASED 2
1822 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1824 boolean click_consumed = FALSE;
1825 boolean anything_clicked = FALSE;
1826 boolean any_part_clicked = FALSE;
1827 boolean any_event_action = FALSE;
1831 // check game modes in reverse draw order (to stop when clicked)
1832 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1834 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1837 // check animations in reverse draw order (to stop when clicked)
1838 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1840 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1843 // check animation parts in reverse draw order (to stop when clicked)
1844 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1846 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1848 if (clicked_event == ANIM_CLICKED_RESET)
1850 part->clicked = FALSE;
1855 if (!part->clickable)
1858 if (!(part->state & ANIM_STATE_RUNNING))
1861 // always handle "any" click events (clicking anywhere on screen) ...
1862 if (clicked_event == ANIM_CLICKED_PRESSED &&
1863 isClickablePart(part, ANIM_EVENT_ANY))
1865 #if DEBUG_ANIM_EVENTS
1866 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
1867 part->old_anim_nr + 1, part->old_nr + 1);
1870 anything_clicked = part->clicked = TRUE;
1871 click_consumed |= clickConsumed(part);
1874 // always handle "unclick:any" events (releasing anywhere on screen) ...
1875 if (clicked_event == ANIM_CLICKED_RELEASED &&
1876 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1878 #if DEBUG_ANIM_EVENTS
1879 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
1880 part->old_anim_nr + 1, part->old_nr + 1);
1883 anything_clicked = part->clicked = TRUE;
1884 click_consumed |= clickConsumed(part);
1887 // ... but only handle the first (topmost) clickable animation
1888 if (any_part_clicked)
1891 if (clicked_event == ANIM_CLICKED_PRESSED &&
1892 isClickedPart(part, mx, my, TRUE))
1895 Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
1896 anim_nr, part_nr, part->control_info.anim_event_action);
1899 // after executing event action, ignore any further actions
1900 if (!any_event_action && DoGlobalAnim_EventAction(part))
1901 any_event_action = TRUE;
1903 // determine if mouse clicks should be blocked from other animations
1904 any_part_clicked |= clickConsumed(part);
1906 if (isClickablePart(part, ANIM_EVENT_SELF))
1908 #if DEBUG_ANIM_EVENTS
1909 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
1910 part->old_anim_nr + 1, part->old_nr + 1);
1913 anything_clicked = part->clicked = TRUE;
1914 click_consumed |= clickConsumed(part);
1917 // determine if mouse clicks should be blocked by this animation
1918 click_consumed |= clickBlocked(part);
1920 // check if this click is defined to trigger other animations
1921 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1922 ANIM_EVENT_CLICK, "CLICK");
1928 if (anything_clicked)
1930 handle_click = TRUE;
1932 for (i = 0; i < NUM_GAME_MODES; i++)
1933 HandleGlobalAnim(ANIM_CONTINUE, i);
1935 handle_click = FALSE;
1937 // prevent ignoring release event if processed within same game frame
1938 StopProcessingEvents();
1941 return (click_consumed || any_event_action);
1944 static void ResetGlobalAnim_Clickable(void)
1946 InitGlobalAnim_Clickable();
1949 static void ResetGlobalAnim_Clicked(void)
1951 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1954 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1956 static boolean click_consumed = FALSE;
1957 static int last_button = 0;
1958 boolean press_event;
1959 boolean release_event;
1960 boolean click_consumed_current = click_consumed;
1962 if (button != 0 && force_click)
1965 // check if button state has changed since last invocation
1966 press_event = (button != 0 && last_button == 0);
1967 release_event = (button == 0 && last_button != 0);
1968 last_button = button;
1972 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1973 click_consumed_current = click_consumed;
1978 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
1979 click_consumed = FALSE;
1982 return click_consumed_current;