1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #include "libgame/libgame.h"
20 /* values for global toon animation definition */
21 #define NUM_GLOBAL_TOON_ANIMS 1
22 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
24 /* values for global animation definition (including toons) */
25 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
26 NUM_GLOBAL_TOON_ANIMS)
27 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
28 NUM_GLOBAL_TOON_PARTS)
30 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
31 #define ANIM_CLASS_BIT_TITLE 1
32 #define ANIM_CLASS_BIT_MAIN 2
33 #define ANIM_CLASS_BIT_SCORES 3
34 #define ANIM_CLASS_BIT_SUBMENU 4
35 #define ANIM_CLASS_BIT_MENU 5
36 #define ANIM_CLASS_BIT_TOONS 6
38 #define NUM_ANIM_CLASSES 7
40 #define ANIM_CLASS_NONE 0
41 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
42 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
43 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
44 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
45 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
46 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
47 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
49 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
52 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
56 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
60 /* values for global animation states */
61 #define ANIM_STATE_INACTIVE 0
62 #define ANIM_STATE_RESTART (1 << 0)
63 #define ANIM_STATE_WAITING (1 << 1)
64 #define ANIM_STATE_RUNNING (1 << 2)
66 /* values for global animation control */
67 #define ANIM_NO_ACTION 0
69 #define ANIM_CONTINUE 2
73 struct GlobalAnimPartControlInfo
75 int old_nr; // position before mapping animation parts linearly
76 int old_anim_nr; // position before mapping animations linearly
82 boolean is_base; // animation part is base/main/default animation part
88 struct GraphicInfo graphic_info;
89 struct GraphicInfo control_info;
97 int step_xoffset, step_yoffset;
99 unsigned int initial_anim_sync_frame;
100 unsigned int step_delay, step_delay_value;
102 int init_delay_counter;
103 int anim_delay_counter;
104 int post_delay_counter;
106 boolean init_event_state;
107 boolean anim_event_state;
115 int last_anim_status;
118 struct GlobalAnimMainControlInfo
120 struct GlobalAnimPartControlInfo base;
121 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
126 struct GraphicInfo control_info;
128 int num_parts; // number of animation parts, but without base part
129 int num_parts_all; // number of animation parts, including base part
133 boolean has_base; // animation has base/main/default animation part
137 int init_delay_counter;
141 int last_state, last_active_part_nr;
144 struct GlobalAnimControlInfo
146 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
152 struct GameModeAnimClass
156 } game_mode_anim_classes_list[] =
158 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
159 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
160 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
161 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
162 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
163 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
164 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
165 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
166 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
167 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
168 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
169 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
170 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
171 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
172 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
173 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
174 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORES },
175 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORES },
180 struct AnimClassGameMode
184 } anim_class_game_modes_list[] =
186 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
187 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
188 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
189 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
190 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
191 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
192 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
197 /* forward declaration for internal use */
198 static void HandleGlobalAnim(int, int);
199 static void DoAnimationExt(void);
200 static void ResetGlobalAnim_Clickable();
201 static void ResetGlobalAnim_Clicked();
203 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
205 static unsigned int anim_sync_frame = 0;
207 static int game_mode_anim_classes[NUM_GAME_MODES];
208 static int anim_class_game_modes[NUM_ANIM_CLASSES];
210 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
211 static int anim_status_last = GAME_MODE_DEFAULT;
212 static int anim_classes_last = ANIM_CLASS_NONE;
214 static boolean drawing_to_fading_buffer = FALSE;
217 /* ========================================================================= */
218 /* generic animation frame calculation */
219 /* ========================================================================= */
221 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
226 sync_frame += start_frame * delay;
228 if (mode & ANIM_LOOP) /* looping animation */
230 frame = (sync_frame % (delay * num_frames)) / delay;
232 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
234 frame = sync_frame / delay;
236 if (frame > num_frames - 1)
237 frame = num_frames - 1;
239 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
241 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
243 frame = (sync_frame % (delay * max_anim_frames)) / delay;
244 frame = (frame < num_frames ? frame : max_anim_frames - frame);
246 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
248 int max_anim_frames = 2 * num_frames;
250 frame = (sync_frame % (delay * max_anim_frames)) / delay;
251 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
253 else if (mode & ANIM_RANDOM) /* play frames in random order */
255 /* note: expect different frames for the same delay cycle! */
257 if (gfx.anim_random_frame < 0)
258 frame = GetSimpleRandom(num_frames);
260 frame = gfx.anim_random_frame % num_frames;
262 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
264 frame = sync_frame % num_frames;
267 if (mode & ANIM_REVERSE) /* use reverse animation direction */
268 frame = num_frames - frame - 1;
274 /* ========================================================================= */
275 /* global animation functions */
276 /* ========================================================================= */
278 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
280 struct GraphicInfo *c = &anim->control_info;
281 int last_anim_random_frame = gfx.anim_random_frame;
284 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
286 part_nr = getAnimationFrame(anim->num_parts, 1,
287 c->anim_mode, c->anim_start_frame,
290 gfx.anim_random_frame = last_anim_random_frame;
295 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
297 const struct GlobalAnimPartControlInfo *o1 =
298 (struct GlobalAnimPartControlInfo *)obj1;
299 const struct GlobalAnimPartControlInfo *o2 =
300 (struct GlobalAnimPartControlInfo *)obj2;
303 if (o1->control_info.draw_order != o2->control_info.draw_order)
304 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
306 compare_result = o1->nr - o2->nr;
308 return compare_result;
311 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
313 const struct GlobalAnimMainControlInfo *o1 =
314 (struct GlobalAnimMainControlInfo *)obj1;
315 const struct GlobalAnimMainControlInfo *o2 =
316 (struct GlobalAnimMainControlInfo *)obj2;
319 if (o1->control_info.draw_order != o2->control_info.draw_order)
320 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
322 compare_result = o1->nr - o2->nr;
324 return compare_result;
327 static void InitToonControls()
329 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
330 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
331 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
332 int mode_nr, anim_nr, part_nr;
333 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
334 int num_toons = MAX_NUM_TOONS;
337 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
338 num_toons = global.num_toons;
340 mode_nr = mode_nr_toons;
341 anim_nr = ctrl->num_anims;
344 anim->mode_nr = mode_nr;
345 anim->control_info = graphic_info[control];
348 anim->num_parts_all = 0;
349 anim->part_counter = 0;
350 anim->active_part_nr = 0;
352 anim->has_base = FALSE;
354 anim->last_x = POS_OFFSCREEN;
355 anim->last_y = POS_OFFSCREEN;
357 anim->init_delay_counter = 0;
359 anim->state = ANIM_STATE_INACTIVE;
363 for (i = 0; i < num_toons; i++)
365 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
366 int sound = SND_UNDEFINED;
367 int music = MUS_UNDEFINED;
368 int graphic = IMG_TOON_1 + i;
369 int control = graphic;
372 part->anim_nr = anim_nr;
373 part->mode_nr = mode_nr;
375 part->is_base = FALSE;
379 part->graphic = graphic;
381 part->graphic_info = graphic_info[graphic];
382 part->control_info = graphic_info[control];
384 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
386 part->control_info.init_delay_fixed = 0;
387 part->control_info.init_delay_random = 150;
389 part->control_info.x = ARG_UNDEFINED_VALUE;
390 part->control_info.y = ARG_UNDEFINED_VALUE;
392 part->initial_anim_sync_frame = 0;
394 part->step_delay = 0;
395 part->step_delay_value = graphic_info[control].step_delay;
397 part->state = ANIM_STATE_INACTIVE;
398 part->last_anim_status = -1;
401 anim->num_parts_all++;
409 void InitGlobalAnimControls()
412 int mode_nr, anim_nr, part_nr;
413 int sound, music, graphic, control;
417 for (m = 0; m < NUM_GAME_MODES; m++)
421 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
428 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
430 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
431 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
433 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
435 // if no base animation parameters defined, use default values
436 if (control == IMG_UNDEFINED)
437 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
440 anim->mode_nr = mode_nr;
441 anim->control_info = graphic_info[control];
444 anim->num_parts_all = 0;
445 anim->part_counter = 0;
446 anim->active_part_nr = 0;
448 anim->has_base = FALSE;
450 anim->last_x = POS_OFFSCREEN;
451 anim->last_y = POS_OFFSCREEN;
453 anim->init_delay_counter = 0;
455 anim->state = ANIM_STATE_INACTIVE;
459 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
461 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
463 sound = global_anim_info[a].sound[p][m];
464 music = global_anim_info[a].music[p][m];
465 graphic = global_anim_info[a].graphic[p][m];
466 control = global_anim_info[ctrl_id].graphic[p][m];
468 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
469 control == IMG_UNDEFINED)
473 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
474 m, a, p, mode_nr, anim_nr, part_nr, control);
478 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
479 m, a, p, mode_nr, anim_nr, part_nr, sound);
483 part->old_anim_nr = a;
486 part->anim_nr = anim_nr;
487 part->mode_nr = mode_nr;
491 part->graphic = graphic;
493 part->graphic_info = graphic_info[graphic];
494 part->control_info = graphic_info[control];
496 part->initial_anim_sync_frame = 0;
498 part->step_delay = 0;
499 part->step_delay_value = graphic_info[control].step_delay;
501 part->state = ANIM_STATE_INACTIVE;
502 part->last_anim_status = -1;
504 anim->num_parts_all++;
506 if (p < GLOBAL_ANIM_ID_PART_BASE)
508 part->is_base = FALSE;
515 part->is_base = TRUE;
518 anim->has_base = TRUE;
522 if (anim->num_parts > 0 || anim->has_base)
532 /* sort all animations according to draw_order and animation number */
533 for (m = 0; m < NUM_GAME_MODES; m++)
535 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
537 /* sort all main animations for this game mode */
538 qsort(ctrl->anim, ctrl->num_anims,
539 sizeof(struct GlobalAnimMainControlInfo),
540 compareGlobalAnimMainControlInfo);
542 for (a = 0; a < ctrl->num_anims; a++)
544 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
546 /* sort all animation parts for this main animation */
547 qsort(anim->part, anim->num_parts,
548 sizeof(struct GlobalAnimPartControlInfo),
549 compareGlobalAnimPartControlInfo);
553 for (i = 0; i < NUM_GAME_MODES; i++)
554 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
555 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
556 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
557 game_mode_anim_classes_list[i].class;
559 for (i = 0; i < NUM_ANIM_CLASSES; i++)
560 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
561 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
562 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
563 anim_class_game_modes_list[i].game_mode;
565 anim_status_last_before_fading = GAME_MODE_LOADING;
566 anim_status_last = GAME_MODE_LOADING;
567 anim_classes_last = ANIM_CLASS_NONE;
570 void InitGlobalAnimations()
572 InitGlobalAnimControls();
575 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
577 Bitmap *fade_bitmap =
578 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
579 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
580 int game_mode_anim_action[NUM_GAME_MODES];
586 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
587 drawing_target == DRAW_TO_SCREEN)
590 // always start with reliable default values (no animation actions)
591 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
592 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
594 if (global.anim_status != anim_status_last)
596 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
597 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
598 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
601 if (drawing_target == DRAW_TO_FADE_TARGET)
604 // special case: changing from/to this screen is done without fading
605 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
606 anim_status_last == GAME_MODE_PSEUDO_TYPENAME)
609 // ---------- part 1 ------------------------------------------------------
610 // start or stop global animations by change of game mode
611 // (special handling of animations for "current screen" and "all screens")
613 if (global.anim_status_next != anim_status_last_before_fading)
615 // stop animations for last screen before fading to new screen
616 game_mode_anim_action[anim_status_last] = ANIM_STOP;
618 // start animations for current screen after fading to new screen
619 game_mode_anim_action[global.anim_status] = ANIM_START;
622 // start animations for all screens after loading new artwork set
623 if (anim_status_last == GAME_MODE_LOADING)
624 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
626 // ---------- part 2 ------------------------------------------------------
627 // start or stop global animations by change of animation class
628 // (generic handling of animations for "class of screens")
630 for (i = 0; i < NUM_ANIM_CLASSES; i++)
632 int anim_class_check = (1 << i);
633 int anim_class_game_mode = anim_class_game_modes[i];
634 int anim_class_last = anim_classes_last & anim_class_check;
635 int anim_class_next = anim_classes_next & anim_class_check;
637 // stop animations for changed screen class before fading to new screen
638 if (before_fading && anim_class_last && !anim_class_next)
639 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
641 // start animations for changed screen class after fading to new screen
642 if (after_fading && !anim_class_last && anim_class_next)
643 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
646 if (drawing_target == DRAW_TO_SCREEN)
650 anim_classes_last = anim_classes_next;
651 anim_status_last_before_fading = global.anim_status;
654 anim_status_last = global.anim_status;
656 // start or stop animations determined to be started or stopped above
657 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
658 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
659 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
661 else if (drawing_target == DRAW_TO_FADE_TARGET)
663 drawing_to_fading_buffer = TRUE;
665 // start animations determined to be (temporary) started above
666 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
667 if (game_mode_anim_action[mode_nr] == ANIM_START)
668 HandleGlobalAnim(ANIM_START, mode_nr);
672 if (global.anim_status == GAME_MODE_LOADING)
675 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
677 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
680 // when preparing source fading buffer, only draw animations to be stopped
681 if (drawing_target == DRAW_TO_FADE_SOURCE &&
682 game_mode_anim_action[mode_nr] != ANIM_STOP)
685 // when preparing target fading buffer, only draw animations to be started
686 if (drawing_target == DRAW_TO_FADE_TARGET &&
687 game_mode_anim_action[mode_nr] != ANIM_START)
691 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
692 mode_nr != game_status)
696 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
698 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
699 struct GraphicInfo *c = &anim->control_info;
700 int part_first, part_last;
703 if (!(anim->state & ANIM_STATE_RUNNING))
706 part_first = part_last = anim->active_part_nr;
708 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
710 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
713 part_last = num_parts - 1;
716 for (part_nr = part_first; part_nr <= part_last; part_nr++)
718 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
719 struct GraphicInfo *g = &part->graphic_info;
722 int width = g->width;
723 int height = g->height;
730 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
731 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
732 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
733 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
735 if (!(part->state & ANIM_STATE_RUNNING))
738 if (part->drawing_stage != drawing_stage)
747 else if (part->x > part->viewport_width - g->width)
748 width -= (part->x - (part->viewport_width - g->width));
756 else if (part->y > part->viewport_height - g->height)
757 height -= (part->y - (part->viewport_height - g->height));
759 if (width <= 0 || height <= 0)
762 dst_x += part->viewport_x;
763 dst_y += part->viewport_y;
765 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
766 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
767 g->anim_mode, g->anim_start_frame,
770 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
776 if (drawing_target == DRAW_TO_SCREEN)
777 blit_screen(src_bitmap, src_x, src_y, width, height,
780 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
786 if (drawing_target == DRAW_TO_FADE_TARGET)
788 // stop animations determined to be (temporary) started above
789 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
790 if (game_mode_anim_action[mode_nr] == ANIM_START)
791 HandleGlobalAnim(ANIM_STOP, mode_nr);
793 drawing_to_fading_buffer = FALSE;
797 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
799 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
800 ResetGlobalAnim_Clickable();
802 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
804 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
805 ResetGlobalAnim_Clicked();
808 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
814 boolean changed = FALSE;
816 if (part->last_anim_status == global.anim_status)
819 part->last_anim_status = global.anim_status;
821 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
823 if (part->control_info.class == get_hash_from_key("window") ||
824 part->control_info.class == get_hash_from_key("border"))
828 viewport_width = WIN_XSIZE;
829 viewport_height = WIN_YSIZE;
831 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
833 else if (part->control_info.class == get_hash_from_key("door_1"))
837 viewport_width = DXSIZE;
838 viewport_height = DYSIZE;
840 else if (part->control_info.class == get_hash_from_key("door_2"))
844 viewport_width = VXSIZE;
845 viewport_height = VYSIZE;
847 else // default: "playfield"
849 viewport_x = REAL_SX;
850 viewport_y = REAL_SY;
851 viewport_width = FULL_SXSIZE;
852 viewport_height = FULL_SYSIZE;
855 if (viewport_x != part->viewport_x ||
856 viewport_y != part->viewport_y ||
857 viewport_width != part->viewport_width ||
858 viewport_height != part->viewport_height)
860 part->viewport_x = viewport_x;
861 part->viewport_y = viewport_y;
862 part->viewport_width = viewport_width;
863 part->viewport_height = viewport_height;
871 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
873 int sound = part->sound;
875 if (sound == SND_UNDEFINED)
878 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
879 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
882 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
883 if (IS_LOOP_SOUND(sound))
884 PlaySoundLoop(sound);
889 printf("::: PLAY SOUND %d.%d.%d: %d\n",
890 part->anim_nr, part->nr, part->mode_nr, sound);
894 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
896 int sound = part->sound;
898 if (sound == SND_UNDEFINED)
904 printf("::: STOP SOUND %d.%d.%d: %d\n",
905 part->anim_nr, part->nr, part->mode_nr, sound);
909 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
911 int music = part->music;
913 if (music == MUS_UNDEFINED)
916 if (!setup.sound_music)
922 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
923 part->anim_nr, part->nr, part->mode_nr, music);
927 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
929 int music = part->music;
931 if (music == MUS_UNDEFINED)
937 printf("::: STOP MUSIC %d.%d.%d: %d\n",
938 part->anim_nr, part->nr, part->mode_nr, music);
942 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
944 // when drawing animations to fading buffer, do not play sounds or music
945 if (drawing_to_fading_buffer)
948 PlayGlobalAnimSound(part);
949 PlayGlobalAnimMusic(part);
952 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
954 StopGlobalAnimSound(part);
955 StopGlobalAnimMusic(part);
958 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
960 struct GraphicInfo *c = &part->control_info;
961 int trigger_mask = ANIM_EVENT_ANIM_MASK | ANIM_EVENT_PART_MASK;
962 int mask_anim_only = mask & ANIM_EVENT_ANIM_MASK;
964 if (mask & ANIM_EVENT_ANY)
965 return (c->init_event & ANIM_EVENT_ANY ||
966 c->anim_event & ANIM_EVENT_ANY);
967 else if (mask & ANIM_EVENT_SELF)
968 return (c->init_event & ANIM_EVENT_SELF ||
969 c->anim_event & ANIM_EVENT_SELF);
971 return ((c->init_event & trigger_mask) == mask ||
972 (c->anim_event & trigger_mask) == mask ||
973 (c->init_event & trigger_mask) == mask_anim_only ||
974 (c->anim_event & trigger_mask) == mask_anim_only);
977 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
978 int mx, int my, boolean clicked)
980 struct GraphicInfo *g = &part->graphic_info;
981 int part_x = part->viewport_x + part->x;
982 int part_y = part->viewport_y + part->y;
983 int part_width = g->width;
984 int part_height = g->height;
986 // check if mouse click was detected at all
990 // check if mouse click is inside the animation part's viewport
991 if (mx < part->viewport_x ||
992 mx >= part->viewport_x + part->viewport_width ||
993 my < part->viewport_y ||
994 my >= part->viewport_y + part->viewport_height)
997 // check if mouse click is inside the animation part's graphic
999 mx >= part_x + part_width ||
1001 my >= part_y + part_height)
1007 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1009 return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1012 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
1014 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1015 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1016 struct GraphicInfo *g = &part->graphic_info;
1017 struct GraphicInfo *c = &part->control_info;
1018 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1020 if (viewport_changed)
1021 state |= ANIM_STATE_RESTART;
1023 if (state & ANIM_STATE_RESTART)
1025 // when drawing animations to fading buffer, only start fixed animations
1026 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1027 c->y == ARG_UNDEFINED_VALUE))
1028 return ANIM_STATE_INACTIVE;
1030 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1032 part->init_delay_counter =
1033 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1035 part->anim_delay_counter =
1036 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1038 part->init_event_state = c->init_event;
1039 part->anim_event_state = c->anim_event;
1041 part->initial_anim_sync_frame =
1042 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1044 if (c->direction & MV_HORIZONTAL)
1046 int pos_bottom = part->viewport_height - g->height;
1048 if (c->position == POS_TOP)
1050 else if (c->position == POS_UPPER)
1051 part->y = GetSimpleRandom(pos_bottom / 2);
1052 else if (c->position == POS_MIDDLE)
1053 part->y = pos_bottom / 2;
1054 else if (c->position == POS_LOWER)
1055 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1056 else if (c->position == POS_BOTTOM)
1057 part->y = pos_bottom;
1059 part->y = GetSimpleRandom(pos_bottom);
1061 if (c->direction == MV_RIGHT)
1063 part->step_xoffset = c->step_offset;
1064 part->x = -g->width + part->step_xoffset;
1068 part->step_xoffset = -c->step_offset;
1069 part->x = part->viewport_width + part->step_xoffset;
1072 part->step_yoffset = 0;
1074 else if (c->direction & MV_VERTICAL)
1076 int pos_right = part->viewport_width - g->width;
1078 if (c->position == POS_LEFT)
1080 else if (c->position == POS_RIGHT)
1081 part->x = pos_right;
1083 part->x = GetSimpleRandom(pos_right);
1085 if (c->direction == MV_DOWN)
1087 part->step_yoffset = c->step_offset;
1088 part->y = -g->height + part->step_yoffset;
1092 part->step_yoffset = -c->step_offset;
1093 part->y = part->viewport_height + part->step_yoffset;
1096 part->step_xoffset = 0;
1103 part->step_xoffset = 0;
1104 part->step_yoffset = 0;
1107 if (c->x != ARG_UNDEFINED_VALUE)
1109 if (c->y != ARG_UNDEFINED_VALUE)
1112 if (c->position == POS_LAST &&
1113 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1114 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1116 part->x = anim->last_x;
1117 part->y = anim->last_y;
1120 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1121 part->step_xoffset = c->step_xoffset;
1122 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1123 part->step_yoffset = c->step_yoffset;
1125 if (part->init_delay_counter == 0 &&
1126 part->init_event_state == ANIM_EVENT_NONE)
1127 PlayGlobalAnimSoundAndMusic(part);
1130 if (part->clicked &&
1131 part->init_event_state != ANIM_EVENT_NONE)
1133 if (part->initial_anim_sync_frame > 0)
1134 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1136 part->init_delay_counter = 1;
1137 part->init_event_state = ANIM_EVENT_NONE;
1139 part->clicked = FALSE;
1142 if (part->clicked &&
1143 part->anim_event_state != ANIM_EVENT_NONE)
1145 part->anim_delay_counter = 1;
1146 part->anim_event_state = ANIM_EVENT_NONE;
1148 part->clicked = FALSE;
1151 if (part->init_delay_counter > 0)
1153 part->init_delay_counter--;
1155 if (part->init_delay_counter == 0)
1157 part->init_event_state = ANIM_EVENT_NONE;
1159 PlayGlobalAnimSoundAndMusic(part);
1162 return ANIM_STATE_WAITING;
1165 if (part->init_event_state != ANIM_EVENT_NONE)
1166 return ANIM_STATE_WAITING;
1168 // animation part is now running/visible and therefore clickable
1169 part->clickable = TRUE;
1171 // check if moving animation has left the visible screen area
1172 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1173 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1174 (part->y <= -g->height && part->step_yoffset <= 0) ||
1175 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1177 // do not wait for "anim" events for off-screen animations
1178 part->anim_event_state = ANIM_EVENT_NONE;
1180 // do not stop animation before "anim" or "post" counter are finished
1181 if (part->anim_delay_counter == 0 &&
1182 part->post_delay_counter == 0)
1184 StopGlobalAnimSoundAndMusic(part);
1186 part->post_delay_counter =
1187 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1189 if (part->post_delay_counter > 0)
1190 return ANIM_STATE_RUNNING;
1192 // drawing last frame not needed here -- animation not visible anymore
1193 return ANIM_STATE_RESTART;
1197 if (part->anim_delay_counter > 0)
1199 part->anim_delay_counter--;
1201 if (part->anim_delay_counter == 0)
1203 part->anim_event_state = ANIM_EVENT_NONE;
1205 StopGlobalAnimSoundAndMusic(part);
1207 part->post_delay_counter =
1208 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1210 if (part->post_delay_counter > 0)
1211 return ANIM_STATE_RUNNING;
1213 // additional state "RUNNING" required to not skip drawing last frame
1214 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1218 if (part->post_delay_counter > 0)
1220 part->post_delay_counter--;
1222 if (part->post_delay_counter == 0)
1223 return ANIM_STATE_RESTART;
1225 return ANIM_STATE_WAITING;
1228 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1230 return ANIM_STATE_RUNNING;
1234 static unsigned int last_counter = -1;
1235 unsigned int counter = Counter();
1237 printf("::: NEXT ANIM PART [%d, %d]\n",
1238 anim_sync_frame, counter - last_counter);
1240 last_counter = counter;
1244 part->x += part->step_xoffset;
1245 part->y += part->step_yoffset;
1247 anim->last_x = part->x;
1248 anim->last_y = part->y;
1250 return ANIM_STATE_RUNNING;
1253 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1255 struct GlobalAnimPartControlInfo *part;
1256 struct GraphicInfo *c = &anim->control_info;
1257 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1258 int state, active_part_nr;
1262 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1263 anim->mode_nr, anim->nr, anim->num_parts);
1264 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1268 printf("::: %s(%d): %d, %d, %d [%d]\n",
1269 (action == ANIM_START ? "ANIM_START" :
1270 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1271 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1273 anim->state & ANIM_STATE_RESTART,
1274 anim->state & ANIM_STATE_WAITING,
1275 anim->state & ANIM_STATE_RUNNING,
1282 anim->state = anim->last_state = ANIM_STATE_RESTART;
1283 anim->active_part_nr = anim->last_active_part_nr = 0;
1284 anim->part_counter = 0;
1289 if (anim->state == ANIM_STATE_INACTIVE)
1292 anim->state = anim->last_state;
1293 anim->active_part_nr = anim->last_active_part_nr;
1298 anim->state = ANIM_STATE_INACTIVE;
1300 for (i = 0; i < num_parts; i++)
1301 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1309 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1312 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1313 anim->mode_nr, anim->nr, num_parts);
1316 for (i = 0; i < num_parts; i++)
1318 part = &anim->part[i];
1323 anim->state = ANIM_STATE_RUNNING;
1324 part->state = ANIM_STATE_RESTART;
1329 if (part->state == ANIM_STATE_INACTIVE)
1335 part->state = ANIM_STATE_INACTIVE;
1343 part->state = HandleGlobalAnim_Part(part, part->state);
1345 // when animation mode is "once", stop after animation was played once
1346 if (c->anim_mode & ANIM_ONCE &&
1347 part->state & ANIM_STATE_RESTART)
1348 part->state = ANIM_STATE_INACTIVE;
1351 anim->last_state = anim->state;
1352 anim->last_active_part_nr = anim->active_part_nr;
1357 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1358 anim->active_part_nr = getGlobalAnimationPart(anim);
1360 part = &anim->part[anim->active_part_nr];
1362 // first set all animation parts to "inactive", ...
1363 for (i = 0; i < num_parts; i++)
1364 anim->part[i].state = ANIM_STATE_INACTIVE;
1366 // ... then set current animation parts to "running"
1367 part->state = ANIM_STATE_RUNNING;
1369 anim->state = HandleGlobalAnim_Part(part, anim->state);
1371 if (anim->state & ANIM_STATE_RESTART)
1372 anim->part_counter++;
1374 // when animation mode is "once", stop after all animations were played once
1375 if (c->anim_mode & ANIM_ONCE &&
1376 anim->part_counter == anim->num_parts)
1377 anim->state = ANIM_STATE_INACTIVE;
1379 state = anim->state;
1380 active_part_nr = anim->active_part_nr;
1382 // while the animation parts are pausing (waiting or inactive), play the base
1383 // (main) animation; this corresponds to the "boring player animation" logic
1384 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1387 if (anim->state == ANIM_STATE_WAITING ||
1388 anim->state == ANIM_STATE_INACTIVE)
1390 anim->active_part_nr = anim->num_parts; // part nr of base animation
1391 part = &anim->part[anim->active_part_nr];
1393 if (anim->state != anim->last_state)
1394 part->state = ANIM_STATE_RESTART;
1396 anim->state = ANIM_STATE_RUNNING;
1397 part->state = HandleGlobalAnim_Part(part, part->state);
1401 anim->last_state = state;
1402 anim->last_active_part_nr = active_part_nr;
1405 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1410 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1411 ctrl->nr, ctrl->num_anims);
1414 for (i = 0; i < ctrl->num_anims; i++)
1415 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1418 static void HandleGlobalAnim(int action, int game_mode)
1421 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1424 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1427 static void DoAnimationExt()
1432 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1435 // global animations now synchronized with frame delay of screen update
1438 for (i = 0; i < NUM_GAME_MODES; i++)
1439 HandleGlobalAnim(ANIM_CONTINUE, i);
1442 // force screen redraw in next frame to continue drawing global animations
1443 redraw_mask = REDRAW_ALL;
1447 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1449 int anim_event_action = part->control_info.anim_event_action;
1451 if (anim_event_action == -1)
1454 boolean action_executed = (DoGadgetAction(anim_event_action) ||
1455 DoScreenAction(anim_event_action));
1457 // check if further actions are allowed to be executed
1458 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1461 return action_executed;
1464 static void InitGlobalAnim_Clickable()
1468 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1470 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1473 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1475 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1478 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1480 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1482 part->clickable = FALSE;
1488 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1490 boolean anything_clicked = FALSE;
1491 boolean any_part_clicked = FALSE;
1492 boolean any_event_action = FALSE;
1495 // check game modes in reverse draw order (to stop when clicked)
1496 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1498 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1501 // check animations in reverse draw order (to stop when clicked)
1502 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1504 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1507 // check animation parts in reverse draw order (to stop when clicked)
1508 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1510 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1514 part->clicked = FALSE;
1519 if (!part->clickable)
1522 if (part->state != ANIM_STATE_RUNNING)
1525 // always handle "any" click events (clicking anywhere on screen) ...
1526 if (isClickablePart(part, ANIM_EVENT_ANY))
1528 part->clicked = TRUE;
1529 anything_clicked = clickConsumed(part);
1532 // ... but only handle the first (topmost) clickable animation
1533 if (any_part_clicked)
1536 if (isClickedPart(part, mx, my, clicked))
1539 printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
1540 part->control_info.anim_event_action);
1543 // after executing event action, ignore any further actions
1544 if (!any_event_action && DoGlobalAnim_EventAction(part))
1545 any_event_action = TRUE;
1547 // determine if mouse clicks should be blocked from other animations
1548 any_part_clicked = clickConsumed(part);
1550 if (isClickablePart(part, ANIM_EVENT_SELF))
1552 part->clicked = TRUE;
1553 anything_clicked = clickConsumed(part);
1556 // check if this click is defined to trigger other animations
1557 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1558 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1559 int mask = gic_anim_nr << ANIM_EVENT_ANIM_BIT;
1562 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1566 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1568 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1571 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1573 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1575 if (part2->state != ANIM_STATE_RUNNING)
1578 if (isClickablePart(part2, mask))
1580 part2->clicked = TRUE;
1581 anything_clicked = clickConsumed(part); // click was on "part"!
1584 printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
1585 part2->control_info.anim_event_action);
1588 // after executing event action, ignore any further actions
1589 if (!any_event_action && DoGlobalAnim_EventAction(part2))
1590 any_event_action = TRUE;
1594 struct GraphicInfo *c = &part2->control_info;
1596 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1597 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1599 if (isClickablePart(part2, mask))
1600 printf(" <--- TRIGGERED BY %d.%d",
1612 return (anything_clicked || any_event_action);
1615 static void ResetGlobalAnim_Clickable()
1617 InitGlobalAnim_Clickable();
1620 static void ResetGlobalAnim_Clicked()
1622 InitGlobalAnim_Clicked(-1, -1, FALSE);
1625 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1627 static boolean click_consumed = FALSE;
1628 static int last_button = 0;
1629 boolean press_event;
1630 boolean release_event;
1631 boolean click_consumed_current = click_consumed;
1633 /* check if button state has changed since last invocation */
1634 press_event = (button != 0 && last_button == 0);
1635 release_event = (button == 0 && last_button != 0);
1636 last_button = button;
1640 click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1641 click_consumed_current = click_consumed;
1645 click_consumed = FALSE;
1647 return click_consumed_current;