1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
17 /* values for global toon animation definition */
18 #define NUM_GLOBAL_TOON_ANIMS 1
19 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
21 /* values for global animation definition (including toons) */
22 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
23 NUM_GLOBAL_TOON_ANIMS)
24 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
25 NUM_GLOBAL_TOON_PARTS)
27 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
28 #define ANIM_CLASS_BIT_TITLE 1
29 #define ANIM_CLASS_BIT_MAIN 2
30 #define ANIM_CLASS_BIT_SUBMENU 3
31 #define ANIM_CLASS_BIT_MENU 4
32 #define ANIM_CLASS_BIT_TOONS 5
34 #define NUM_ANIM_CLASSES 6
36 #define ANIM_CLASS_NONE 0
37 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
38 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
39 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
40 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
41 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
42 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
44 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
48 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
52 /* values for global animation states */
53 #define ANIM_STATE_INACTIVE 0
54 #define ANIM_STATE_RESTART (1 << 0)
55 #define ANIM_STATE_WAITING (1 << 1)
56 #define ANIM_STATE_RUNNING (1 << 2)
58 /* values for global animation control */
59 #define ANIM_NO_ACTION 0
61 #define ANIM_CONTINUE 2
65 struct GlobalAnimPartControlInfo
75 struct GraphicInfo graphic_info;
76 struct GraphicInfo control_info;
84 int step_xoffset, step_yoffset;
86 unsigned int initial_anim_sync_frame;
87 unsigned int step_delay, step_delay_value;
89 int init_delay_counter;
90 int anim_delay_counter;
91 int post_delay_counter;
99 struct GlobalAnimMainControlInfo
101 struct GlobalAnimPartControlInfo base;
102 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
107 struct GraphicInfo control_info;
115 int init_delay_counter;
119 int last_state, last_active_part_nr;
122 struct GlobalAnimControlInfo
124 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
130 struct GameModeAnimClass
134 } game_mode_anim_classes_list[] =
136 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
137 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
138 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
139 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
140 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
141 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
142 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
143 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
144 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
145 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
146 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
147 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
148 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
149 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
150 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
151 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
152 { GAME_MODE_SCORES, ANIM_CLASS_TOONS },
157 struct AnimClassGameMode
161 } anim_class_game_modes_list[] =
163 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
164 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
165 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
166 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
167 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
168 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
173 /* forward declaration for internal use */
174 static void HandleGlobalAnim(int, int);
175 static void DoAnimationExt(void);
177 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
179 static unsigned int anim_sync_frame = 0;
181 static int game_mode_anim_classes[NUM_GAME_MODES];
182 static int anim_class_game_modes[NUM_ANIM_CLASSES];
184 static int anim_status_last = GAME_MODE_DEFAULT;
185 static int anim_classes_last = ANIM_CLASS_NONE;
187 static boolean drawing_to_fading_buffer = FALSE;
190 /* ========================================================================= */
191 /* generic animation frame calculation */
192 /* ========================================================================= */
194 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
199 sync_frame += start_frame * delay;
201 if (mode & ANIM_LOOP) /* looping animation */
203 frame = (sync_frame % (delay * num_frames)) / delay;
205 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
207 frame = sync_frame / delay;
209 if (frame > num_frames - 1)
210 frame = num_frames - 1;
212 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
214 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
216 frame = (sync_frame % (delay * max_anim_frames)) / delay;
217 frame = (frame < num_frames ? frame : max_anim_frames - frame);
219 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
221 int max_anim_frames = 2 * num_frames;
223 frame = (sync_frame % (delay * max_anim_frames)) / delay;
224 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
226 else if (mode & ANIM_RANDOM) /* play frames in random order */
228 /* note: expect different frames for the same delay cycle! */
230 if (gfx.anim_random_frame < 0)
231 frame = GetSimpleRandom(num_frames);
233 frame = gfx.anim_random_frame % num_frames;
235 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
237 frame = sync_frame % num_frames;
240 if (mode & ANIM_REVERSE) /* use reverse animation direction */
241 frame = num_frames - frame - 1;
247 /* ========================================================================= */
248 /* global animation functions */
249 /* ========================================================================= */
251 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
253 struct GraphicInfo *c = &anim->control_info;
254 int last_anim_random_frame = gfx.anim_random_frame;
257 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
259 part_nr = getAnimationFrame(anim->num_parts, 1,
260 c->anim_mode, c->anim_start_frame,
263 gfx.anim_random_frame = last_anim_random_frame;
268 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
270 const struct GlobalAnimPartControlInfo *o1 =
271 (struct GlobalAnimPartControlInfo *)obj1;
272 const struct GlobalAnimPartControlInfo *o2 =
273 (struct GlobalAnimPartControlInfo *)obj2;
276 if (o1->control_info.draw_order != o2->control_info.draw_order)
277 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
279 compare_result = o1->nr - o2->nr;
281 return compare_result;
284 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
286 const struct GlobalAnimMainControlInfo *o1 =
287 (struct GlobalAnimMainControlInfo *)obj1;
288 const struct GlobalAnimMainControlInfo *o2 =
289 (struct GlobalAnimMainControlInfo *)obj2;
292 if (o1->control_info.draw_order != o2->control_info.draw_order)
293 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
295 compare_result = o1->nr - o2->nr;
297 return compare_result;
300 static void InitToonControls()
302 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
303 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
304 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
305 int mode_nr, anim_nr, part_nr;
306 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
307 int num_toons = MAX_NUM_TOONS;
310 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
311 num_toons = global.num_toons;
313 mode_nr = mode_nr_toons;
314 anim_nr = ctrl->num_anims;
317 anim->mode_nr = mode_nr;
318 anim->control_info = graphic_info[control];
321 anim->part_counter = 0;
322 anim->active_part_nr = 0;
324 anim->has_base = FALSE;
326 anim->init_delay_counter = 0;
328 anim->state = ANIM_STATE_INACTIVE;
332 for (i = 0; i < num_toons; i++)
334 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
335 int sound = SND_UNDEFINED;
336 int music = MUS_UNDEFINED;
337 int graphic = IMG_TOON_1 + i;
338 int control = graphic;
341 part->anim_nr = anim_nr;
342 part->mode_nr = mode_nr;
345 part->graphic = graphic;
346 part->graphic_info = graphic_info[graphic];
347 part->control_info = graphic_info[control];
349 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
351 part->control_info.init_delay_fixed = 0;
352 part->control_info.init_delay_random = 150;
354 part->control_info.x = ARG_UNDEFINED_VALUE;
355 part->control_info.y = ARG_UNDEFINED_VALUE;
357 part->initial_anim_sync_frame = 0;
359 part->step_delay = 0;
360 part->step_delay_value = graphic_info[control].step_delay;
362 part->state = ANIM_STATE_INACTIVE;
363 part->last_anim_status = -1;
372 void InitGlobalAnimControls()
375 int mode_nr, anim_nr, part_nr;
376 int sound, music, graphic, control;
380 for (m = 0; m < NUM_GAME_MODES; m++)
384 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
391 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
393 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
394 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
396 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
398 // if no base animation parameters defined, use default values
399 if (control == IMG_UNDEFINED)
400 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
403 anim->mode_nr = mode_nr;
404 anim->control_info = graphic_info[control];
407 anim->part_counter = 0;
408 anim->active_part_nr = 0;
410 anim->has_base = FALSE;
412 anim->init_delay_counter = 0;
414 anim->state = ANIM_STATE_INACTIVE;
418 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
420 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
422 sound = global_anim_info[a].sound[p][m];
423 music = global_anim_info[a].music[p][m];
424 graphic = global_anim_info[a].graphic[p][m];
425 control = global_anim_info[ctrl_id].graphic[p][m];
427 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
428 control == IMG_UNDEFINED)
432 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
433 m, a, p, mode_nr, anim_nr, part_nr, control);
437 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
438 m, a, p, mode_nr, anim_nr, part_nr, sound);
442 part->anim_nr = anim_nr;
443 part->mode_nr = mode_nr;
446 part->graphic = graphic;
447 part->graphic_info = graphic_info[graphic];
448 part->control_info = graphic_info[control];
450 part->initial_anim_sync_frame = 0;
452 part->step_delay = 0;
453 part->step_delay_value = graphic_info[control].step_delay;
455 part->state = ANIM_STATE_INACTIVE;
456 part->last_anim_status = -1;
458 if (p < GLOBAL_ANIM_ID_PART_BASE)
466 anim->has_base = TRUE;
470 if (anim->num_parts > 0 || anim->has_base)
480 /* sort all animations according to draw_order and animation number */
481 for (m = 0; m < NUM_GAME_MODES; m++)
483 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
485 /* sort all main animations for this game mode */
486 qsort(ctrl->anim, ctrl->num_anims,
487 sizeof(struct GlobalAnimMainControlInfo),
488 compareGlobalAnimMainControlInfo);
490 for (a = 0; a < ctrl->num_anims; a++)
492 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
494 /* sort all animation parts for this main animation */
495 qsort(anim->part, anim->num_parts,
496 sizeof(struct GlobalAnimPartControlInfo),
497 compareGlobalAnimPartControlInfo);
501 for (i = 0; i < NUM_GAME_MODES; i++)
502 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
503 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
504 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
505 game_mode_anim_classes_list[i].class;
507 for (i = 0; i < NUM_ANIM_CLASSES; i++)
508 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
509 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
510 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
511 anim_class_game_modes_list[i].game_mode;
513 anim_status_last = GAME_MODE_LOADING;
514 anim_classes_last = ANIM_CLASS_NONE;
517 void InitGlobalAnimations()
519 InitGlobalAnimControls();
522 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
524 Bitmap *fade_bitmap =
525 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
526 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
527 int game_mode_anim_action[NUM_GAME_MODES];
533 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
534 drawing_target == DRAW_TO_SCREEN)
537 // always start with reliable default values (no animation actions)
538 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
539 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
541 if (global.anim_status != anim_status_last)
543 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
544 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
545 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
548 if (drawing_target == DRAW_TO_FADE_TARGET)
551 // ---------- part 1 ------------------------------------------------------
552 // start or stop global animations by change of game mode
553 // (special handling of animations for "current screen" and "all screens")
555 // stop animations for last screen
556 game_mode_anim_action[anim_status_last] = ANIM_STOP;
558 // start animations for current screen
559 game_mode_anim_action[global.anim_status] = ANIM_START;
561 // start animations for all screens after loading new artwork set
562 if (anim_status_last == GAME_MODE_LOADING)
563 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
565 // ---------- part 2 ------------------------------------------------------
566 // start or stop global animations by change of animation class
567 // (generic handling of animations for "class of screens")
569 for (i = 0; i < NUM_ANIM_CLASSES; i++)
571 int anim_class_check = (1 << i);
572 int anim_class_game_mode = anim_class_game_modes[i];
573 int anim_class_last = anim_classes_last & anim_class_check;
574 int anim_class_next = anim_classes_next & anim_class_check;
576 // stop animations for changed screen class before fading to new screen
577 if (before_fading && anim_class_last && !anim_class_next)
578 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
580 // start animations for changed screen class after fading to new screen
581 if (after_fading && !anim_class_last && anim_class_next)
582 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
585 if (drawing_target == DRAW_TO_SCREEN)
588 anim_classes_last = anim_classes_next;
590 anim_status_last = global.anim_status;
592 // start or stop animations determined to be started or stopped above
593 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
594 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
595 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
597 else if (drawing_target == DRAW_TO_FADE_TARGET)
599 drawing_to_fading_buffer = TRUE;
601 // start animations determined to be (temporary) started above
602 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
603 if (game_mode_anim_action[mode_nr] == ANIM_START)
604 HandleGlobalAnim(ANIM_START, mode_nr);
608 if (global.anim_status == GAME_MODE_LOADING)
611 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
613 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
616 // when preparing source fading buffer, only draw animations to be stopped
617 if (drawing_target == DRAW_TO_FADE_SOURCE &&
618 game_mode_anim_action[mode_nr] != ANIM_STOP)
621 // when preparing target fading buffer, only draw animations to be started
622 if (drawing_target == DRAW_TO_FADE_TARGET &&
623 game_mode_anim_action[mode_nr] != ANIM_START)
627 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
628 mode_nr != game_status)
632 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
634 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
635 struct GraphicInfo *c = &anim->control_info;
636 int part_first, part_last;
639 if (!(anim->state & ANIM_STATE_RUNNING))
642 part_first = part_last = anim->active_part_nr;
644 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
646 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
649 part_last = num_parts - 1;
652 for (part_nr = part_first; part_nr <= part_last; part_nr++)
654 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
655 struct GraphicInfo *g = &part->graphic_info;
658 int width = g->width;
659 int height = g->height;
667 if (!(part->state & ANIM_STATE_RUNNING))
670 if (part->drawing_stage != drawing_stage)
679 else if (part->x > part->viewport_width - g->width)
680 width -= (part->x - (part->viewport_width - g->width));
688 else if (part->y > part->viewport_height - g->height)
689 height -= (part->y - (part->viewport_height - g->height));
691 if (width <= 0 || height <= 0)
694 dst_x += part->viewport_x;
695 dst_y += part->viewport_y;
697 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
698 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
699 g->anim_mode, g->anim_start_frame,
702 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
708 if (drawing_target == DRAW_TO_SCREEN)
709 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
712 BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
718 if (drawing_target == DRAW_TO_FADE_TARGET)
720 // stop animations determined to be (temporary) started above
721 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
722 if (game_mode_anim_action[mode_nr] == ANIM_START)
723 HandleGlobalAnim(ANIM_STOP, mode_nr);
725 drawing_to_fading_buffer = FALSE;
729 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
731 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
734 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
740 boolean changed = FALSE;
742 if (part->last_anim_status == global.anim_status)
745 part->last_anim_status = global.anim_status;
747 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
749 if (part->control_info.class == get_hash_from_key("window") ||
750 part->control_info.class == get_hash_from_key("border"))
754 viewport_width = WIN_XSIZE;
755 viewport_height = WIN_YSIZE;
757 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
759 else if (part->control_info.class == get_hash_from_key("door_1"))
763 viewport_width = DXSIZE;
764 viewport_height = DYSIZE;
766 else if (part->control_info.class == get_hash_from_key("door_2"))
770 viewport_width = VXSIZE;
771 viewport_height = VYSIZE;
773 else // default: "playfield"
775 viewport_x = REAL_SX;
776 viewport_y = REAL_SY;
777 viewport_width = FULL_SXSIZE;
778 viewport_height = FULL_SYSIZE;
781 if (viewport_x != part->viewport_x ||
782 viewport_y != part->viewport_y ||
783 viewport_width != part->viewport_width ||
784 viewport_height != part->viewport_height)
786 part->viewport_x = viewport_x;
787 part->viewport_y = viewport_y;
788 part->viewport_width = viewport_width;
789 part->viewport_height = viewport_height;
797 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
799 int sound = part->sound;
801 if (sound == SND_UNDEFINED)
804 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
805 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
808 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
809 if (IS_LOOP_SOUND(sound))
810 PlaySoundLoop(sound);
815 printf("::: PLAY SOUND %d.%d.%d: %d\n",
816 part->anim_nr, part->nr, part->mode_nr, sound);
820 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
822 int sound = part->sound;
824 if (sound == SND_UNDEFINED)
830 printf("::: STOP SOUND %d.%d.%d: %d\n",
831 part->anim_nr, part->nr, part->mode_nr, sound);
835 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
837 int music = part->music;
839 if (music == MUS_UNDEFINED)
842 if (!setup.sound_music)
848 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
849 part->anim_nr, part->nr, part->mode_nr, music);
853 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
855 int music = part->music;
857 if (music == MUS_UNDEFINED)
863 printf("::: STOP MUSIC %d.%d.%d: %d\n",
864 part->anim_nr, part->nr, part->mode_nr, music);
868 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
870 // when drawing animations to fading buffer, do not play sounds or music
871 if (drawing_to_fading_buffer)
874 PlayGlobalAnimSound(part);
875 PlayGlobalAnimMusic(part);
878 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
880 StopGlobalAnimSound(part);
881 StopGlobalAnimMusic(part);
884 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
886 struct GraphicInfo *g = &part->graphic_info;
887 struct GraphicInfo *c = &part->control_info;
888 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
890 if (viewport_changed)
891 state |= ANIM_STATE_RESTART;
893 if (state & ANIM_STATE_RESTART)
895 // when drawing animations to fading buffer, only start fixed animations
896 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
897 c->y == ARG_UNDEFINED_VALUE))
898 return ANIM_STATE_INACTIVE;
900 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
902 part->init_delay_counter =
903 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
905 part->anim_delay_counter =
906 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
908 part->initial_anim_sync_frame =
909 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
911 if (c->direction & MV_HORIZONTAL)
913 int pos_bottom = part->viewport_height - g->height;
915 if (c->position == POS_TOP)
917 else if (c->position == POS_UPPER)
918 part->y = GetSimpleRandom(pos_bottom / 2);
919 else if (c->position == POS_MIDDLE)
920 part->y = pos_bottom / 2;
921 else if (c->position == POS_LOWER)
922 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
923 else if (c->position == POS_BOTTOM)
924 part->y = pos_bottom;
926 part->y = GetSimpleRandom(pos_bottom);
928 if (c->direction == MV_RIGHT)
930 part->step_xoffset = c->step_offset;
931 part->x = -g->width + part->step_xoffset;
935 part->step_xoffset = -c->step_offset;
936 part->x = part->viewport_width + part->step_xoffset;
939 part->step_yoffset = 0;
941 else if (c->direction & MV_VERTICAL)
943 int pos_right = part->viewport_width - g->width;
945 if (c->position == POS_LEFT)
947 else if (c->position == POS_RIGHT)
950 part->x = GetSimpleRandom(pos_right);
952 if (c->direction == MV_DOWN)
954 part->step_yoffset = c->step_offset;
955 part->y = -g->height + part->step_yoffset;
959 part->step_yoffset = -c->step_offset;
960 part->y = part->viewport_height + part->step_yoffset;
963 part->step_xoffset = 0;
970 part->step_xoffset = 0;
971 part->step_yoffset = 0;
974 if (c->x != ARG_UNDEFINED_VALUE)
976 if (c->y != ARG_UNDEFINED_VALUE)
979 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
980 part->step_xoffset = c->step_xoffset;
981 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
982 part->step_yoffset = c->step_yoffset;
984 if (part->init_delay_counter == 0)
985 PlayGlobalAnimSoundAndMusic(part);
988 if (part->init_delay_counter > 0)
990 part->init_delay_counter--;
992 if (part->init_delay_counter == 0)
993 PlayGlobalAnimSoundAndMusic(part);
995 return ANIM_STATE_WAITING;
998 // check if moving animation has left the visible screen area
999 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1000 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1001 (part->y <= -g->height && part->step_yoffset <= 0) ||
1002 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1004 // do not stop animation before "anim" or "post" counter are finished
1005 if (part->anim_delay_counter == 0 &&
1006 part->post_delay_counter == 0)
1008 StopGlobalAnimSoundAndMusic(part);
1010 part->post_delay_counter =
1011 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1013 if (part->post_delay_counter > 0)
1014 return ANIM_STATE_RUNNING;
1016 // drawing last frame not needed here -- animation not visible anymore
1017 return ANIM_STATE_RESTART;
1021 if (part->anim_delay_counter > 0)
1023 part->anim_delay_counter--;
1025 if (part->anim_delay_counter == 0)
1027 StopGlobalAnimSoundAndMusic(part);
1029 part->post_delay_counter =
1030 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1032 if (part->post_delay_counter > 0)
1033 return ANIM_STATE_RUNNING;
1035 // additional state "RUNNING" required to not skip drawing last frame
1036 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1040 if (part->post_delay_counter > 0)
1042 part->post_delay_counter--;
1044 if (part->post_delay_counter == 0)
1045 return ANIM_STATE_RESTART;
1047 return ANIM_STATE_WAITING;
1050 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1052 return ANIM_STATE_RUNNING;
1056 static unsigned int last_counter = -1;
1057 unsigned int counter = Counter();
1059 printf("::: NEXT ANIM PART [%d, %d]\n",
1060 anim_sync_frame, counter - last_counter);
1062 last_counter = counter;
1066 part->x += part->step_xoffset;
1067 part->y += part->step_yoffset;
1069 return ANIM_STATE_RUNNING;
1072 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1074 struct GlobalAnimPartControlInfo *part;
1075 struct GraphicInfo *c = &anim->control_info;
1076 int state, active_part_nr;
1079 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1080 anim->mode_nr, anim->nr, anim->num_parts);
1081 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1085 printf("::: %s(%d): %d, %d, %d [%d]\n",
1086 (action == ANIM_START ? "ANIM_START" :
1087 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1088 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1090 anim->state & ANIM_STATE_RESTART,
1091 anim->state & ANIM_STATE_WAITING,
1092 anim->state & ANIM_STATE_RUNNING,
1099 anim->state = anim->last_state = ANIM_STATE_RESTART;
1100 anim->active_part_nr = anim->last_active_part_nr = 0;
1101 anim->part_counter = 0;
1106 if (anim->state == ANIM_STATE_INACTIVE)
1109 anim->state = anim->last_state;
1110 anim->active_part_nr = anim->last_active_part_nr;
1115 anim->state = ANIM_STATE_INACTIVE;
1118 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1121 for (i = 0; i < num_parts; i++)
1122 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1131 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1133 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1137 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1138 anim->mode_nr, anim->nr, num_parts);
1141 for (i = 0; i < num_parts; i++)
1143 part = &anim->part[i];
1148 anim->state = ANIM_STATE_RUNNING;
1149 part->state = ANIM_STATE_RESTART;
1154 if (part->state == ANIM_STATE_INACTIVE)
1160 part->state = ANIM_STATE_INACTIVE;
1168 part->state = HandleGlobalAnim_Part(part, part->state);
1170 // when animation mode is "once", stop after animation was played once
1171 if (c->anim_mode & ANIM_ONCE &&
1172 part->state & ANIM_STATE_RESTART)
1173 part->state = ANIM_STATE_INACTIVE;
1176 anim->last_state = anim->state;
1177 anim->last_active_part_nr = anim->active_part_nr;
1182 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1183 anim->active_part_nr = getGlobalAnimationPart(anim);
1185 part = &anim->part[anim->active_part_nr];
1187 part->state = ANIM_STATE_RUNNING;
1189 anim->state = HandleGlobalAnim_Part(part, anim->state);
1191 if (anim->state & ANIM_STATE_RESTART)
1192 anim->part_counter++;
1194 // when animation mode is "once", stop after all animations were played once
1195 if (c->anim_mode & ANIM_ONCE &&
1196 anim->part_counter == anim->num_parts)
1197 anim->state = ANIM_STATE_INACTIVE;
1199 state = anim->state;
1200 active_part_nr = anim->active_part_nr;
1202 // while the animation parts are pausing (waiting or inactive), play the base
1203 // (main) animation; this corresponds to the "boring player animation" logic
1204 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1207 if (anim->state == ANIM_STATE_WAITING ||
1208 anim->state == ANIM_STATE_INACTIVE)
1210 anim->active_part_nr = anim->num_parts; // part nr of base animation
1211 part = &anim->part[anim->active_part_nr];
1213 if (anim->state != anim->last_state)
1214 part->state = ANIM_STATE_RESTART;
1216 anim->state = ANIM_STATE_RUNNING;
1217 part->state = HandleGlobalAnim_Part(part, part->state);
1221 anim->last_state = state;
1222 anim->last_active_part_nr = active_part_nr;
1225 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1230 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1231 ctrl->nr, ctrl->num_anims);
1234 for (i = 0; i < ctrl->num_anims; i++)
1235 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1238 static void HandleGlobalAnim(int action, int game_mode)
1241 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1244 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1247 static void DoAnimationExt()
1252 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1255 // global animations now synchronized with frame delay of screen update
1258 for (i = 0; i < NUM_GAME_MODES; i++)
1259 HandleGlobalAnim(ANIM_CONTINUE, i);
1262 // force screen redraw in next frame to continue drawing global animations
1263 redraw_mask = REDRAW_ALL;