1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #include "libgame/libgame.h"
22 #define DEBUG_ANIM_DELAY 0
23 #define DEBUG_ANIM_EVENTS 0
26 // values for global toon animation definition
27 #define NUM_GLOBAL_TOON_ANIMS 1
28 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
30 // values for global animation definition (including toons)
31 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
32 NUM_GLOBAL_TOON_ANIMS)
33 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
34 NUM_GLOBAL_TOON_PARTS)
36 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
37 #define ANIM_CLASS_BIT_TITLE 1
38 #define ANIM_CLASS_BIT_MAIN 2
39 #define ANIM_CLASS_BIT_NAMES 3
40 #define ANIM_CLASS_BIT_SCORES 4
41 #define ANIM_CLASS_BIT_SCORESONLY 5
42 #define ANIM_CLASS_BIT_SUBMENU 6
43 #define ANIM_CLASS_BIT_MENU 7
44 #define ANIM_CLASS_BIT_TOONS 8
45 #define ANIM_CLASS_BIT_NO_TITLE 9
47 #define NUM_ANIM_CLASSES 10
49 #define ANIM_CLASS_NONE 0
50 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
51 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
52 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
53 #define ANIM_CLASS_NAMES (1 << ANIM_CLASS_BIT_NAMES)
54 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
55 #define ANIM_CLASS_SCORESONLY (1 << ANIM_CLASS_BIT_SCORESONLY)
56 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
57 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
58 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
59 #define ANIM_CLASS_NO_TITLE (1 << ANIM_CLASS_BIT_NO_TITLE)
61 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
65 #define ANIM_CLASS_TOONS_SCORESONLY (ANIM_CLASS_TOONS | \
67 ANIM_CLASS_SCORESONLY | \
70 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
75 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
77 ANIM_CLASS_SUBMENU | \
80 #define ANIM_CLASS_TOONS_MENU_SUBMENU_2 (ANIM_CLASS_TOONS | \
82 ANIM_CLASS_SUBMENU | \
86 // values for global animation states
87 #define ANIM_STATE_INACTIVE 0
88 #define ANIM_STATE_RESTART (1 << 0)
89 #define ANIM_STATE_WAITING (1 << 1)
90 #define ANIM_STATE_RUNNING (1 << 2)
92 // values for global animation control
93 #define ANIM_NO_ACTION 0
95 #define ANIM_CONTINUE 2
99 struct GlobalAnimPartControlInfo
101 int old_nr; // position before mapping animation parts linearly
102 int old_anim_nr; // position before mapping animations linearly
108 boolean is_base; // animation part is base/main/default animation part
114 struct GraphicInfo graphic_info;
115 struct GraphicInfo control_info;
123 int step_xoffset, step_yoffset;
125 unsigned int initial_anim_sync_frame;
126 unsigned int anim_random_frame;
128 DelayCounter step_delay;
130 int init_delay_counter;
131 int anim_delay_counter;
132 int post_delay_counter;
134 boolean init_event_state;
135 boolean anim_event_state;
144 int last_anim_status;
147 struct GlobalAnimMainControlInfo
149 struct GlobalAnimPartControlInfo base;
150 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
155 struct GraphicInfo control_info;
157 int num_parts; // number of animation parts, but without base part
158 int num_parts_all; // number of animation parts, including base part
162 boolean has_base; // animation has base/main/default animation part
166 int init_delay_counter;
170 int last_state, last_active_part_nr;
173 struct GlobalAnimControlInfo
175 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
181 struct GameModeAnimClass
185 } game_mode_anim_classes_list[] =
187 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
188 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
189 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
190 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
191 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
192 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
193 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
194 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
195 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
196 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
197 { GAME_MODE_NAMES, ANIM_CLASS_TOONS_MENU_SUBMENU },
198 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
199 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
200 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
201 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
202 { GAME_MODE_PSEUDO_NAMESONLY, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
203 { GAME_MODE_PSEUDO_TYPENAMES, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
204 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
205 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
206 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORESONLY },
207 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORESONLY },
208 { GAME_MODE_SCOREINFO, ANIM_CLASS_TOONS_SCORES },
209 { GAME_MODE_EDITOR, ANIM_CLASS_NO_TITLE },
210 { GAME_MODE_PLAYING, ANIM_CLASS_NO_TITLE },
215 struct AnimClassGameMode
219 } anim_class_game_modes_list[] =
221 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
222 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
223 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
224 { ANIM_CLASS_BIT_NAMES, GAME_MODE_NAMES },
225 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
226 { ANIM_CLASS_BIT_SCORESONLY, GAME_MODE_PSEUDO_SCORESONLY },
227 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
228 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
229 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
230 { ANIM_CLASS_BIT_NO_TITLE, GAME_MODE_PSEUDO_NO_TITLE },
235 // forward declaration for internal use
236 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
237 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
238 static void HandleGlobalAnim(int, int);
239 static void DoAnimationExt(void);
240 static void ResetGlobalAnim_Clickable(void);
241 static void ResetGlobalAnim_Clicked(void);
243 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
245 static unsigned int anim_sync_frame = 0;
247 static int game_mode_anim_classes[NUM_GAME_MODES];
248 static int anim_class_game_modes[NUM_ANIM_CLASSES];
250 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
251 static int anim_status_last = GAME_MODE_DEFAULT;
252 static int anim_classes_last = ANIM_CLASS_NONE;
254 static boolean drawing_to_fading_buffer = FALSE;
256 static boolean handle_click = FALSE;
259 // ============================================================================
260 // generic animation frame calculation
261 // ============================================================================
263 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
268 if (delay < 1) // delay must be at least 1
271 sync_frame += start_frame * delay;
273 if (mode & ANIM_LOOP) // looping animation
275 frame = (sync_frame % (delay * num_frames)) / delay;
277 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
279 frame = sync_frame / delay;
281 if (frame > num_frames - 1)
282 frame = num_frames - 1;
284 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
286 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
288 frame = (sync_frame % (delay * max_anim_frames)) / delay;
289 frame = (frame < num_frames ? frame : max_anim_frames - frame);
291 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
293 int max_anim_frames = 2 * num_frames;
295 frame = (sync_frame % (delay * max_anim_frames)) / delay;
296 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
298 else if (mode & ANIM_RANDOM) // play frames in random order
300 // note: expect different frames for the same delay cycle!
302 if (gfx.anim_random_frame < 0)
303 frame = GetSimpleRandom(num_frames);
305 frame = gfx.anim_random_frame % num_frames;
307 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
309 frame = sync_frame % num_frames;
312 if (mode & ANIM_REVERSE) // use reverse animation direction
313 frame = num_frames - frame - 1;
319 // ============================================================================
320 // global animation functions
321 // ============================================================================
323 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
325 struct GraphicInfo *c = &anim->control_info;
326 int last_anim_random_frame = gfx.anim_random_frame;
329 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
331 part_nr = getAnimationFrame(anim->num_parts, 1,
332 c->anim_mode, c->anim_start_frame,
335 gfx.anim_random_frame = last_anim_random_frame;
340 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
342 const struct GlobalAnimPartControlInfo *o1 =
343 (struct GlobalAnimPartControlInfo *)obj1;
344 const struct GlobalAnimPartControlInfo *o2 =
345 (struct GlobalAnimPartControlInfo *)obj2;
348 if (o1->control_info.draw_order != o2->control_info.draw_order)
349 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
351 compare_result = o1->nr - o2->nr;
353 return compare_result;
356 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
358 const struct GlobalAnimMainControlInfo *o1 =
359 (struct GlobalAnimMainControlInfo *)obj1;
360 const struct GlobalAnimMainControlInfo *o2 =
361 (struct GlobalAnimMainControlInfo *)obj2;
364 if (o1->control_info.draw_order != o2->control_info.draw_order)
365 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
367 compare_result = o1->nr - o2->nr;
369 return compare_result;
372 static void InitToonControls(void)
374 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
375 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
376 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
377 int mode_nr, anim_nr, part_nr;
378 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
379 int num_toons = MAX_NUM_TOONS;
382 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
383 num_toons = global.num_toons;
385 mode_nr = mode_nr_toons;
386 anim_nr = ctrl->num_anims;
389 anim->mode_nr = mode_nr;
390 anim->control_info = graphic_info[control];
393 anim->num_parts_all = 0;
394 anim->part_counter = 0;
395 anim->active_part_nr = 0;
397 anim->has_base = FALSE;
399 anim->last_x = POS_OFFSCREEN;
400 anim->last_y = POS_OFFSCREEN;
402 anim->init_delay_counter = 0;
404 anim->state = ANIM_STATE_INACTIVE;
408 for (i = 0; i < num_toons; i++)
410 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
411 int sound = SND_UNDEFINED;
412 int music = MUS_UNDEFINED;
413 int graphic = IMG_TOON_1 + i;
418 part->anim_nr = anim_nr;
419 part->mode_nr = mode_nr;
421 part->is_base = FALSE;
425 part->graphic = graphic;
427 part->graphic_info = graphic_info[graphic];
428 part->control_info = graphic_info[control];
430 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
432 part->control_info.init_delay_fixed = 0;
433 part->control_info.init_delay_random = 150;
435 part->control_info.x = ARG_UNDEFINED_VALUE;
436 part->control_info.y = ARG_UNDEFINED_VALUE;
438 part->initial_anim_sync_frame = 0;
439 part->anim_random_frame = -1;
441 part->step_delay.count = 0;
442 part->step_delay.value = graphic_info[control].step_delay;
444 part->state = ANIM_STATE_INACTIVE;
445 part->last_anim_status = -1;
448 anim->num_parts_all++;
456 static void InitGlobalAnimControls(void)
459 int mode_nr, anim_nr, part_nr;
460 int sound, music, graphic, control;
464 for (m = 0; m < NUM_GAME_MODES; m++)
468 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
475 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
477 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
478 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
480 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
482 // if no base animation parameters defined, use default values
483 if (control == IMG_UNDEFINED)
484 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
487 anim->mode_nr = mode_nr;
488 anim->control_info = graphic_info[control];
491 anim->num_parts_all = 0;
492 anim->part_counter = 0;
493 anim->active_part_nr = 0;
495 anim->has_base = FALSE;
497 anim->last_x = POS_OFFSCREEN;
498 anim->last_y = POS_OFFSCREEN;
500 anim->init_delay_counter = 0;
502 anim->state = ANIM_STATE_INACTIVE;
506 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
508 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
510 sound = global_anim_info[a].sound[p][m];
511 music = global_anim_info[a].music[p][m];
512 graphic = global_anim_info[a].graphic[p][m];
513 control = global_anim_info[ctrl_id].graphic[p][m];
515 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
516 control == IMG_UNDEFINED)
520 Debug("anim:InitGlobalAnimControls",
521 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
522 m, a, p, mode_nr, anim_nr, part_nr, control);
526 Debug("anim:InitGlobalAnimControls",
527 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
528 m, a, p, mode_nr, anim_nr, part_nr, sound);
532 part->old_anim_nr = a;
535 part->anim_nr = anim_nr;
536 part->mode_nr = mode_nr;
540 part->graphic = graphic;
542 part->graphic_info = graphic_info[graphic];
543 part->control_info = graphic_info[control];
545 part->initial_anim_sync_frame = 0;
546 part->anim_random_frame = -1;
548 part->step_delay.count = 0;
549 part->step_delay.value = graphic_info[control].step_delay;
551 part->state = ANIM_STATE_INACTIVE;
552 part->last_anim_status = -1;
554 anim->num_parts_all++;
556 if (p < GLOBAL_ANIM_ID_PART_BASE)
558 part->is_base = FALSE;
565 part->is_base = TRUE;
568 anim->has_base = TRUE;
571 // apply special settings for pointer-style animations
572 if (part->control_info.class == get_hash_from_key("pointer"))
574 // force animation to be on top (must set anim and part control)
575 if (anim->control_info.draw_order == 0)
576 anim->control_info.draw_order = 1000000;
577 if (part->control_info.draw_order == 0)
578 part->control_info.draw_order = 1000000;
580 // force animation to pass-through clicks (must set part control)
581 if (part->control_info.style == STYLE_DEFAULT)
582 part->control_info.style |= STYLE_PASSTHROUGH;
586 if (anim->num_parts > 0 || anim->has_base)
596 // sort all animations according to draw_order and animation number
597 for (m = 0; m < NUM_GAME_MODES; m++)
599 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
601 // sort all main animations for this game mode
602 qsort(ctrl->anim, ctrl->num_anims,
603 sizeof(struct GlobalAnimMainControlInfo),
604 compareGlobalAnimMainControlInfo);
606 for (a = 0; a < ctrl->num_anims; a++)
608 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
610 // sort all animation parts for this main animation
611 qsort(anim->part, anim->num_parts,
612 sizeof(struct GlobalAnimPartControlInfo),
613 compareGlobalAnimPartControlInfo);
617 for (i = 0; i < NUM_GAME_MODES; i++)
618 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
619 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
620 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
621 game_mode_anim_classes_list[i].class;
623 for (i = 0; i < NUM_ANIM_CLASSES; i++)
624 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
625 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
626 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
627 anim_class_game_modes_list[i].game_mode;
629 anim_status_last_before_fading = GAME_MODE_LOADING;
630 anim_status_last = GAME_MODE_LOADING;
631 anim_classes_last = ANIM_CLASS_NONE;
634 void InitGlobalAnimations(void)
636 InitGlobalAnimControls();
639 static void BlitGlobalAnimation(struct GraphicInfo *g,
640 struct GraphicInfo *c,
642 int src_x, int src_y, int width, int height,
643 int dst_x, int dst_y, int drawing_target)
645 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
646 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
647 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
648 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
649 Bitmap *fade_bitmap =
650 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
651 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
654 for (y = 0; y < c->stacked_yfactor; y++)
656 for (x = 0; x < c->stacked_xfactor; x++)
658 int dst_x_final = dst_x + x * (g->width + c->stacked_xoffset);
659 int dst_y_final = dst_y + y * (g->height + c->stacked_yoffset);
661 if (drawing_target == DRAW_TO_SCREEN)
662 blit_screen(src_bitmap, src_x, src_y, width, height,
663 dst_x_final, dst_y_final);
665 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
666 dst_x_final, dst_y_final);
671 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
673 int game_mode_anim_action[NUM_GAME_MODES];
679 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
680 drawing_target == DRAW_TO_SCREEN)
683 // always start with reliable default values (no animation actions)
684 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
685 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
687 if (global.anim_status != anim_status_last)
689 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
690 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
691 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
694 if (drawing_target == DRAW_TO_FADE_TARGET)
697 // special case: changing from/to these screens is done without fading
698 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
699 global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
700 anim_status_last == GAME_MODE_PSEUDO_TYPENAME ||
701 anim_status_last == GAME_MODE_PSEUDO_TYPENAMES)
704 // ---------- part 1 ------------------------------------------------------
705 // start or stop global animations by change of game mode
706 // (special handling of animations for "current screen" and "all screens")
708 if (global.anim_status_next != anim_status_last_before_fading)
710 // stop animations for last screen before fading to new screen
711 game_mode_anim_action[anim_status_last] = ANIM_STOP;
713 // start animations for current screen after fading to new screen
714 game_mode_anim_action[global.anim_status] = ANIM_START;
717 // start animations for all screens after loading new artwork set
718 if (anim_status_last == GAME_MODE_LOADING)
719 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
721 // ---------- part 2 ------------------------------------------------------
722 // start or stop global animations by change of animation class
723 // (generic handling of animations for "class of screens")
725 for (i = 0; i < NUM_ANIM_CLASSES; i++)
727 int anim_class_check = (1 << i);
728 int anim_class_game_mode = anim_class_game_modes[i];
729 int anim_class_last = anim_classes_last & anim_class_check;
730 int anim_class_next = anim_classes_next & anim_class_check;
732 // stop animations for changed screen class before fading to new screen
733 if (before_fading && anim_class_last && !anim_class_next)
734 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
736 // start animations for changed screen class after fading to new screen
737 if (after_fading && !anim_class_last && anim_class_next)
738 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
741 if (drawing_target == DRAW_TO_SCREEN)
745 anim_classes_last = anim_classes_next;
746 anim_status_last_before_fading = global.anim_status;
749 anim_status_last = global.anim_status;
751 // start or stop animations determined to be started or stopped above
752 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
753 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
754 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
756 else if (drawing_target == DRAW_TO_FADE_TARGET)
758 drawing_to_fading_buffer = TRUE;
760 // start animations determined to be (temporary) started above
761 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
762 if (game_mode_anim_action[mode_nr] == ANIM_START)
763 HandleGlobalAnim(ANIM_START, mode_nr);
767 if (global.anim_status == GAME_MODE_LOADING)
770 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
772 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
775 // when preparing source fading buffer, only draw animations to be stopped
776 if (drawing_target == DRAW_TO_FADE_SOURCE &&
777 game_mode_anim_action[mode_nr] != ANIM_STOP)
780 // when preparing target fading buffer, only draw animations to be started
781 if (drawing_target == DRAW_TO_FADE_TARGET &&
782 game_mode_anim_action[mode_nr] != ANIM_START)
786 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
787 mode_nr != game_status)
791 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
793 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
794 struct GraphicInfo *c = &anim->control_info;
795 int part_first, part_last;
798 if (!(anim->state & ANIM_STATE_RUNNING))
801 part_first = part_last = anim->active_part_nr;
803 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
805 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
808 part_last = num_parts - 1;
811 for (part_nr = part_first; part_nr <= part_last; part_nr++)
813 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
814 struct GraphicInfo *g = &part->graphic_info;
815 struct GraphicInfo *c = &part->control_info;
818 int width = g->width;
819 int height = g->height;
826 int last_anim_random_frame = gfx.anim_random_frame;
828 if (!(part->state & ANIM_STATE_RUNNING))
831 if (part->drawing_stage != drawing_stage)
840 else if (part->x > part->viewport_width - g->width)
841 width -= (part->x - (part->viewport_width - g->width));
849 else if (part->y > part->viewport_height - g->height)
850 height -= (part->y - (part->viewport_height - g->height));
852 if (width <= 0 || height <= 0)
855 dst_x += part->viewport_x;
856 dst_y += part->viewport_y;
858 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
860 // re-initialize random animation frame after animation delay
861 if (g->anim_mode == ANIM_RANDOM &&
862 sync_frame % g->anim_delay == 0 &&
864 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
866 gfx.anim_random_frame = part->anim_random_frame;
868 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
869 g->anim_mode, g->anim_start_frame,
872 gfx.anim_random_frame = last_anim_random_frame;
874 getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
880 BlitGlobalAnimation(g, c, src_bitmap, src_x, src_y, width, height,
881 dst_x, dst_y, drawing_target);
886 if (drawing_target == DRAW_TO_FADE_TARGET)
888 // stop animations determined to be (temporary) started above
889 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
890 if (game_mode_anim_action[mode_nr] == ANIM_START)
891 HandleGlobalAnim(ANIM_STOP, mode_nr);
893 drawing_to_fading_buffer = FALSE;
897 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
899 int last_cursor_mode_override = gfx.cursor_mode_override;
901 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
903 ResetGlobalAnim_Clickable();
905 gfx.cursor_mode_override = CURSOR_UNDEFINED;
908 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
910 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
912 ResetGlobalAnim_Clicked();
915 DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
917 if (gfx.cursor_mode_override != last_cursor_mode_override)
918 SetMouseCursor(gfx.cursor_mode);
921 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
927 boolean changed = FALSE;
929 if (part->last_anim_status == global.anim_status &&
930 part->control_info.class != get_hash_from_key("pointer"))
933 part->last_anim_status = global.anim_status;
935 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
937 if (part->control_info.class == get_hash_from_key("window") ||
938 part->control_info.class == get_hash_from_key("border"))
942 viewport_width = WIN_XSIZE;
943 viewport_height = WIN_YSIZE;
945 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
947 else if (part->control_info.class == get_hash_from_key("pointer"))
949 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
950 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
952 // prevent displaying off-screen custom mouse cursor in upper left corner
953 if (gfx.mouse_x == POS_OFFSCREEN &&
954 gfx.mouse_y == POS_OFFSCREEN)
955 mx = my = POS_OFFSCREEN;
957 viewport_x = mx - part->control_info.x;
958 viewport_y = my - part->control_info.y;
959 viewport_width = part->graphic_info.width;
960 viewport_height = part->graphic_info.height;
962 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
964 // do not use global animation mouse pointer when reloading artwork
965 if (global.anim_status != GAME_MODE_LOADING)
966 gfx.cursor_mode_override = CURSOR_NONE;
968 else if (part->control_info.class == get_hash_from_key("door_1"))
972 viewport_width = DXSIZE;
973 viewport_height = DYSIZE;
975 else if (part->control_info.class == get_hash_from_key("door_2"))
977 if (part->mode_nr == GAME_MODE_EDITOR)
981 viewport_width = EXSIZE;
982 viewport_height = EYSIZE;
988 viewport_width = VXSIZE;
989 viewport_height = VYSIZE;
992 else // default: "playfield"
994 viewport_x = REAL_SX;
995 viewport_y = REAL_SY;
996 viewport_width = FULL_SXSIZE;
997 viewport_height = FULL_SYSIZE;
1000 if (viewport_x != part->viewport_x ||
1001 viewport_y != part->viewport_y ||
1002 viewport_width != part->viewport_width ||
1003 viewport_height != part->viewport_height)
1005 part->viewport_x = viewport_x;
1006 part->viewport_y = viewport_y;
1007 part->viewport_width = viewport_width;
1008 part->viewport_height = viewport_height;
1010 if (part->control_info.class != get_hash_from_key("pointer"))
1017 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1019 int sound = part->sound;
1021 if (sound == SND_UNDEFINED)
1024 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1025 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1028 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1029 if (IS_LOOP_SOUND(sound))
1030 PlaySoundLoop(sound);
1035 Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1036 part->anim_nr, part->nr, part->mode_nr, sound);
1040 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1042 int sound = part->sound;
1044 if (sound == SND_UNDEFINED)
1050 Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1051 part->anim_nr, part->nr, part->mode_nr, sound);
1055 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1057 int music = part->music;
1059 if (music == MUS_UNDEFINED)
1062 if (!setup.sound_music)
1065 if (IS_LOOP_MUSIC(music))
1066 PlayMusicLoop(music);
1071 Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1072 part->anim_nr, part->nr, part->mode_nr, music);
1076 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1078 int music = part->music;
1080 if (music == MUS_UNDEFINED)
1086 Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1087 part->anim_nr, part->nr, part->mode_nr, music);
1091 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1093 // when drawing animations to fading buffer, do not play sounds or music
1094 if (drawing_to_fading_buffer)
1097 PlayGlobalAnimSound(part);
1098 PlayGlobalAnimMusic(part);
1101 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1103 StopGlobalAnimSound(part);
1104 StopGlobalAnimMusic(part);
1107 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1109 // when drawing animations to fading buffer, do not play sounds
1110 if (drawing_to_fading_buffer)
1113 // loop sounds only expire when playing
1114 if (game_status != GAME_MODE_PLAYING)
1117 // check if any sound is defined for this animation part
1118 if (part->sound == SND_UNDEFINED)
1121 // normal (non-loop) sounds do not expire when playing
1122 if (!IS_LOOP_SOUND(part->sound))
1125 // prevent expiring loop sounds when playing
1126 PlayGlobalAnimSound(part);
1129 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1131 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1133 if (mask & ANIM_EVENT_ANY)
1134 return (anim_event & ANIM_EVENT_ANY);
1135 else if (mask & ANIM_EVENT_SELF)
1136 return (anim_event & ANIM_EVENT_SELF);
1137 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1138 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1140 return (anim_event == mask ||
1141 anim_event == mask_anim_only);
1144 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1146 struct GraphicInfo *c = &part->control_info;
1147 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1148 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1151 for (i = 0; i < num_init_events; i++)
1153 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1155 if (checkGlobalAnimEvent(init_event, mask))
1159 for (i = 0; i < num_anim_events; i++)
1161 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1163 if (checkGlobalAnimEvent(anim_event, mask))
1170 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1171 int mx, int my, boolean clicked)
1173 struct GraphicInfo *g = &part->graphic_info;
1174 int part_x = part->viewport_x + part->x;
1175 int part_y = part->viewport_y + part->y;
1176 int part_width = g->width;
1177 int part_height = g->height;
1179 // check if mouse click was detected at all
1183 // check if mouse click is inside the animation part's viewport
1184 if (mx < part->viewport_x ||
1185 mx >= part->viewport_x + part->viewport_width ||
1186 my < part->viewport_y ||
1187 my >= part->viewport_y + part->viewport_height)
1190 // check if mouse click is inside the animation part's graphic
1192 mx >= part_x + part_width ||
1194 my >= part_y + part_height)
1200 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1202 return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1205 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1207 return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1210 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1211 boolean *click_consumed,
1212 boolean *any_event_action,
1213 int event_value, char *info_text)
1215 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1217 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1218 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1219 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1222 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1226 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1228 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1231 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1233 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1235 if (!(part2->state & ANIM_STATE_RUNNING))
1238 if (isClickablePart(part2, mask))
1240 part2->triggered = TRUE;
1241 *click_consumed |= clickConsumed(part); // click was on "part"!
1243 #if DEBUG_ANIM_EVENTS
1244 Debug("anim:InitGlobalAnim_Triggered",
1245 "%d.%d TRIGGERED BY %s OF %d.%d",
1246 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1247 part->old_anim_nr + 1, part->old_nr + 1);
1250 Debug("anim:InitGlobalAnim_Triggered",
1251 "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1252 part2->control_info.anim_event_action);
1255 // after executing event action, ignore any further actions
1256 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1257 *any_event_action = TRUE;
1261 struct GraphicInfo *c = &part2->control_info;
1263 if (isClickablePart(part2, mask))
1264 Debug("anim:InitGlobalAnim_Triggered",
1265 "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1266 anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1269 Debug("anim:InitGlobalAnim_Triggered",
1270 "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1271 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1277 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1278 int delay_type, char *info_text)
1280 #if DEBUG_ANIM_DELAY
1281 Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1282 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1285 DoGlobalAnim_DelayAction(part, delay_type);
1288 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1289 int event_value, char *info_text)
1291 #if DEBUG_ANIM_EVENTS
1292 Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1293 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1296 boolean click_consumed = FALSE;
1297 boolean any_event_action = FALSE;
1299 // check if this event is defined to trigger other animations
1300 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1301 event_value, info_text);
1304 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1307 if (handle_click && !part->clicked)
1310 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1311 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1312 struct GraphicInfo *g = &part->graphic_info;
1313 struct GraphicInfo *c = &part->control_info;
1314 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1316 if (viewport_changed)
1317 state |= ANIM_STATE_RESTART;
1319 if (state & ANIM_STATE_RESTART)
1321 // when drawing animations to fading buffer, only start fixed animations
1322 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1323 c->y == ARG_UNDEFINED_VALUE))
1324 return ANIM_STATE_INACTIVE;
1326 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1328 part->init_delay_counter =
1329 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1331 part->anim_delay_counter =
1332 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1334 part->post_delay_counter = 0;
1336 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1337 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1339 part->initial_anim_sync_frame =
1340 (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1341 anim_sync_frame + part->init_delay_counter);
1343 // do not re-initialize random animation frame after fade-in
1344 if (part->anim_random_frame == -1)
1345 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1347 if (c->direction & MV_HORIZONTAL)
1349 int pos_bottom = part->viewport_height - g->height;
1351 if (c->position == POS_TOP)
1353 else if (c->position == POS_UPPER)
1354 part->y = GetSimpleRandom(pos_bottom / 2);
1355 else if (c->position == POS_MIDDLE)
1356 part->y = pos_bottom / 2;
1357 else if (c->position == POS_LOWER)
1358 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1359 else if (c->position == POS_BOTTOM)
1360 part->y = pos_bottom;
1362 part->y = GetSimpleRandom(pos_bottom);
1364 if (c->direction == MV_RIGHT)
1366 part->step_xoffset = c->step_offset;
1367 part->x = -g->width + part->step_xoffset;
1371 part->step_xoffset = -c->step_offset;
1372 part->x = part->viewport_width + part->step_xoffset;
1375 part->step_yoffset = 0;
1377 else if (c->direction & MV_VERTICAL)
1379 int pos_right = part->viewport_width - g->width;
1381 if (c->position == POS_LEFT)
1383 else if (c->position == POS_RIGHT)
1384 part->x = pos_right;
1386 part->x = GetSimpleRandom(pos_right);
1388 if (c->direction == MV_DOWN)
1390 part->step_yoffset = c->step_offset;
1391 part->y = -g->height + part->step_yoffset;
1395 part->step_yoffset = -c->step_offset;
1396 part->y = part->viewport_height + part->step_yoffset;
1399 part->step_xoffset = 0;
1406 part->step_xoffset = 0;
1407 part->step_yoffset = 0;
1410 if (part->control_info.class != get_hash_from_key("pointer"))
1412 if (c->x != ARG_UNDEFINED_VALUE)
1414 if (c->y != ARG_UNDEFINED_VALUE)
1418 if (c->position == POS_LAST &&
1419 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1420 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1422 part->x = anim->last_x;
1423 part->y = anim->last_y;
1426 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1427 part->step_xoffset = c->step_xoffset;
1428 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1429 part->step_yoffset = c->step_yoffset;
1431 if (part->init_delay_counter == 0 &&
1432 !part->init_event_state)
1434 PlayGlobalAnimSoundAndMusic(part);
1436 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1437 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1441 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1445 if (part->clicked &&
1446 part->init_event_state)
1448 if (part->initial_anim_sync_frame > 0)
1449 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1451 part->init_delay_counter = 1;
1452 part->init_event_state = FALSE;
1454 part->clicked = FALSE;
1457 if (part->clicked &&
1458 part->anim_event_state)
1460 part->anim_delay_counter = 1;
1461 part->anim_event_state = FALSE;
1463 part->clicked = FALSE;
1466 if (part->init_delay_counter > 0)
1468 part->init_delay_counter--;
1470 if (part->init_delay_counter == 0)
1472 part->init_event_state = FALSE;
1474 PlayGlobalAnimSoundAndMusic(part);
1476 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1477 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1480 return ANIM_STATE_WAITING;
1483 if (part->init_event_state)
1484 return ANIM_STATE_WAITING;
1486 // animation part is now running/visible and therefore clickable
1487 part->clickable = TRUE;
1489 // check if moving animation has left the visible screen area
1490 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1491 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1492 (part->y <= -g->height && part->step_yoffset <= 0) ||
1493 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1495 // do not wait for "anim" events for off-screen animations
1496 part->anim_event_state = FALSE;
1498 // do not stop animation before "anim" or "post" counter are finished
1499 if (part->anim_delay_counter == 0 &&
1500 part->post_delay_counter == 0)
1502 StopGlobalAnimSoundAndMusic(part);
1504 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1506 part->post_delay_counter =
1507 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1509 if (part->post_delay_counter > 0)
1510 return ANIM_STATE_RUNNING;
1512 // drawing last frame not needed here -- animation not visible anymore
1513 return ANIM_STATE_RESTART;
1517 if (part->anim_delay_counter > 0)
1519 part->anim_delay_counter--;
1521 if (part->anim_delay_counter == 0)
1523 part->anim_event_state = FALSE;
1525 StopGlobalAnimSoundAndMusic(part);
1527 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1528 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1530 part->post_delay_counter =
1531 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1533 if (part->post_delay_counter > 0)
1534 return ANIM_STATE_RUNNING;
1536 // additional state "RUNNING" required to not skip drawing last frame
1537 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1541 if (part->post_delay_counter > 0)
1543 part->post_delay_counter--;
1545 if (part->post_delay_counter == 0)
1547 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1548 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1550 return ANIM_STATE_RESTART;
1553 return ANIM_STATE_WAITING;
1556 // special case to prevent expiring loop sounds when playing
1557 PlayGlobalAnimSoundIfLoop(part);
1559 if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1560 return ANIM_STATE_RUNNING;
1564 static unsigned int last_counter = -1;
1565 unsigned int counter = Counter();
1567 Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1568 anim_sync_frame, counter - last_counter);
1570 last_counter = counter;
1574 part->x += part->step_xoffset;
1575 part->y += part->step_yoffset;
1577 anim->last_x = part->x;
1578 anim->last_y = part->y;
1580 return ANIM_STATE_RUNNING;
1583 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1586 struct GlobalAnimPartControlInfo *part;
1587 struct GraphicInfo *c = &anim->control_info;
1588 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1589 int state, active_part_nr;
1593 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1594 anim->mode_nr, anim->nr, anim->num_parts);
1595 Debug("anim:HandleGlobalAnim_Main",
1596 "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1600 Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1601 (action == ANIM_START ? "ANIM_START" :
1602 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1603 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1605 anim->state & ANIM_STATE_RESTART,
1606 anim->state & ANIM_STATE_WAITING,
1607 anim->state & ANIM_STATE_RUNNING,
1614 anim->state = anim->last_state = ANIM_STATE_RESTART;
1615 anim->active_part_nr = anim->last_active_part_nr = 0;
1616 anim->part_counter = 0;
1621 if (anim->state == ANIM_STATE_INACTIVE)
1624 anim->state = anim->last_state;
1625 anim->active_part_nr = anim->last_active_part_nr;
1630 anim->state = ANIM_STATE_INACTIVE;
1632 for (i = 0; i < num_parts; i++)
1633 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1641 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1644 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1645 anim->mode_nr, anim->nr, num_parts);
1648 for (i = 0; i < num_parts; i++)
1650 part = &anim->part[i];
1655 anim->state = ANIM_STATE_RUNNING;
1656 part->state = ANIM_STATE_RESTART;
1661 if (part->state == ANIM_STATE_INACTIVE)
1667 part->state = ANIM_STATE_INACTIVE;
1675 part->state = HandleGlobalAnim_Part(part, part->state);
1677 // when animation mode is "once", stop after animation was played once
1678 if (c->anim_mode & ANIM_ONCE &&
1679 part->state & ANIM_STATE_RESTART)
1680 part->state = ANIM_STATE_INACTIVE;
1683 anim->last_state = anim->state;
1684 anim->last_active_part_nr = anim->active_part_nr;
1689 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1690 anim->active_part_nr = getGlobalAnimationPart(anim);
1692 part = &anim->part[anim->active_part_nr];
1694 // first set all animation parts to "inactive", ...
1695 for (i = 0; i < num_parts; i++)
1696 anim->part[i].state = ANIM_STATE_INACTIVE;
1698 // ... then set current animation parts to "running"
1699 part->state = ANIM_STATE_RUNNING;
1701 anim->state = HandleGlobalAnim_Part(part, anim->state);
1703 if (anim->state & ANIM_STATE_RESTART)
1704 anim->part_counter++;
1706 // when animation mode is "once", stop after all animations were played once
1707 if (c->anim_mode & ANIM_ONCE &&
1708 anim->part_counter == anim->num_parts)
1709 anim->state = ANIM_STATE_INACTIVE;
1711 state = anim->state;
1712 active_part_nr = anim->active_part_nr;
1714 // while the animation parts are pausing (waiting or inactive), play the base
1715 // (main) animation; this corresponds to the "boring player animation" logic
1716 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1719 if (anim->state == ANIM_STATE_WAITING ||
1720 anim->state == ANIM_STATE_INACTIVE)
1722 anim->active_part_nr = anim->num_parts; // part nr of base animation
1723 part = &anim->part[anim->active_part_nr];
1725 if (anim->state != anim->last_state)
1726 part->state = ANIM_STATE_RESTART;
1728 anim->state = ANIM_STATE_RUNNING;
1729 part->state = HandleGlobalAnim_Part(part, part->state);
1733 anim->last_state = state;
1734 anim->last_active_part_nr = active_part_nr;
1737 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1742 Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1745 for (i = 0; i < ctrl->num_anims; i++)
1746 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1749 static void HandleGlobalAnim(int action, int game_mode)
1752 Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1755 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1758 static void DoAnimationExt(void)
1763 Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1766 // global animations now synchronized with frame delay of screen update
1769 for (i = 0; i < NUM_GAME_MODES; i++)
1770 HandleGlobalAnim(ANIM_CONTINUE, i);
1773 // force screen redraw in next frame to continue drawing global animations
1774 redraw_mask = REDRAW_ALL;
1778 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1782 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1783 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1784 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1785 ANIM_DELAY_ACTION_NONE);
1787 if (delay_action == ANIM_DELAY_ACTION_NONE)
1790 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1793 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1795 int event_action = (part->init_event_state ?
1796 part->control_info.init_event_action :
1797 part->control_info.anim_event_action);
1799 if (event_action == ANIM_EVENT_ACTION_NONE)
1802 if (event_action < MAX_IMAGE_FILES)
1803 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1805 OpenURLFromHash(anim_url_hash, event_action);
1807 // check if further actions are allowed to be executed
1808 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1814 static void InitGlobalAnim_Clickable(void)
1818 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1820 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1823 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1825 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1828 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1830 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1832 if (part->triggered)
1833 part->clicked = TRUE;
1835 part->triggered = FALSE;
1836 part->clickable = FALSE;
1842 #define ANIM_CLICKED_RESET 0
1843 #define ANIM_CLICKED_PRESSED 1
1844 #define ANIM_CLICKED_RELEASED 2
1846 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1848 boolean click_consumed = FALSE;
1849 boolean anything_clicked = FALSE;
1850 boolean any_part_clicked = FALSE;
1851 boolean any_event_action = FALSE;
1855 // check game modes in reverse draw order (to stop when clicked)
1856 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1858 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1861 // check animations in reverse draw order (to stop when clicked)
1862 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1864 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1867 // check animation parts in reverse draw order (to stop when clicked)
1868 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1870 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1872 if (clicked_event == ANIM_CLICKED_RESET)
1874 part->clicked = FALSE;
1879 if (!part->clickable)
1882 if (!(part->state & ANIM_STATE_RUNNING))
1885 // always handle "any" click events (clicking anywhere on screen) ...
1886 if (clicked_event == ANIM_CLICKED_PRESSED &&
1887 isClickablePart(part, ANIM_EVENT_ANY))
1889 #if DEBUG_ANIM_EVENTS
1890 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
1891 part->old_anim_nr + 1, part->old_nr + 1);
1894 anything_clicked = part->clicked = TRUE;
1895 click_consumed |= clickConsumed(part);
1898 // always handle "unclick:any" events (releasing anywhere on screen) ...
1899 if (clicked_event == ANIM_CLICKED_RELEASED &&
1900 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1902 #if DEBUG_ANIM_EVENTS
1903 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
1904 part->old_anim_nr + 1, part->old_nr + 1);
1907 anything_clicked = part->clicked = TRUE;
1908 click_consumed |= clickConsumed(part);
1911 // ... but only handle the first (topmost) clickable animation
1912 if (any_part_clicked)
1915 if (clicked_event == ANIM_CLICKED_PRESSED &&
1916 isClickedPart(part, mx, my, TRUE))
1919 Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
1920 anim_nr, part_nr, part->control_info.anim_event_action);
1923 // after executing event action, ignore any further actions
1924 if (!any_event_action && DoGlobalAnim_EventAction(part))
1925 any_event_action = TRUE;
1927 // determine if mouse clicks should be blocked from other animations
1928 any_part_clicked |= clickConsumed(part);
1930 if (isClickablePart(part, ANIM_EVENT_SELF))
1932 #if DEBUG_ANIM_EVENTS
1933 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
1934 part->old_anim_nr + 1, part->old_nr + 1);
1937 anything_clicked = part->clicked = TRUE;
1938 click_consumed |= clickConsumed(part);
1941 // determine if mouse clicks should be blocked by this animation
1942 click_consumed |= clickBlocked(part);
1944 // check if this click is defined to trigger other animations
1945 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1946 ANIM_EVENT_CLICK, "CLICK");
1952 if (anything_clicked)
1954 handle_click = TRUE;
1956 for (i = 0; i < NUM_GAME_MODES; i++)
1957 HandleGlobalAnim(ANIM_CONTINUE, i);
1959 handle_click = FALSE;
1961 // prevent ignoring release event if processed within same game frame
1962 StopProcessingEvents();
1965 return (click_consumed || any_event_action);
1968 static void ResetGlobalAnim_Clickable(void)
1970 InitGlobalAnim_Clickable();
1973 static void ResetGlobalAnim_Clicked(void)
1975 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1978 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1980 static boolean click_consumed = FALSE;
1981 static int last_button = 0;
1982 boolean press_event;
1983 boolean release_event;
1984 boolean click_consumed_current = click_consumed;
1986 if (button != 0 && force_click)
1989 // check if button state has changed since last invocation
1990 press_event = (button != 0 && last_button == 0);
1991 release_event = (button == 0 && last_button != 0);
1992 last_button = button;
1996 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1997 click_consumed_current = click_consumed;
2002 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2003 click_consumed = FALSE;
2006 return click_consumed_current;
2009 int getGlobalAnimSyncFrame(void)
2011 return anim_sync_frame;