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;
126 int tile_xoffset, tile_yoffset;
128 unsigned int initial_anim_sync_frame;
129 unsigned int anim_random_frame;
131 DelayCounter step_delay;
133 int init_delay_counter;
134 int anim_delay_counter;
135 int post_delay_counter;
137 boolean init_event_state;
138 boolean anim_event_state;
147 int last_anim_status;
150 struct GlobalAnimMainControlInfo
152 struct GlobalAnimPartControlInfo base;
153 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
158 struct GraphicInfo control_info;
160 int num_parts; // number of animation parts, but without base part
161 int num_parts_all; // number of animation parts, including base part
165 boolean has_base; // animation has base/main/default animation part
169 int init_delay_counter;
173 int last_state, last_active_part_nr;
176 struct GlobalAnimControlInfo
178 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
184 struct GameModeAnimClass
188 } game_mode_anim_classes_list[] =
190 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
191 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
192 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
193 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
194 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
195 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
196 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
197 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
198 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
199 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
200 { GAME_MODE_NAMES, ANIM_CLASS_TOONS_MENU_SUBMENU },
201 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
202 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
203 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
204 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
205 { GAME_MODE_PSEUDO_NAMESONLY, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
206 { GAME_MODE_PSEUDO_TYPENAMES, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
207 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
208 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
209 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORESONLY },
210 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORESONLY },
211 { GAME_MODE_SCOREINFO, ANIM_CLASS_TOONS_SCORES },
212 { GAME_MODE_EDITOR, ANIM_CLASS_NO_TITLE },
213 { GAME_MODE_PLAYING, ANIM_CLASS_NO_TITLE },
218 struct AnimClassGameMode
222 } anim_class_game_modes_list[] =
224 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
225 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
226 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
227 { ANIM_CLASS_BIT_NAMES, GAME_MODE_NAMES },
228 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
229 { ANIM_CLASS_BIT_SCORESONLY, GAME_MODE_PSEUDO_SCORESONLY },
230 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
231 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
232 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
233 { ANIM_CLASS_BIT_NO_TITLE, GAME_MODE_PSEUDO_NO_TITLE },
238 // forward declaration for internal use
239 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
240 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
241 static void HandleGlobalAnim(int, int);
242 static void DoAnimationExt(void);
243 static void ResetGlobalAnim_Clickable(void);
244 static void ResetGlobalAnim_Clicked(void);
246 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
248 static unsigned int anim_sync_frame = 0;
250 static int game_mode_anim_classes[NUM_GAME_MODES];
251 static int anim_class_game_modes[NUM_ANIM_CLASSES];
253 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
254 static int anim_status_last = GAME_MODE_DEFAULT;
255 static int anim_classes_last = ANIM_CLASS_NONE;
257 static boolean drawing_to_fading_buffer = FALSE;
259 static boolean handle_click = FALSE;
262 // ============================================================================
263 // generic animation frame calculation
264 // ============================================================================
266 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
271 if (delay < 1) // delay must be at least 1
274 sync_frame += start_frame * delay;
276 if (mode & ANIM_LOOP) // looping animation
278 frame = (sync_frame % (delay * num_frames)) / delay;
280 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
282 frame = sync_frame / delay;
284 if (frame > num_frames - 1)
285 frame = num_frames - 1;
287 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
289 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
291 frame = (sync_frame % (delay * max_anim_frames)) / delay;
292 frame = (frame < num_frames ? frame : max_anim_frames - frame);
294 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
296 int max_anim_frames = 2 * num_frames;
298 frame = (sync_frame % (delay * max_anim_frames)) / delay;
299 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
301 else if (mode & ANIM_RANDOM) // play frames in random order
303 // note: expect different frames for the same delay cycle!
305 if (gfx.anim_random_frame < 0)
306 frame = GetSimpleRandom(num_frames);
308 frame = gfx.anim_random_frame % num_frames;
310 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
312 frame = sync_frame % num_frames;
315 if (mode & ANIM_REVERSE) // use reverse animation direction
316 frame = num_frames - frame - 1;
322 // ============================================================================
323 // global animation functions
324 // ============================================================================
326 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
328 struct GraphicInfo *c = &anim->control_info;
329 int last_anim_random_frame = gfx.anim_random_frame;
332 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
334 part_nr = getAnimationFrame(anim->num_parts, 1,
335 c->anim_mode, c->anim_start_frame,
338 gfx.anim_random_frame = last_anim_random_frame;
343 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
345 const struct GlobalAnimPartControlInfo *o1 =
346 (struct GlobalAnimPartControlInfo *)obj1;
347 const struct GlobalAnimPartControlInfo *o2 =
348 (struct GlobalAnimPartControlInfo *)obj2;
351 if (o1->control_info.draw_order != o2->control_info.draw_order)
352 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
354 compare_result = o1->nr - o2->nr;
356 return compare_result;
359 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
361 const struct GlobalAnimMainControlInfo *o1 =
362 (struct GlobalAnimMainControlInfo *)obj1;
363 const struct GlobalAnimMainControlInfo *o2 =
364 (struct GlobalAnimMainControlInfo *)obj2;
367 if (o1->control_info.draw_order != o2->control_info.draw_order)
368 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
370 compare_result = o1->nr - o2->nr;
372 return compare_result;
375 static void InitToonControls(void)
377 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
378 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
379 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
380 int mode_nr, anim_nr, part_nr;
381 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
382 int num_toons = MAX_NUM_TOONS;
385 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
386 num_toons = global.num_toons;
388 mode_nr = mode_nr_toons;
389 anim_nr = ctrl->num_anims;
392 anim->mode_nr = mode_nr;
393 anim->control_info = graphic_info[control];
396 anim->num_parts_all = 0;
397 anim->part_counter = 0;
398 anim->active_part_nr = 0;
400 anim->has_base = FALSE;
402 anim->last_x = POS_OFFSCREEN;
403 anim->last_y = POS_OFFSCREEN;
405 anim->init_delay_counter = 0;
407 anim->state = ANIM_STATE_INACTIVE;
411 for (i = 0; i < num_toons; i++)
413 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
414 int sound = SND_UNDEFINED;
415 int music = MUS_UNDEFINED;
416 int graphic = IMG_TOON_1 + i;
421 part->anim_nr = anim_nr;
422 part->mode_nr = mode_nr;
424 part->is_base = FALSE;
428 part->graphic = graphic;
430 part->graphic_info = graphic_info[graphic];
431 part->control_info = graphic_info[control];
433 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
435 part->control_info.init_delay_fixed = 0;
436 part->control_info.init_delay_random = 150;
438 part->control_info.x = ARG_UNDEFINED_VALUE;
439 part->control_info.y = ARG_UNDEFINED_VALUE;
441 part->initial_anim_sync_frame = 0;
442 part->anim_random_frame = -1;
444 part->step_delay.count = 0;
445 part->step_delay.value = graphic_info[control].step_delay;
447 part->state = ANIM_STATE_INACTIVE;
448 part->last_anim_status = -1;
451 anim->num_parts_all++;
459 static void InitGlobalAnimControls(void)
462 int mode_nr, anim_nr, part_nr;
463 int sound, music, graphic, control;
467 for (m = 0; m < NUM_GAME_MODES; m++)
471 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
478 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
480 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
481 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
483 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
485 // if no base animation parameters defined, use default values
486 if (control == IMG_UNDEFINED)
487 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
490 anim->mode_nr = mode_nr;
491 anim->control_info = graphic_info[control];
494 anim->num_parts_all = 0;
495 anim->part_counter = 0;
496 anim->active_part_nr = 0;
498 anim->has_base = FALSE;
500 anim->last_x = POS_OFFSCREEN;
501 anim->last_y = POS_OFFSCREEN;
503 anim->init_delay_counter = 0;
505 anim->state = ANIM_STATE_INACTIVE;
509 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
511 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
513 sound = global_anim_info[a].sound[p][m];
514 music = global_anim_info[a].music[p][m];
515 graphic = global_anim_info[a].graphic[p][m];
516 control = global_anim_info[ctrl_id].graphic[p][m];
518 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
519 control == IMG_UNDEFINED)
523 Debug("anim:InitGlobalAnimControls",
524 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
525 m, a, p, mode_nr, anim_nr, part_nr, control);
529 Debug("anim:InitGlobalAnimControls",
530 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
531 m, a, p, mode_nr, anim_nr, part_nr, sound);
535 part->old_anim_nr = a;
538 part->anim_nr = anim_nr;
539 part->mode_nr = mode_nr;
543 part->graphic = graphic;
545 part->graphic_info = graphic_info[graphic];
546 part->control_info = graphic_info[control];
548 part->initial_anim_sync_frame = 0;
549 part->anim_random_frame = -1;
551 part->step_delay.count = 0;
552 part->step_delay.value = graphic_info[control].step_delay;
554 part->state = ANIM_STATE_INACTIVE;
555 part->last_anim_status = -1;
557 anim->num_parts_all++;
559 if (p < GLOBAL_ANIM_ID_PART_BASE)
561 part->is_base = FALSE;
568 part->is_base = TRUE;
571 anim->has_base = TRUE;
574 // apply special settings for pointer-style animations
575 if (part->control_info.class == get_hash_from_key("pointer"))
577 // force animation to be on top (must set anim and part control)
578 if (anim->control_info.draw_order == 0)
579 anim->control_info.draw_order = 1000000;
580 if (part->control_info.draw_order == 0)
581 part->control_info.draw_order = 1000000;
583 // force animation to pass-through clicks (must set part control)
584 if (part->control_info.style == STYLE_DEFAULT)
585 part->control_info.style |= STYLE_PASSTHROUGH;
589 if (anim->num_parts > 0 || anim->has_base)
599 // sort all animations according to draw_order and animation number
600 for (m = 0; m < NUM_GAME_MODES; m++)
602 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
604 // sort all main animations for this game mode
605 qsort(ctrl->anim, ctrl->num_anims,
606 sizeof(struct GlobalAnimMainControlInfo),
607 compareGlobalAnimMainControlInfo);
609 for (a = 0; a < ctrl->num_anims; a++)
611 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
613 // sort all animation parts for this main animation
614 qsort(anim->part, anim->num_parts,
615 sizeof(struct GlobalAnimPartControlInfo),
616 compareGlobalAnimPartControlInfo);
620 for (i = 0; i < NUM_GAME_MODES; i++)
621 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
622 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
623 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
624 game_mode_anim_classes_list[i].class;
626 for (i = 0; i < NUM_ANIM_CLASSES; i++)
627 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
628 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
629 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
630 anim_class_game_modes_list[i].game_mode;
632 anim_status_last_before_fading = GAME_MODE_LOADING;
633 anim_status_last = GAME_MODE_LOADING;
634 anim_classes_last = ANIM_CLASS_NONE;
637 static void SetGlobalAnimEventsForCustomElements(int list_pos)
639 int num_events = GetGlobalAnimEventValueCount(list_pos);
642 for (i = 0; i < num_events; i++)
644 int event = GetGlobalAnimEventValue(list_pos, i);
646 if (event & ANIM_EVENT_CE_CHANGE)
648 int nr = (event >> ANIM_EVENT_CE_BIT) & 0xff;
650 if (nr >= 0 && nr < NUM_CUSTOM_ELEMENTS)
651 element_info[EL_CUSTOM_START + nr].has_anim_event = TRUE;
656 void InitGlobalAnimEventsForCustomElements(void)
661 // custom element events for global animations only relevant while playing
662 m = GAME_MODE_PLAYING;
664 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
666 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
668 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
670 // if no base animation parameters defined, use default values
671 if (control == IMG_UNDEFINED)
672 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
674 SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
675 SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
677 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
679 control = global_anim_info[ctrl_id].graphic[p][m];
681 if (control == IMG_UNDEFINED)
684 SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
685 SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
690 void InitGlobalAnimations(void)
692 InitGlobalAnimControls();
695 static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
696 Bitmap *src_bitmap, int src_x0, int src_y0,
699 struct GraphicInfo *g = &part->graphic_info;
700 struct GraphicInfo *c = &part->control_info;
701 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
702 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
703 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
704 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
705 Bitmap *fade_bitmap =
706 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
707 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
710 for (y = 0; y < c->stacked_yfactor; y++)
712 for (x = 0; x < c->stacked_xfactor; x++)
716 int dst_x = part->x + x * (g->width + c->stacked_xoffset);
717 int dst_y = part->y + y * (g->height + c->stacked_yoffset);
720 int width = g->width;
721 int height = g->height;
729 else if (dst_x > part->viewport_width - g->width)
731 width -= (dst_x - (part->viewport_width - g->width));
740 else if (dst_y > part->viewport_height - g->height)
742 height -= (dst_y - (part->viewport_height - g->height));
745 if (width <= 0 || height <= 0)
751 dst_x += part->viewport_x;
752 dst_y += part->viewport_y;
754 if (drawing_target == DRAW_TO_SCREEN)
755 blit_screen(src_bitmap, src_x, src_y, width, height,
758 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
764 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
766 int game_mode_anim_action[NUM_GAME_MODES];
772 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
773 drawing_target == DRAW_TO_SCREEN)
776 // always start with reliable default values (no animation actions)
777 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
778 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
780 if (global.anim_status != anim_status_last)
782 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
783 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
784 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
787 if (drawing_target == DRAW_TO_FADE_TARGET)
790 // special case: changing from/to these screens is done without fading
791 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
792 global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
793 anim_status_last == GAME_MODE_PSEUDO_TYPENAME ||
794 anim_status_last == GAME_MODE_PSEUDO_TYPENAMES)
797 // ---------- part 1 ------------------------------------------------------
798 // start or stop global animations by change of game mode
799 // (special handling of animations for "current screen" and "all screens")
801 if (global.anim_status_next != anim_status_last_before_fading)
803 // stop animations for last screen before fading to new screen
804 game_mode_anim_action[anim_status_last] = ANIM_STOP;
806 // start animations for current screen after fading to new screen
807 game_mode_anim_action[global.anim_status] = ANIM_START;
810 // start animations for all screens after loading new artwork set
811 if (anim_status_last == GAME_MODE_LOADING)
812 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
814 // ---------- part 2 ------------------------------------------------------
815 // start or stop global animations by change of animation class
816 // (generic handling of animations for "class of screens")
818 for (i = 0; i < NUM_ANIM_CLASSES; i++)
820 int anim_class_check = (1 << i);
821 int anim_class_game_mode = anim_class_game_modes[i];
822 int anim_class_last = anim_classes_last & anim_class_check;
823 int anim_class_next = anim_classes_next & anim_class_check;
825 // stop animations for changed screen class before fading to new screen
826 if (before_fading && anim_class_last && !anim_class_next)
827 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
829 // start animations for changed screen class after fading to new screen
830 if (after_fading && !anim_class_last && anim_class_next)
831 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
834 if (drawing_target == DRAW_TO_SCREEN)
838 anim_classes_last = anim_classes_next;
839 anim_status_last_before_fading = global.anim_status;
842 anim_status_last = global.anim_status;
844 // start or stop animations determined to be started or stopped above
845 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
846 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
847 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
849 else if (drawing_target == DRAW_TO_FADE_TARGET)
851 drawing_to_fading_buffer = TRUE;
853 // start animations determined to be (temporary) started above
854 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
855 if (game_mode_anim_action[mode_nr] == ANIM_START)
856 HandleGlobalAnim(ANIM_START, mode_nr);
860 // when restarting global animations, do not redraw them, but stop here
861 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
864 if (global.anim_status == GAME_MODE_LOADING)
867 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
869 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
872 // when preparing source fading buffer, only draw animations to be stopped
873 if (drawing_target == DRAW_TO_FADE_SOURCE &&
874 game_mode_anim_action[mode_nr] != ANIM_STOP)
877 // when preparing target fading buffer, only draw animations to be started
878 if (drawing_target == DRAW_TO_FADE_TARGET &&
879 game_mode_anim_action[mode_nr] != ANIM_START)
883 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
884 mode_nr != game_status)
888 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
890 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
891 struct GraphicInfo *c = &anim->control_info;
892 int part_first, part_last;
895 if (!(anim->state & ANIM_STATE_RUNNING))
898 part_first = part_last = anim->active_part_nr;
900 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
902 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
905 part_last = num_parts - 1;
908 for (part_nr = part_first; part_nr <= part_last; part_nr++)
910 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
911 struct GraphicInfo *g = &part->graphic_info;
916 int last_anim_random_frame = gfx.anim_random_frame;
918 if (!(part->state & ANIM_STATE_RUNNING))
921 if (part->drawing_stage != drawing_stage)
924 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
926 // re-initialize random animation frame after animation delay
927 if (g->anim_mode == ANIM_RANDOM &&
928 sync_frame % g->anim_delay == 0 &&
930 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
932 gfx.anim_random_frame = part->anim_random_frame;
934 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
935 g->anim_mode, g->anim_start_frame,
938 gfx.anim_random_frame = last_anim_random_frame;
940 getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
943 BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
948 if (drawing_target == DRAW_TO_FADE_TARGET)
950 // stop animations determined to be (temporary) started above
951 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
952 if (game_mode_anim_action[mode_nr] == ANIM_START)
953 HandleGlobalAnim(ANIM_STOP, mode_nr);
955 drawing_to_fading_buffer = FALSE;
959 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
961 int last_cursor_mode_override = gfx.cursor_mode_override;
963 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
965 ResetGlobalAnim_Clickable();
967 gfx.cursor_mode_override = CURSOR_UNDEFINED;
970 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
972 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
974 ResetGlobalAnim_Clicked();
977 if (gfx.cursor_mode_override != last_cursor_mode_override)
978 SetMouseCursor(gfx.cursor_mode);
981 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
987 boolean changed = FALSE;
989 if (part->last_anim_status == global.anim_status &&
990 part->control_info.class != get_hash_from_key("pointer"))
993 part->last_anim_status = global.anim_status;
995 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
997 if (part->control_info.class == get_hash_from_key("window") ||
998 part->control_info.class == get_hash_from_key("border"))
1002 viewport_width = WIN_XSIZE;
1003 viewport_height = WIN_YSIZE;
1005 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
1007 else if (part->control_info.class == get_hash_from_key("pointer"))
1009 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1010 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1012 // prevent displaying off-screen custom mouse cursor in upper left corner
1013 if (gfx.mouse_x == POS_OFFSCREEN &&
1014 gfx.mouse_y == POS_OFFSCREEN)
1015 mx = my = POS_OFFSCREEN;
1017 viewport_x = mx - part->control_info.x;
1018 viewport_y = my - part->control_info.y;
1019 viewport_width = part->graphic_info.width;
1020 viewport_height = part->graphic_info.height;
1022 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1024 // do not use global animation mouse pointer when reloading artwork
1025 if (global.anim_status != GAME_MODE_LOADING)
1026 gfx.cursor_mode_override = CURSOR_NONE;
1028 else if (part->control_info.class == get_hash_from_key("door_1"))
1032 viewport_width = DXSIZE;
1033 viewport_height = DYSIZE;
1035 else if (part->control_info.class == get_hash_from_key("door_2"))
1037 if (part->mode_nr == GAME_MODE_EDITOR)
1041 viewport_width = EXSIZE;
1042 viewport_height = EYSIZE;
1048 viewport_width = VXSIZE;
1049 viewport_height = VYSIZE;
1052 else // default: "playfield"
1054 viewport_x = REAL_SX;
1055 viewport_y = REAL_SY;
1056 viewport_width = FULL_SXSIZE;
1057 viewport_height = FULL_SYSIZE;
1060 if (viewport_x != part->viewport_x ||
1061 viewport_y != part->viewport_y ||
1062 viewport_width != part->viewport_width ||
1063 viewport_height != part->viewport_height)
1065 part->viewport_x = viewport_x;
1066 part->viewport_y = viewport_y;
1067 part->viewport_width = viewport_width;
1068 part->viewport_height = viewport_height;
1070 if (part->control_info.class != get_hash_from_key("pointer"))
1077 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1079 int sound = part->sound;
1081 if (sound == SND_UNDEFINED)
1084 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1085 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1088 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1089 if (IS_LOOP_SOUND(sound))
1090 PlaySoundLoop(sound);
1095 Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1096 part->anim_nr, part->nr, part->mode_nr, sound);
1100 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1102 int sound = part->sound;
1104 if (sound == SND_UNDEFINED)
1110 Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1111 part->anim_nr, part->nr, part->mode_nr, sound);
1115 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1117 int music = part->music;
1119 if (music == MUS_UNDEFINED)
1122 if (!setup.sound_music)
1125 if (IS_LOOP_MUSIC(music))
1126 PlayMusicLoop(music);
1131 Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1132 part->anim_nr, part->nr, part->mode_nr, music);
1136 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1138 int music = part->music;
1140 if (music == MUS_UNDEFINED)
1143 char *anim_music = getMusicInfoEntryFilename(music);
1144 char *curr_music = getCurrentlyPlayingMusicFilename();
1146 // do not stop music if global anim music differs from current music
1147 if (!strEqual(curr_music, anim_music))
1153 Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1154 part->anim_nr, part->nr, part->mode_nr, music);
1158 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1160 // when drawing animations to fading buffer, do not play sounds or music
1161 if (drawing_to_fading_buffer)
1164 PlayGlobalAnimSound(part);
1165 PlayGlobalAnimMusic(part);
1168 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1170 StopGlobalAnimSound(part);
1171 StopGlobalAnimMusic(part);
1174 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1176 // when drawing animations to fading buffer, do not play sounds
1177 if (drawing_to_fading_buffer)
1180 // loop sounds only expire when playing
1181 if (game_status != GAME_MODE_PLAYING)
1184 // check if any sound is defined for this animation part
1185 if (part->sound == SND_UNDEFINED)
1188 // normal (non-loop) sounds do not expire when playing
1189 if (!IS_LOOP_SOUND(part->sound))
1192 // prevent expiring loop sounds when playing
1193 PlayGlobalAnimSound(part);
1196 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1198 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1199 int mask_ce_only = mask & ~ANIM_EVENT_PAGE_MASK;
1201 if (mask & ANIM_EVENT_ANY)
1202 return (anim_event & ANIM_EVENT_ANY);
1203 else if (mask & ANIM_EVENT_SELF)
1204 return (anim_event & ANIM_EVENT_SELF);
1205 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1206 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1207 else if (mask & ANIM_EVENT_CE_CHANGE)
1208 return (anim_event == mask ||
1209 anim_event == mask_ce_only);
1211 return (anim_event == mask ||
1212 anim_event == mask_anim_only);
1215 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1217 struct GraphicInfo *c = &part->control_info;
1220 if (part->init_event_state)
1222 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1224 for (i = 0; i < num_init_events; i++)
1226 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1228 if (checkGlobalAnimEvent(init_event, mask))
1233 if (part->anim_event_state)
1235 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1237 for (i = 0; i < num_anim_events; i++)
1239 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1241 if (checkGlobalAnimEvent(anim_event, mask))
1249 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1252 struct GraphicInfo *g = &part->graphic_info;
1253 struct GraphicInfo *c = &part->control_info;
1254 int part_x = part->viewport_x + part->x;
1255 int part_y = part->viewport_y + part->y;
1256 int part_width = g->width;
1257 int part_height = g->height;
1260 for (y = 0; y < c->stacked_yfactor; y++)
1262 for (x = 0; x < c->stacked_xfactor; x++)
1264 int part_stacked_x = part_x + x * (part_width + c->stacked_xoffset);
1265 int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1267 if (mx >= part_stacked_x &&
1268 mx < part_stacked_x + part_width &&
1269 my >= part_stacked_y &&
1270 my < part_stacked_y + part_height)
1278 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1279 int mx, int my, boolean clicked)
1281 // check if mouse click was detected at all
1285 // check if mouse click is outside the animation part's viewport
1286 if (mx < part->viewport_x ||
1287 mx >= part->viewport_x + part->viewport_width ||
1288 my < part->viewport_y ||
1289 my >= part->viewport_y + part->viewport_height)
1292 // check if mouse click is inside the animation part's (stacked) graphic
1293 if (isInsidePartStacked(part, mx, my))
1299 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1301 return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1304 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1306 return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1309 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1310 boolean *click_consumed,
1311 boolean *any_event_action,
1312 int event_value, char *info_text)
1314 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1316 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1317 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1318 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1321 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1325 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1327 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1330 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1332 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1334 if (!(part2->state & ANIM_STATE_RUNNING))
1337 if (isClickablePart(part2, mask))
1339 part2->triggered = TRUE;
1340 *click_consumed |= clickConsumed(part); // click was on "part"!
1342 #if DEBUG_ANIM_EVENTS
1343 Debug("anim:InitGlobalAnim_Triggered",
1344 "%d.%d TRIGGERED BY %s OF %d.%d",
1345 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1346 part->old_anim_nr + 1, part->old_nr + 1);
1349 Debug("anim:InitGlobalAnim_Triggered",
1350 "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1351 part2->control_info.anim_event_action);
1354 // after executing event action, ignore any further actions
1355 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1356 *any_event_action = TRUE;
1360 struct GraphicInfo *c = &part2->control_info;
1362 if (isClickablePart(part2, mask))
1363 Debug("anim:InitGlobalAnim_Triggered",
1364 "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1365 anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1368 Debug("anim:InitGlobalAnim_Triggered",
1369 "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1370 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1376 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1379 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1381 int event_value = ANIM_EVENT_CE_CHANGE;
1382 int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1383 int mask = event_value | event_bits;
1386 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1388 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1391 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1393 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1395 if (!(part2->state & ANIM_STATE_RUNNING))
1398 if (isClickablePart(part2, mask) && !part2->triggered)
1400 struct GraphicInfo *c = &part2->control_info;
1402 if (c->position == POS_CE)
1404 // store CE tile and offset position to handle scrolling
1407 part2->tile_xoffset = c->x;
1408 part2->tile_yoffset = c->y;
1410 // restart animation (by using current sync frame)
1411 part2->initial_anim_sync_frame = anim_sync_frame;
1414 part2->triggered = TRUE;
1416 // do not trigger any other animation if CE change event was consumed
1417 if (c->style == STYLE_CONSUME_CE_EVENT)
1421 Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1422 "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1429 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1430 int delay_type, char *info_text)
1432 #if DEBUG_ANIM_DELAY
1433 Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1434 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1437 DoGlobalAnim_DelayAction(part, delay_type);
1440 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1441 int event_value, char *info_text)
1443 #if DEBUG_ANIM_EVENTS
1444 Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1445 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1448 boolean click_consumed = FALSE;
1449 boolean any_event_action = FALSE;
1451 // check if this event is defined to trigger other animations
1452 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1453 event_value, info_text);
1456 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1459 if (handle_click && !part->clicked)
1462 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1463 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1464 struct GraphicInfo *g = &part->graphic_info;
1465 struct GraphicInfo *c = &part->control_info;
1466 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1468 if (viewport_changed)
1469 state |= ANIM_STATE_RESTART;
1471 if (state & ANIM_STATE_RESTART)
1473 // when drawing animations to fading buffer, only start fixed animations
1474 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1475 c->y == ARG_UNDEFINED_VALUE))
1476 return ANIM_STATE_INACTIVE;
1478 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1480 part->init_delay_counter =
1481 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1483 part->anim_delay_counter =
1484 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1486 part->post_delay_counter = 0;
1488 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1489 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1491 part->initial_anim_sync_frame =
1492 (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1493 anim_sync_frame + part->init_delay_counter);
1495 // do not re-initialize random animation frame after fade-in
1496 if (part->anim_random_frame == -1)
1497 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1499 if (c->direction & MV_HORIZONTAL)
1501 int pos_bottom = part->viewport_height - g->height;
1503 if (c->position == POS_TOP)
1505 else if (c->position == POS_UPPER)
1506 part->y = GetSimpleRandom(pos_bottom / 2);
1507 else if (c->position == POS_MIDDLE)
1508 part->y = pos_bottom / 2;
1509 else if (c->position == POS_LOWER)
1510 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1511 else if (c->position == POS_BOTTOM)
1512 part->y = pos_bottom;
1514 part->y = GetSimpleRandom(pos_bottom);
1516 if (c->direction == MV_RIGHT)
1518 part->step_xoffset = c->step_offset;
1519 part->x = -g->width + part->step_xoffset;
1523 part->step_xoffset = -c->step_offset;
1524 part->x = part->viewport_width + part->step_xoffset;
1527 part->step_yoffset = 0;
1529 else if (c->direction & MV_VERTICAL)
1531 int pos_right = part->viewport_width - g->width;
1533 if (c->position == POS_LEFT)
1535 else if (c->position == POS_RIGHT)
1536 part->x = pos_right;
1538 part->x = GetSimpleRandom(pos_right);
1540 if (c->direction == MV_DOWN)
1542 part->step_yoffset = c->step_offset;
1543 part->y = -g->height + part->step_yoffset;
1547 part->step_yoffset = -c->step_offset;
1548 part->y = part->viewport_height + part->step_yoffset;
1551 part->step_xoffset = 0;
1558 part->step_xoffset = 0;
1559 part->step_yoffset = 0;
1562 if (part->control_info.class != get_hash_from_key("pointer"))
1564 if (c->x != ARG_UNDEFINED_VALUE)
1566 if (c->y != ARG_UNDEFINED_VALUE)
1570 if (c->position == POS_LAST &&
1571 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1572 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1574 part->x = anim->last_x;
1575 part->y = anim->last_y;
1578 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1579 part->step_xoffset = c->step_xoffset;
1580 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1581 part->step_yoffset = c->step_yoffset;
1583 if (part->init_delay_counter == 0 &&
1584 !part->init_event_state)
1586 PlayGlobalAnimSoundAndMusic(part);
1588 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1589 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1593 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1597 if (part->clicked &&
1598 part->init_event_state)
1600 if (part->initial_anim_sync_frame > 0)
1601 part->initial_anim_sync_frame = anim_sync_frame;
1603 part->init_delay_counter = 1;
1604 part->init_event_state = FALSE;
1606 part->clicked = FALSE;
1609 if (part->clicked &&
1610 part->anim_event_state)
1612 part->anim_delay_counter = 1;
1613 part->anim_event_state = FALSE;
1615 part->clicked = FALSE;
1618 if (part->init_delay_counter > 0)
1620 part->init_delay_counter--;
1622 if (part->init_delay_counter == 0)
1624 part->init_event_state = FALSE;
1626 PlayGlobalAnimSoundAndMusic(part);
1628 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1629 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1631 // continue with state ANIM_STATE_RUNNING (set below)
1635 return ANIM_STATE_WAITING;
1639 if (part->init_event_state)
1640 return ANIM_STATE_WAITING;
1642 // animation part is now running/visible and therefore clickable
1643 part->clickable = TRUE;
1645 // check if moving animation has left the visible screen area
1646 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1647 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1648 (part->y <= -g->height && part->step_yoffset <= 0) ||
1649 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1651 // do not wait for "anim" events for off-screen animations
1652 part->anim_event_state = FALSE;
1654 // do not stop animation before "anim" or "post" counter are finished
1655 if (part->anim_delay_counter == 0 &&
1656 part->post_delay_counter == 0)
1658 StopGlobalAnimSoundAndMusic(part);
1660 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1662 part->post_delay_counter =
1663 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1665 if (part->post_delay_counter > 0)
1666 return ANIM_STATE_RUNNING;
1668 // drawing last frame not needed here -- animation not visible anymore
1669 return ANIM_STATE_RESTART;
1673 if (part->anim_delay_counter > 0)
1675 part->anim_delay_counter--;
1677 if (part->anim_delay_counter == 0)
1679 part->anim_event_state = FALSE;
1681 StopGlobalAnimSoundAndMusic(part);
1683 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1684 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1686 part->post_delay_counter =
1687 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1689 if (part->post_delay_counter > 0)
1690 return ANIM_STATE_RUNNING;
1692 // additional state "RUNNING" required to not skip drawing last frame
1693 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1697 if (part->post_delay_counter > 0)
1699 part->post_delay_counter--;
1701 if (part->post_delay_counter == 0)
1703 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1704 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1706 return ANIM_STATE_RESTART;
1709 return ANIM_STATE_WAITING;
1712 // special case to prevent expiring loop sounds when playing
1713 PlayGlobalAnimSoundIfLoop(part);
1715 if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1716 return ANIM_STATE_RUNNING;
1720 static unsigned int last_counter = -1;
1721 unsigned int counter = Counter();
1723 Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1724 anim_sync_frame, counter - last_counter);
1726 last_counter = counter;
1730 if (c->position == POS_CE)
1732 // calculate playfield position (with scrolling) for related CE tile
1733 // (do not use FX/FY, which are incorrect during envelope requests)
1734 int FX0 = 2 * TILEX_VAR; // same as FX during DRAW_TO_FIELDBUFFER
1735 int FY0 = 2 * TILEY_VAR; // same as FY during DRAW_TO_FIELDBUFFER
1736 int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1737 int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1738 int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
1739 int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
1740 int cx = SX - REAL_SX;
1741 int cy = SY - REAL_SY;
1742 int x = sx - fx + cx;
1743 int y = sy - fy + cy;
1745 part->tile_xoffset += part->step_xoffset;
1746 part->tile_yoffset += part->step_yoffset;
1748 part->x = x + part->tile_xoffset;
1749 part->y = y + part->tile_yoffset;
1753 part->x += part->step_xoffset;
1754 part->y += part->step_yoffset;
1757 anim->last_x = part->x;
1758 anim->last_y = part->y;
1760 return ANIM_STATE_RUNNING;
1763 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1766 struct GlobalAnimPartControlInfo *part;
1767 struct GraphicInfo *c = &anim->control_info;
1768 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1769 int state, active_part_nr;
1773 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1774 anim->mode_nr, anim->nr, anim->num_parts);
1775 Debug("anim:HandleGlobalAnim_Main",
1776 "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1780 Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1781 (action == ANIM_START ? "ANIM_START" :
1782 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1783 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1785 anim->state & ANIM_STATE_RESTART,
1786 anim->state & ANIM_STATE_WAITING,
1787 anim->state & ANIM_STATE_RUNNING,
1794 anim->state = anim->last_state = ANIM_STATE_RESTART;
1795 anim->active_part_nr = anim->last_active_part_nr = 0;
1796 anim->part_counter = 0;
1801 if (anim->state == ANIM_STATE_INACTIVE)
1804 anim->state = anim->last_state;
1805 anim->active_part_nr = anim->last_active_part_nr;
1810 anim->state = ANIM_STATE_INACTIVE;
1812 for (i = 0; i < num_parts; i++)
1813 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1821 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1824 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1825 anim->mode_nr, anim->nr, num_parts);
1828 for (i = 0; i < num_parts; i++)
1830 part = &anim->part[i];
1835 anim->state = ANIM_STATE_RUNNING;
1836 part->state = ANIM_STATE_RESTART;
1841 if (part->state == ANIM_STATE_INACTIVE)
1847 part->state = ANIM_STATE_INACTIVE;
1855 part->state = HandleGlobalAnim_Part(part, part->state);
1857 // when animation mode is "once", stop after animation was played once
1858 if (c->anim_mode & ANIM_ONCE &&
1859 part->state & ANIM_STATE_RESTART)
1860 part->state = ANIM_STATE_INACTIVE;
1863 anim->last_state = anim->state;
1864 anim->last_active_part_nr = anim->active_part_nr;
1869 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1870 anim->active_part_nr = getGlobalAnimationPart(anim);
1872 part = &anim->part[anim->active_part_nr];
1874 // first set all animation parts to "inactive", ...
1875 for (i = 0; i < num_parts; i++)
1876 anim->part[i].state = ANIM_STATE_INACTIVE;
1878 // ... then set current animation parts to "running"
1879 part->state = ANIM_STATE_RUNNING;
1881 anim->state = HandleGlobalAnim_Part(part, anim->state);
1883 if (anim->state & ANIM_STATE_RESTART)
1884 anim->part_counter++;
1886 // when animation mode is "once", stop after all animations were played once
1887 if (c->anim_mode & ANIM_ONCE &&
1888 anim->part_counter == anim->num_parts)
1889 anim->state = ANIM_STATE_INACTIVE;
1891 state = anim->state;
1892 active_part_nr = anim->active_part_nr;
1894 // while the animation parts are pausing (waiting or inactive), play the base
1895 // (main) animation; this corresponds to the "boring player animation" logic
1896 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1899 if (anim->state == ANIM_STATE_WAITING ||
1900 anim->state == ANIM_STATE_INACTIVE)
1902 anim->active_part_nr = anim->num_parts; // part nr of base animation
1903 part = &anim->part[anim->active_part_nr];
1905 if (anim->state != anim->last_state)
1906 part->state = ANIM_STATE_RESTART;
1908 anim->state = ANIM_STATE_RUNNING;
1909 part->state = HandleGlobalAnim_Part(part, part->state);
1913 anim->last_state = state;
1914 anim->last_active_part_nr = active_part_nr;
1917 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1922 Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1925 for (i = 0; i < ctrl->num_anims; i++)
1926 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1929 static void HandleGlobalAnim(int action, int game_mode)
1932 Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1935 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1938 static void DoAnimationExt(void)
1943 Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1946 // global animations now synchronized with frame delay of screen update
1949 for (i = 0; i < NUM_GAME_MODES; i++)
1950 HandleGlobalAnim(ANIM_CONTINUE, i);
1953 // force screen redraw in next frame to continue drawing global animations
1954 redraw_mask = REDRAW_ALL;
1958 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1962 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1963 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1964 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1965 ANIM_DELAY_ACTION_NONE);
1967 if (delay_action == ANIM_DELAY_ACTION_NONE)
1970 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1973 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1975 int event_action = (part->init_event_state ?
1976 part->control_info.init_event_action :
1977 part->control_info.anim_event_action);
1979 if (event_action == ANIM_EVENT_ACTION_NONE)
1982 if (event_action < MAX_IMAGE_FILES)
1983 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1985 OpenURLFromHash(anim_url_hash, event_action);
1987 // check if further actions are allowed to be executed
1988 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1994 static void InitGlobalAnim_Clickable(void)
1998 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
2000 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2003 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
2005 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2008 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
2010 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2012 if (part->triggered)
2013 part->clicked = TRUE;
2015 part->triggered = FALSE;
2016 part->clickable = FALSE;
2022 #define ANIM_CLICKED_RESET 0
2023 #define ANIM_CLICKED_PRESSED 1
2024 #define ANIM_CLICKED_RELEASED 2
2026 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2028 boolean click_consumed = FALSE;
2029 boolean anything_clicked = FALSE;
2030 boolean any_part_clicked = FALSE;
2031 boolean any_event_action = FALSE;
2035 // check game modes in reverse draw order (to stop when clicked)
2036 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
2038 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2041 // check animations in reverse draw order (to stop when clicked)
2042 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
2044 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2047 // check animation parts in reverse draw order (to stop when clicked)
2048 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
2050 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2052 if (clicked_event == ANIM_CLICKED_RESET)
2054 part->clicked = FALSE;
2059 if (!part->clickable)
2062 if (!(part->state & ANIM_STATE_RUNNING))
2065 // always handle "any" click events (clicking anywhere on screen) ...
2066 if (clicked_event == ANIM_CLICKED_PRESSED &&
2067 isClickablePart(part, ANIM_EVENT_ANY))
2069 #if DEBUG_ANIM_EVENTS
2070 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2071 part->old_anim_nr + 1, part->old_nr + 1);
2074 anything_clicked = part->clicked = TRUE;
2075 click_consumed |= clickConsumed(part);
2078 // always handle "unclick:any" events (releasing anywhere on screen) ...
2079 if (clicked_event == ANIM_CLICKED_RELEASED &&
2080 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2082 #if DEBUG_ANIM_EVENTS
2083 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2084 part->old_anim_nr + 1, part->old_nr + 1);
2087 anything_clicked = part->clicked = TRUE;
2088 click_consumed |= clickConsumed(part);
2091 // ... but only handle the first (topmost) clickable animation
2092 if (any_part_clicked)
2095 if (clicked_event == ANIM_CLICKED_PRESSED &&
2096 isClickedPart(part, mx, my, TRUE))
2099 Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2100 anim_nr, part_nr, part->control_info.anim_event_action);
2103 // after executing event action, ignore any further actions
2104 if (!any_event_action && DoGlobalAnim_EventAction(part))
2105 any_event_action = TRUE;
2107 // determine if mouse clicks should be blocked from other animations
2108 any_part_clicked |= clickConsumed(part);
2110 if (isClickablePart(part, ANIM_EVENT_SELF))
2112 #if DEBUG_ANIM_EVENTS
2113 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2114 part->old_anim_nr + 1, part->old_nr + 1);
2117 anything_clicked = part->clicked = TRUE;
2118 click_consumed |= clickConsumed(part);
2121 // determine if mouse clicks should be blocked by this animation
2122 click_consumed |= clickBlocked(part);
2124 // check if this click is defined to trigger other animations
2125 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2126 ANIM_EVENT_CLICK, "CLICK");
2132 if (anything_clicked)
2134 handle_click = TRUE;
2136 for (i = 0; i < NUM_GAME_MODES; i++)
2137 HandleGlobalAnim(ANIM_CONTINUE, i);
2139 handle_click = FALSE;
2141 // prevent ignoring release event if processed within same game frame
2142 StopProcessingEvents();
2145 return (click_consumed || any_event_action);
2148 static void ResetGlobalAnim_Clickable(void)
2150 InitGlobalAnim_Clickable();
2153 static void ResetGlobalAnim_Clicked(void)
2155 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2158 void RestartGlobalAnimsByStatus(int status)
2160 int anim_status_last = global.anim_status;
2162 global.anim_status = status;
2164 // force restarting global animations by changed global animation status
2165 DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
2167 global.anim_status = anim_status_last;
2170 void SetAnimStatusBeforeFading(int status)
2172 anim_status_last_before_fading = status;
2175 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2177 static boolean click_consumed = FALSE;
2178 static int last_button = 0;
2179 boolean press_event;
2180 boolean release_event;
2181 boolean click_consumed_current = click_consumed;
2183 if (button != 0 && force_click)
2186 // check if button state has changed since last invocation
2187 press_event = (button != 0 && last_button == 0);
2188 release_event = (button == 0 && last_button != 0);
2189 last_button = button;
2193 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2194 click_consumed_current = click_consumed;
2199 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2200 click_consumed = FALSE;
2203 return click_consumed_current;
2206 int getGlobalAnimSyncFrame(void)
2208 return anim_sync_frame;
2211 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y)
2213 if (!IS_CUSTOM_ELEMENT(element))
2216 // custom element stored as 0 to 255, change page stored as 1 to 32
2217 InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,