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_stage)
527 if (global.anim_status != anim_status_last)
529 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
530 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
531 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
534 // ---------- part 1 ------------------------------------------------------
535 // start or stop global animations by change of game mode
536 // (special handling of animations for "current screen" and "all screens")
538 // stop animations for last screen
539 HandleGlobalAnim(ANIM_STOP, anim_status_last);
541 // start animations for current screen
542 HandleGlobalAnim(ANIM_START, global.anim_status);
544 // start animations for all screens after loading new artwork set
545 if (anim_status_last == GAME_MODE_LOADING)
546 HandleGlobalAnim(ANIM_START, GAME_MODE_DEFAULT);
548 // ---------- part 2 ------------------------------------------------------
549 // start or stop global animations by change of animation class
550 // (generic handling of animations for "class of screens")
552 for (i = 0; i < NUM_ANIM_CLASSES; i++)
554 int anim_class_check = (1 << i);
555 int anim_class_game_mode = anim_class_game_modes[i];
556 int anim_class_last = anim_classes_last & anim_class_check;
557 int anim_class_next = anim_classes_next & anim_class_check;
559 // stop animations for changed screen class before fading to new screen
560 if (before_fading && anim_class_last && !anim_class_next)
561 HandleGlobalAnim(ANIM_STOP, anim_class_game_mode);
563 // start animations for changed screen class after fading to new screen
564 if (after_fading && !anim_class_last && anim_class_next)
565 HandleGlobalAnim(ANIM_START, anim_class_game_mode);
569 anim_classes_last = anim_classes_next;
571 anim_status_last = global.anim_status;
574 if (global.anim_status == GAME_MODE_LOADING)
577 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
580 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
582 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
586 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
587 mode_nr != game_status)
591 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
593 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
594 struct GraphicInfo *c = &anim->control_info;
595 int part_first, part_last;
598 if (!(anim->state & ANIM_STATE_RUNNING))
601 part_first = part_last = anim->active_part_nr;
603 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
605 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
608 part_last = num_parts - 1;
611 for (part_nr = part_first; part_nr <= part_last; part_nr++)
613 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
614 struct GraphicInfo *g = &part->graphic_info;
617 int width = g->width;
618 int height = g->height;
626 if (!(part->state & ANIM_STATE_RUNNING))
629 if (part->drawing_stage != drawing_stage)
638 else if (part->x > part->viewport_width - g->width)
639 width -= (part->x - (part->viewport_width - g->width));
647 else if (part->y > part->viewport_height - g->height)
648 height -= (part->y - (part->viewport_height - g->height));
650 if (width <= 0 || height <= 0)
653 dst_x += part->viewport_x;
654 dst_y += part->viewport_y;
656 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
657 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
658 g->anim_mode, g->anim_start_frame,
661 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
667 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
674 void DrawGlobalAnimations(int drawing_stage)
676 DrawGlobalAnimationsExt(drawing_stage);
679 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
685 boolean changed = FALSE;
687 if (part->last_anim_status == global.anim_status)
690 part->last_anim_status = global.anim_status;
692 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
694 if (part->control_info.class == get_hash_from_key("window") ||
695 part->control_info.class == get_hash_from_key("border"))
699 viewport_width = WIN_XSIZE;
700 viewport_height = WIN_YSIZE;
702 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
704 else if (part->control_info.class == get_hash_from_key("door_1"))
708 viewport_width = DXSIZE;
709 viewport_height = DYSIZE;
711 else if (part->control_info.class == get_hash_from_key("door_2"))
715 viewport_width = VXSIZE;
716 viewport_height = VYSIZE;
718 else // default: "playfield"
720 viewport_x = REAL_SX;
721 viewport_y = REAL_SY;
722 viewport_width = FULL_SXSIZE;
723 viewport_height = FULL_SYSIZE;
726 if (viewport_x != part->viewport_x ||
727 viewport_y != part->viewport_y ||
728 viewport_width != part->viewport_width ||
729 viewport_height != part->viewport_height)
731 part->viewport_x = viewport_x;
732 part->viewport_y = viewport_y;
733 part->viewport_width = viewport_width;
734 part->viewport_height = viewport_height;
742 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
744 int sound = part->sound;
746 if (sound == SND_UNDEFINED)
749 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
750 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
753 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
754 if (IS_LOOP_SOUND(sound))
755 PlaySoundLoop(sound);
760 printf("::: PLAY SOUND %d.%d.%d: %d\n",
761 part->anim_nr, part->nr, part->mode_nr, sound);
765 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
767 int sound = part->sound;
769 if (sound == SND_UNDEFINED)
775 printf("::: STOP SOUND %d.%d.%d: %d\n",
776 part->anim_nr, part->nr, part->mode_nr, sound);
780 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
782 int music = part->music;
784 if (music == MUS_UNDEFINED)
787 if (!setup.sound_music)
793 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
794 part->anim_nr, part->nr, part->mode_nr, music);
798 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
800 int music = part->music;
802 if (music == MUS_UNDEFINED)
808 printf("::: STOP MUSIC %d.%d.%d: %d\n",
809 part->anim_nr, part->nr, part->mode_nr, music);
813 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
815 PlayGlobalAnimSound(part);
816 PlayGlobalAnimMusic(part);
819 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
821 StopGlobalAnimSound(part);
822 StopGlobalAnimMusic(part);
825 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
827 struct GraphicInfo *g = &part->graphic_info;
828 struct GraphicInfo *c = &part->control_info;
829 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
831 if (viewport_changed)
832 state |= ANIM_STATE_RESTART;
834 if (state & ANIM_STATE_RESTART)
836 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
838 part->init_delay_counter =
839 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
841 part->anim_delay_counter =
842 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
844 part->initial_anim_sync_frame =
845 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
847 if (c->direction & MV_HORIZONTAL)
849 int pos_bottom = part->viewport_height - g->height;
851 if (c->position == POS_TOP)
853 else if (c->position == POS_UPPER)
854 part->y = GetSimpleRandom(pos_bottom / 2);
855 else if (c->position == POS_MIDDLE)
856 part->y = pos_bottom / 2;
857 else if (c->position == POS_LOWER)
858 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
859 else if (c->position == POS_BOTTOM)
860 part->y = pos_bottom;
862 part->y = GetSimpleRandom(pos_bottom);
864 if (c->direction == MV_RIGHT)
866 part->step_xoffset = c->step_offset;
867 part->x = -g->width + part->step_xoffset;
871 part->step_xoffset = -c->step_offset;
872 part->x = part->viewport_width + part->step_xoffset;
875 part->step_yoffset = 0;
877 else if (c->direction & MV_VERTICAL)
879 int pos_right = part->viewport_width - g->width;
881 if (c->position == POS_LEFT)
883 else if (c->position == POS_RIGHT)
886 part->x = GetSimpleRandom(pos_right);
888 if (c->direction == MV_DOWN)
890 part->step_yoffset = c->step_offset;
891 part->y = -g->height + part->step_yoffset;
895 part->step_yoffset = -c->step_offset;
896 part->y = part->viewport_height + part->step_yoffset;
899 part->step_xoffset = 0;
906 part->step_xoffset = 0;
907 part->step_yoffset = 0;
910 if (c->x != ARG_UNDEFINED_VALUE)
912 if (c->y != ARG_UNDEFINED_VALUE)
915 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
916 part->step_xoffset = c->step_xoffset;
917 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
918 part->step_yoffset = c->step_yoffset;
920 if (part->init_delay_counter == 0)
921 PlayGlobalAnimSoundAndMusic(part);
924 if (part->init_delay_counter > 0)
926 part->init_delay_counter--;
928 if (part->init_delay_counter == 0)
929 PlayGlobalAnimSoundAndMusic(part);
931 return ANIM_STATE_WAITING;
934 // check if moving animation has left the visible screen area
935 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
936 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
937 (part->y <= -g->height && part->step_yoffset <= 0) ||
938 (part->y >= part->viewport_height && part->step_yoffset >= 0))
940 // do not stop animation before "anim" or "post" counter are finished
941 if (part->anim_delay_counter == 0 &&
942 part->post_delay_counter == 0)
944 StopGlobalAnimSoundAndMusic(part);
946 part->post_delay_counter =
947 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
949 if (part->post_delay_counter > 0)
950 return ANIM_STATE_RUNNING;
952 // drawing last frame not needed here -- animation not visible anymore
953 return ANIM_STATE_RESTART;
957 if (part->anim_delay_counter > 0)
959 part->anim_delay_counter--;
961 if (part->anim_delay_counter == 0)
963 StopGlobalAnimSoundAndMusic(part);
965 part->post_delay_counter =
966 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
968 if (part->post_delay_counter > 0)
969 return ANIM_STATE_RUNNING;
971 // additional state "RUNNING" required to not skip drawing last frame
972 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
976 if (part->post_delay_counter > 0)
978 part->post_delay_counter--;
980 if (part->post_delay_counter == 0)
981 return ANIM_STATE_RESTART;
983 return ANIM_STATE_WAITING;
986 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
988 return ANIM_STATE_RUNNING;
992 static unsigned int last_counter = -1;
993 unsigned int counter = Counter();
995 printf("::: NEXT ANIM PART [%d, %d]\n",
996 anim_sync_frame, counter - last_counter);
998 last_counter = counter;
1002 part->x += part->step_xoffset;
1003 part->y += part->step_yoffset;
1005 return ANIM_STATE_RUNNING;
1008 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1010 struct GlobalAnimPartControlInfo *part;
1011 struct GraphicInfo *c = &anim->control_info;
1012 int state, active_part_nr;
1015 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1016 anim->mode_nr, anim->nr, anim->num_parts);
1017 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1021 printf("::: %s(%d): %d, %d, %d [%d]\n",
1022 (action == ANIM_START ? "ANIM_START" :
1023 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1024 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1026 anim->state & ANIM_STATE_RESTART,
1027 anim->state & ANIM_STATE_WAITING,
1028 anim->state & ANIM_STATE_RUNNING,
1035 anim->state = anim->last_state = ANIM_STATE_RESTART;
1036 anim->active_part_nr = anim->last_active_part_nr = 0;
1037 anim->part_counter = 0;
1042 if (anim->state == ANIM_STATE_INACTIVE)
1045 anim->state = anim->last_state;
1046 anim->active_part_nr = anim->last_active_part_nr;
1051 anim->state = ANIM_STATE_INACTIVE;
1054 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1057 for (i = 0; i < num_parts; i++)
1058 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1067 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1069 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1073 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1074 anim->mode_nr, anim->nr, num_parts);
1077 for (i = 0; i < num_parts; i++)
1079 part = &anim->part[i];
1084 anim->state = ANIM_STATE_RUNNING;
1085 part->state = ANIM_STATE_RESTART;
1090 if (part->state == ANIM_STATE_INACTIVE)
1096 part->state = ANIM_STATE_INACTIVE;
1104 part->state = HandleGlobalAnim_Part(part, part->state);
1106 // when animation mode is "once", stop after animation was played once
1107 if (c->anim_mode & ANIM_ONCE &&
1108 part->state & ANIM_STATE_RESTART)
1109 part->state = ANIM_STATE_INACTIVE;
1112 anim->last_state = anim->state;
1113 anim->last_active_part_nr = anim->active_part_nr;
1118 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1119 anim->active_part_nr = getGlobalAnimationPart(anim);
1121 part = &anim->part[anim->active_part_nr];
1123 part->state = ANIM_STATE_RUNNING;
1125 anim->state = HandleGlobalAnim_Part(part, anim->state);
1127 if (anim->state & ANIM_STATE_RESTART)
1128 anim->part_counter++;
1130 // when animation mode is "once", stop after all animations were played once
1131 if (c->anim_mode & ANIM_ONCE &&
1132 anim->part_counter == anim->num_parts)
1133 anim->state = ANIM_STATE_INACTIVE;
1135 state = anim->state;
1136 active_part_nr = anim->active_part_nr;
1138 // while the animation parts are pausing (waiting or inactive), play the base
1139 // (main) animation; this corresponds to the "boring player animation" logic
1140 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1143 if (anim->state == ANIM_STATE_WAITING ||
1144 anim->state == ANIM_STATE_INACTIVE)
1146 anim->active_part_nr = anim->num_parts; // part nr of base animation
1147 part = &anim->part[anim->active_part_nr];
1149 if (anim->state != anim->last_state)
1150 part->state = ANIM_STATE_RESTART;
1152 anim->state = ANIM_STATE_RUNNING;
1153 part->state = HandleGlobalAnim_Part(part, part->state);
1157 anim->last_state = state;
1158 anim->last_active_part_nr = active_part_nr;
1161 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1166 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1167 ctrl->nr, ctrl->num_anims);
1170 for (i = 0; i < ctrl->num_anims; i++)
1171 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1174 static void HandleGlobalAnim(int action, int game_mode)
1177 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1180 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1183 static void DoAnimationExt()
1188 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1191 // global animations now synchronized with frame delay of screen update
1194 for (i = 0; i < NUM_GAME_MODES; i++)
1195 HandleGlobalAnim(ANIM_CONTINUE, i);
1198 // force screen redraw in next frame to continue drawing global animations
1199 redraw_mask = REDRAW_ALL;