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;
188 /* ========================================================================= */
189 /* generic animation frame calculation */
190 /* ========================================================================= */
192 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
197 sync_frame += start_frame * delay;
199 if (mode & ANIM_LOOP) /* looping animation */
201 frame = (sync_frame % (delay * num_frames)) / delay;
203 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
205 frame = sync_frame / delay;
207 if (frame > num_frames - 1)
208 frame = num_frames - 1;
210 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
212 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
214 frame = (sync_frame % (delay * max_anim_frames)) / delay;
215 frame = (frame < num_frames ? frame : max_anim_frames - frame);
217 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
219 int max_anim_frames = 2 * num_frames;
221 frame = (sync_frame % (delay * max_anim_frames)) / delay;
222 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
224 else if (mode & ANIM_RANDOM) /* play frames in random order */
226 /* note: expect different frames for the same delay cycle! */
228 if (gfx.anim_random_frame < 0)
229 frame = GetSimpleRandom(num_frames);
231 frame = gfx.anim_random_frame % num_frames;
233 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
235 frame = sync_frame % num_frames;
238 if (mode & ANIM_REVERSE) /* use reverse animation direction */
239 frame = num_frames - frame - 1;
245 /* ========================================================================= */
246 /* global animation functions */
247 /* ========================================================================= */
249 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
251 struct GraphicInfo *c = &anim->control_info;
252 int last_anim_random_frame = gfx.anim_random_frame;
255 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
257 part_nr = getAnimationFrame(anim->num_parts, 1,
258 c->anim_mode, c->anim_start_frame,
261 gfx.anim_random_frame = last_anim_random_frame;
266 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
268 const struct GlobalAnimPartControlInfo *o1 =
269 (struct GlobalAnimPartControlInfo *)obj1;
270 const struct GlobalAnimPartControlInfo *o2 =
271 (struct GlobalAnimPartControlInfo *)obj2;
274 if (o1->control_info.draw_order != o2->control_info.draw_order)
275 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
277 compare_result = o1->nr - o2->nr;
279 return compare_result;
282 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
284 const struct GlobalAnimMainControlInfo *o1 =
285 (struct GlobalAnimMainControlInfo *)obj1;
286 const struct GlobalAnimMainControlInfo *o2 =
287 (struct GlobalAnimMainControlInfo *)obj2;
290 if (o1->control_info.draw_order != o2->control_info.draw_order)
291 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
293 compare_result = o1->nr - o2->nr;
295 return compare_result;
298 static void InitToonControls()
300 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
301 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
302 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
303 int mode_nr, anim_nr, part_nr;
304 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
305 int num_toons = MAX_NUM_TOONS;
308 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
309 num_toons = global.num_toons;
311 mode_nr = mode_nr_toons;
312 anim_nr = ctrl->num_anims;
315 anim->mode_nr = mode_nr;
316 anim->control_info = graphic_info[control];
319 anim->part_counter = 0;
320 anim->active_part_nr = 0;
322 anim->has_base = FALSE;
324 anim->init_delay_counter = 0;
326 anim->state = ANIM_STATE_INACTIVE;
330 for (i = 0; i < num_toons; i++)
332 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
333 int sound = SND_UNDEFINED;
334 int music = MUS_UNDEFINED;
335 int graphic = IMG_TOON_1 + i;
336 int control = graphic;
339 part->anim_nr = anim_nr;
340 part->mode_nr = mode_nr;
343 part->graphic = graphic;
344 part->graphic_info = graphic_info[graphic];
345 part->control_info = graphic_info[control];
347 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
349 part->control_info.init_delay_fixed = 0;
350 part->control_info.init_delay_random = 150;
352 part->control_info.x = ARG_UNDEFINED_VALUE;
353 part->control_info.y = ARG_UNDEFINED_VALUE;
355 part->initial_anim_sync_frame = 0;
357 part->step_delay = 0;
358 part->step_delay_value = graphic_info[control].step_delay;
360 part->state = ANIM_STATE_INACTIVE;
361 part->last_anim_status = -1;
370 void InitGlobalAnimControls()
373 int mode_nr, anim_nr, part_nr;
374 int sound, music, graphic, control;
378 for (m = 0; m < NUM_GAME_MODES; m++)
382 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
389 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
391 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
392 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
394 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
396 // if no base animation parameters defined, use default values
397 if (control == IMG_UNDEFINED)
398 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
401 anim->mode_nr = mode_nr;
402 anim->control_info = graphic_info[control];
405 anim->part_counter = 0;
406 anim->active_part_nr = 0;
408 anim->has_base = FALSE;
410 anim->init_delay_counter = 0;
412 anim->state = ANIM_STATE_INACTIVE;
416 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
418 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
420 sound = global_anim_info[a].sound[p][m];
421 music = global_anim_info[a].music[p][m];
422 graphic = global_anim_info[a].graphic[p][m];
423 control = global_anim_info[ctrl_id].graphic[p][m];
425 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
426 control == IMG_UNDEFINED)
430 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
431 m, a, p, mode_nr, anim_nr, part_nr, control);
435 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
436 m, a, p, mode_nr, anim_nr, part_nr, sound);
440 part->anim_nr = anim_nr;
441 part->mode_nr = mode_nr;
444 part->graphic = graphic;
445 part->graphic_info = graphic_info[graphic];
446 part->control_info = graphic_info[control];
448 part->initial_anim_sync_frame = 0;
450 part->step_delay = 0;
451 part->step_delay_value = graphic_info[control].step_delay;
453 part->state = ANIM_STATE_INACTIVE;
454 part->last_anim_status = -1;
456 if (p < GLOBAL_ANIM_ID_PART_BASE)
464 anim->has_base = TRUE;
468 if (anim->num_parts > 0 || anim->has_base)
478 /* sort all animations according to draw_order and animation number */
479 for (m = 0; m < NUM_GAME_MODES; m++)
481 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
483 /* sort all main animations for this game mode */
484 qsort(ctrl->anim, ctrl->num_anims,
485 sizeof(struct GlobalAnimMainControlInfo),
486 compareGlobalAnimMainControlInfo);
488 for (a = 0; a < ctrl->num_anims; a++)
490 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
492 /* sort all animation parts for this main animation */
493 qsort(anim->part, anim->num_parts,
494 sizeof(struct GlobalAnimPartControlInfo),
495 compareGlobalAnimPartControlInfo);
499 for (i = 0; i < NUM_GAME_MODES; i++)
500 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
501 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
502 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
503 game_mode_anim_classes_list[i].class;
505 for (i = 0; i < NUM_ANIM_CLASSES; i++)
506 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
507 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
508 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
509 anim_class_game_modes_list[i].game_mode;
511 anim_status_last = GAME_MODE_LOADING;
512 anim_classes_last = ANIM_CLASS_NONE;
515 void InitGlobalAnimations()
517 InitGlobalAnimControls();
520 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
522 int game_mode_anim_action[NUM_GAME_MODES];
528 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
531 // always start with reliable default values (no animation actions)
532 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
533 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
535 if (global.anim_status != anim_status_last)
537 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
538 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
539 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
542 // ---------- part 1 ------------------------------------------------------
543 // start or stop global animations by change of game mode
544 // (special handling of animations for "current screen" and "all screens")
546 // stop animations for last screen
547 game_mode_anim_action[anim_status_last] = ANIM_STOP;
549 // start animations for current screen
550 game_mode_anim_action[global.anim_status] = ANIM_START;
552 // start animations for all screens after loading new artwork set
553 if (anim_status_last == GAME_MODE_LOADING)
554 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
556 // ---------- part 2 ------------------------------------------------------
557 // start or stop global animations by change of animation class
558 // (generic handling of animations for "class of screens")
560 for (i = 0; i < NUM_ANIM_CLASSES; i++)
562 int anim_class_check = (1 << i);
563 int anim_class_game_mode = anim_class_game_modes[i];
564 int anim_class_last = anim_classes_last & anim_class_check;
565 int anim_class_next = anim_classes_next & anim_class_check;
567 // stop animations for changed screen class before fading to new screen
568 if (before_fading && anim_class_last && !anim_class_next)
569 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
571 // start animations for changed screen class after fading to new screen
572 if (after_fading && !anim_class_last && anim_class_next)
573 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
577 anim_classes_last = anim_classes_next;
579 anim_status_last = global.anim_status;
581 // start or stop animations determined to be started or stopped above
582 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
583 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
584 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
587 if (global.anim_status == GAME_MODE_LOADING)
590 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
592 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
596 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
597 mode_nr != game_status)
601 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
603 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
604 struct GraphicInfo *c = &anim->control_info;
605 int part_first, part_last;
608 if (!(anim->state & ANIM_STATE_RUNNING))
611 part_first = part_last = anim->active_part_nr;
613 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
615 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
618 part_last = num_parts - 1;
621 for (part_nr = part_first; part_nr <= part_last; part_nr++)
623 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
624 struct GraphicInfo *g = &part->graphic_info;
627 int width = g->width;
628 int height = g->height;
636 if (!(part->state & ANIM_STATE_RUNNING))
639 if (part->drawing_stage != drawing_stage)
648 else if (part->x > part->viewport_width - g->width)
649 width -= (part->x - (part->viewport_width - g->width));
657 else if (part->y > part->viewport_height - g->height)
658 height -= (part->y - (part->viewport_height - g->height));
660 if (width <= 0 || height <= 0)
663 dst_x += part->viewport_x;
664 dst_y += part->viewport_y;
666 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
667 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
668 g->anim_mode, g->anim_start_frame,
671 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
677 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
684 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
686 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
689 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
695 boolean changed = FALSE;
697 if (part->last_anim_status == global.anim_status)
700 part->last_anim_status = global.anim_status;
702 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
704 if (part->control_info.class == get_hash_from_key("window") ||
705 part->control_info.class == get_hash_from_key("border"))
709 viewport_width = WIN_XSIZE;
710 viewport_height = WIN_YSIZE;
712 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
714 else if (part->control_info.class == get_hash_from_key("door_1"))
718 viewport_width = DXSIZE;
719 viewport_height = DYSIZE;
721 else if (part->control_info.class == get_hash_from_key("door_2"))
725 viewport_width = VXSIZE;
726 viewport_height = VYSIZE;
728 else // default: "playfield"
730 viewport_x = REAL_SX;
731 viewport_y = REAL_SY;
732 viewport_width = FULL_SXSIZE;
733 viewport_height = FULL_SYSIZE;
736 if (viewport_x != part->viewport_x ||
737 viewport_y != part->viewport_y ||
738 viewport_width != part->viewport_width ||
739 viewport_height != part->viewport_height)
741 part->viewport_x = viewport_x;
742 part->viewport_y = viewport_y;
743 part->viewport_width = viewport_width;
744 part->viewport_height = viewport_height;
752 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
754 int sound = part->sound;
756 if (sound == SND_UNDEFINED)
759 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
760 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
763 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
764 if (IS_LOOP_SOUND(sound))
765 PlaySoundLoop(sound);
770 printf("::: PLAY SOUND %d.%d.%d: %d\n",
771 part->anim_nr, part->nr, part->mode_nr, sound);
775 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
777 int sound = part->sound;
779 if (sound == SND_UNDEFINED)
785 printf("::: STOP SOUND %d.%d.%d: %d\n",
786 part->anim_nr, part->nr, part->mode_nr, sound);
790 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
792 int music = part->music;
794 if (music == MUS_UNDEFINED)
797 if (!setup.sound_music)
803 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
804 part->anim_nr, part->nr, part->mode_nr, music);
808 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
810 int music = part->music;
812 if (music == MUS_UNDEFINED)
818 printf("::: STOP MUSIC %d.%d.%d: %d\n",
819 part->anim_nr, part->nr, part->mode_nr, music);
823 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
825 PlayGlobalAnimSound(part);
826 PlayGlobalAnimMusic(part);
829 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
831 StopGlobalAnimSound(part);
832 StopGlobalAnimMusic(part);
835 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
837 struct GraphicInfo *g = &part->graphic_info;
838 struct GraphicInfo *c = &part->control_info;
839 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
841 if (viewport_changed)
842 state |= ANIM_STATE_RESTART;
844 if (state & ANIM_STATE_RESTART)
846 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
848 part->init_delay_counter =
849 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
851 part->anim_delay_counter =
852 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
854 part->initial_anim_sync_frame =
855 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
857 if (c->direction & MV_HORIZONTAL)
859 int pos_bottom = part->viewport_height - g->height;
861 if (c->position == POS_TOP)
863 else if (c->position == POS_UPPER)
864 part->y = GetSimpleRandom(pos_bottom / 2);
865 else if (c->position == POS_MIDDLE)
866 part->y = pos_bottom / 2;
867 else if (c->position == POS_LOWER)
868 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
869 else if (c->position == POS_BOTTOM)
870 part->y = pos_bottom;
872 part->y = GetSimpleRandom(pos_bottom);
874 if (c->direction == MV_RIGHT)
876 part->step_xoffset = c->step_offset;
877 part->x = -g->width + part->step_xoffset;
881 part->step_xoffset = -c->step_offset;
882 part->x = part->viewport_width + part->step_xoffset;
885 part->step_yoffset = 0;
887 else if (c->direction & MV_VERTICAL)
889 int pos_right = part->viewport_width - g->width;
891 if (c->position == POS_LEFT)
893 else if (c->position == POS_RIGHT)
896 part->x = GetSimpleRandom(pos_right);
898 if (c->direction == MV_DOWN)
900 part->step_yoffset = c->step_offset;
901 part->y = -g->height + part->step_yoffset;
905 part->step_yoffset = -c->step_offset;
906 part->y = part->viewport_height + part->step_yoffset;
909 part->step_xoffset = 0;
916 part->step_xoffset = 0;
917 part->step_yoffset = 0;
920 if (c->x != ARG_UNDEFINED_VALUE)
922 if (c->y != ARG_UNDEFINED_VALUE)
925 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
926 part->step_xoffset = c->step_xoffset;
927 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
928 part->step_yoffset = c->step_yoffset;
930 if (part->init_delay_counter == 0)
931 PlayGlobalAnimSoundAndMusic(part);
934 if (part->init_delay_counter > 0)
936 part->init_delay_counter--;
938 if (part->init_delay_counter == 0)
939 PlayGlobalAnimSoundAndMusic(part);
941 return ANIM_STATE_WAITING;
944 // check if moving animation has left the visible screen area
945 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
946 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
947 (part->y <= -g->height && part->step_yoffset <= 0) ||
948 (part->y >= part->viewport_height && part->step_yoffset >= 0))
950 // do not stop animation before "anim" or "post" counter are finished
951 if (part->anim_delay_counter == 0 &&
952 part->post_delay_counter == 0)
954 StopGlobalAnimSoundAndMusic(part);
956 part->post_delay_counter =
957 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
959 if (part->post_delay_counter > 0)
960 return ANIM_STATE_RUNNING;
962 // drawing last frame not needed here -- animation not visible anymore
963 return ANIM_STATE_RESTART;
967 if (part->anim_delay_counter > 0)
969 part->anim_delay_counter--;
971 if (part->anim_delay_counter == 0)
973 StopGlobalAnimSoundAndMusic(part);
975 part->post_delay_counter =
976 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
978 if (part->post_delay_counter > 0)
979 return ANIM_STATE_RUNNING;
981 // additional state "RUNNING" required to not skip drawing last frame
982 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
986 if (part->post_delay_counter > 0)
988 part->post_delay_counter--;
990 if (part->post_delay_counter == 0)
991 return ANIM_STATE_RESTART;
993 return ANIM_STATE_WAITING;
996 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
998 return ANIM_STATE_RUNNING;
1002 static unsigned int last_counter = -1;
1003 unsigned int counter = Counter();
1005 printf("::: NEXT ANIM PART [%d, %d]\n",
1006 anim_sync_frame, counter - last_counter);
1008 last_counter = counter;
1012 part->x += part->step_xoffset;
1013 part->y += part->step_yoffset;
1015 return ANIM_STATE_RUNNING;
1018 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1020 struct GlobalAnimPartControlInfo *part;
1021 struct GraphicInfo *c = &anim->control_info;
1022 int state, active_part_nr;
1025 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1026 anim->mode_nr, anim->nr, anim->num_parts);
1027 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1031 printf("::: %s(%d): %d, %d, %d [%d]\n",
1032 (action == ANIM_START ? "ANIM_START" :
1033 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1034 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1036 anim->state & ANIM_STATE_RESTART,
1037 anim->state & ANIM_STATE_WAITING,
1038 anim->state & ANIM_STATE_RUNNING,
1045 anim->state = anim->last_state = ANIM_STATE_RESTART;
1046 anim->active_part_nr = anim->last_active_part_nr = 0;
1047 anim->part_counter = 0;
1052 if (anim->state == ANIM_STATE_INACTIVE)
1055 anim->state = anim->last_state;
1056 anim->active_part_nr = anim->last_active_part_nr;
1061 anim->state = ANIM_STATE_INACTIVE;
1064 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1067 for (i = 0; i < num_parts; i++)
1068 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1077 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1079 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1083 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1084 anim->mode_nr, anim->nr, num_parts);
1087 for (i = 0; i < num_parts; i++)
1089 part = &anim->part[i];
1094 anim->state = ANIM_STATE_RUNNING;
1095 part->state = ANIM_STATE_RESTART;
1100 if (part->state == ANIM_STATE_INACTIVE)
1106 part->state = ANIM_STATE_INACTIVE;
1114 part->state = HandleGlobalAnim_Part(part, part->state);
1116 // when animation mode is "once", stop after animation was played once
1117 if (c->anim_mode & ANIM_ONCE &&
1118 part->state & ANIM_STATE_RESTART)
1119 part->state = ANIM_STATE_INACTIVE;
1122 anim->last_state = anim->state;
1123 anim->last_active_part_nr = anim->active_part_nr;
1128 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1129 anim->active_part_nr = getGlobalAnimationPart(anim);
1131 part = &anim->part[anim->active_part_nr];
1133 part->state = ANIM_STATE_RUNNING;
1135 anim->state = HandleGlobalAnim_Part(part, anim->state);
1137 if (anim->state & ANIM_STATE_RESTART)
1138 anim->part_counter++;
1140 // when animation mode is "once", stop after all animations were played once
1141 if (c->anim_mode & ANIM_ONCE &&
1142 anim->part_counter == anim->num_parts)
1143 anim->state = ANIM_STATE_INACTIVE;
1145 state = anim->state;
1146 active_part_nr = anim->active_part_nr;
1148 // while the animation parts are pausing (waiting or inactive), play the base
1149 // (main) animation; this corresponds to the "boring player animation" logic
1150 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1153 if (anim->state == ANIM_STATE_WAITING ||
1154 anim->state == ANIM_STATE_INACTIVE)
1156 anim->active_part_nr = anim->num_parts; // part nr of base animation
1157 part = &anim->part[anim->active_part_nr];
1159 if (anim->state != anim->last_state)
1160 part->state = ANIM_STATE_RESTART;
1162 anim->state = ANIM_STATE_RUNNING;
1163 part->state = HandleGlobalAnim_Part(part, part->state);
1167 anim->last_state = state;
1168 anim->last_active_part_nr = active_part_nr;
1171 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1176 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1177 ctrl->nr, ctrl->num_anims);
1180 for (i = 0; i < ctrl->num_anims; i++)
1181 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1184 static void HandleGlobalAnim(int action, int game_mode)
1187 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1190 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1193 static void DoAnimationExt()
1198 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1201 // global animations now synchronized with frame delay of screen update
1204 for (i = 0; i < NUM_GAME_MODES; i++)
1205 HandleGlobalAnim(ANIM_CONTINUE, i);
1208 // force screen redraw in next frame to continue drawing global animations
1209 redraw_mask = REDRAW_ALL;