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"
21 /* values for global toon animation definition */
22 #define NUM_GLOBAL_TOON_ANIMS 1
23 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
25 /* values for global animation definition (including toons) */
26 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
27 NUM_GLOBAL_TOON_ANIMS)
28 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
29 NUM_GLOBAL_TOON_PARTS)
31 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
32 #define ANIM_CLASS_BIT_TITLE 1
33 #define ANIM_CLASS_BIT_MAIN 2
34 #define ANIM_CLASS_BIT_SCORES 3
35 #define ANIM_CLASS_BIT_SUBMENU 4
36 #define ANIM_CLASS_BIT_MENU 5
37 #define ANIM_CLASS_BIT_TOONS 6
39 #define NUM_ANIM_CLASSES 7
41 #define ANIM_CLASS_NONE 0
42 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
43 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
44 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
45 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
46 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
47 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
48 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
50 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
53 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
57 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
61 /* values for global animation states */
62 #define ANIM_STATE_INACTIVE 0
63 #define ANIM_STATE_RESTART (1 << 0)
64 #define ANIM_STATE_WAITING (1 << 1)
65 #define ANIM_STATE_RUNNING (1 << 2)
67 /* values for global animation control */
68 #define ANIM_NO_ACTION 0
70 #define ANIM_CONTINUE 2
74 struct GlobalAnimPartControlInfo
76 int old_nr; // position before mapping animation parts linearly
77 int old_anim_nr; // position before mapping animations linearly
83 boolean is_base; // animation part is base/main/default animation part
89 struct GraphicInfo graphic_info;
90 struct GraphicInfo control_info;
98 int step_xoffset, step_yoffset;
100 unsigned int initial_anim_sync_frame;
101 unsigned int step_delay, step_delay_value;
103 int init_delay_counter;
104 int anim_delay_counter;
105 int post_delay_counter;
107 boolean init_event_state;
108 boolean anim_event_state;
116 int last_anim_status;
119 struct GlobalAnimMainControlInfo
121 struct GlobalAnimPartControlInfo base;
122 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
127 struct GraphicInfo control_info;
129 int num_parts; // number of animation parts, but without base part
130 int num_parts_all; // number of animation parts, including base part
134 boolean has_base; // animation has base/main/default animation part
138 int init_delay_counter;
142 int last_state, last_active_part_nr;
145 struct GlobalAnimControlInfo
147 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
153 struct GameModeAnimClass
157 } game_mode_anim_classes_list[] =
159 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
160 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
161 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
162 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
163 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
164 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
165 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
166 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
167 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
168 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
169 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
170 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
171 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
172 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
173 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
174 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
175 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORES },
176 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORES },
181 struct AnimClassGameMode
185 } anim_class_game_modes_list[] =
187 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
188 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
189 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
190 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
191 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
192 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
193 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
198 /* forward declaration for internal use */
199 static void HandleGlobalAnim(int, int);
200 static void DoAnimationExt(void);
201 static void ResetGlobalAnim_Clickable();
202 static void ResetGlobalAnim_Clicked();
204 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
206 static unsigned int anim_sync_frame = 0;
208 static int game_mode_anim_classes[NUM_GAME_MODES];
209 static int anim_class_game_modes[NUM_ANIM_CLASSES];
211 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
212 static int anim_status_last = GAME_MODE_DEFAULT;
213 static int anim_classes_last = ANIM_CLASS_NONE;
215 static boolean drawing_to_fading_buffer = FALSE;
218 /* ========================================================================= */
219 /* generic animation frame calculation */
220 /* ========================================================================= */
222 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
227 sync_frame += start_frame * delay;
229 if (mode & ANIM_LOOP) /* looping animation */
231 frame = (sync_frame % (delay * num_frames)) / delay;
233 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
235 frame = sync_frame / delay;
237 if (frame > num_frames - 1)
238 frame = num_frames - 1;
240 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
242 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
244 frame = (sync_frame % (delay * max_anim_frames)) / delay;
245 frame = (frame < num_frames ? frame : max_anim_frames - frame);
247 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
249 int max_anim_frames = 2 * num_frames;
251 frame = (sync_frame % (delay * max_anim_frames)) / delay;
252 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
254 else if (mode & ANIM_RANDOM) /* play frames in random order */
256 /* note: expect different frames for the same delay cycle! */
258 if (gfx.anim_random_frame < 0)
259 frame = GetSimpleRandom(num_frames);
261 frame = gfx.anim_random_frame % num_frames;
263 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
265 frame = sync_frame % num_frames;
268 if (mode & ANIM_REVERSE) /* use reverse animation direction */
269 frame = num_frames - frame - 1;
275 /* ========================================================================= */
276 /* global animation functions */
277 /* ========================================================================= */
279 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
281 struct GraphicInfo *c = &anim->control_info;
282 int last_anim_random_frame = gfx.anim_random_frame;
285 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
287 part_nr = getAnimationFrame(anim->num_parts, 1,
288 c->anim_mode, c->anim_start_frame,
291 gfx.anim_random_frame = last_anim_random_frame;
296 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
298 const struct GlobalAnimPartControlInfo *o1 =
299 (struct GlobalAnimPartControlInfo *)obj1;
300 const struct GlobalAnimPartControlInfo *o2 =
301 (struct GlobalAnimPartControlInfo *)obj2;
304 if (o1->control_info.draw_order != o2->control_info.draw_order)
305 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
307 compare_result = o1->nr - o2->nr;
309 return compare_result;
312 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
314 const struct GlobalAnimMainControlInfo *o1 =
315 (struct GlobalAnimMainControlInfo *)obj1;
316 const struct GlobalAnimMainControlInfo *o2 =
317 (struct GlobalAnimMainControlInfo *)obj2;
320 if (o1->control_info.draw_order != o2->control_info.draw_order)
321 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
323 compare_result = o1->nr - o2->nr;
325 return compare_result;
328 static void InitToonControls()
330 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
331 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
332 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
333 int mode_nr, anim_nr, part_nr;
334 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
335 int num_toons = MAX_NUM_TOONS;
338 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
339 num_toons = global.num_toons;
341 mode_nr = mode_nr_toons;
342 anim_nr = ctrl->num_anims;
345 anim->mode_nr = mode_nr;
346 anim->control_info = graphic_info[control];
349 anim->num_parts_all = 0;
350 anim->part_counter = 0;
351 anim->active_part_nr = 0;
353 anim->has_base = FALSE;
355 anim->last_x = POS_OFFSCREEN;
356 anim->last_y = POS_OFFSCREEN;
358 anim->init_delay_counter = 0;
360 anim->state = ANIM_STATE_INACTIVE;
364 for (i = 0; i < num_toons; i++)
366 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
367 int sound = SND_UNDEFINED;
368 int music = MUS_UNDEFINED;
369 int graphic = IMG_TOON_1 + i;
370 int control = graphic;
373 part->anim_nr = anim_nr;
374 part->mode_nr = mode_nr;
376 part->is_base = FALSE;
380 part->graphic = graphic;
382 part->graphic_info = graphic_info[graphic];
383 part->control_info = graphic_info[control];
385 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
387 part->control_info.init_delay_fixed = 0;
388 part->control_info.init_delay_random = 150;
390 part->control_info.x = ARG_UNDEFINED_VALUE;
391 part->control_info.y = ARG_UNDEFINED_VALUE;
393 part->initial_anim_sync_frame = 0;
395 part->step_delay = 0;
396 part->step_delay_value = graphic_info[control].step_delay;
398 part->state = ANIM_STATE_INACTIVE;
399 part->last_anim_status = -1;
402 anim->num_parts_all++;
410 void InitGlobalAnimControls()
413 int mode_nr, anim_nr, part_nr;
414 int sound, music, graphic, control;
418 for (m = 0; m < NUM_GAME_MODES; m++)
422 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
429 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
431 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
432 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
434 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
436 // if no base animation parameters defined, use default values
437 if (control == IMG_UNDEFINED)
438 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
441 anim->mode_nr = mode_nr;
442 anim->control_info = graphic_info[control];
445 anim->num_parts_all = 0;
446 anim->part_counter = 0;
447 anim->active_part_nr = 0;
449 anim->has_base = FALSE;
451 anim->last_x = POS_OFFSCREEN;
452 anim->last_y = POS_OFFSCREEN;
454 anim->init_delay_counter = 0;
456 anim->state = ANIM_STATE_INACTIVE;
460 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
462 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
464 sound = global_anim_info[a].sound[p][m];
465 music = global_anim_info[a].music[p][m];
466 graphic = global_anim_info[a].graphic[p][m];
467 control = global_anim_info[ctrl_id].graphic[p][m];
469 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
470 control == IMG_UNDEFINED)
474 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
475 m, a, p, mode_nr, anim_nr, part_nr, control);
479 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
480 m, a, p, mode_nr, anim_nr, part_nr, sound);
484 part->old_anim_nr = a;
487 part->anim_nr = anim_nr;
488 part->mode_nr = mode_nr;
492 part->graphic = graphic;
494 part->graphic_info = graphic_info[graphic];
495 part->control_info = graphic_info[control];
497 part->initial_anim_sync_frame = 0;
499 part->step_delay = 0;
500 part->step_delay_value = graphic_info[control].step_delay;
502 part->state = ANIM_STATE_INACTIVE;
503 part->last_anim_status = -1;
505 anim->num_parts_all++;
507 if (p < GLOBAL_ANIM_ID_PART_BASE)
509 part->is_base = FALSE;
516 part->is_base = TRUE;
519 anim->has_base = TRUE;
523 if (anim->num_parts > 0 || anim->has_base)
533 /* sort all animations according to draw_order and animation number */
534 for (m = 0; m < NUM_GAME_MODES; m++)
536 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
538 /* sort all main animations for this game mode */
539 qsort(ctrl->anim, ctrl->num_anims,
540 sizeof(struct GlobalAnimMainControlInfo),
541 compareGlobalAnimMainControlInfo);
543 for (a = 0; a < ctrl->num_anims; a++)
545 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
547 /* sort all animation parts for this main animation */
548 qsort(anim->part, anim->num_parts,
549 sizeof(struct GlobalAnimPartControlInfo),
550 compareGlobalAnimPartControlInfo);
554 for (i = 0; i < NUM_GAME_MODES; i++)
555 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
556 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
557 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
558 game_mode_anim_classes_list[i].class;
560 for (i = 0; i < NUM_ANIM_CLASSES; i++)
561 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
562 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
563 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
564 anim_class_game_modes_list[i].game_mode;
566 anim_status_last_before_fading = GAME_MODE_LOADING;
567 anim_status_last = GAME_MODE_LOADING;
568 anim_classes_last = ANIM_CLASS_NONE;
571 void InitGlobalAnimations()
573 InitGlobalAnimControls();
576 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
578 Bitmap *fade_bitmap =
579 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
580 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
581 int game_mode_anim_action[NUM_GAME_MODES];
587 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
588 drawing_target == DRAW_TO_SCREEN)
591 // always start with reliable default values (no animation actions)
592 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
593 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
595 if (global.anim_status != anim_status_last)
597 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
598 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
599 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
602 if (drawing_target == DRAW_TO_FADE_TARGET)
605 // special case: changing from/to this screen is done without fading
606 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
607 anim_status_last == GAME_MODE_PSEUDO_TYPENAME)
610 // ---------- part 1 ------------------------------------------------------
611 // start or stop global animations by change of game mode
612 // (special handling of animations for "current screen" and "all screens")
614 if (global.anim_status_next != anim_status_last_before_fading)
616 // stop animations for last screen before fading to new screen
617 game_mode_anim_action[anim_status_last] = ANIM_STOP;
619 // start animations for current screen after fading to new screen
620 game_mode_anim_action[global.anim_status] = ANIM_START;
623 // start animations for all screens after loading new artwork set
624 if (anim_status_last == GAME_MODE_LOADING)
625 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
627 // ---------- part 2 ------------------------------------------------------
628 // start or stop global animations by change of animation class
629 // (generic handling of animations for "class of screens")
631 for (i = 0; i < NUM_ANIM_CLASSES; i++)
633 int anim_class_check = (1 << i);
634 int anim_class_game_mode = anim_class_game_modes[i];
635 int anim_class_last = anim_classes_last & anim_class_check;
636 int anim_class_next = anim_classes_next & anim_class_check;
638 // stop animations for changed screen class before fading to new screen
639 if (before_fading && anim_class_last && !anim_class_next)
640 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
642 // start animations for changed screen class after fading to new screen
643 if (after_fading && !anim_class_last && anim_class_next)
644 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
647 if (drawing_target == DRAW_TO_SCREEN)
651 anim_classes_last = anim_classes_next;
652 anim_status_last_before_fading = global.anim_status;
655 anim_status_last = global.anim_status;
657 // start or stop animations determined to be started or stopped above
658 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
659 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
660 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
662 else if (drawing_target == DRAW_TO_FADE_TARGET)
664 drawing_to_fading_buffer = TRUE;
666 // start animations determined to be (temporary) started above
667 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
668 if (game_mode_anim_action[mode_nr] == ANIM_START)
669 HandleGlobalAnim(ANIM_START, mode_nr);
673 if (global.anim_status == GAME_MODE_LOADING)
676 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
678 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
681 // when preparing source fading buffer, only draw animations to be stopped
682 if (drawing_target == DRAW_TO_FADE_SOURCE &&
683 game_mode_anim_action[mode_nr] != ANIM_STOP)
686 // when preparing target fading buffer, only draw animations to be started
687 if (drawing_target == DRAW_TO_FADE_TARGET &&
688 game_mode_anim_action[mode_nr] != ANIM_START)
692 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
693 mode_nr != game_status)
697 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
699 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
700 struct GraphicInfo *c = &anim->control_info;
701 int part_first, part_last;
704 if (!(anim->state & ANIM_STATE_RUNNING))
707 part_first = part_last = anim->active_part_nr;
709 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
711 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
714 part_last = num_parts - 1;
717 for (part_nr = part_first; part_nr <= part_last; part_nr++)
719 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
720 struct GraphicInfo *g = &part->graphic_info;
723 int width = g->width;
724 int height = g->height;
731 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
732 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
733 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
734 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
736 if (!(part->state & ANIM_STATE_RUNNING))
739 if (part->drawing_stage != drawing_stage)
748 else if (part->x > part->viewport_width - g->width)
749 width -= (part->x - (part->viewport_width - g->width));
757 else if (part->y > part->viewport_height - g->height)
758 height -= (part->y - (part->viewport_height - g->height));
760 if (width <= 0 || height <= 0)
763 dst_x += part->viewport_x;
764 dst_y += part->viewport_y;
766 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
767 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
768 g->anim_mode, g->anim_start_frame,
771 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
777 if (drawing_target == DRAW_TO_SCREEN)
778 blit_screen(src_bitmap, src_x, src_y, width, height,
781 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
787 if (drawing_target == DRAW_TO_FADE_TARGET)
789 // stop animations determined to be (temporary) started above
790 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
791 if (game_mode_anim_action[mode_nr] == ANIM_START)
792 HandleGlobalAnim(ANIM_STOP, mode_nr);
794 drawing_to_fading_buffer = FALSE;
798 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
800 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
801 ResetGlobalAnim_Clickable();
803 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
805 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
806 ResetGlobalAnim_Clicked();
809 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
815 boolean changed = FALSE;
817 if (part->last_anim_status == global.anim_status)
820 part->last_anim_status = global.anim_status;
822 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
824 if (part->control_info.class == get_hash_from_key("window") ||
825 part->control_info.class == get_hash_from_key("border"))
829 viewport_width = WIN_XSIZE;
830 viewport_height = WIN_YSIZE;
832 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
834 else if (part->control_info.class == get_hash_from_key("door_1"))
838 viewport_width = DXSIZE;
839 viewport_height = DYSIZE;
841 else if (part->control_info.class == get_hash_from_key("door_2"))
845 viewport_width = VXSIZE;
846 viewport_height = VYSIZE;
848 else // default: "playfield"
850 viewport_x = REAL_SX;
851 viewport_y = REAL_SY;
852 viewport_width = FULL_SXSIZE;
853 viewport_height = FULL_SYSIZE;
856 if (viewport_x != part->viewport_x ||
857 viewport_y != part->viewport_y ||
858 viewport_width != part->viewport_width ||
859 viewport_height != part->viewport_height)
861 part->viewport_x = viewport_x;
862 part->viewport_y = viewport_y;
863 part->viewport_width = viewport_width;
864 part->viewport_height = viewport_height;
872 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
874 int sound = part->sound;
876 if (sound == SND_UNDEFINED)
879 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
880 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
883 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
884 if (IS_LOOP_SOUND(sound))
885 PlaySoundLoop(sound);
890 printf("::: PLAY SOUND %d.%d.%d: %d\n",
891 part->anim_nr, part->nr, part->mode_nr, sound);
895 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
897 int sound = part->sound;
899 if (sound == SND_UNDEFINED)
905 printf("::: STOP SOUND %d.%d.%d: %d\n",
906 part->anim_nr, part->nr, part->mode_nr, sound);
910 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
912 int music = part->music;
914 if (music == MUS_UNDEFINED)
917 if (!setup.sound_music)
923 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
924 part->anim_nr, part->nr, part->mode_nr, music);
928 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
930 int music = part->music;
932 if (music == MUS_UNDEFINED)
938 printf("::: STOP MUSIC %d.%d.%d: %d\n",
939 part->anim_nr, part->nr, part->mode_nr, music);
943 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
945 // when drawing animations to fading buffer, do not play sounds or music
946 if (drawing_to_fading_buffer)
949 PlayGlobalAnimSound(part);
950 PlayGlobalAnimMusic(part);
953 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
955 StopGlobalAnimSound(part);
956 StopGlobalAnimMusic(part);
959 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
961 struct GraphicInfo *c = &part->control_info;
962 int trigger_mask = ANIM_EVENT_ANIM_MASK | ANIM_EVENT_PART_MASK;
963 int mask_anim_only = mask & ANIM_EVENT_ANIM_MASK;
965 if (mask & ANIM_EVENT_ANY)
966 return (c->init_event & ANIM_EVENT_ANY ||
967 c->anim_event & ANIM_EVENT_ANY);
968 else if (mask & ANIM_EVENT_SELF)
969 return (c->init_event & ANIM_EVENT_SELF ||
970 c->anim_event & ANIM_EVENT_SELF);
972 return ((c->init_event & trigger_mask) == mask ||
973 (c->anim_event & trigger_mask) == mask ||
974 (c->init_event & trigger_mask) == mask_anim_only ||
975 (c->anim_event & trigger_mask) == mask_anim_only);
978 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
979 int mx, int my, boolean clicked)
981 struct GraphicInfo *g = &part->graphic_info;
982 int part_x = part->viewport_x + part->x;
983 int part_y = part->viewport_y + part->y;
984 int part_width = g->width;
985 int part_height = g->height;
987 // check if mouse click was detected at all
991 // check if mouse click is inside the animation part's viewport
992 if (mx < part->viewport_x ||
993 mx >= part->viewport_x + part->viewport_width ||
994 my < part->viewport_y ||
995 my >= part->viewport_y + part->viewport_height)
998 // check if mouse click is inside the animation part's graphic
1000 mx >= part_x + part_width ||
1002 my >= part_y + part_height)
1008 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1010 return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1013 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
1015 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1016 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1017 struct GraphicInfo *g = &part->graphic_info;
1018 struct GraphicInfo *c = &part->control_info;
1019 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1021 if (viewport_changed)
1022 state |= ANIM_STATE_RESTART;
1024 if (state & ANIM_STATE_RESTART)
1026 // when drawing animations to fading buffer, only start fixed animations
1027 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1028 c->y == ARG_UNDEFINED_VALUE))
1029 return ANIM_STATE_INACTIVE;
1031 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1033 part->init_delay_counter =
1034 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1036 part->anim_delay_counter =
1037 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1039 part->init_event_state = c->init_event;
1040 part->anim_event_state = c->anim_event;
1042 part->initial_anim_sync_frame =
1043 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1045 if (c->direction & MV_HORIZONTAL)
1047 int pos_bottom = part->viewport_height - g->height;
1049 if (c->position == POS_TOP)
1051 else if (c->position == POS_UPPER)
1052 part->y = GetSimpleRandom(pos_bottom / 2);
1053 else if (c->position == POS_MIDDLE)
1054 part->y = pos_bottom / 2;
1055 else if (c->position == POS_LOWER)
1056 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1057 else if (c->position == POS_BOTTOM)
1058 part->y = pos_bottom;
1060 part->y = GetSimpleRandom(pos_bottom);
1062 if (c->direction == MV_RIGHT)
1064 part->step_xoffset = c->step_offset;
1065 part->x = -g->width + part->step_xoffset;
1069 part->step_xoffset = -c->step_offset;
1070 part->x = part->viewport_width + part->step_xoffset;
1073 part->step_yoffset = 0;
1075 else if (c->direction & MV_VERTICAL)
1077 int pos_right = part->viewport_width - g->width;
1079 if (c->position == POS_LEFT)
1081 else if (c->position == POS_RIGHT)
1082 part->x = pos_right;
1084 part->x = GetSimpleRandom(pos_right);
1086 if (c->direction == MV_DOWN)
1088 part->step_yoffset = c->step_offset;
1089 part->y = -g->height + part->step_yoffset;
1093 part->step_yoffset = -c->step_offset;
1094 part->y = part->viewport_height + part->step_yoffset;
1097 part->step_xoffset = 0;
1104 part->step_xoffset = 0;
1105 part->step_yoffset = 0;
1108 if (c->x != ARG_UNDEFINED_VALUE)
1110 if (c->y != ARG_UNDEFINED_VALUE)
1113 if (c->position == POS_LAST &&
1114 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1115 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1117 part->x = anim->last_x;
1118 part->y = anim->last_y;
1121 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1122 part->step_xoffset = c->step_xoffset;
1123 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1124 part->step_yoffset = c->step_yoffset;
1126 if (part->init_delay_counter == 0 &&
1127 part->init_event_state == ANIM_EVENT_NONE)
1128 PlayGlobalAnimSoundAndMusic(part);
1131 if (part->clicked &&
1132 part->init_event_state != ANIM_EVENT_NONE)
1134 if (part->initial_anim_sync_frame > 0)
1135 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1137 part->init_delay_counter = 1;
1138 part->init_event_state = ANIM_EVENT_NONE;
1140 part->clicked = FALSE;
1143 if (part->clicked &&
1144 part->anim_event_state != ANIM_EVENT_NONE)
1146 part->anim_delay_counter = 1;
1147 part->anim_event_state = ANIM_EVENT_NONE;
1149 part->clicked = FALSE;
1152 if (part->init_delay_counter > 0)
1154 part->init_delay_counter--;
1156 if (part->init_delay_counter == 0)
1158 part->init_event_state = ANIM_EVENT_NONE;
1160 PlayGlobalAnimSoundAndMusic(part);
1163 return ANIM_STATE_WAITING;
1166 if (part->init_event_state != ANIM_EVENT_NONE)
1167 return ANIM_STATE_WAITING;
1169 // animation part is now running/visible and therefore clickable
1170 part->clickable = TRUE;
1172 // check if moving animation has left the visible screen area
1173 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1174 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1175 (part->y <= -g->height && part->step_yoffset <= 0) ||
1176 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1178 // do not wait for "anim" events for off-screen animations
1179 part->anim_event_state = ANIM_EVENT_NONE;
1181 // do not stop animation before "anim" or "post" counter are finished
1182 if (part->anim_delay_counter == 0 &&
1183 part->post_delay_counter == 0)
1185 StopGlobalAnimSoundAndMusic(part);
1187 part->post_delay_counter =
1188 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1190 if (part->post_delay_counter > 0)
1191 return ANIM_STATE_RUNNING;
1193 // drawing last frame not needed here -- animation not visible anymore
1194 return ANIM_STATE_RESTART;
1198 if (part->anim_delay_counter > 0)
1200 part->anim_delay_counter--;
1202 if (part->anim_delay_counter == 0)
1204 part->anim_event_state = ANIM_EVENT_NONE;
1206 StopGlobalAnimSoundAndMusic(part);
1208 part->post_delay_counter =
1209 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1211 if (part->post_delay_counter > 0)
1212 return ANIM_STATE_RUNNING;
1214 // additional state "RUNNING" required to not skip drawing last frame
1215 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1219 if (part->post_delay_counter > 0)
1221 part->post_delay_counter--;
1223 if (part->post_delay_counter == 0)
1224 return ANIM_STATE_RESTART;
1226 return ANIM_STATE_WAITING;
1229 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1231 return ANIM_STATE_RUNNING;
1235 static unsigned int last_counter = -1;
1236 unsigned int counter = Counter();
1238 printf("::: NEXT ANIM PART [%d, %d]\n",
1239 anim_sync_frame, counter - last_counter);
1241 last_counter = counter;
1245 part->x += part->step_xoffset;
1246 part->y += part->step_yoffset;
1248 anim->last_x = part->x;
1249 anim->last_y = part->y;
1251 return ANIM_STATE_RUNNING;
1254 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1256 struct GlobalAnimPartControlInfo *part;
1257 struct GraphicInfo *c = &anim->control_info;
1258 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1259 int state, active_part_nr;
1263 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1264 anim->mode_nr, anim->nr, anim->num_parts);
1265 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1269 printf("::: %s(%d): %d, %d, %d [%d]\n",
1270 (action == ANIM_START ? "ANIM_START" :
1271 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1272 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1274 anim->state & ANIM_STATE_RESTART,
1275 anim->state & ANIM_STATE_WAITING,
1276 anim->state & ANIM_STATE_RUNNING,
1283 anim->state = anim->last_state = ANIM_STATE_RESTART;
1284 anim->active_part_nr = anim->last_active_part_nr = 0;
1285 anim->part_counter = 0;
1290 if (anim->state == ANIM_STATE_INACTIVE)
1293 anim->state = anim->last_state;
1294 anim->active_part_nr = anim->last_active_part_nr;
1299 anim->state = ANIM_STATE_INACTIVE;
1301 for (i = 0; i < num_parts; i++)
1302 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1310 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1313 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1314 anim->mode_nr, anim->nr, num_parts);
1317 for (i = 0; i < num_parts; i++)
1319 part = &anim->part[i];
1324 anim->state = ANIM_STATE_RUNNING;
1325 part->state = ANIM_STATE_RESTART;
1330 if (part->state == ANIM_STATE_INACTIVE)
1336 part->state = ANIM_STATE_INACTIVE;
1344 part->state = HandleGlobalAnim_Part(part, part->state);
1346 // when animation mode is "once", stop after animation was played once
1347 if (c->anim_mode & ANIM_ONCE &&
1348 part->state & ANIM_STATE_RESTART)
1349 part->state = ANIM_STATE_INACTIVE;
1352 anim->last_state = anim->state;
1353 anim->last_active_part_nr = anim->active_part_nr;
1358 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1359 anim->active_part_nr = getGlobalAnimationPart(anim);
1361 part = &anim->part[anim->active_part_nr];
1363 // first set all animation parts to "inactive", ...
1364 for (i = 0; i < num_parts; i++)
1365 anim->part[i].state = ANIM_STATE_INACTIVE;
1367 // ... then set current animation parts to "running"
1368 part->state = ANIM_STATE_RUNNING;
1370 anim->state = HandleGlobalAnim_Part(part, anim->state);
1372 if (anim->state & ANIM_STATE_RESTART)
1373 anim->part_counter++;
1375 // when animation mode is "once", stop after all animations were played once
1376 if (c->anim_mode & ANIM_ONCE &&
1377 anim->part_counter == anim->num_parts)
1378 anim->state = ANIM_STATE_INACTIVE;
1380 state = anim->state;
1381 active_part_nr = anim->active_part_nr;
1383 // while the animation parts are pausing (waiting or inactive), play the base
1384 // (main) animation; this corresponds to the "boring player animation" logic
1385 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1388 if (anim->state == ANIM_STATE_WAITING ||
1389 anim->state == ANIM_STATE_INACTIVE)
1391 anim->active_part_nr = anim->num_parts; // part nr of base animation
1392 part = &anim->part[anim->active_part_nr];
1394 if (anim->state != anim->last_state)
1395 part->state = ANIM_STATE_RESTART;
1397 anim->state = ANIM_STATE_RUNNING;
1398 part->state = HandleGlobalAnim_Part(part, part->state);
1402 anim->last_state = state;
1403 anim->last_active_part_nr = active_part_nr;
1406 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1411 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1412 ctrl->nr, ctrl->num_anims);
1415 for (i = 0; i < ctrl->num_anims; i++)
1416 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1419 static void HandleGlobalAnim(int action, int game_mode)
1422 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1425 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1428 static void DoAnimationExt()
1433 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1436 // global animations now synchronized with frame delay of screen update
1439 for (i = 0; i < NUM_GAME_MODES; i++)
1440 HandleGlobalAnim(ANIM_CONTINUE, i);
1443 // force screen redraw in next frame to continue drawing global animations
1444 redraw_mask = REDRAW_ALL;
1448 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1450 int anim_event_action = part->control_info.anim_event_action;
1452 if (anim_event_action == -1)
1455 boolean action_executed = (DoGadgetAction(anim_event_action) ||
1456 DoScreenAction(anim_event_action) ||
1457 DoKeysymAction(anim_event_action));
1459 // check if further actions are allowed to be executed
1460 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1463 return action_executed;
1466 static void InitGlobalAnim_Clickable()
1470 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1472 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1475 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1477 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1480 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1482 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1484 part->clickable = FALSE;
1490 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1492 boolean anything_clicked = FALSE;
1493 boolean any_part_clicked = FALSE;
1494 boolean any_event_action = FALSE;
1497 // check game modes in reverse draw order (to stop when clicked)
1498 for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1500 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1503 // check animations in reverse draw order (to stop when clicked)
1504 for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1506 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1509 // check animation parts in reverse draw order (to stop when clicked)
1510 for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1512 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1516 part->clicked = FALSE;
1521 if (!part->clickable)
1524 if (part->state != ANIM_STATE_RUNNING)
1527 // always handle "any" click events (clicking anywhere on screen) ...
1528 if (isClickablePart(part, ANIM_EVENT_ANY))
1530 part->clicked = TRUE;
1531 anything_clicked = clickConsumed(part);
1534 // ... but only handle the first (topmost) clickable animation
1535 if (any_part_clicked)
1538 if (isClickedPart(part, mx, my, clicked))
1541 printf("::: %d.%d CLICKED [%d]\n", anim_nr, part_nr,
1542 part->control_info.anim_event_action);
1545 // after executing event action, ignore any further actions
1546 if (!any_event_action && DoGlobalAnim_EventAction(part))
1547 any_event_action = TRUE;
1549 // determine if mouse clicks should be blocked from other animations
1550 any_part_clicked = clickConsumed(part);
1552 if (isClickablePart(part, ANIM_EVENT_SELF))
1554 part->clicked = TRUE;
1555 anything_clicked = clickConsumed(part);
1558 // check if this click is defined to trigger other animations
1559 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1560 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1561 int mask = gic_anim_nr << ANIM_EVENT_ANIM_BIT;
1564 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1568 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1570 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1573 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1575 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1577 if (part2->state != ANIM_STATE_RUNNING)
1580 if (isClickablePart(part2, mask))
1582 part2->clicked = TRUE;
1583 anything_clicked = clickConsumed(part); // click was on "part"!
1586 printf("::: %d.%d TRIGGER CLICKED [%d]\n", anim2_nr, part2_nr,
1587 part2->control_info.anim_event_action);
1590 // after executing event action, ignore any further actions
1591 if (!any_event_action && DoGlobalAnim_EventAction(part2))
1592 any_event_action = TRUE;
1596 struct GraphicInfo *c = &part2->control_info;
1598 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1599 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1601 if (isClickablePart(part2, mask))
1602 printf(" <--- TRIGGERED BY %d.%d",
1614 return (anything_clicked || any_event_action);
1617 static void ResetGlobalAnim_Clickable()
1619 InitGlobalAnim_Clickable();
1622 static void ResetGlobalAnim_Clicked()
1624 InitGlobalAnim_Clicked(-1, -1, FALSE);
1627 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1629 static boolean click_consumed = FALSE;
1630 static int last_button = 0;
1631 boolean press_event;
1632 boolean release_event;
1633 boolean click_consumed_current = click_consumed;
1635 /* check if button state has changed since last invocation */
1636 press_event = (button != 0 && last_button == 0);
1637 release_event = (button == 0 && last_button != 0);
1638 last_button = button;
1642 click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1643 click_consumed_current = click_consumed;
1647 click_consumed = FALSE;
1649 return click_consumed_current;