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 if (global.anim_status == GAME_MODE_LOADING)
863 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
865 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
868 // when preparing source fading buffer, only draw animations to be stopped
869 if (drawing_target == DRAW_TO_FADE_SOURCE &&
870 game_mode_anim_action[mode_nr] != ANIM_STOP)
873 // when preparing target fading buffer, only draw animations to be started
874 if (drawing_target == DRAW_TO_FADE_TARGET &&
875 game_mode_anim_action[mode_nr] != ANIM_START)
879 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
880 mode_nr != game_status)
884 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
886 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
887 struct GraphicInfo *c = &anim->control_info;
888 int part_first, part_last;
891 if (!(anim->state & ANIM_STATE_RUNNING))
894 part_first = part_last = anim->active_part_nr;
896 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
898 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
901 part_last = num_parts - 1;
904 for (part_nr = part_first; part_nr <= part_last; part_nr++)
906 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
907 struct GraphicInfo *g = &part->graphic_info;
912 int last_anim_random_frame = gfx.anim_random_frame;
914 if (!(part->state & ANIM_STATE_RUNNING))
917 if (part->drawing_stage != drawing_stage)
920 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
922 // re-initialize random animation frame after animation delay
923 if (g->anim_mode == ANIM_RANDOM &&
924 sync_frame % g->anim_delay == 0 &&
926 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
928 gfx.anim_random_frame = part->anim_random_frame;
930 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
931 g->anim_mode, g->anim_start_frame,
934 gfx.anim_random_frame = last_anim_random_frame;
936 getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
939 BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
944 if (drawing_target == DRAW_TO_FADE_TARGET)
946 // stop animations determined to be (temporary) started above
947 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
948 if (game_mode_anim_action[mode_nr] == ANIM_START)
949 HandleGlobalAnim(ANIM_STOP, mode_nr);
951 drawing_to_fading_buffer = FALSE;
955 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
957 int last_cursor_mode_override = gfx.cursor_mode_override;
959 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
961 ResetGlobalAnim_Clickable();
963 gfx.cursor_mode_override = CURSOR_UNDEFINED;
966 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
968 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
970 ResetGlobalAnim_Clicked();
973 DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
975 if (gfx.cursor_mode_override != last_cursor_mode_override)
976 SetMouseCursor(gfx.cursor_mode);
979 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
985 boolean changed = FALSE;
987 if (part->last_anim_status == global.anim_status &&
988 part->control_info.class != get_hash_from_key("pointer"))
991 part->last_anim_status = global.anim_status;
993 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
995 if (part->control_info.class == get_hash_from_key("window") ||
996 part->control_info.class == get_hash_from_key("border"))
1000 viewport_width = WIN_XSIZE;
1001 viewport_height = WIN_YSIZE;
1003 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
1005 else if (part->control_info.class == get_hash_from_key("pointer"))
1007 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1008 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1010 // prevent displaying off-screen custom mouse cursor in upper left corner
1011 if (gfx.mouse_x == POS_OFFSCREEN &&
1012 gfx.mouse_y == POS_OFFSCREEN)
1013 mx = my = POS_OFFSCREEN;
1015 viewport_x = mx - part->control_info.x;
1016 viewport_y = my - part->control_info.y;
1017 viewport_width = part->graphic_info.width;
1018 viewport_height = part->graphic_info.height;
1020 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1022 // do not use global animation mouse pointer when reloading artwork
1023 if (global.anim_status != GAME_MODE_LOADING)
1024 gfx.cursor_mode_override = CURSOR_NONE;
1026 else if (part->control_info.class == get_hash_from_key("door_1"))
1030 viewport_width = DXSIZE;
1031 viewport_height = DYSIZE;
1033 else if (part->control_info.class == get_hash_from_key("door_2"))
1035 if (part->mode_nr == GAME_MODE_EDITOR)
1039 viewport_width = EXSIZE;
1040 viewport_height = EYSIZE;
1046 viewport_width = VXSIZE;
1047 viewport_height = VYSIZE;
1050 else // default: "playfield"
1052 viewport_x = REAL_SX;
1053 viewport_y = REAL_SY;
1054 viewport_width = FULL_SXSIZE;
1055 viewport_height = FULL_SYSIZE;
1058 if (viewport_x != part->viewport_x ||
1059 viewport_y != part->viewport_y ||
1060 viewport_width != part->viewport_width ||
1061 viewport_height != part->viewport_height)
1063 part->viewport_x = viewport_x;
1064 part->viewport_y = viewport_y;
1065 part->viewport_width = viewport_width;
1066 part->viewport_height = viewport_height;
1068 if (part->control_info.class != get_hash_from_key("pointer"))
1075 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1077 int sound = part->sound;
1079 if (sound == SND_UNDEFINED)
1082 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1083 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1086 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1087 if (IS_LOOP_SOUND(sound))
1088 PlaySoundLoop(sound);
1093 Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1094 part->anim_nr, part->nr, part->mode_nr, sound);
1098 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1100 int sound = part->sound;
1102 if (sound == SND_UNDEFINED)
1108 Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1109 part->anim_nr, part->nr, part->mode_nr, sound);
1113 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1115 int music = part->music;
1117 if (music == MUS_UNDEFINED)
1120 if (!setup.sound_music)
1123 if (IS_LOOP_MUSIC(music))
1124 PlayMusicLoop(music);
1129 Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1130 part->anim_nr, part->nr, part->mode_nr, music);
1134 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1136 int music = part->music;
1138 if (music == MUS_UNDEFINED)
1141 char *anim_music = getMusicInfoEntryFilename(music);
1142 char *curr_music = getCurrentlyPlayingMusicFilename();
1144 // do not stop music if global anim music differs from current music
1145 if (!strEqual(curr_music, anim_music))
1151 Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1152 part->anim_nr, part->nr, part->mode_nr, music);
1156 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1158 // when drawing animations to fading buffer, do not play sounds or music
1159 if (drawing_to_fading_buffer)
1162 PlayGlobalAnimSound(part);
1163 PlayGlobalAnimMusic(part);
1166 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1168 StopGlobalAnimSound(part);
1169 StopGlobalAnimMusic(part);
1172 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1174 // when drawing animations to fading buffer, do not play sounds
1175 if (drawing_to_fading_buffer)
1178 // loop sounds only expire when playing
1179 if (game_status != GAME_MODE_PLAYING)
1182 // check if any sound is defined for this animation part
1183 if (part->sound == SND_UNDEFINED)
1186 // normal (non-loop) sounds do not expire when playing
1187 if (!IS_LOOP_SOUND(part->sound))
1190 // prevent expiring loop sounds when playing
1191 PlayGlobalAnimSound(part);
1194 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1196 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1197 int mask_ce_only = mask & ~ANIM_EVENT_PAGE_MASK;
1199 if (mask & ANIM_EVENT_ANY)
1200 return (anim_event & ANIM_EVENT_ANY);
1201 else if (mask & ANIM_EVENT_SELF)
1202 return (anim_event & ANIM_EVENT_SELF);
1203 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1204 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1205 else if (mask & ANIM_EVENT_CE_CHANGE)
1206 return (anim_event == mask ||
1207 anim_event == mask_ce_only);
1209 return (anim_event == mask ||
1210 anim_event == mask_anim_only);
1213 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1215 struct GraphicInfo *c = &part->control_info;
1218 if (part->init_event_state)
1220 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1222 for (i = 0; i < num_init_events; i++)
1224 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1226 if (checkGlobalAnimEvent(init_event, mask))
1231 if (part->anim_event_state)
1233 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1235 for (i = 0; i < num_anim_events; i++)
1237 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1239 if (checkGlobalAnimEvent(anim_event, mask))
1247 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1250 struct GraphicInfo *g = &part->graphic_info;
1251 struct GraphicInfo *c = &part->control_info;
1252 int part_x = part->viewport_x + part->x;
1253 int part_y = part->viewport_y + part->y;
1254 int part_width = g->width;
1255 int part_height = g->height;
1258 for (y = 0; y < c->stacked_yfactor; y++)
1260 for (x = 0; x < c->stacked_xfactor; x++)
1262 int part_stacked_x = part_x + x * (part_width + c->stacked_xoffset);
1263 int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1265 if (mx >= part_stacked_x &&
1266 mx < part_stacked_x + part_width &&
1267 my >= part_stacked_y &&
1268 my < part_stacked_y + part_height)
1276 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1277 int mx, int my, boolean clicked)
1279 // check if mouse click was detected at all
1283 // check if mouse click is outside the animation part's viewport
1284 if (mx < part->viewport_x ||
1285 mx >= part->viewport_x + part->viewport_width ||
1286 my < part->viewport_y ||
1287 my >= part->viewport_y + part->viewport_height)
1290 // check if mouse click is inside the animation part's (stacked) graphic
1291 if (isInsidePartStacked(part, mx, my))
1297 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1299 return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1302 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1304 return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1307 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1308 boolean *click_consumed,
1309 boolean *any_event_action,
1310 int event_value, char *info_text)
1312 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1314 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1315 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1316 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1319 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1323 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1325 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1328 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1330 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1332 if (!(part2->state & ANIM_STATE_RUNNING))
1335 if (isClickablePart(part2, mask))
1337 part2->triggered = TRUE;
1338 *click_consumed |= clickConsumed(part); // click was on "part"!
1340 #if DEBUG_ANIM_EVENTS
1341 Debug("anim:InitGlobalAnim_Triggered",
1342 "%d.%d TRIGGERED BY %s OF %d.%d",
1343 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1344 part->old_anim_nr + 1, part->old_nr + 1);
1347 Debug("anim:InitGlobalAnim_Triggered",
1348 "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1349 part2->control_info.anim_event_action);
1352 // after executing event action, ignore any further actions
1353 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1354 *any_event_action = TRUE;
1358 struct GraphicInfo *c = &part2->control_info;
1360 if (isClickablePart(part2, mask))
1361 Debug("anim:InitGlobalAnim_Triggered",
1362 "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1363 anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1366 Debug("anim:InitGlobalAnim_Triggered",
1367 "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1368 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1374 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1377 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1379 int event_value = ANIM_EVENT_CE_CHANGE;
1380 int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1381 int mask = event_value | event_bits;
1384 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1386 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1389 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1391 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1393 if (!(part2->state & ANIM_STATE_RUNNING))
1396 if (isClickablePart(part2, mask) && !part2->triggered)
1398 struct GraphicInfo *c = &part2->control_info;
1400 if (c->position == POS_CE)
1402 // store CE tile and offset position to handle scrolling
1405 part2->tile_xoffset = c->x;
1406 part2->tile_yoffset = c->y;
1408 // restart animation (by using current sync frame)
1409 part2->initial_anim_sync_frame = anim_sync_frame;
1412 part2->triggered = TRUE;
1414 // do not trigger any other animation if CE change event was consumed
1415 if (c->style == STYLE_CONSUME_CE_EVENT)
1419 Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1420 "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1427 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1428 int delay_type, char *info_text)
1430 #if DEBUG_ANIM_DELAY
1431 Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1432 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1435 DoGlobalAnim_DelayAction(part, delay_type);
1438 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1439 int event_value, char *info_text)
1441 #if DEBUG_ANIM_EVENTS
1442 Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1443 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1446 boolean click_consumed = FALSE;
1447 boolean any_event_action = FALSE;
1449 // check if this event is defined to trigger other animations
1450 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1451 event_value, info_text);
1454 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1457 if (handle_click && !part->clicked)
1460 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1461 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1462 struct GraphicInfo *g = &part->graphic_info;
1463 struct GraphicInfo *c = &part->control_info;
1464 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1466 if (viewport_changed)
1467 state |= ANIM_STATE_RESTART;
1469 if (state & ANIM_STATE_RESTART)
1471 // when drawing animations to fading buffer, only start fixed animations
1472 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1473 c->y == ARG_UNDEFINED_VALUE))
1474 return ANIM_STATE_INACTIVE;
1476 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1478 part->init_delay_counter =
1479 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1481 part->anim_delay_counter =
1482 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1484 part->post_delay_counter = 0;
1486 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1487 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1489 part->initial_anim_sync_frame =
1490 (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1491 anim_sync_frame + part->init_delay_counter);
1493 // do not re-initialize random animation frame after fade-in
1494 if (part->anim_random_frame == -1)
1495 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1497 if (c->direction & MV_HORIZONTAL)
1499 int pos_bottom = part->viewport_height - g->height;
1501 if (c->position == POS_TOP)
1503 else if (c->position == POS_UPPER)
1504 part->y = GetSimpleRandom(pos_bottom / 2);
1505 else if (c->position == POS_MIDDLE)
1506 part->y = pos_bottom / 2;
1507 else if (c->position == POS_LOWER)
1508 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1509 else if (c->position == POS_BOTTOM)
1510 part->y = pos_bottom;
1512 part->y = GetSimpleRandom(pos_bottom);
1514 if (c->direction == MV_RIGHT)
1516 part->step_xoffset = c->step_offset;
1517 part->x = -g->width + part->step_xoffset;
1521 part->step_xoffset = -c->step_offset;
1522 part->x = part->viewport_width + part->step_xoffset;
1525 part->step_yoffset = 0;
1527 else if (c->direction & MV_VERTICAL)
1529 int pos_right = part->viewport_width - g->width;
1531 if (c->position == POS_LEFT)
1533 else if (c->position == POS_RIGHT)
1534 part->x = pos_right;
1536 part->x = GetSimpleRandom(pos_right);
1538 if (c->direction == MV_DOWN)
1540 part->step_yoffset = c->step_offset;
1541 part->y = -g->height + part->step_yoffset;
1545 part->step_yoffset = -c->step_offset;
1546 part->y = part->viewport_height + part->step_yoffset;
1549 part->step_xoffset = 0;
1556 part->step_xoffset = 0;
1557 part->step_yoffset = 0;
1560 if (part->control_info.class != get_hash_from_key("pointer"))
1562 if (c->x != ARG_UNDEFINED_VALUE)
1564 if (c->y != ARG_UNDEFINED_VALUE)
1568 if (c->position == POS_LAST &&
1569 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1570 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1572 part->x = anim->last_x;
1573 part->y = anim->last_y;
1576 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1577 part->step_xoffset = c->step_xoffset;
1578 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1579 part->step_yoffset = c->step_yoffset;
1581 if (part->init_delay_counter == 0 &&
1582 !part->init_event_state)
1584 PlayGlobalAnimSoundAndMusic(part);
1586 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1587 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1591 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1595 if (part->clicked &&
1596 part->init_event_state)
1598 if (part->initial_anim_sync_frame > 0)
1599 part->initial_anim_sync_frame = anim_sync_frame;
1601 part->init_delay_counter = 1;
1602 part->init_event_state = FALSE;
1604 part->clicked = FALSE;
1607 if (part->clicked &&
1608 part->anim_event_state)
1610 part->anim_delay_counter = 1;
1611 part->anim_event_state = FALSE;
1613 part->clicked = FALSE;
1616 if (part->init_delay_counter > 0)
1618 part->init_delay_counter--;
1620 if (part->init_delay_counter == 0)
1622 part->init_event_state = FALSE;
1624 PlayGlobalAnimSoundAndMusic(part);
1626 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1627 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1629 // continue with state ANIM_STATE_RUNNING (set below)
1633 return ANIM_STATE_WAITING;
1637 if (part->init_event_state)
1638 return ANIM_STATE_WAITING;
1640 // animation part is now running/visible and therefore clickable
1641 part->clickable = TRUE;
1643 // check if moving animation has left the visible screen area
1644 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1645 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1646 (part->y <= -g->height && part->step_yoffset <= 0) ||
1647 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1649 // do not wait for "anim" events for off-screen animations
1650 part->anim_event_state = FALSE;
1652 // do not stop animation before "anim" or "post" counter are finished
1653 if (part->anim_delay_counter == 0 &&
1654 part->post_delay_counter == 0)
1656 StopGlobalAnimSoundAndMusic(part);
1658 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1660 part->post_delay_counter =
1661 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1663 if (part->post_delay_counter > 0)
1664 return ANIM_STATE_RUNNING;
1666 // drawing last frame not needed here -- animation not visible anymore
1667 return ANIM_STATE_RESTART;
1671 if (part->anim_delay_counter > 0)
1673 part->anim_delay_counter--;
1675 if (part->anim_delay_counter == 0)
1677 part->anim_event_state = FALSE;
1679 StopGlobalAnimSoundAndMusic(part);
1681 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1682 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1684 part->post_delay_counter =
1685 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1687 if (part->post_delay_counter > 0)
1688 return ANIM_STATE_RUNNING;
1690 // additional state "RUNNING" required to not skip drawing last frame
1691 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1695 if (part->post_delay_counter > 0)
1697 part->post_delay_counter--;
1699 if (part->post_delay_counter == 0)
1701 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1702 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1704 return ANIM_STATE_RESTART;
1707 return ANIM_STATE_WAITING;
1710 // special case to prevent expiring loop sounds when playing
1711 PlayGlobalAnimSoundIfLoop(part);
1713 if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1714 return ANIM_STATE_RUNNING;
1718 static unsigned int last_counter = -1;
1719 unsigned int counter = Counter();
1721 Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1722 anim_sync_frame, counter - last_counter);
1724 last_counter = counter;
1728 if (c->position == POS_CE)
1730 // calculate playfield position (with scrolling) for related CE tile
1731 int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1732 int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1733 int sx = FX + SCREENX(part->tile_x) * TILEX_VAR;
1734 int sy = FY + SCREENY(part->tile_y) * TILEY_VAR;
1738 part->tile_xoffset += part->step_xoffset;
1739 part->tile_yoffset += part->step_yoffset;
1741 part->x = x + part->tile_xoffset;
1742 part->y = y + part->tile_yoffset;
1746 part->x += part->step_xoffset;
1747 part->y += part->step_yoffset;
1750 anim->last_x = part->x;
1751 anim->last_y = part->y;
1753 return ANIM_STATE_RUNNING;
1756 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1759 struct GlobalAnimPartControlInfo *part;
1760 struct GraphicInfo *c = &anim->control_info;
1761 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1762 int state, active_part_nr;
1766 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1767 anim->mode_nr, anim->nr, anim->num_parts);
1768 Debug("anim:HandleGlobalAnim_Main",
1769 "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1773 Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1774 (action == ANIM_START ? "ANIM_START" :
1775 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1776 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1778 anim->state & ANIM_STATE_RESTART,
1779 anim->state & ANIM_STATE_WAITING,
1780 anim->state & ANIM_STATE_RUNNING,
1787 anim->state = anim->last_state = ANIM_STATE_RESTART;
1788 anim->active_part_nr = anim->last_active_part_nr = 0;
1789 anim->part_counter = 0;
1794 if (anim->state == ANIM_STATE_INACTIVE)
1797 anim->state = anim->last_state;
1798 anim->active_part_nr = anim->last_active_part_nr;
1803 anim->state = ANIM_STATE_INACTIVE;
1805 for (i = 0; i < num_parts; i++)
1806 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1814 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1817 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1818 anim->mode_nr, anim->nr, num_parts);
1821 for (i = 0; i < num_parts; i++)
1823 part = &anim->part[i];
1828 anim->state = ANIM_STATE_RUNNING;
1829 part->state = ANIM_STATE_RESTART;
1834 if (part->state == ANIM_STATE_INACTIVE)
1840 part->state = ANIM_STATE_INACTIVE;
1848 part->state = HandleGlobalAnim_Part(part, part->state);
1850 // when animation mode is "once", stop after animation was played once
1851 if (c->anim_mode & ANIM_ONCE &&
1852 part->state & ANIM_STATE_RESTART)
1853 part->state = ANIM_STATE_INACTIVE;
1856 anim->last_state = anim->state;
1857 anim->last_active_part_nr = anim->active_part_nr;
1862 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1863 anim->active_part_nr = getGlobalAnimationPart(anim);
1865 part = &anim->part[anim->active_part_nr];
1867 // first set all animation parts to "inactive", ...
1868 for (i = 0; i < num_parts; i++)
1869 anim->part[i].state = ANIM_STATE_INACTIVE;
1871 // ... then set current animation parts to "running"
1872 part->state = ANIM_STATE_RUNNING;
1874 anim->state = HandleGlobalAnim_Part(part, anim->state);
1876 if (anim->state & ANIM_STATE_RESTART)
1877 anim->part_counter++;
1879 // when animation mode is "once", stop after all animations were played once
1880 if (c->anim_mode & ANIM_ONCE &&
1881 anim->part_counter == anim->num_parts)
1882 anim->state = ANIM_STATE_INACTIVE;
1884 state = anim->state;
1885 active_part_nr = anim->active_part_nr;
1887 // while the animation parts are pausing (waiting or inactive), play the base
1888 // (main) animation; this corresponds to the "boring player animation" logic
1889 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1892 if (anim->state == ANIM_STATE_WAITING ||
1893 anim->state == ANIM_STATE_INACTIVE)
1895 anim->active_part_nr = anim->num_parts; // part nr of base animation
1896 part = &anim->part[anim->active_part_nr];
1898 if (anim->state != anim->last_state)
1899 part->state = ANIM_STATE_RESTART;
1901 anim->state = ANIM_STATE_RUNNING;
1902 part->state = HandleGlobalAnim_Part(part, part->state);
1906 anim->last_state = state;
1907 anim->last_active_part_nr = active_part_nr;
1910 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1915 Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1918 for (i = 0; i < ctrl->num_anims; i++)
1919 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1922 static void HandleGlobalAnim(int action, int game_mode)
1925 Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1928 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1931 static void DoAnimationExt(void)
1936 Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1939 // global animations now synchronized with frame delay of screen update
1942 for (i = 0; i < NUM_GAME_MODES; i++)
1943 HandleGlobalAnim(ANIM_CONTINUE, i);
1946 // force screen redraw in next frame to continue drawing global animations
1947 redraw_mask = REDRAW_ALL;
1951 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1955 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1956 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1957 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1958 ANIM_DELAY_ACTION_NONE);
1960 if (delay_action == ANIM_DELAY_ACTION_NONE)
1963 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1966 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1968 int event_action = (part->init_event_state ?
1969 part->control_info.init_event_action :
1970 part->control_info.anim_event_action);
1972 if (event_action == ANIM_EVENT_ACTION_NONE)
1975 if (event_action < MAX_IMAGE_FILES)
1976 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1978 OpenURLFromHash(anim_url_hash, event_action);
1980 // check if further actions are allowed to be executed
1981 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1987 static void InitGlobalAnim_Clickable(void)
1991 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1993 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1996 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1998 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2001 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
2003 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2005 if (part->triggered)
2006 part->clicked = TRUE;
2008 part->triggered = FALSE;
2009 part->clickable = FALSE;
2015 #define ANIM_CLICKED_RESET 0
2016 #define ANIM_CLICKED_PRESSED 1
2017 #define ANIM_CLICKED_RELEASED 2
2019 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2021 boolean click_consumed = FALSE;
2022 boolean anything_clicked = FALSE;
2023 boolean any_part_clicked = FALSE;
2024 boolean any_event_action = FALSE;
2028 // check game modes in reverse draw order (to stop when clicked)
2029 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
2031 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
2034 // check animations in reverse draw order (to stop when clicked)
2035 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
2037 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
2040 // check animation parts in reverse draw order (to stop when clicked)
2041 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
2043 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
2045 if (clicked_event == ANIM_CLICKED_RESET)
2047 part->clicked = FALSE;
2052 if (!part->clickable)
2055 if (!(part->state & ANIM_STATE_RUNNING))
2058 // always handle "any" click events (clicking anywhere on screen) ...
2059 if (clicked_event == ANIM_CLICKED_PRESSED &&
2060 isClickablePart(part, ANIM_EVENT_ANY))
2062 #if DEBUG_ANIM_EVENTS
2063 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2064 part->old_anim_nr + 1, part->old_nr + 1);
2067 anything_clicked = part->clicked = TRUE;
2068 click_consumed |= clickConsumed(part);
2071 // always handle "unclick:any" events (releasing anywhere on screen) ...
2072 if (clicked_event == ANIM_CLICKED_RELEASED &&
2073 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2075 #if DEBUG_ANIM_EVENTS
2076 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2077 part->old_anim_nr + 1, part->old_nr + 1);
2080 anything_clicked = part->clicked = TRUE;
2081 click_consumed |= clickConsumed(part);
2084 // ... but only handle the first (topmost) clickable animation
2085 if (any_part_clicked)
2088 if (clicked_event == ANIM_CLICKED_PRESSED &&
2089 isClickedPart(part, mx, my, TRUE))
2092 Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2093 anim_nr, part_nr, part->control_info.anim_event_action);
2096 // after executing event action, ignore any further actions
2097 if (!any_event_action && DoGlobalAnim_EventAction(part))
2098 any_event_action = TRUE;
2100 // determine if mouse clicks should be blocked from other animations
2101 any_part_clicked |= clickConsumed(part);
2103 if (isClickablePart(part, ANIM_EVENT_SELF))
2105 #if DEBUG_ANIM_EVENTS
2106 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2107 part->old_anim_nr + 1, part->old_nr + 1);
2110 anything_clicked = part->clicked = TRUE;
2111 click_consumed |= clickConsumed(part);
2114 // determine if mouse clicks should be blocked by this animation
2115 click_consumed |= clickBlocked(part);
2117 // check if this click is defined to trigger other animations
2118 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2119 ANIM_EVENT_CLICK, "CLICK");
2125 if (anything_clicked)
2127 handle_click = TRUE;
2129 for (i = 0; i < NUM_GAME_MODES; i++)
2130 HandleGlobalAnim(ANIM_CONTINUE, i);
2132 handle_click = FALSE;
2134 // prevent ignoring release event if processed within same game frame
2135 StopProcessingEvents();
2138 return (click_consumed || any_event_action);
2141 static void ResetGlobalAnim_Clickable(void)
2143 InitGlobalAnim_Clickable();
2146 static void ResetGlobalAnim_Clicked(void)
2148 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2151 void RestartGlobalAnimsByStatus(int status)
2153 int anim_status_last = global.anim_status;
2155 global.anim_status = status;
2157 // force restarting global animations by changed global animation status
2160 global.anim_status = anim_status_last;
2163 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2165 static boolean click_consumed = FALSE;
2166 static int last_button = 0;
2167 boolean press_event;
2168 boolean release_event;
2169 boolean click_consumed_current = click_consumed;
2171 if (button != 0 && force_click)
2174 // check if button state has changed since last invocation
2175 press_event = (button != 0 && last_button == 0);
2176 release_event = (button == 0 && last_button != 0);
2177 last_button = button;
2181 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2182 click_consumed_current = click_consumed;
2187 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2188 click_consumed = FALSE;
2191 return click_consumed_current;
2194 int getGlobalAnimSyncFrame(void)
2196 return anim_sync_frame;
2199 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y)
2201 if (!IS_CUSTOM_ELEMENT(element))
2204 // custom element stored as 0 to 255, change page stored as 1 to 32
2205 InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,