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"
19 /* values for global toon animation definition */
20 #define NUM_GLOBAL_TOON_ANIMS 1
21 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
23 /* values for global animation definition (including toons) */
24 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
25 NUM_GLOBAL_TOON_ANIMS)
26 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
27 NUM_GLOBAL_TOON_PARTS)
29 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
30 #define ANIM_CLASS_BIT_TITLE 1
31 #define ANIM_CLASS_BIT_MAIN 2
32 #define ANIM_CLASS_BIT_SCORES 3
33 #define ANIM_CLASS_BIT_SUBMENU 4
34 #define ANIM_CLASS_BIT_MENU 5
35 #define ANIM_CLASS_BIT_TOONS 6
37 #define NUM_ANIM_CLASSES 7
39 #define ANIM_CLASS_NONE 0
40 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
41 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
42 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
43 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
44 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
45 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
46 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
48 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
51 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
55 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
59 /* values for global animation states */
60 #define ANIM_STATE_INACTIVE 0
61 #define ANIM_STATE_RESTART (1 << 0)
62 #define ANIM_STATE_WAITING (1 << 1)
63 #define ANIM_STATE_RUNNING (1 << 2)
65 /* values for global animation control */
66 #define ANIM_NO_ACTION 0
68 #define ANIM_CONTINUE 2
72 struct GlobalAnimPartControlInfo
74 int old_nr; // position before mapping animation parts linearly
75 int old_anim_nr; // position before mapping animations linearly
81 boolean is_base; // animation part is base/main/default animation part
87 struct GraphicInfo graphic_info;
88 struct GraphicInfo control_info;
96 int step_xoffset, step_yoffset;
98 unsigned int initial_anim_sync_frame;
99 unsigned int step_delay, step_delay_value;
101 int init_delay_counter;
102 int anim_delay_counter;
103 int post_delay_counter;
105 boolean init_event_state;
106 boolean anim_event_state;
114 int last_anim_status;
117 struct GlobalAnimMainControlInfo
119 struct GlobalAnimPartControlInfo base;
120 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
125 struct GraphicInfo control_info;
127 int num_parts; // number of animation parts, but without base part
128 int num_parts_all; // number of animation parts, including base part
132 boolean has_base; // animation has base/main/default animation part
136 int init_delay_counter;
140 int last_state, last_active_part_nr;
143 struct GlobalAnimControlInfo
145 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
151 struct GameModeAnimClass
155 } game_mode_anim_classes_list[] =
157 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
158 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
159 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
160 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
161 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
162 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
163 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
164 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
165 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
166 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
167 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
168 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
169 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
170 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
171 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
172 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
173 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORES },
174 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORES },
179 struct AnimClassGameMode
183 } anim_class_game_modes_list[] =
185 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
186 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
187 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
188 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
189 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
190 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
191 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
196 /* forward declaration for internal use */
197 static void HandleGlobalAnim(int, int);
198 static void DoAnimationExt(void);
199 static void ResetGlobalAnim_Clickable();
200 static void ResetGlobalAnim_Clicked();
202 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
204 static unsigned int anim_sync_frame = 0;
206 static int game_mode_anim_classes[NUM_GAME_MODES];
207 static int anim_class_game_modes[NUM_ANIM_CLASSES];
209 static int anim_status_last = GAME_MODE_DEFAULT;
210 static int anim_classes_last = ANIM_CLASS_NONE;
212 static boolean drawing_to_fading_buffer = FALSE;
215 /* ========================================================================= */
216 /* generic animation frame calculation */
217 /* ========================================================================= */
219 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
224 sync_frame += start_frame * delay;
226 if (mode & ANIM_LOOP) /* looping animation */
228 frame = (sync_frame % (delay * num_frames)) / delay;
230 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
232 frame = sync_frame / delay;
234 if (frame > num_frames - 1)
235 frame = num_frames - 1;
237 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
239 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
241 frame = (sync_frame % (delay * max_anim_frames)) / delay;
242 frame = (frame < num_frames ? frame : max_anim_frames - frame);
244 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
246 int max_anim_frames = 2 * num_frames;
248 frame = (sync_frame % (delay * max_anim_frames)) / delay;
249 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
251 else if (mode & ANIM_RANDOM) /* play frames in random order */
253 /* note: expect different frames for the same delay cycle! */
255 if (gfx.anim_random_frame < 0)
256 frame = GetSimpleRandom(num_frames);
258 frame = gfx.anim_random_frame % num_frames;
260 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
262 frame = sync_frame % num_frames;
265 if (mode & ANIM_REVERSE) /* use reverse animation direction */
266 frame = num_frames - frame - 1;
272 /* ========================================================================= */
273 /* global animation functions */
274 /* ========================================================================= */
276 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
278 struct GraphicInfo *c = &anim->control_info;
279 int last_anim_random_frame = gfx.anim_random_frame;
282 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
284 part_nr = getAnimationFrame(anim->num_parts, 1,
285 c->anim_mode, c->anim_start_frame,
288 gfx.anim_random_frame = last_anim_random_frame;
293 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
295 const struct GlobalAnimPartControlInfo *o1 =
296 (struct GlobalAnimPartControlInfo *)obj1;
297 const struct GlobalAnimPartControlInfo *o2 =
298 (struct GlobalAnimPartControlInfo *)obj2;
301 if (o1->control_info.draw_order != o2->control_info.draw_order)
302 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
304 compare_result = o1->nr - o2->nr;
306 return compare_result;
309 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
311 const struct GlobalAnimMainControlInfo *o1 =
312 (struct GlobalAnimMainControlInfo *)obj1;
313 const struct GlobalAnimMainControlInfo *o2 =
314 (struct GlobalAnimMainControlInfo *)obj2;
317 if (o1->control_info.draw_order != o2->control_info.draw_order)
318 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
320 compare_result = o1->nr - o2->nr;
322 return compare_result;
325 static void InitToonControls()
327 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
328 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
329 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
330 int mode_nr, anim_nr, part_nr;
331 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
332 int num_toons = MAX_NUM_TOONS;
335 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
336 num_toons = global.num_toons;
338 mode_nr = mode_nr_toons;
339 anim_nr = ctrl->num_anims;
342 anim->mode_nr = mode_nr;
343 anim->control_info = graphic_info[control];
346 anim->num_parts_all = 0;
347 anim->part_counter = 0;
348 anim->active_part_nr = 0;
350 anim->has_base = FALSE;
352 anim->last_x = POS_OFFSCREEN;
353 anim->last_y = POS_OFFSCREEN;
355 anim->init_delay_counter = 0;
357 anim->state = ANIM_STATE_INACTIVE;
361 for (i = 0; i < num_toons; i++)
363 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
364 int sound = SND_UNDEFINED;
365 int music = MUS_UNDEFINED;
366 int graphic = IMG_TOON_1 + i;
367 int control = graphic;
370 part->anim_nr = anim_nr;
371 part->mode_nr = mode_nr;
373 part->is_base = FALSE;
377 part->graphic = graphic;
379 part->graphic_info = graphic_info[graphic];
380 part->control_info = graphic_info[control];
382 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
384 part->control_info.init_delay_fixed = 0;
385 part->control_info.init_delay_random = 150;
387 part->control_info.x = ARG_UNDEFINED_VALUE;
388 part->control_info.y = ARG_UNDEFINED_VALUE;
390 part->initial_anim_sync_frame = 0;
392 part->step_delay = 0;
393 part->step_delay_value = graphic_info[control].step_delay;
395 part->state = ANIM_STATE_INACTIVE;
396 part->last_anim_status = -1;
399 anim->num_parts_all++;
407 void InitGlobalAnimControls()
410 int mode_nr, anim_nr, part_nr;
411 int sound, music, graphic, control;
415 for (m = 0; m < NUM_GAME_MODES; m++)
419 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
426 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
428 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
429 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
431 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
433 // if no base animation parameters defined, use default values
434 if (control == IMG_UNDEFINED)
435 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
438 anim->mode_nr = mode_nr;
439 anim->control_info = graphic_info[control];
442 anim->num_parts_all = 0;
443 anim->part_counter = 0;
444 anim->active_part_nr = 0;
446 anim->has_base = FALSE;
448 anim->last_x = POS_OFFSCREEN;
449 anim->last_y = POS_OFFSCREEN;
451 anim->init_delay_counter = 0;
453 anim->state = ANIM_STATE_INACTIVE;
457 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
459 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
461 sound = global_anim_info[a].sound[p][m];
462 music = global_anim_info[a].music[p][m];
463 graphic = global_anim_info[a].graphic[p][m];
464 control = global_anim_info[ctrl_id].graphic[p][m];
466 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
467 control == IMG_UNDEFINED)
471 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
472 m, a, p, mode_nr, anim_nr, part_nr, control);
476 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
477 m, a, p, mode_nr, anim_nr, part_nr, sound);
481 part->old_anim_nr = a;
484 part->anim_nr = anim_nr;
485 part->mode_nr = mode_nr;
489 part->graphic = graphic;
491 part->graphic_info = graphic_info[graphic];
492 part->control_info = graphic_info[control];
494 part->initial_anim_sync_frame = 0;
496 part->step_delay = 0;
497 part->step_delay_value = graphic_info[control].step_delay;
499 part->state = ANIM_STATE_INACTIVE;
500 part->last_anim_status = -1;
502 anim->num_parts_all++;
504 if (p < GLOBAL_ANIM_ID_PART_BASE)
506 part->is_base = FALSE;
513 part->is_base = TRUE;
516 anim->has_base = TRUE;
520 if (anim->num_parts > 0 || anim->has_base)
530 /* sort all animations according to draw_order and animation number */
531 for (m = 0; m < NUM_GAME_MODES; m++)
533 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
535 /* sort all main animations for this game mode */
536 qsort(ctrl->anim, ctrl->num_anims,
537 sizeof(struct GlobalAnimMainControlInfo),
538 compareGlobalAnimMainControlInfo);
540 for (a = 0; a < ctrl->num_anims; a++)
542 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
544 /* sort all animation parts for this main animation */
545 qsort(anim->part, anim->num_parts,
546 sizeof(struct GlobalAnimPartControlInfo),
547 compareGlobalAnimPartControlInfo);
551 for (i = 0; i < NUM_GAME_MODES; i++)
552 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
553 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
554 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
555 game_mode_anim_classes_list[i].class;
557 for (i = 0; i < NUM_ANIM_CLASSES; i++)
558 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
559 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
560 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
561 anim_class_game_modes_list[i].game_mode;
563 anim_status_last = GAME_MODE_LOADING;
564 anim_classes_last = ANIM_CLASS_NONE;
567 void InitGlobalAnimations()
569 InitGlobalAnimControls();
572 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
574 Bitmap *fade_bitmap =
575 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
576 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
577 int game_mode_anim_action[NUM_GAME_MODES];
583 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
584 drawing_target == DRAW_TO_SCREEN)
587 // always start with reliable default values (no animation actions)
588 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
589 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
591 if (global.anim_status != anim_status_last)
593 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
594 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
595 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
598 if (drawing_target == DRAW_TO_FADE_TARGET)
601 // ---------- part 1 ------------------------------------------------------
602 // start or stop global animations by change of game mode
603 // (special handling of animations for "current screen" and "all screens")
605 // stop animations for last screen
606 game_mode_anim_action[anim_status_last] = ANIM_STOP;
608 // start animations for current screen
609 game_mode_anim_action[global.anim_status] = ANIM_START;
611 // start animations for all screens after loading new artwork set
612 if (anim_status_last == GAME_MODE_LOADING)
613 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
615 // ---------- part 2 ------------------------------------------------------
616 // start or stop global animations by change of animation class
617 // (generic handling of animations for "class of screens")
619 for (i = 0; i < NUM_ANIM_CLASSES; i++)
621 int anim_class_check = (1 << i);
622 int anim_class_game_mode = anim_class_game_modes[i];
623 int anim_class_last = anim_classes_last & anim_class_check;
624 int anim_class_next = anim_classes_next & anim_class_check;
626 // stop animations for changed screen class before fading to new screen
627 if (before_fading && anim_class_last && !anim_class_next)
628 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
630 // start animations for changed screen class after fading to new screen
631 if (after_fading && !anim_class_last && anim_class_next)
632 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
635 if (drawing_target == DRAW_TO_SCREEN)
638 anim_classes_last = anim_classes_next;
640 anim_status_last = global.anim_status;
642 // start or stop animations determined to be started or stopped above
643 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
644 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
645 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
647 else if (drawing_target == DRAW_TO_FADE_TARGET)
649 drawing_to_fading_buffer = TRUE;
651 // start animations determined to be (temporary) started above
652 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
653 if (game_mode_anim_action[mode_nr] == ANIM_START)
654 HandleGlobalAnim(ANIM_START, mode_nr);
658 if (global.anim_status == GAME_MODE_LOADING)
661 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
663 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
666 // when preparing source fading buffer, only draw animations to be stopped
667 if (drawing_target == DRAW_TO_FADE_SOURCE &&
668 game_mode_anim_action[mode_nr] != ANIM_STOP)
671 // when preparing target fading buffer, only draw animations to be started
672 if (drawing_target == DRAW_TO_FADE_TARGET &&
673 game_mode_anim_action[mode_nr] != ANIM_START)
677 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
678 mode_nr != game_status)
682 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
684 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
685 struct GraphicInfo *c = &anim->control_info;
686 int part_first, part_last;
689 if (!(anim->state & ANIM_STATE_RUNNING))
692 part_first = part_last = anim->active_part_nr;
694 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
696 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
699 part_last = num_parts - 1;
702 for (part_nr = part_first; part_nr <= part_last; part_nr++)
704 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
705 struct GraphicInfo *g = &part->graphic_info;
708 int width = g->width;
709 int height = g->height;
716 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
717 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
718 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
719 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
721 if (!(part->state & ANIM_STATE_RUNNING))
724 if (part->drawing_stage != drawing_stage)
733 else if (part->x > part->viewport_width - g->width)
734 width -= (part->x - (part->viewport_width - g->width));
742 else if (part->y > part->viewport_height - g->height)
743 height -= (part->y - (part->viewport_height - g->height));
745 if (width <= 0 || height <= 0)
748 dst_x += part->viewport_x;
749 dst_y += part->viewport_y;
751 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
752 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
753 g->anim_mode, g->anim_start_frame,
756 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
762 if (drawing_target == DRAW_TO_SCREEN)
763 blit_screen(src_bitmap, src_x, src_y, width, height,
766 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
772 if (drawing_target == DRAW_TO_FADE_TARGET)
774 // stop animations determined to be (temporary) started above
775 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
776 if (game_mode_anim_action[mode_nr] == ANIM_START)
777 HandleGlobalAnim(ANIM_STOP, mode_nr);
779 drawing_to_fading_buffer = FALSE;
783 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
785 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
786 ResetGlobalAnim_Clickable();
788 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
790 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
791 ResetGlobalAnim_Clicked();
794 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
800 boolean changed = FALSE;
802 if (part->last_anim_status == global.anim_status)
805 part->last_anim_status = global.anim_status;
807 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
809 if (part->control_info.class == get_hash_from_key("window") ||
810 part->control_info.class == get_hash_from_key("border"))
814 viewport_width = WIN_XSIZE;
815 viewport_height = WIN_YSIZE;
817 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
819 else if (part->control_info.class == get_hash_from_key("door_1"))
823 viewport_width = DXSIZE;
824 viewport_height = DYSIZE;
826 else if (part->control_info.class == get_hash_from_key("door_2"))
830 viewport_width = VXSIZE;
831 viewport_height = VYSIZE;
833 else // default: "playfield"
835 viewport_x = REAL_SX;
836 viewport_y = REAL_SY;
837 viewport_width = FULL_SXSIZE;
838 viewport_height = FULL_SYSIZE;
841 if (viewport_x != part->viewport_x ||
842 viewport_y != part->viewport_y ||
843 viewport_width != part->viewport_width ||
844 viewport_height != part->viewport_height)
846 part->viewport_x = viewport_x;
847 part->viewport_y = viewport_y;
848 part->viewport_width = viewport_width;
849 part->viewport_height = viewport_height;
857 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
859 int sound = part->sound;
861 if (sound == SND_UNDEFINED)
864 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
865 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
868 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
869 if (IS_LOOP_SOUND(sound))
870 PlaySoundLoop(sound);
875 printf("::: PLAY SOUND %d.%d.%d: %d\n",
876 part->anim_nr, part->nr, part->mode_nr, sound);
880 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
882 int sound = part->sound;
884 if (sound == SND_UNDEFINED)
890 printf("::: STOP SOUND %d.%d.%d: %d\n",
891 part->anim_nr, part->nr, part->mode_nr, sound);
895 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
897 int music = part->music;
899 if (music == MUS_UNDEFINED)
902 if (!setup.sound_music)
908 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
909 part->anim_nr, part->nr, part->mode_nr, music);
913 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
915 int music = part->music;
917 if (music == MUS_UNDEFINED)
923 printf("::: STOP MUSIC %d.%d.%d: %d\n",
924 part->anim_nr, part->nr, part->mode_nr, music);
928 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
930 // when drawing animations to fading buffer, do not play sounds or music
931 if (drawing_to_fading_buffer)
934 PlayGlobalAnimSound(part);
935 PlayGlobalAnimMusic(part);
938 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
940 StopGlobalAnimSound(part);
941 StopGlobalAnimMusic(part);
944 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
946 struct GraphicInfo *c = &part->control_info;
947 int trigger_mask = ANIM_EVENT_ANIM_MASK | ANIM_EVENT_PART_MASK;
948 int mask_anim_only = mask & ANIM_EVENT_ANIM_MASK;
950 if (mask & ANIM_EVENT_ANY)
951 return (c->init_event & ANIM_EVENT_ANY ||
952 c->anim_event & ANIM_EVENT_ANY);
953 else if (mask & ANIM_EVENT_SELF)
954 return (c->init_event & ANIM_EVENT_SELF ||
955 c->anim_event & ANIM_EVENT_SELF);
957 return ((c->init_event & trigger_mask) == mask ||
958 (c->anim_event & trigger_mask) == mask ||
959 (c->init_event & trigger_mask) == mask_anim_only ||
960 (c->anim_event & trigger_mask) == mask_anim_only);
963 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
964 int mx, int my, boolean clicked)
966 struct GraphicInfo *g = &part->graphic_info;
967 int part_x = part->viewport_x + part->x;
968 int part_y = part->viewport_y + part->y;
969 int part_width = g->width;
970 int part_height = g->height;
972 // check if mouse click was detected at all
976 // check if mouse click is inside the animation part's viewport
977 if (mx < part->viewport_x ||
978 mx >= part->viewport_x + part->viewport_width ||
979 my < part->viewport_y ||
980 my >= part->viewport_y + part->viewport_height)
983 // check if mouse click is inside the animation part's graphic
985 mx >= part_x + part_width ||
987 my >= part_y + part_height)
993 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
995 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
996 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
997 struct GraphicInfo *g = &part->graphic_info;
998 struct GraphicInfo *c = &part->control_info;
999 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1001 if (viewport_changed)
1002 state |= ANIM_STATE_RESTART;
1004 if (state & ANIM_STATE_RESTART)
1006 // when drawing animations to fading buffer, only start fixed animations
1007 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1008 c->y == ARG_UNDEFINED_VALUE))
1009 return ANIM_STATE_INACTIVE;
1011 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1013 part->init_delay_counter =
1014 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1016 part->anim_delay_counter =
1017 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1019 part->init_event_state = c->init_event;
1020 part->anim_event_state = c->anim_event;
1022 part->initial_anim_sync_frame =
1023 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1025 if (c->direction & MV_HORIZONTAL)
1027 int pos_bottom = part->viewport_height - g->height;
1029 if (c->position == POS_TOP)
1031 else if (c->position == POS_UPPER)
1032 part->y = GetSimpleRandom(pos_bottom / 2);
1033 else if (c->position == POS_MIDDLE)
1034 part->y = pos_bottom / 2;
1035 else if (c->position == POS_LOWER)
1036 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1037 else if (c->position == POS_BOTTOM)
1038 part->y = pos_bottom;
1040 part->y = GetSimpleRandom(pos_bottom);
1042 if (c->direction == MV_RIGHT)
1044 part->step_xoffset = c->step_offset;
1045 part->x = -g->width + part->step_xoffset;
1049 part->step_xoffset = -c->step_offset;
1050 part->x = part->viewport_width + part->step_xoffset;
1053 part->step_yoffset = 0;
1055 else if (c->direction & MV_VERTICAL)
1057 int pos_right = part->viewport_width - g->width;
1059 if (c->position == POS_LEFT)
1061 else if (c->position == POS_RIGHT)
1062 part->x = pos_right;
1064 part->x = GetSimpleRandom(pos_right);
1066 if (c->direction == MV_DOWN)
1068 part->step_yoffset = c->step_offset;
1069 part->y = -g->height + part->step_yoffset;
1073 part->step_yoffset = -c->step_offset;
1074 part->y = part->viewport_height + part->step_yoffset;
1077 part->step_xoffset = 0;
1084 part->step_xoffset = 0;
1085 part->step_yoffset = 0;
1088 if (c->x != ARG_UNDEFINED_VALUE)
1090 if (c->y != ARG_UNDEFINED_VALUE)
1093 if (c->position == POS_LAST &&
1094 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1095 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1097 part->x = anim->last_x;
1098 part->y = anim->last_y;
1101 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1102 part->step_xoffset = c->step_xoffset;
1103 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1104 part->step_yoffset = c->step_yoffset;
1106 if (part->init_delay_counter == 0 &&
1107 part->init_event_state == ANIM_EVENT_NONE)
1108 PlayGlobalAnimSoundAndMusic(part);
1111 if (part->clicked &&
1112 part->init_event_state != ANIM_EVENT_NONE)
1114 if (part->initial_anim_sync_frame > 0)
1115 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1117 part->init_delay_counter = 1;
1118 part->init_event_state = ANIM_EVENT_NONE;
1120 part->clicked = FALSE;
1123 if (part->clicked &&
1124 part->anim_event_state != ANIM_EVENT_NONE)
1126 part->anim_delay_counter = 1;
1127 part->anim_event_state = ANIM_EVENT_NONE;
1129 part->clicked = FALSE;
1132 if (part->init_delay_counter > 0)
1134 part->init_delay_counter--;
1136 if (part->init_delay_counter == 0)
1138 part->init_event_state = ANIM_EVENT_NONE;
1140 PlayGlobalAnimSoundAndMusic(part);
1143 return ANIM_STATE_WAITING;
1146 if (part->init_event_state != ANIM_EVENT_NONE)
1147 return ANIM_STATE_WAITING;
1149 // animation part is now running/visible and therefore clickable
1150 part->clickable = TRUE;
1152 // check if moving animation has left the visible screen area
1153 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1154 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1155 (part->y <= -g->height && part->step_yoffset <= 0) ||
1156 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1158 // do not wait for "anim" events for off-screen animations
1159 part->anim_event_state = ANIM_EVENT_NONE;
1161 // do not stop animation before "anim" or "post" counter are finished
1162 if (part->anim_delay_counter == 0 &&
1163 part->post_delay_counter == 0)
1165 StopGlobalAnimSoundAndMusic(part);
1167 part->post_delay_counter =
1168 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1170 if (part->post_delay_counter > 0)
1171 return ANIM_STATE_RUNNING;
1173 // drawing last frame not needed here -- animation not visible anymore
1174 return ANIM_STATE_RESTART;
1178 if (part->anim_delay_counter > 0)
1180 part->anim_delay_counter--;
1182 if (part->anim_delay_counter == 0)
1184 part->anim_event_state = ANIM_EVENT_NONE;
1186 StopGlobalAnimSoundAndMusic(part);
1188 part->post_delay_counter =
1189 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1191 if (part->post_delay_counter > 0)
1192 return ANIM_STATE_RUNNING;
1194 // additional state "RUNNING" required to not skip drawing last frame
1195 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1199 if (part->post_delay_counter > 0)
1201 part->post_delay_counter--;
1203 if (part->post_delay_counter == 0)
1204 return ANIM_STATE_RESTART;
1206 return ANIM_STATE_WAITING;
1209 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1211 return ANIM_STATE_RUNNING;
1215 static unsigned int last_counter = -1;
1216 unsigned int counter = Counter();
1218 printf("::: NEXT ANIM PART [%d, %d]\n",
1219 anim_sync_frame, counter - last_counter);
1221 last_counter = counter;
1225 part->x += part->step_xoffset;
1226 part->y += part->step_yoffset;
1228 anim->last_x = part->x;
1229 anim->last_y = part->y;
1231 return ANIM_STATE_RUNNING;
1234 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1236 struct GlobalAnimPartControlInfo *part;
1237 struct GraphicInfo *c = &anim->control_info;
1238 int state, active_part_nr;
1241 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1242 anim->mode_nr, anim->nr, anim->num_parts);
1243 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1247 printf("::: %s(%d): %d, %d, %d [%d]\n",
1248 (action == ANIM_START ? "ANIM_START" :
1249 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1250 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1252 anim->state & ANIM_STATE_RESTART,
1253 anim->state & ANIM_STATE_WAITING,
1254 anim->state & ANIM_STATE_RUNNING,
1261 anim->state = anim->last_state = ANIM_STATE_RESTART;
1262 anim->active_part_nr = anim->last_active_part_nr = 0;
1263 anim->part_counter = 0;
1268 if (anim->state == ANIM_STATE_INACTIVE)
1271 anim->state = anim->last_state;
1272 anim->active_part_nr = anim->last_active_part_nr;
1277 anim->state = ANIM_STATE_INACTIVE;
1280 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1283 for (i = 0; i < num_parts; i++)
1284 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1293 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1295 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1299 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1300 anim->mode_nr, anim->nr, num_parts);
1303 for (i = 0; i < num_parts; i++)
1305 part = &anim->part[i];
1310 anim->state = ANIM_STATE_RUNNING;
1311 part->state = ANIM_STATE_RESTART;
1316 if (part->state == ANIM_STATE_INACTIVE)
1322 part->state = ANIM_STATE_INACTIVE;
1330 part->state = HandleGlobalAnim_Part(part, part->state);
1332 // when animation mode is "once", stop after animation was played once
1333 if (c->anim_mode & ANIM_ONCE &&
1334 part->state & ANIM_STATE_RESTART)
1335 part->state = ANIM_STATE_INACTIVE;
1338 anim->last_state = anim->state;
1339 anim->last_active_part_nr = anim->active_part_nr;
1344 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1345 anim->active_part_nr = getGlobalAnimationPart(anim);
1347 part = &anim->part[anim->active_part_nr];
1349 part->state = ANIM_STATE_RUNNING;
1351 anim->state = HandleGlobalAnim_Part(part, anim->state);
1353 if (anim->state & ANIM_STATE_RESTART)
1354 anim->part_counter++;
1356 // when animation mode is "once", stop after all animations were played once
1357 if (c->anim_mode & ANIM_ONCE &&
1358 anim->part_counter == anim->num_parts)
1359 anim->state = ANIM_STATE_INACTIVE;
1361 state = anim->state;
1362 active_part_nr = anim->active_part_nr;
1364 // while the animation parts are pausing (waiting or inactive), play the base
1365 // (main) animation; this corresponds to the "boring player animation" logic
1366 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1369 if (anim->state == ANIM_STATE_WAITING ||
1370 anim->state == ANIM_STATE_INACTIVE)
1372 anim->active_part_nr = anim->num_parts; // part nr of base animation
1373 part = &anim->part[anim->active_part_nr];
1375 if (anim->state != anim->last_state)
1376 part->state = ANIM_STATE_RESTART;
1378 anim->state = ANIM_STATE_RUNNING;
1379 part->state = HandleGlobalAnim_Part(part, part->state);
1383 anim->last_state = state;
1384 anim->last_active_part_nr = active_part_nr;
1387 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1392 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1393 ctrl->nr, ctrl->num_anims);
1396 for (i = 0; i < ctrl->num_anims; i++)
1397 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1400 static void HandleGlobalAnim(int action, int game_mode)
1403 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1406 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1409 static void DoAnimationExt()
1414 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1417 // global animations now synchronized with frame delay of screen update
1420 for (i = 0; i < NUM_GAME_MODES; i++)
1421 HandleGlobalAnim(ANIM_CONTINUE, i);
1424 // force screen redraw in next frame to continue drawing global animations
1425 redraw_mask = REDRAW_ALL;
1429 static void InitGlobalAnim_Clickable()
1433 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1435 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1438 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1440 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1443 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1445 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1447 part->clickable = FALSE;
1453 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1455 boolean any_part_clicked = FALSE;
1458 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1460 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1463 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1465 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1468 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1470 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1474 part->clicked = FALSE;
1479 if (!part->clickable)
1482 if (isClickablePart(part, ANIM_EVENT_ANY))
1483 any_part_clicked = part->clicked = TRUE;
1485 if (isClickedPart(part, mx, my, clicked))
1488 printf("::: %d.%d CLICKED\n", anim_nr, part_nr);
1491 if (isClickablePart(part, ANIM_EVENT_SELF))
1492 any_part_clicked = part->clicked = TRUE;
1494 // check if this click is defined to trigger other animations
1495 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1496 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1497 int mask = gic_anim_nr << ANIM_EVENT_ANIM_BIT;
1500 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1504 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1506 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1509 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1511 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1513 if (isClickablePart(part2, mask))
1514 any_part_clicked = part2->clicked = TRUE;
1517 struct GraphicInfo *c = &part2->control_info;
1519 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1520 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1522 if (isClickablePart(part2, mask))
1523 printf(" <--- TRIGGERED BY %d.%d",
1535 return any_part_clicked;
1538 static void ResetGlobalAnim_Clickable()
1540 InitGlobalAnim_Clickable();
1543 static void ResetGlobalAnim_Clicked()
1545 InitGlobalAnim_Clicked(-1, -1, FALSE);
1548 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1550 static boolean click_consumed = FALSE;
1551 static int last_button = 0;
1552 boolean press_event;
1553 boolean release_event;
1554 boolean click_consumed_current = click_consumed;
1556 /* check if button state has changed since last invocation */
1557 press_event = (button != 0 && last_button == 0);
1558 release_event = (button == 0 && last_button != 0);
1559 last_button = button;
1563 click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1564 click_consumed_current = click_consumed;
1568 click_consumed = FALSE;
1570 return click_consumed_current;