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_SUBMENU 3
33 #define ANIM_CLASS_BIT_MENU 4
34 #define ANIM_CLASS_BIT_TOONS 5
36 #define NUM_ANIM_CLASSES 6
38 #define ANIM_CLASS_NONE 0
39 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
40 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
41 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
42 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
43 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
44 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
46 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
50 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
54 /* values for global animation states */
55 #define ANIM_STATE_INACTIVE 0
56 #define ANIM_STATE_RESTART (1 << 0)
57 #define ANIM_STATE_WAITING (1 << 1)
58 #define ANIM_STATE_RUNNING (1 << 2)
60 /* values for global animation control */
61 #define ANIM_NO_ACTION 0
63 #define ANIM_CONTINUE 2
67 struct GlobalAnimPartControlInfo
77 struct GraphicInfo graphic_info;
78 struct GraphicInfo control_info;
86 int step_xoffset, step_yoffset;
88 unsigned int initial_anim_sync_frame;
89 unsigned int step_delay, step_delay_value;
91 int init_delay_counter;
92 int anim_delay_counter;
93 int post_delay_counter;
95 boolean init_event_state;
96 boolean anim_event_state;
101 int last_anim_status;
104 struct GlobalAnimMainControlInfo
106 struct GlobalAnimPartControlInfo base;
107 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
112 struct GraphicInfo control_info;
120 int init_delay_counter;
124 int last_state, last_active_part_nr;
127 struct GlobalAnimControlInfo
129 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
135 struct GameModeAnimClass
139 } game_mode_anim_classes_list[] =
141 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
142 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
143 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
144 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
145 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
146 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
147 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
148 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
149 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
150 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
151 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
152 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
153 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
154 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
155 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
156 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
157 { GAME_MODE_SCORES, ANIM_CLASS_TOONS },
162 struct AnimClassGameMode
166 } anim_class_game_modes_list[] =
168 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
169 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
170 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
171 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
172 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
173 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
178 /* forward declaration for internal use */
179 static void HandleGlobalAnim(int, int);
180 static void DoAnimationExt(void);
182 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
184 static unsigned int anim_sync_frame = 0;
186 static int game_mode_anim_classes[NUM_GAME_MODES];
187 static int anim_class_game_modes[NUM_ANIM_CLASSES];
189 static int anim_status_last = GAME_MODE_DEFAULT;
190 static int anim_classes_last = ANIM_CLASS_NONE;
192 static boolean drawing_to_fading_buffer = FALSE;
194 static boolean anim_click_event = FALSE;
195 static int anim_click_mx = 0;
196 static int anim_click_my = 0;
199 /* ========================================================================= */
200 /* generic animation frame calculation */
201 /* ========================================================================= */
203 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
208 sync_frame += start_frame * delay;
210 if (mode & ANIM_LOOP) /* looping animation */
212 frame = (sync_frame % (delay * num_frames)) / delay;
214 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
216 frame = sync_frame / delay;
218 if (frame > num_frames - 1)
219 frame = num_frames - 1;
221 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
223 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
225 frame = (sync_frame % (delay * max_anim_frames)) / delay;
226 frame = (frame < num_frames ? frame : max_anim_frames - frame);
228 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
230 int max_anim_frames = 2 * num_frames;
232 frame = (sync_frame % (delay * max_anim_frames)) / delay;
233 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
235 else if (mode & ANIM_RANDOM) /* play frames in random order */
237 /* note: expect different frames for the same delay cycle! */
239 if (gfx.anim_random_frame < 0)
240 frame = GetSimpleRandom(num_frames);
242 frame = gfx.anim_random_frame % num_frames;
244 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
246 frame = sync_frame % num_frames;
249 if (mode & ANIM_REVERSE) /* use reverse animation direction */
250 frame = num_frames - frame - 1;
256 /* ========================================================================= */
257 /* global animation functions */
258 /* ========================================================================= */
260 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
262 struct GraphicInfo *c = &anim->control_info;
263 int last_anim_random_frame = gfx.anim_random_frame;
266 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
268 part_nr = getAnimationFrame(anim->num_parts, 1,
269 c->anim_mode, c->anim_start_frame,
272 gfx.anim_random_frame = last_anim_random_frame;
277 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
279 const struct GlobalAnimPartControlInfo *o1 =
280 (struct GlobalAnimPartControlInfo *)obj1;
281 const struct GlobalAnimPartControlInfo *o2 =
282 (struct GlobalAnimPartControlInfo *)obj2;
285 if (o1->control_info.draw_order != o2->control_info.draw_order)
286 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
288 compare_result = o1->nr - o2->nr;
290 return compare_result;
293 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
295 const struct GlobalAnimMainControlInfo *o1 =
296 (struct GlobalAnimMainControlInfo *)obj1;
297 const struct GlobalAnimMainControlInfo *o2 =
298 (struct GlobalAnimMainControlInfo *)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 void InitToonControls()
311 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
312 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
313 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
314 int mode_nr, anim_nr, part_nr;
315 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
316 int num_toons = MAX_NUM_TOONS;
319 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
320 num_toons = global.num_toons;
322 mode_nr = mode_nr_toons;
323 anim_nr = ctrl->num_anims;
326 anim->mode_nr = mode_nr;
327 anim->control_info = graphic_info[control];
330 anim->part_counter = 0;
331 anim->active_part_nr = 0;
333 anim->has_base = FALSE;
335 anim->init_delay_counter = 0;
337 anim->state = ANIM_STATE_INACTIVE;
341 for (i = 0; i < num_toons; i++)
343 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
344 int sound = SND_UNDEFINED;
345 int music = MUS_UNDEFINED;
346 int graphic = IMG_TOON_1 + i;
347 int control = graphic;
350 part->anim_nr = anim_nr;
351 part->mode_nr = mode_nr;
354 part->graphic = graphic;
355 part->graphic_info = graphic_info[graphic];
356 part->control_info = graphic_info[control];
358 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
360 part->control_info.init_delay_fixed = 0;
361 part->control_info.init_delay_random = 150;
363 part->control_info.x = ARG_UNDEFINED_VALUE;
364 part->control_info.y = ARG_UNDEFINED_VALUE;
366 part->initial_anim_sync_frame = 0;
368 part->step_delay = 0;
369 part->step_delay_value = graphic_info[control].step_delay;
371 part->state = ANIM_STATE_INACTIVE;
372 part->last_anim_status = -1;
381 void InitGlobalAnimControls()
384 int mode_nr, anim_nr, part_nr;
385 int sound, music, graphic, control;
389 for (m = 0; m < NUM_GAME_MODES; m++)
393 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
400 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
402 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
403 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
405 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
407 // if no base animation parameters defined, use default values
408 if (control == IMG_UNDEFINED)
409 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
412 anim->mode_nr = mode_nr;
413 anim->control_info = graphic_info[control];
416 anim->part_counter = 0;
417 anim->active_part_nr = 0;
419 anim->has_base = FALSE;
421 anim->init_delay_counter = 0;
423 anim->state = ANIM_STATE_INACTIVE;
427 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
429 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
431 sound = global_anim_info[a].sound[p][m];
432 music = global_anim_info[a].music[p][m];
433 graphic = global_anim_info[a].graphic[p][m];
434 control = global_anim_info[ctrl_id].graphic[p][m];
436 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
437 control == IMG_UNDEFINED)
441 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
442 m, a, p, mode_nr, anim_nr, part_nr, control);
446 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
447 m, a, p, mode_nr, anim_nr, part_nr, sound);
451 part->anim_nr = anim_nr;
452 part->mode_nr = mode_nr;
455 part->graphic = graphic;
456 part->graphic_info = graphic_info[graphic];
457 part->control_info = graphic_info[control];
459 part->initial_anim_sync_frame = 0;
461 part->step_delay = 0;
462 part->step_delay_value = graphic_info[control].step_delay;
464 part->state = ANIM_STATE_INACTIVE;
465 part->last_anim_status = -1;
467 if (p < GLOBAL_ANIM_ID_PART_BASE)
475 anim->has_base = TRUE;
479 if (anim->num_parts > 0 || anim->has_base)
489 /* sort all animations according to draw_order and animation number */
490 for (m = 0; m < NUM_GAME_MODES; m++)
492 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
494 /* sort all main animations for this game mode */
495 qsort(ctrl->anim, ctrl->num_anims,
496 sizeof(struct GlobalAnimMainControlInfo),
497 compareGlobalAnimMainControlInfo);
499 for (a = 0; a < ctrl->num_anims; a++)
501 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
503 /* sort all animation parts for this main animation */
504 qsort(anim->part, anim->num_parts,
505 sizeof(struct GlobalAnimPartControlInfo),
506 compareGlobalAnimPartControlInfo);
510 for (i = 0; i < NUM_GAME_MODES; i++)
511 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
512 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
513 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
514 game_mode_anim_classes_list[i].class;
516 for (i = 0; i < NUM_ANIM_CLASSES; i++)
517 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
518 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
519 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
520 anim_class_game_modes_list[i].game_mode;
522 anim_status_last = GAME_MODE_LOADING;
523 anim_classes_last = ANIM_CLASS_NONE;
526 void InitGlobalAnimations()
528 InitGlobalAnimControls();
531 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
533 Bitmap *fade_bitmap =
534 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
535 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
536 int game_mode_anim_action[NUM_GAME_MODES];
542 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
543 drawing_target == DRAW_TO_SCREEN)
546 // always start with reliable default values (no animation actions)
547 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
548 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
550 if (global.anim_status != anim_status_last)
552 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
553 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
554 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
557 if (drawing_target == DRAW_TO_FADE_TARGET)
560 // ---------- part 1 ------------------------------------------------------
561 // start or stop global animations by change of game mode
562 // (special handling of animations for "current screen" and "all screens")
564 // stop animations for last screen
565 game_mode_anim_action[anim_status_last] = ANIM_STOP;
567 // start animations for current screen
568 game_mode_anim_action[global.anim_status] = ANIM_START;
570 // start animations for all screens after loading new artwork set
571 if (anim_status_last == GAME_MODE_LOADING)
572 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
574 // ---------- part 2 ------------------------------------------------------
575 // start or stop global animations by change of animation class
576 // (generic handling of animations for "class of screens")
578 for (i = 0; i < NUM_ANIM_CLASSES; i++)
580 int anim_class_check = (1 << i);
581 int anim_class_game_mode = anim_class_game_modes[i];
582 int anim_class_last = anim_classes_last & anim_class_check;
583 int anim_class_next = anim_classes_next & anim_class_check;
585 // stop animations for changed screen class before fading to new screen
586 if (before_fading && anim_class_last && !anim_class_next)
587 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
589 // start animations for changed screen class after fading to new screen
590 if (after_fading && !anim_class_last && anim_class_next)
591 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
594 if (drawing_target == DRAW_TO_SCREEN)
597 anim_classes_last = anim_classes_next;
599 anim_status_last = global.anim_status;
601 // start or stop animations determined to be started or stopped above
602 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
603 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
604 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
606 else if (drawing_target == DRAW_TO_FADE_TARGET)
608 drawing_to_fading_buffer = TRUE;
610 // start animations determined to be (temporary) started above
611 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
612 if (game_mode_anim_action[mode_nr] == ANIM_START)
613 HandleGlobalAnim(ANIM_START, mode_nr);
617 if (global.anim_status == GAME_MODE_LOADING)
620 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
622 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
625 // when preparing source fading buffer, only draw animations to be stopped
626 if (drawing_target == DRAW_TO_FADE_SOURCE &&
627 game_mode_anim_action[mode_nr] != ANIM_STOP)
630 // when preparing target fading buffer, only draw animations to be started
631 if (drawing_target == DRAW_TO_FADE_TARGET &&
632 game_mode_anim_action[mode_nr] != ANIM_START)
636 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
637 mode_nr != game_status)
641 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
643 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
644 struct GraphicInfo *c = &anim->control_info;
645 int part_first, part_last;
648 if (!(anim->state & ANIM_STATE_RUNNING))
651 part_first = part_last = anim->active_part_nr;
653 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
655 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
658 part_last = num_parts - 1;
661 for (part_nr = part_first; part_nr <= part_last; part_nr++)
663 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
664 struct GraphicInfo *g = &part->graphic_info;
667 int width = g->width;
668 int height = g->height;
676 if (!(part->state & ANIM_STATE_RUNNING))
679 if (part->drawing_stage != drawing_stage)
688 else if (part->x > part->viewport_width - g->width)
689 width -= (part->x - (part->viewport_width - g->width));
697 else if (part->y > part->viewport_height - g->height)
698 height -= (part->y - (part->viewport_height - g->height));
700 if (width <= 0 || height <= 0)
703 dst_x += part->viewport_x;
704 dst_y += part->viewport_y;
706 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
707 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
708 g->anim_mode, g->anim_start_frame,
711 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
717 if (drawing_target == DRAW_TO_SCREEN)
718 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
721 BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
727 if (drawing_target == DRAW_TO_FADE_TARGET)
729 // stop animations determined to be (temporary) started above
730 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
731 if (game_mode_anim_action[mode_nr] == ANIM_START)
732 HandleGlobalAnim(ANIM_STOP, mode_nr);
734 drawing_to_fading_buffer = FALSE;
738 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
740 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
743 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
749 boolean changed = FALSE;
751 if (part->last_anim_status == global.anim_status)
754 part->last_anim_status = global.anim_status;
756 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
758 if (part->control_info.class == get_hash_from_key("window") ||
759 part->control_info.class == get_hash_from_key("border"))
763 viewport_width = WIN_XSIZE;
764 viewport_height = WIN_YSIZE;
766 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
768 else if (part->control_info.class == get_hash_from_key("door_1"))
772 viewport_width = DXSIZE;
773 viewport_height = DYSIZE;
775 else if (part->control_info.class == get_hash_from_key("door_2"))
779 viewport_width = VXSIZE;
780 viewport_height = VYSIZE;
782 else // default: "playfield"
784 viewport_x = REAL_SX;
785 viewport_y = REAL_SY;
786 viewport_width = FULL_SXSIZE;
787 viewport_height = FULL_SYSIZE;
790 if (viewport_x != part->viewport_x ||
791 viewport_y != part->viewport_y ||
792 viewport_width != part->viewport_width ||
793 viewport_height != part->viewport_height)
795 part->viewport_x = viewport_x;
796 part->viewport_y = viewport_y;
797 part->viewport_width = viewport_width;
798 part->viewport_height = viewport_height;
806 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
808 int sound = part->sound;
810 if (sound == SND_UNDEFINED)
813 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
814 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
817 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
818 if (IS_LOOP_SOUND(sound))
819 PlaySoundLoop(sound);
824 printf("::: PLAY SOUND %d.%d.%d: %d\n",
825 part->anim_nr, part->nr, part->mode_nr, sound);
829 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
831 int sound = part->sound;
833 if (sound == SND_UNDEFINED)
839 printf("::: STOP SOUND %d.%d.%d: %d\n",
840 part->anim_nr, part->nr, part->mode_nr, sound);
844 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
846 int music = part->music;
848 if (music == MUS_UNDEFINED)
851 if (!setup.sound_music)
857 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
858 part->anim_nr, part->nr, part->mode_nr, music);
862 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
864 int music = part->music;
866 if (music == MUS_UNDEFINED)
872 printf("::: STOP MUSIC %d.%d.%d: %d\n",
873 part->anim_nr, part->nr, part->mode_nr, music);
877 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
879 // when drawing animations to fading buffer, do not play sounds or music
880 if (drawing_to_fading_buffer)
883 PlayGlobalAnimSound(part);
884 PlayGlobalAnimMusic(part);
887 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
889 StopGlobalAnimSound(part);
890 StopGlobalAnimMusic(part);
893 static boolean getPartClickEvent(struct GlobalAnimPartControlInfo *part)
895 struct GraphicInfo *g = &part->graphic_info;
896 int part_x = part->viewport_x + part->x;
897 int part_y = part->viewport_y + part->y;
898 int part_width = g->width;
899 int part_height = g->height;
900 int mx = anim_click_mx;
901 int my = anim_click_my;
903 // check if mouse click event was detected at all
904 if (!anim_click_event)
907 // check if mouse click is inside the animation part's viewport
908 if (mx < part->viewport_x ||
909 mx >= part->viewport_x + part->viewport_width ||
910 my < part->viewport_y ||
911 my >= part->viewport_y + part->viewport_height)
914 // check if mouse click is inside the animation part's graphic
916 mx >= part_x + part_width ||
918 my >= part_y + part_height)
924 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
926 struct GraphicInfo *g = &part->graphic_info;
927 struct GraphicInfo *c = &part->control_info;
928 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
929 boolean part_click_event = getPartClickEvent(part);
931 if (viewport_changed)
932 state |= ANIM_STATE_RESTART;
934 if (state & ANIM_STATE_RESTART)
936 // when drawing animations to fading buffer, only start fixed animations
937 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
938 c->y == ARG_UNDEFINED_VALUE))
939 return ANIM_STATE_INACTIVE;
941 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
943 part->init_delay_counter =
944 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
946 part->anim_delay_counter =
947 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
949 part->init_event_state = c->init_event;
950 part->anim_event_state = c->anim_event;
952 part->initial_anim_sync_frame =
953 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
955 if (c->direction & MV_HORIZONTAL)
957 int pos_bottom = part->viewport_height - g->height;
959 if (c->position == POS_TOP)
961 else if (c->position == POS_UPPER)
962 part->y = GetSimpleRandom(pos_bottom / 2);
963 else if (c->position == POS_MIDDLE)
964 part->y = pos_bottom / 2;
965 else if (c->position == POS_LOWER)
966 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
967 else if (c->position == POS_BOTTOM)
968 part->y = pos_bottom;
970 part->y = GetSimpleRandom(pos_bottom);
972 if (c->direction == MV_RIGHT)
974 part->step_xoffset = c->step_offset;
975 part->x = -g->width + part->step_xoffset;
979 part->step_xoffset = -c->step_offset;
980 part->x = part->viewport_width + part->step_xoffset;
983 part->step_yoffset = 0;
985 else if (c->direction & MV_VERTICAL)
987 int pos_right = part->viewport_width - g->width;
989 if (c->position == POS_LEFT)
991 else if (c->position == POS_RIGHT)
994 part->x = GetSimpleRandom(pos_right);
996 if (c->direction == MV_DOWN)
998 part->step_yoffset = c->step_offset;
999 part->y = -g->height + part->step_yoffset;
1003 part->step_yoffset = -c->step_offset;
1004 part->y = part->viewport_height + part->step_yoffset;
1007 part->step_xoffset = 0;
1014 part->step_xoffset = 0;
1015 part->step_yoffset = 0;
1018 if (c->x != ARG_UNDEFINED_VALUE)
1020 if (c->y != ARG_UNDEFINED_VALUE)
1023 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1024 part->step_xoffset = c->step_xoffset;
1025 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1026 part->step_yoffset = c->step_yoffset;
1028 if (part->init_delay_counter == 0 &&
1029 part->init_event_state == ANIM_EVENT_NONE)
1030 PlayGlobalAnimSoundAndMusic(part);
1033 if (part_click_event &&
1034 part->init_event_state != ANIM_EVENT_NONE)
1036 if (part->initial_anim_sync_frame > 0)
1037 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1039 part->init_delay_counter = 1;
1040 part->init_event_state = ANIM_EVENT_NONE;
1042 anim_click_event = FALSE;
1045 if (part_click_event &&
1046 part->anim_event_state != ANIM_EVENT_NONE)
1048 part->anim_delay_counter = 1;
1049 part->anim_event_state = ANIM_EVENT_NONE;
1051 anim_click_event = FALSE;
1054 if (part->init_delay_counter > 0)
1056 part->init_delay_counter--;
1058 if (part->init_delay_counter == 0)
1060 part->init_event_state = ANIM_EVENT_NONE;
1062 PlayGlobalAnimSoundAndMusic(part);
1065 return ANIM_STATE_WAITING;
1068 if (part->init_event_state != ANIM_EVENT_NONE)
1069 return ANIM_STATE_WAITING;
1071 // check if moving animation has left the visible screen area
1072 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1073 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1074 (part->y <= -g->height && part->step_yoffset <= 0) ||
1075 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1077 // do not wait for "anim" events for off-screen animations
1078 part->anim_event_state = ANIM_EVENT_NONE;
1080 // do not stop animation before "anim" or "post" counter are finished
1081 if (part->anim_delay_counter == 0 &&
1082 part->post_delay_counter == 0)
1084 StopGlobalAnimSoundAndMusic(part);
1086 part->post_delay_counter =
1087 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1089 if (part->post_delay_counter > 0)
1090 return ANIM_STATE_RUNNING;
1092 // drawing last frame not needed here -- animation not visible anymore
1093 return ANIM_STATE_RESTART;
1097 if (part->anim_delay_counter > 0)
1099 part->anim_delay_counter--;
1101 if (part->anim_delay_counter == 0)
1103 part->anim_event_state = ANIM_EVENT_NONE;
1105 StopGlobalAnimSoundAndMusic(part);
1107 part->post_delay_counter =
1108 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1110 if (part->post_delay_counter > 0)
1111 return ANIM_STATE_RUNNING;
1113 // additional state "RUNNING" required to not skip drawing last frame
1114 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1118 if (part->post_delay_counter > 0)
1120 part->post_delay_counter--;
1122 if (part->post_delay_counter == 0)
1123 return ANIM_STATE_RESTART;
1125 return ANIM_STATE_WAITING;
1128 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1130 return ANIM_STATE_RUNNING;
1134 static unsigned int last_counter = -1;
1135 unsigned int counter = Counter();
1137 printf("::: NEXT ANIM PART [%d, %d]\n",
1138 anim_sync_frame, counter - last_counter);
1140 last_counter = counter;
1144 part->x += part->step_xoffset;
1145 part->y += part->step_yoffset;
1147 return ANIM_STATE_RUNNING;
1150 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1152 struct GlobalAnimPartControlInfo *part;
1153 struct GraphicInfo *c = &anim->control_info;
1154 int state, active_part_nr;
1157 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1158 anim->mode_nr, anim->nr, anim->num_parts);
1159 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1163 printf("::: %s(%d): %d, %d, %d [%d]\n",
1164 (action == ANIM_START ? "ANIM_START" :
1165 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1166 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1168 anim->state & ANIM_STATE_RESTART,
1169 anim->state & ANIM_STATE_WAITING,
1170 anim->state & ANIM_STATE_RUNNING,
1177 anim->state = anim->last_state = ANIM_STATE_RESTART;
1178 anim->active_part_nr = anim->last_active_part_nr = 0;
1179 anim->part_counter = 0;
1184 if (anim->state == ANIM_STATE_INACTIVE)
1187 anim->state = anim->last_state;
1188 anim->active_part_nr = anim->last_active_part_nr;
1193 anim->state = ANIM_STATE_INACTIVE;
1196 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1199 for (i = 0; i < num_parts; i++)
1200 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1209 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1211 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1215 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1216 anim->mode_nr, anim->nr, num_parts);
1219 for (i = 0; i < num_parts; i++)
1221 part = &anim->part[i];
1226 anim->state = ANIM_STATE_RUNNING;
1227 part->state = ANIM_STATE_RESTART;
1232 if (part->state == ANIM_STATE_INACTIVE)
1238 part->state = ANIM_STATE_INACTIVE;
1246 part->state = HandleGlobalAnim_Part(part, part->state);
1248 // when animation mode is "once", stop after animation was played once
1249 if (c->anim_mode & ANIM_ONCE &&
1250 part->state & ANIM_STATE_RESTART)
1251 part->state = ANIM_STATE_INACTIVE;
1254 anim->last_state = anim->state;
1255 anim->last_active_part_nr = anim->active_part_nr;
1260 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1261 anim->active_part_nr = getGlobalAnimationPart(anim);
1263 part = &anim->part[anim->active_part_nr];
1265 part->state = ANIM_STATE_RUNNING;
1267 anim->state = HandleGlobalAnim_Part(part, anim->state);
1269 if (anim->state & ANIM_STATE_RESTART)
1270 anim->part_counter++;
1272 // when animation mode is "once", stop after all animations were played once
1273 if (c->anim_mode & ANIM_ONCE &&
1274 anim->part_counter == anim->num_parts)
1275 anim->state = ANIM_STATE_INACTIVE;
1277 state = anim->state;
1278 active_part_nr = anim->active_part_nr;
1280 // while the animation parts are pausing (waiting or inactive), play the base
1281 // (main) animation; this corresponds to the "boring player animation" logic
1282 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1285 if (anim->state == ANIM_STATE_WAITING ||
1286 anim->state == ANIM_STATE_INACTIVE)
1288 anim->active_part_nr = anim->num_parts; // part nr of base animation
1289 part = &anim->part[anim->active_part_nr];
1291 if (anim->state != anim->last_state)
1292 part->state = ANIM_STATE_RESTART;
1294 anim->state = ANIM_STATE_RUNNING;
1295 part->state = HandleGlobalAnim_Part(part, part->state);
1299 anim->last_state = state;
1300 anim->last_active_part_nr = active_part_nr;
1303 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1308 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1309 ctrl->nr, ctrl->num_anims);
1312 for (i = 0; i < ctrl->num_anims; i++)
1313 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1316 static void HandleGlobalAnim(int action, int game_mode)
1319 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1322 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1325 static void DoAnimationExt()
1330 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1333 // global animations now synchronized with frame delay of screen update
1336 for (i = 0; i < NUM_GAME_MODES; i++)
1337 HandleGlobalAnim(ANIM_CONTINUE, i);
1340 // force screen redraw in next frame to continue drawing global animations
1341 redraw_mask = REDRAW_ALL;
1345 void HandleGlobalAnimClicks(int mx, int my, int button)
1347 static int last_button = 0;
1348 boolean press_event;
1349 boolean release_event;
1351 /* check if button state has changed since last invocation */
1352 press_event = (button != 0 && last_button == 0);
1353 release_event = (button == 0 && last_button != 0);
1354 last_button = button;
1358 anim_click_event = (press_event ? TRUE :
1359 release_event ? FALSE : anim_click_event);