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"
23 #define DEBUG_ANIM_DELAY 0
24 #define DEBUG_ANIM_EVENTS 0
27 // values for global toon animation definition
28 #define NUM_GLOBAL_TOON_ANIMS 1
29 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
31 // values for global animation definition (including toons)
32 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
33 NUM_GLOBAL_TOON_ANIMS)
34 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
35 NUM_GLOBAL_TOON_PARTS)
37 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
38 #define ANIM_CLASS_BIT_TITLE 1
39 #define ANIM_CLASS_BIT_MAIN 2
40 #define ANIM_CLASS_BIT_NAMES 3
41 #define ANIM_CLASS_BIT_SCORES 4
42 #define ANIM_CLASS_BIT_SCORESONLY 5
43 #define ANIM_CLASS_BIT_SUBMENU 6
44 #define ANIM_CLASS_BIT_MENU 7
45 #define ANIM_CLASS_BIT_TOONS 8
46 #define ANIM_CLASS_BIT_NO_TITLE 9
48 #define NUM_ANIM_CLASSES 10
50 #define ANIM_CLASS_NONE 0
51 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
52 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
53 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
54 #define ANIM_CLASS_NAMES (1 << ANIM_CLASS_BIT_NAMES)
55 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
56 #define ANIM_CLASS_SCORESONLY (1 << ANIM_CLASS_BIT_SCORESONLY)
57 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
58 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
59 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
60 #define ANIM_CLASS_NO_TITLE (1 << ANIM_CLASS_BIT_NO_TITLE)
62 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
66 #define ANIM_CLASS_TOONS_SCORESONLY (ANIM_CLASS_TOONS | \
68 ANIM_CLASS_SCORESONLY | \
71 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
76 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
78 ANIM_CLASS_SUBMENU | \
81 #define ANIM_CLASS_TOONS_MENU_SUBMENU_2 (ANIM_CLASS_TOONS | \
83 ANIM_CLASS_SUBMENU | \
87 // values for global animation states
88 #define ANIM_STATE_INACTIVE 0
89 #define ANIM_STATE_RESTART (1 << 0)
90 #define ANIM_STATE_WAITING (1 << 1)
91 #define ANIM_STATE_RUNNING (1 << 2)
93 // values for global animation control
94 #define ANIM_NO_ACTION 0
96 #define ANIM_CONTINUE 2
100 struct GlobalAnimPartControlInfo
102 int old_nr; // position before mapping animation parts linearly
103 int old_anim_nr; // position before mapping animations linearly
109 boolean is_base; // animation part is base/main/default animation part
115 struct GraphicInfo graphic_info;
116 struct GraphicInfo control_info;
118 boolean class_playfield_or_door;
126 int step_xoffset, step_yoffset;
129 int tile_xoffset, tile_yoffset;
131 unsigned int initial_anim_sync_frame;
132 unsigned int anim_random_frame;
134 DelayCounter step_delay;
136 int init_delay_counter;
137 int anim_delay_counter;
138 int post_delay_counter;
140 boolean init_event_state;
141 boolean anim_event_state;
150 int last_anim_status;
153 struct GlobalAnimMainControlInfo
155 struct GlobalAnimPartControlInfo base;
156 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
161 struct GraphicInfo control_info;
163 int num_parts; // number of animation parts, but without base part
164 int num_parts_all; // number of animation parts, including base part
168 boolean has_base; // animation has base/main/default animation part
172 int init_delay_counter;
176 int last_state, last_active_part_nr;
179 struct GlobalAnimControlInfo
181 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
187 struct GameModeAnimClass
191 } game_mode_anim_classes_list[] =
193 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
194 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
195 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
196 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
197 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
198 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
199 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
200 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
201 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
202 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
203 { GAME_MODE_NAMES, ANIM_CLASS_TOONS_MENU_SUBMENU },
204 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
205 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
206 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
207 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
208 { GAME_MODE_PSEUDO_NAMESONLY, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
209 { GAME_MODE_PSEUDO_TYPENAMES, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
210 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
211 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
212 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORESONLY },
213 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORESONLY },
214 { GAME_MODE_SCOREINFO, ANIM_CLASS_TOONS_SCORES },
215 { GAME_MODE_EDITOR, ANIM_CLASS_NO_TITLE },
216 { GAME_MODE_PLAYING, ANIM_CLASS_NO_TITLE },
221 struct AnimClassGameMode
225 } anim_class_game_modes_list[] =
227 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
228 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
229 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
230 { ANIM_CLASS_BIT_NAMES, GAME_MODE_NAMES },
231 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
232 { ANIM_CLASS_BIT_SCORESONLY, GAME_MODE_PSEUDO_SCORESONLY },
233 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
234 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
235 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
236 { ANIM_CLASS_BIT_NO_TITLE, GAME_MODE_PSEUDO_NO_TITLE },
241 // forward declaration for internal use
242 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
243 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
244 static void HandleGlobalAnim(int, int);
245 static void DoAnimationExt(void);
246 static void ResetGlobalAnim_Clickable(void);
247 static void ResetGlobalAnim_Clicked(void);
249 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
251 static unsigned int anim_sync_frame = 0;
253 static int game_mode_anim_classes[NUM_GAME_MODES];
254 static int anim_class_game_modes[NUM_ANIM_CLASSES];
256 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
257 static int anim_status_last = GAME_MODE_DEFAULT;
258 static int anim_classes_last = ANIM_CLASS_NONE;
260 static boolean drawing_to_fading_buffer = FALSE;
262 static boolean handle_click = FALSE;
265 // ============================================================================
266 // generic animation frame calculation
267 // ============================================================================
269 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
274 if (delay < 1) // delay must be at least 1
277 sync_frame += start_frame * delay;
279 if (mode & ANIM_LOOP) // looping animation
281 frame = (sync_frame % (delay * num_frames)) / delay;
283 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
285 frame = sync_frame / delay;
287 if (frame > num_frames - 1)
288 frame = num_frames - 1;
290 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
292 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
294 frame = (sync_frame % (delay * max_anim_frames)) / delay;
295 frame = (frame < num_frames ? frame : max_anim_frames - frame);
297 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
299 int max_anim_frames = 2 * num_frames;
301 frame = (sync_frame % (delay * max_anim_frames)) / delay;
302 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
304 else if (mode & ANIM_RANDOM) // play frames in random order
306 // note: expect different frames for the same delay cycle!
308 if (gfx.anim_random_frame < 0)
309 frame = GetSimpleRandom(num_frames);
311 frame = gfx.anim_random_frame % num_frames;
313 else if (mode & ANIM_LEVEL_NR) // play frames by level number
315 int level_pos = level_nr - leveldir_current->first_level;
317 frame = level_pos % num_frames;
319 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
321 frame = sync_frame % num_frames;
324 if (mode & ANIM_REVERSE) // use reverse animation direction
325 frame = num_frames - frame - 1;
331 // ============================================================================
332 // global animation functions
333 // ============================================================================
335 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
337 struct GraphicInfo *c = &anim->control_info;
338 int last_anim_random_frame = gfx.anim_random_frame;
341 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
343 part_nr = getAnimationFrame(anim->num_parts, 1,
344 c->anim_mode, c->anim_start_frame,
347 gfx.anim_random_frame = last_anim_random_frame;
352 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
354 const struct GlobalAnimPartControlInfo *o1 =
355 (struct GlobalAnimPartControlInfo *)obj1;
356 const struct GlobalAnimPartControlInfo *o2 =
357 (struct GlobalAnimPartControlInfo *)obj2;
360 if (o1->control_info.draw_order != o2->control_info.draw_order)
361 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
363 compare_result = o1->nr - o2->nr;
365 return compare_result;
368 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
370 const struct GlobalAnimMainControlInfo *o1 =
371 (struct GlobalAnimMainControlInfo *)obj1;
372 const struct GlobalAnimMainControlInfo *o2 =
373 (struct GlobalAnimMainControlInfo *)obj2;
376 if (o1->control_info.draw_order != o2->control_info.draw_order)
377 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
379 compare_result = o1->nr - o2->nr;
381 return compare_result;
384 static boolean isPausedOnPlayfieldOrDoor(struct GlobalAnimPartControlInfo *part)
386 // only pause playfield and door animations when playing
387 if (game_status != GAME_MODE_PLAYING)
390 // do not pause animations when game ended (and engine is running)
391 if (checkGameEnded())
394 // only pause animations on playfield and doors
395 if (!part->class_playfield_or_door)
398 // only pause animations when engine is paused or request dialog is open(ing)
399 if (!tape.pausing && !game.request_active_or_moving)
405 static void InitToonControls(void)
407 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
408 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
409 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
410 int mode_nr, anim_nr, part_nr;
411 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
412 int num_toons = MAX_NUM_TOONS;
415 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
416 num_toons = global.num_toons;
418 mode_nr = mode_nr_toons;
419 anim_nr = ctrl->num_anims;
422 anim->mode_nr = mode_nr;
423 anim->control_info = graphic_info[control];
426 anim->num_parts_all = 0;
427 anim->part_counter = 0;
428 anim->active_part_nr = 0;
430 anim->has_base = FALSE;
432 anim->last_x = POS_OFFSCREEN;
433 anim->last_y = POS_OFFSCREEN;
435 anim->init_delay_counter = 0;
437 anim->state = ANIM_STATE_INACTIVE;
441 for (i = 0; i < num_toons; i++)
443 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
444 int sound = SND_UNDEFINED;
445 int music = MUS_UNDEFINED;
446 int graphic = IMG_TOON_1 + i;
451 part->anim_nr = anim_nr;
452 part->mode_nr = mode_nr;
454 part->is_base = FALSE;
458 part->graphic = graphic;
460 part->graphic_info = graphic_info[graphic];
461 part->control_info = graphic_info[control];
463 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
465 part->control_info.init_delay_fixed = 0;
466 part->control_info.init_delay_random = 150;
468 part->control_info.x = ARG_UNDEFINED_VALUE;
469 part->control_info.y = ARG_UNDEFINED_VALUE;
471 part->initial_anim_sync_frame = 0;
472 part->anim_random_frame = -1;
474 part->step_delay.count = 0;
475 part->step_delay.value = graphic_info[control].step_delay;
477 part->state = ANIM_STATE_INACTIVE;
478 part->last_anim_status = -1;
481 anim->num_parts_all++;
489 static void InitGlobalAnimControls(void)
492 int mode_nr, anim_nr, part_nr;
493 int sound, music, graphic, control;
497 for (m = 0; m < NUM_GAME_MODES; m++)
501 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
508 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
510 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
511 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
513 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
515 // if no base animation parameters defined, use default values
516 if (control == IMG_UNDEFINED)
517 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
520 anim->mode_nr = mode_nr;
521 anim->control_info = graphic_info[control];
524 anim->num_parts_all = 0;
525 anim->part_counter = 0;
526 anim->active_part_nr = 0;
528 anim->has_base = FALSE;
530 anim->last_x = POS_OFFSCREEN;
531 anim->last_y = POS_OFFSCREEN;
533 anim->init_delay_counter = 0;
535 anim->state = ANIM_STATE_INACTIVE;
539 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
541 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
543 sound = global_anim_info[a].sound[p][m];
544 music = global_anim_info[a].music[p][m];
545 graphic = global_anim_info[a].graphic[p][m];
546 control = global_anim_info[ctrl_id].graphic[p][m];
548 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
549 control == IMG_UNDEFINED)
553 Debug("anim:InitGlobalAnimControls",
554 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
555 m, a, p, mode_nr, anim_nr, part_nr, control);
559 Debug("anim:InitGlobalAnimControls",
560 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
561 m, a, p, mode_nr, anim_nr, part_nr, sound);
565 part->old_anim_nr = a;
568 part->anim_nr = anim_nr;
569 part->mode_nr = mode_nr;
573 part->graphic = graphic;
575 part->graphic_info = graphic_info[graphic];
576 part->control_info = graphic_info[control];
578 part->initial_anim_sync_frame = 0;
579 part->anim_random_frame = -1;
581 part->step_delay.count = 0;
582 part->step_delay.value = graphic_info[control].step_delay;
584 part->state = ANIM_STATE_INACTIVE;
585 part->last_anim_status = -1;
587 anim->num_parts_all++;
589 if (p < GLOBAL_ANIM_ID_PART_BASE)
591 part->is_base = FALSE;
598 part->is_base = TRUE;
601 anim->has_base = TRUE;
604 // apply special settings for pointer-style animations
605 if (part->control_info.class == get_hash_from_key("pointer"))
607 // force animation to be on top (must set anim and part control)
608 if (anim->control_info.draw_order == 0)
609 anim->control_info.draw_order = 1000000;
610 if (part->control_info.draw_order == 0)
611 part->control_info.draw_order = 1000000;
613 // force animation to pass-through clicks (must set part control)
614 if (part->control_info.style == STYLE_DEFAULT)
615 part->control_info.style |= STYLE_PASSTHROUGH;
619 if (anim->num_parts > 0 || anim->has_base)
629 // sort all animations according to draw_order and animation number
630 for (m = 0; m < NUM_GAME_MODES; m++)
632 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
634 // sort all main animations for this game mode
635 qsort(ctrl->anim, ctrl->num_anims,
636 sizeof(struct GlobalAnimMainControlInfo),
637 compareGlobalAnimMainControlInfo);
639 for (a = 0; a < ctrl->num_anims; a++)
641 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
643 // sort all animation parts for this main animation
644 qsort(anim->part, anim->num_parts,
645 sizeof(struct GlobalAnimPartControlInfo),
646 compareGlobalAnimPartControlInfo);
650 for (i = 0; i < NUM_GAME_MODES; i++)
651 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
652 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
653 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
654 game_mode_anim_classes_list[i].class;
656 for (i = 0; i < NUM_ANIM_CLASSES; i++)
657 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
658 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
659 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
660 anim_class_game_modes_list[i].game_mode;
662 anim_status_last_before_fading = GAME_MODE_LOADING;
663 anim_status_last = GAME_MODE_LOADING;
664 anim_classes_last = ANIM_CLASS_NONE;
667 static void SetGlobalAnimEventsForCustomElements(int list_pos)
669 int num_events = GetGlobalAnimEventValueCount(list_pos);
672 for (i = 0; i < num_events; i++)
674 int event = GetGlobalAnimEventValue(list_pos, i);
676 if (event & ANIM_EVENT_CE_CHANGE)
678 int nr = (event >> ANIM_EVENT_CE_BIT) & 0xff;
680 if (nr >= 0 && nr < NUM_CUSTOM_ELEMENTS)
681 element_info[EL_CUSTOM_START + nr].has_anim_event = TRUE;
686 void InitGlobalAnimEventsForCustomElements(void)
691 // custom element events for global animations only relevant while playing
692 m = GAME_MODE_PLAYING;
694 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
696 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
698 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
700 // if no base animation parameters defined, use default values
701 if (control == IMG_UNDEFINED)
702 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
704 SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
705 SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
707 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
709 control = global_anim_info[ctrl_id].graphic[p][m];
711 if (control == IMG_UNDEFINED)
714 SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
715 SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
720 void InitGlobalAnimations(void)
722 InitGlobalAnimControls();
725 static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
726 Bitmap *src_bitmap, int src_x0, int src_y0,
729 struct GraphicInfo *g = &part->graphic_info;
730 struct GraphicInfo *c = &part->control_info;
731 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
732 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
733 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
734 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
735 Bitmap *fade_bitmap =
736 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
737 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
740 for (y = 0; y < c->stacked_yfactor; y++)
742 for (x = 0; x < c->stacked_xfactor; x++)
746 int dst_x = part->x + x * (g->width + c->stacked_xoffset);
747 int dst_y = part->y + y * (g->height + c->stacked_yoffset);
750 int width = g->width;
751 int height = g->height;
759 else if (dst_x > part->viewport_width - g->width)
761 width -= (dst_x - (part->viewport_width - g->width));
770 else if (dst_y > part->viewport_height - g->height)
772 height -= (dst_y - (part->viewport_height - g->height));
775 if (width <= 0 || height <= 0)
781 dst_x += part->viewport_x;
782 dst_y += part->viewport_y;
784 if (drawing_target == DRAW_TO_SCREEN)
785 blit_screen(src_bitmap, src_x, src_y, width, height,
788 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
794 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
796 int game_mode_anim_action[NUM_GAME_MODES];
802 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
803 drawing_target == DRAW_TO_SCREEN)
806 // always start with reliable default values (no animation actions)
807 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
808 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
810 if (global.anim_status != anim_status_last)
812 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
813 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
814 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
817 if (drawing_target == DRAW_TO_FADE_TARGET)
820 // special case: changing from/to these screens is done without fading
821 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
822 global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
823 anim_status_last == GAME_MODE_PSEUDO_TYPENAME ||
824 anim_status_last == GAME_MODE_PSEUDO_TYPENAMES)
827 // ---------- part 1 ------------------------------------------------------
828 // start or stop global animations by change of game mode
829 // (special handling of animations for "current screen" and "all screens")
831 if (global.anim_status_next != anim_status_last_before_fading)
833 // stop animations for last screen before fading to new screen
834 game_mode_anim_action[anim_status_last] = ANIM_STOP;
836 // start animations for current screen after fading to new screen
837 game_mode_anim_action[global.anim_status] = ANIM_START;
840 // start animations for all screens after loading new artwork set
841 if (anim_status_last == GAME_MODE_LOADING)
842 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
844 // ---------- part 2 ------------------------------------------------------
845 // start or stop global animations by change of animation class
846 // (generic handling of animations for "class of screens")
848 for (i = 0; i < NUM_ANIM_CLASSES; i++)
850 int anim_class_check = (1 << i);
851 int anim_class_game_mode = anim_class_game_modes[i];
852 int anim_class_last = anim_classes_last & anim_class_check;
853 int anim_class_next = anim_classes_next & anim_class_check;
855 // stop animations for changed screen class before fading to new screen
856 if (before_fading && anim_class_last && !anim_class_next)
857 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
859 // start animations for changed screen class after fading to new screen
860 if (after_fading && !anim_class_last && anim_class_next)
861 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
864 if (drawing_target == DRAW_TO_SCREEN)
868 anim_classes_last = anim_classes_next;
869 anim_status_last_before_fading = global.anim_status;
872 anim_status_last = global.anim_status;
874 // start or stop animations determined to be started or stopped above
875 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
876 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
877 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
879 else if (drawing_target == DRAW_TO_FADE_TARGET)
881 drawing_to_fading_buffer = TRUE;
883 // start animations determined to be (temporary) started above
884 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
885 if (game_mode_anim_action[mode_nr] == ANIM_START)
886 HandleGlobalAnim(ANIM_START, mode_nr);
890 // when restarting global animations, do not redraw them, but stop here
891 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
894 if (global.anim_status == GAME_MODE_LOADING)
897 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
899 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
902 // when preparing source fading buffer, only draw animations to be stopped
903 if (drawing_target == DRAW_TO_FADE_SOURCE &&
904 game_mode_anim_action[mode_nr] != ANIM_STOP)
907 // when preparing target fading buffer, only draw animations to be started
908 if (drawing_target == DRAW_TO_FADE_TARGET &&
909 game_mode_anim_action[mode_nr] != ANIM_START)
913 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
914 mode_nr != game_status)
918 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
920 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
921 struct GraphicInfo *c = &anim->control_info;
922 int part_first, part_last;
925 if (!(anim->state & ANIM_STATE_RUNNING))
928 part_first = part_last = anim->active_part_nr;
930 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
932 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
935 part_last = num_parts - 1;
938 for (part_nr = part_first; part_nr <= part_last; part_nr++)
940 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
941 struct GraphicInfo *g = &part->graphic_info;
946 int last_anim_random_frame = gfx.anim_random_frame;
948 if (!(part->state & ANIM_STATE_RUNNING))
951 if (part->drawing_stage != drawing_stage)
954 // if game is paused, also pause playfield and door animations
955 if (isPausedOnPlayfieldOrDoor(part))
956 part->initial_anim_sync_frame++;
958 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
960 // re-initialize random animation frame after animation delay
961 if (g->anim_mode == ANIM_RANDOM &&
962 sync_frame % g->anim_delay == 0 &&
964 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
966 gfx.anim_random_frame = part->anim_random_frame;
968 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
969 g->anim_mode, g->anim_start_frame,
972 gfx.anim_random_frame = last_anim_random_frame;
974 getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
977 BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
982 if (drawing_target == DRAW_TO_FADE_TARGET)
984 // stop animations determined to be (temporary) started above
985 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
986 if (game_mode_anim_action[mode_nr] == ANIM_START)
987 HandleGlobalAnim(ANIM_STOP, mode_nr);
989 drawing_to_fading_buffer = FALSE;
993 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
995 int last_cursor_mode_override = gfx.cursor_mode_override;
997 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
999 ResetGlobalAnim_Clickable();
1001 gfx.cursor_mode_override = CURSOR_UNDEFINED;
1004 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
1006 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
1008 ResetGlobalAnim_Clicked();
1011 if (gfx.cursor_mode_override != last_cursor_mode_override)
1012 SetMouseCursor(gfx.cursor_mode);
1015 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
1020 int viewport_height;
1021 boolean changed = FALSE;
1023 if (part->last_anim_status == global.anim_status &&
1024 part->control_info.class != get_hash_from_key("pointer"))
1027 part->last_anim_status = global.anim_status;
1029 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
1031 part->class_playfield_or_door = FALSE;
1033 if (part->control_info.class == get_hash_from_key("window") ||
1034 part->control_info.class == get_hash_from_key("border"))
1038 viewport_width = WIN_XSIZE;
1039 viewport_height = WIN_YSIZE;
1041 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
1043 else if (part->control_info.class == get_hash_from_key("pointer"))
1045 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1046 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1048 // prevent displaying off-screen custom mouse cursor in upper left corner
1049 if (gfx.mouse_x == POS_OFFSCREEN &&
1050 gfx.mouse_y == POS_OFFSCREEN)
1051 mx = my = POS_OFFSCREEN;
1053 viewport_x = mx - part->control_info.x;
1054 viewport_y = my - part->control_info.y;
1055 viewport_width = part->graphic_info.width;
1056 viewport_height = part->graphic_info.height;
1058 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1060 // do not use global animation mouse pointer when reloading artwork
1061 if (global.anim_status != GAME_MODE_LOADING)
1062 gfx.cursor_mode_override = CURSOR_NONE;
1064 else if (part->control_info.class == get_hash_from_key("door_1"))
1068 viewport_width = DXSIZE;
1069 viewport_height = DYSIZE;
1071 part->class_playfield_or_door = TRUE;
1073 else if (part->control_info.class == get_hash_from_key("door_2"))
1075 if (part->mode_nr == GAME_MODE_EDITOR)
1079 viewport_width = EXSIZE;
1080 viewport_height = EYSIZE;
1086 viewport_width = VXSIZE;
1087 viewport_height = VYSIZE;
1090 part->class_playfield_or_door = TRUE;
1092 else // default: "playfield"
1094 viewport_x = REAL_SX;
1095 viewport_y = REAL_SY;
1096 viewport_width = FULL_SXSIZE;
1097 viewport_height = FULL_SYSIZE;
1099 part->class_playfield_or_door = TRUE;
1102 if (viewport_x != part->viewport_x ||
1103 viewport_y != part->viewport_y ||
1104 viewport_width != part->viewport_width ||
1105 viewport_height != part->viewport_height)
1107 part->viewport_x = viewport_x;
1108 part->viewport_y = viewport_y;
1109 part->viewport_width = viewport_width;
1110 part->viewport_height = viewport_height;
1112 if (part->control_info.class != get_hash_from_key("pointer"))
1119 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1121 int sound = part->sound;
1123 if (sound == SND_UNDEFINED)
1126 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1127 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1130 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1131 if (IS_LOOP_SOUND(sound))
1132 PlaySoundLoop(sound);
1137 Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1138 part->anim_nr, part->nr, part->mode_nr, sound);
1142 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1144 int sound = part->sound;
1146 if (sound == SND_UNDEFINED)
1152 Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1153 part->anim_nr, part->nr, part->mode_nr, sound);
1157 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1159 int music = part->music;
1161 if (music == MUS_UNDEFINED)
1164 if (!setup.sound_music)
1167 if (IS_LOOP_MUSIC(music))
1168 PlayMusicLoop(music);
1173 Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1174 part->anim_nr, part->nr, part->mode_nr, music);
1178 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1180 int music = part->music;
1182 if (music == MUS_UNDEFINED)
1185 char *anim_music = getMusicInfoEntryFilename(music);
1186 char *curr_music = getCurrentlyPlayingMusicFilename();
1188 // do not stop music if global anim music differs from current music
1189 if (!strEqual(curr_music, anim_music))
1195 Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1196 part->anim_nr, part->nr, part->mode_nr, music);
1200 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1202 // when drawing animations to fading buffer, do not play sounds or music
1203 if (drawing_to_fading_buffer)
1206 PlayGlobalAnimSound(part);
1207 PlayGlobalAnimMusic(part);
1210 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1212 StopGlobalAnimSound(part);
1213 StopGlobalAnimMusic(part);
1216 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1218 // when drawing animations to fading buffer, do not play sounds
1219 if (drawing_to_fading_buffer)
1222 // loop sounds only expire when playing
1223 if (game_status != GAME_MODE_PLAYING)
1226 // check if any sound is defined for this animation part
1227 if (part->sound == SND_UNDEFINED)
1230 // normal (non-loop) sounds do not expire when playing
1231 if (!IS_LOOP_SOUND(part->sound))
1234 // prevent expiring loop sounds when playing
1235 PlayGlobalAnimSound(part);
1238 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1240 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1241 int mask_ce_only = mask & ~ANIM_EVENT_PAGE_MASK;
1243 if (mask & ANIM_EVENT_ANY)
1244 return (anim_event & ANIM_EVENT_ANY);
1245 else if (mask & ANIM_EVENT_SELF)
1246 return (anim_event & ANIM_EVENT_SELF);
1247 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1248 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1249 else if (mask & ANIM_EVENT_CE_CHANGE)
1250 return (anim_event == mask ||
1251 anim_event == mask_ce_only);
1253 return (anim_event == mask ||
1254 anim_event == mask_anim_only);
1257 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1259 struct GraphicInfo *c = &part->control_info;
1262 if (part->init_event_state)
1264 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1266 for (i = 0; i < num_init_events; i++)
1268 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1270 if (checkGlobalAnimEvent(init_event, mask))
1274 else if (part->anim_event_state)
1276 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1278 for (i = 0; i < num_anim_events; i++)
1280 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1282 if (checkGlobalAnimEvent(anim_event, mask))
1290 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1293 struct GraphicInfo *g = &part->graphic_info;
1294 struct GraphicInfo *c = &part->control_info;
1295 int part_x = part->viewport_x + part->x;
1296 int part_y = part->viewport_y + part->y;
1297 int part_width = g->width;
1298 int part_height = g->height;
1301 for (y = 0; y < c->stacked_yfactor; y++)
1303 for (x = 0; x < c->stacked_xfactor; x++)
1305 int part_stacked_x = part_x + x * (part_width + c->stacked_xoffset);
1306 int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1308 if (mx >= part_stacked_x &&
1309 mx < part_stacked_x + part_width &&
1310 my >= part_stacked_y &&
1311 my < part_stacked_y + part_height)
1319 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1320 int mx, int my, boolean clicked)
1322 // check if mouse click was detected at all
1326 // check if mouse click is outside the animation part's viewport
1327 if (mx < part->viewport_x ||
1328 mx >= part->viewport_x + part->viewport_width ||
1329 my < part->viewport_y ||
1330 my >= part->viewport_y + part->viewport_height)
1333 // check if mouse click is inside the animation part's (stacked) graphic
1334 if (isInsidePartStacked(part, mx, my))
1340 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1342 return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1345 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1347 return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1350 static void SetGlobalAnimPartTileXY(struct GlobalAnimPartControlInfo *part)
1352 // calculate playfield position (with scrolling) for related CE tile
1353 // (do not use FX/FY, which are incorrect during envelope requests)
1354 int FX0 = 2 * TILEX_VAR; // same as FX during DRAW_TO_FIELDBUFFER
1355 int FY0 = 2 * TILEY_VAR; // same as FY during DRAW_TO_FIELDBUFFER
1356 int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1357 int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1358 int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
1359 int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
1360 int cx = SX - REAL_SX;
1361 int cy = SY - REAL_SY;
1362 int x = sx - fx + cx;
1363 int y = sy - fy + cy;
1365 part->tile_xoffset += part->step_xoffset;
1366 part->tile_yoffset += part->step_yoffset;
1368 part->x = x + part->tile_xoffset;
1369 part->y = y + part->tile_yoffset;
1372 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1373 boolean *click_consumed,
1374 boolean *any_event_action,
1375 int event_value, char *info_text)
1377 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1379 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1380 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1381 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1384 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1388 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1390 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1393 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1395 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1397 if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1400 if (isClickablePart(part2, mask))
1402 part2->triggered = TRUE;
1403 *click_consumed |= clickConsumed(part); // click was on "part"!
1405 #if DEBUG_ANIM_EVENTS
1406 Debug("anim:InitGlobalAnim_Triggered",
1407 "%d.%d TRIGGERED BY %s OF %d.%d",
1408 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1409 part->old_anim_nr + 1, part->old_nr + 1);
1412 Debug("anim:InitGlobalAnim_Triggered",
1413 "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1414 part2->control_info.anim_event_action);
1417 // after executing event action, ignore any further actions
1418 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1419 *any_event_action = TRUE;
1423 struct GraphicInfo *c = &part2->control_info;
1425 if (isClickablePart(part2, mask))
1426 Debug("anim:InitGlobalAnim_Triggered",
1427 "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1428 anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1431 Debug("anim:InitGlobalAnim_Triggered",
1432 "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1433 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1439 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1444 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1446 int event_value = ANIM_EVENT_CE_CHANGE;
1447 int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1448 int mask = event_value | event_bits;
1451 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1453 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1456 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1458 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1460 if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1463 if (isClickablePart(part2, mask) && !part2->triggered)
1465 struct GraphicInfo *c = &part2->control_info;
1467 if (c->position == POS_CE ||
1468 c->position == POS_CE_TRIGGER)
1470 // store CE tile and offset position to handle scrolling
1471 part2->tile_x = (c->position == POS_CE_TRIGGER ? trigger_x : x);
1472 part2->tile_y = (c->position == POS_CE_TRIGGER ? trigger_y : y);
1473 part2->tile_xoffset = c->x;
1474 part2->tile_yoffset = c->y;
1476 // set resulting animation position relative to CE tile position
1477 // (but only for ".init_event", not ".anim_event" type events)
1478 if (part2->init_event_state)
1479 SetGlobalAnimPartTileXY(part2);
1481 // restart animation (by using current sync frame)
1482 part2->initial_anim_sync_frame = anim_sync_frame;
1485 part2->triggered = TRUE;
1487 // do not trigger any other animation if CE change event was consumed
1488 if (c->style == STYLE_CONSUME_CE_EVENT)
1492 Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1493 "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1500 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1501 int delay_type, char *info_text)
1503 #if DEBUG_ANIM_DELAY
1504 Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1505 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1508 DoGlobalAnim_DelayAction(part, delay_type);
1511 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1512 int event_value, char *info_text)
1514 #if DEBUG_ANIM_EVENTS
1515 Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1516 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1519 boolean click_consumed = FALSE;
1520 boolean any_event_action = FALSE;
1522 // check if this event is defined to trigger other animations
1523 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1524 event_value, info_text);
1527 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1530 if (handle_click && !part->clicked)
1533 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1534 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1535 struct GraphicInfo *g = &part->graphic_info;
1536 struct GraphicInfo *c = &part->control_info;
1537 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1539 // if game is paused, also pause playfield and door animations
1540 if (isPausedOnPlayfieldOrDoor(part))
1543 if (viewport_changed)
1544 state |= ANIM_STATE_RESTART;
1546 if (state & ANIM_STATE_RESTART)
1548 // when drawing animations to fading buffer, only start fixed animations
1549 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1550 c->y == ARG_UNDEFINED_VALUE))
1551 return ANIM_STATE_INACTIVE;
1553 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1555 part->init_delay_counter =
1556 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1558 part->anim_delay_counter =
1559 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1561 part->post_delay_counter = 0;
1563 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1564 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1566 part->initial_anim_sync_frame =
1567 (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1568 anim_sync_frame + part->init_delay_counter);
1570 // do not re-initialize random animation frame after fade-in
1571 if (part->anim_random_frame == -1)
1572 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1574 if (c->direction & MV_HORIZONTAL)
1576 int pos_bottom = part->viewport_height - g->height;
1578 if (c->position == POS_TOP)
1580 else if (c->position == POS_UPPER)
1581 part->y = GetSimpleRandom(pos_bottom / 2);
1582 else if (c->position == POS_MIDDLE)
1583 part->y = pos_bottom / 2;
1584 else if (c->position == POS_LOWER)
1585 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1586 else if (c->position == POS_BOTTOM)
1587 part->y = pos_bottom;
1589 part->y = GetSimpleRandom(pos_bottom);
1591 if (c->direction == MV_RIGHT)
1593 part->step_xoffset = c->step_offset;
1594 part->x = -g->width + part->step_xoffset;
1598 part->step_xoffset = -c->step_offset;
1599 part->x = part->viewport_width + part->step_xoffset;
1602 part->step_yoffset = 0;
1604 else if (c->direction & MV_VERTICAL)
1606 int pos_right = part->viewport_width - g->width;
1608 if (c->position == POS_LEFT)
1610 else if (c->position == POS_RIGHT)
1611 part->x = pos_right;
1613 part->x = GetSimpleRandom(pos_right);
1615 if (c->direction == MV_DOWN)
1617 part->step_yoffset = c->step_offset;
1618 part->y = -g->height + part->step_yoffset;
1622 part->step_yoffset = -c->step_offset;
1623 part->y = part->viewport_height + part->step_yoffset;
1626 part->step_xoffset = 0;
1633 part->step_xoffset = 0;
1634 part->step_yoffset = 0;
1637 if (part->control_info.class != get_hash_from_key("pointer"))
1639 if (c->x != ARG_UNDEFINED_VALUE)
1641 if (c->y != ARG_UNDEFINED_VALUE)
1645 if (c->position == POS_LAST &&
1646 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1647 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1649 part->x = anim->last_x;
1650 part->y = anim->last_y;
1653 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1654 part->step_xoffset = c->step_xoffset;
1655 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1656 part->step_yoffset = c->step_yoffset;
1658 if (part->init_delay_counter == 0 &&
1659 !part->init_event_state)
1661 PlayGlobalAnimSoundAndMusic(part);
1663 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1664 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1668 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1672 if (part->clicked &&
1673 part->init_event_state)
1675 if (part->initial_anim_sync_frame > 0)
1676 part->initial_anim_sync_frame = anim_sync_frame;
1678 part->init_delay_counter = 1;
1679 part->init_event_state = FALSE;
1681 part->clicked = FALSE;
1684 if (part->clicked &&
1685 part->anim_event_state)
1687 part->anim_delay_counter = 1;
1688 part->anim_event_state = FALSE;
1690 part->clicked = FALSE;
1693 if (part->init_delay_counter > 0)
1695 part->init_delay_counter--;
1697 if (part->init_delay_counter == 0)
1699 part->init_event_state = FALSE;
1701 PlayGlobalAnimSoundAndMusic(part);
1703 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1704 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1706 // continue with state ANIM_STATE_RUNNING (set below)
1710 return ANIM_STATE_WAITING;
1714 if (part->init_event_state)
1715 return ANIM_STATE_WAITING;
1717 // animation part is now running/visible and therefore clickable
1718 part->clickable = TRUE;
1720 // check if moving animation has left the visible screen area
1721 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1722 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1723 (part->y <= -g->height && part->step_yoffset <= 0) ||
1724 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1726 // do not wait for "anim" events for off-screen animations
1727 part->anim_event_state = FALSE;
1729 // do not stop animation before "anim" or "post" counter are finished
1730 if (part->anim_delay_counter == 0 &&
1731 part->post_delay_counter == 0)
1733 StopGlobalAnimSoundAndMusic(part);
1735 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1737 part->post_delay_counter =
1738 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1740 if (part->post_delay_counter > 0)
1741 return ANIM_STATE_RUNNING;
1743 // drawing last frame not needed here -- animation not visible anymore
1744 return ANIM_STATE_RESTART;
1748 if (part->anim_delay_counter > 0)
1750 part->anim_delay_counter--;
1752 if (part->anim_delay_counter == 0)
1754 part->anim_event_state = FALSE;
1756 StopGlobalAnimSoundAndMusic(part);
1758 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1759 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1761 part->post_delay_counter =
1762 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1764 if (part->post_delay_counter > 0)
1765 return ANIM_STATE_RUNNING;
1767 // additional state "RUNNING" required to not skip drawing last frame
1768 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1772 if (part->post_delay_counter > 0)
1774 part->post_delay_counter--;
1776 if (part->post_delay_counter == 0)
1778 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1779 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1781 return ANIM_STATE_RESTART;
1784 return ANIM_STATE_WAITING;
1787 // special case to prevent expiring loop sounds when playing
1788 PlayGlobalAnimSoundIfLoop(part);
1790 if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1791 return ANIM_STATE_RUNNING;
1795 static unsigned int last_counter = -1;
1796 unsigned int counter = Counter();
1798 Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1799 anim_sync_frame, counter - last_counter);
1801 last_counter = counter;
1805 if (c->position == POS_CE ||
1806 c->position == POS_CE_TRIGGER)
1808 SetGlobalAnimPartTileXY(part);
1812 part->x += part->step_xoffset;
1813 part->y += part->step_yoffset;
1816 anim->last_x = part->x;
1817 anim->last_y = part->y;
1819 return ANIM_STATE_RUNNING;
1822 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1825 struct GlobalAnimPartControlInfo *part;
1826 struct GraphicInfo *c = &anim->control_info;
1827 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1828 int state, active_part_nr;
1832 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1833 anim->mode_nr, anim->nr, anim->num_parts);
1834 Debug("anim:HandleGlobalAnim_Main",
1835 "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1839 Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1840 (action == ANIM_START ? "ANIM_START" :
1841 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1842 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1844 anim->state & ANIM_STATE_RESTART,
1845 anim->state & ANIM_STATE_WAITING,
1846 anim->state & ANIM_STATE_RUNNING,
1853 anim->state = anim->last_state = ANIM_STATE_RESTART;
1854 anim->active_part_nr = anim->last_active_part_nr = 0;
1855 anim->part_counter = 0;
1860 if (anim->state == ANIM_STATE_INACTIVE)
1863 anim->state = anim->last_state;
1864 anim->active_part_nr = anim->last_active_part_nr;
1869 anim->state = ANIM_STATE_INACTIVE;
1871 for (i = 0; i < num_parts; i++)
1872 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1880 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1883 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1884 anim->mode_nr, anim->nr, num_parts);
1887 for (i = 0; i < num_parts; i++)
1889 part = &anim->part[i];
1894 anim->state = ANIM_STATE_RUNNING;
1895 part->state = ANIM_STATE_RESTART;
1900 if (part->state == ANIM_STATE_INACTIVE)
1906 part->state = ANIM_STATE_INACTIVE;
1914 part->state = HandleGlobalAnim_Part(part, part->state);
1916 // when animation mode is "once", stop after animation was played once
1917 if (c->anim_mode & ANIM_ONCE &&
1918 part->state & ANIM_STATE_RESTART)
1919 part->state = ANIM_STATE_INACTIVE;
1922 anim->last_state = anim->state;
1923 anim->last_active_part_nr = anim->active_part_nr;
1928 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1929 anim->active_part_nr = getGlobalAnimationPart(anim);
1931 part = &anim->part[anim->active_part_nr];
1933 // first set all animation parts to "inactive", ...
1934 for (i = 0; i < num_parts; i++)
1935 anim->part[i].state = ANIM_STATE_INACTIVE;
1937 // ... then set current animation part to "running" ...
1938 part->state = ANIM_STATE_RUNNING;
1940 // ... unless it is waiting for an initial event
1941 if (part->init_event_state)
1942 part->state = ANIM_STATE_WAITING;
1944 anim->state = HandleGlobalAnim_Part(part, anim->state);
1946 if (anim->state & ANIM_STATE_RESTART)
1947 anim->part_counter++;
1949 // when animation mode is "once", stop after all animations were played once
1950 if (c->anim_mode & ANIM_ONCE &&
1951 anim->part_counter == anim->num_parts)
1952 anim->state = ANIM_STATE_INACTIVE;
1954 state = anim->state;
1955 active_part_nr = anim->active_part_nr;
1957 // while the animation parts are pausing (waiting or inactive), play the base
1958 // (main) animation; this corresponds to the "boring player animation" logic
1959 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1962 if (anim->state == ANIM_STATE_WAITING ||
1963 anim->state == ANIM_STATE_INACTIVE)
1965 anim->active_part_nr = anim->num_parts; // part nr of base animation
1966 part = &anim->part[anim->active_part_nr];
1968 if (anim->state != anim->last_state)
1969 part->state = ANIM_STATE_RESTART;
1971 anim->state = ANIM_STATE_RUNNING;
1972 part->state = HandleGlobalAnim_Part(part, part->state);
1976 anim->last_state = state;
1977 anim->last_active_part_nr = active_part_nr;
1980 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1985 Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1988 for (i = 0; i < ctrl->num_anims; i++)
1989 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1992 static void HandleGlobalAnim(int action, int game_mode)
1995 Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1998 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
2001 static void DoAnimationExt(void)
2006 Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
2009 // global animations now synchronized with frame delay of screen update
2012 for (i = 0; i < NUM_GAME_MODES; i++)
2013 HandleGlobalAnim(ANIM_CONTINUE, i);
2016 // force screen redraw in next frame to continue drawing global animations
2017 redraw_mask = REDRAW_ALL;
2021 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
2025 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
2026 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
2027 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
2028 ANIM_DELAY_ACTION_NONE);
2030 if (delay_action == ANIM_DELAY_ACTION_NONE)
2033 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
2036 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
2038 int event_action = (part->init_event_state ?
2039 part->control_info.init_event_action :
2040 part->control_info.anim_event_action);
2042 if (event_action == ANIM_EVENT_ACTION_NONE)
2045 if (event_action < MAX_IMAGE_FILES)
2046 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
2048 OpenURLFromHash(anim_url_hash, event_action);
2050 // check if further actions are allowed to be executed
2051 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
2057 static void InitGlobalAnim_Clickable(void)
2061 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
2063 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2066 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
2068 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2071 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
2073 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2075 if (part->triggered)
2076 part->clicked = TRUE;
2078 part->triggered = FALSE;
2079 part->clickable = FALSE;
2085 #define ANIM_CLICKED_RESET 0
2086 #define ANIM_CLICKED_PRESSED 1
2087 #define ANIM_CLICKED_RELEASED 2
2089 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2091 boolean click_consumed = FALSE;
2092 boolean anything_clicked = FALSE;
2093 boolean any_part_clicked = FALSE;
2094 boolean any_event_action = FALSE;
2098 // check game modes in reverse draw order (to stop when clicked)
2099 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
2101 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2104 // check animations in reverse draw order (to stop when clicked)
2105 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
2107 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2110 // check animation parts in reverse draw order (to stop when clicked)
2111 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
2113 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2115 if (clicked_event == ANIM_CLICKED_RESET)
2117 part->clicked = FALSE;
2122 if (!part->clickable)
2125 if (!(part->state & ANIM_STATE_RUNNING))
2128 // always handle "any" click events (clicking anywhere on screen) ...
2129 if (clicked_event == ANIM_CLICKED_PRESSED &&
2130 isClickablePart(part, ANIM_EVENT_ANY))
2132 #if DEBUG_ANIM_EVENTS
2133 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2134 part->old_anim_nr + 1, part->old_nr + 1);
2137 anything_clicked = part->clicked = TRUE;
2138 click_consumed |= clickConsumed(part);
2141 // always handle "unclick:any" events (releasing anywhere on screen) ...
2142 if (clicked_event == ANIM_CLICKED_RELEASED &&
2143 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2145 #if DEBUG_ANIM_EVENTS
2146 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2147 part->old_anim_nr + 1, part->old_nr + 1);
2150 anything_clicked = part->clicked = TRUE;
2151 click_consumed |= clickConsumed(part);
2154 // ... but only handle the first (topmost) clickable animation
2155 if (any_part_clicked)
2158 if (clicked_event == ANIM_CLICKED_PRESSED &&
2159 isClickedPart(part, mx, my, TRUE))
2162 Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2163 anim_nr, part_nr, part->control_info.anim_event_action);
2166 // after executing event action, ignore any further actions
2167 if (!any_event_action && DoGlobalAnim_EventAction(part))
2168 any_event_action = TRUE;
2170 // determine if mouse clicks should be blocked from other animations
2171 any_part_clicked |= clickConsumed(part);
2173 if (isClickablePart(part, ANIM_EVENT_SELF))
2175 #if DEBUG_ANIM_EVENTS
2176 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2177 part->old_anim_nr + 1, part->old_nr + 1);
2180 anything_clicked = part->clicked = TRUE;
2181 click_consumed |= clickConsumed(part);
2184 // determine if mouse clicks should be blocked by this animation
2185 click_consumed |= clickBlocked(part);
2187 // check if this click is defined to trigger other animations
2188 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2189 ANIM_EVENT_CLICK, "CLICK");
2195 if (anything_clicked)
2197 handle_click = TRUE;
2199 for (i = 0; i < NUM_GAME_MODES; i++)
2200 HandleGlobalAnim(ANIM_CONTINUE, i);
2202 handle_click = FALSE;
2204 // prevent ignoring release event if processed within same game frame
2205 StopProcessingEvents();
2208 return (click_consumed || any_event_action);
2211 static void ResetGlobalAnim_Clickable(void)
2213 InitGlobalAnim_Clickable();
2216 static void ResetGlobalAnim_Clicked(void)
2218 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2221 void RestartGlobalAnimsByStatus(int status)
2223 int anim_status_last = global.anim_status;
2225 global.anim_status = status;
2227 // force restarting global animations by changed global animation status
2228 DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
2230 global.anim_status = anim_status_last;
2233 void SetAnimStatusBeforeFading(int status)
2235 anim_status_last_before_fading = status;
2238 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2240 static boolean click_consumed = FALSE;
2241 static int last_button = 0;
2242 boolean press_event;
2243 boolean release_event;
2244 boolean click_consumed_current = click_consumed;
2246 if (button != 0 && force_click)
2249 // check if button state has changed since last invocation
2250 press_event = (button != 0 && last_button == 0);
2251 release_event = (button == 0 && last_button != 0);
2252 last_button = button;
2256 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2257 click_consumed_current = click_consumed;
2262 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2263 click_consumed = FALSE;
2266 return click_consumed_current;
2269 int getGlobalAnimSyncFrame(void)
2271 return anim_sync_frame;
2274 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y,
2275 int trigger_x, int trigger_y)
2277 if (!IS_CUSTOM_ELEMENT(element))
2280 // custom element stored as 0 to 255, change page stored as 1 to 32
2281 InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,
2282 x, y, trigger_x, trigger_y);