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;
104 int last_anim_status;
107 struct GlobalAnimMainControlInfo
109 struct GlobalAnimPartControlInfo base;
110 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
115 struct GraphicInfo control_info;
123 int init_delay_counter;
127 int last_state, last_active_part_nr;
130 struct GlobalAnimControlInfo
132 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
138 struct GameModeAnimClass
142 } game_mode_anim_classes_list[] =
144 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
145 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
146 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
147 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
148 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
149 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
150 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
151 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
152 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
153 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
154 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
155 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
156 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
157 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
158 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
159 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
160 { GAME_MODE_SCORES, ANIM_CLASS_TOONS },
165 struct AnimClassGameMode
169 } anim_class_game_modes_list[] =
171 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
172 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
173 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
174 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
175 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
176 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
181 /* forward declaration for internal use */
182 static void HandleGlobalAnim(int, int);
183 static void DoAnimationExt(void);
184 static void ResetGlobalAnim_Clickable();
185 static void ResetGlobalAnim_Clicked();
187 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
189 static unsigned int anim_sync_frame = 0;
191 static int game_mode_anim_classes[NUM_GAME_MODES];
192 static int anim_class_game_modes[NUM_ANIM_CLASSES];
194 static int anim_status_last = GAME_MODE_DEFAULT;
195 static int anim_classes_last = ANIM_CLASS_NONE;
197 static boolean drawing_to_fading_buffer = FALSE;
200 /* ========================================================================= */
201 /* generic animation frame calculation */
202 /* ========================================================================= */
204 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
209 sync_frame += start_frame * delay;
211 if (mode & ANIM_LOOP) /* looping animation */
213 frame = (sync_frame % (delay * num_frames)) / delay;
215 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
217 frame = sync_frame / delay;
219 if (frame > num_frames - 1)
220 frame = num_frames - 1;
222 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
224 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
226 frame = (sync_frame % (delay * max_anim_frames)) / delay;
227 frame = (frame < num_frames ? frame : max_anim_frames - frame);
229 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
231 int max_anim_frames = 2 * num_frames;
233 frame = (sync_frame % (delay * max_anim_frames)) / delay;
234 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
236 else if (mode & ANIM_RANDOM) /* play frames in random order */
238 /* note: expect different frames for the same delay cycle! */
240 if (gfx.anim_random_frame < 0)
241 frame = GetSimpleRandom(num_frames);
243 frame = gfx.anim_random_frame % num_frames;
245 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
247 frame = sync_frame % num_frames;
250 if (mode & ANIM_REVERSE) /* use reverse animation direction */
251 frame = num_frames - frame - 1;
257 /* ========================================================================= */
258 /* global animation functions */
259 /* ========================================================================= */
261 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
263 struct GraphicInfo *c = &anim->control_info;
264 int last_anim_random_frame = gfx.anim_random_frame;
267 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
269 part_nr = getAnimationFrame(anim->num_parts, 1,
270 c->anim_mode, c->anim_start_frame,
273 gfx.anim_random_frame = last_anim_random_frame;
278 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
280 const struct GlobalAnimPartControlInfo *o1 =
281 (struct GlobalAnimPartControlInfo *)obj1;
282 const struct GlobalAnimPartControlInfo *o2 =
283 (struct GlobalAnimPartControlInfo *)obj2;
286 if (o1->control_info.draw_order != o2->control_info.draw_order)
287 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
289 compare_result = o1->nr - o2->nr;
291 return compare_result;
294 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
296 const struct GlobalAnimMainControlInfo *o1 =
297 (struct GlobalAnimMainControlInfo *)obj1;
298 const struct GlobalAnimMainControlInfo *o2 =
299 (struct GlobalAnimMainControlInfo *)obj2;
302 if (o1->control_info.draw_order != o2->control_info.draw_order)
303 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
305 compare_result = o1->nr - o2->nr;
307 return compare_result;
310 static void InitToonControls()
312 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
313 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
314 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
315 int mode_nr, anim_nr, part_nr;
316 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
317 int num_toons = MAX_NUM_TOONS;
320 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
321 num_toons = global.num_toons;
323 mode_nr = mode_nr_toons;
324 anim_nr = ctrl->num_anims;
327 anim->mode_nr = mode_nr;
328 anim->control_info = graphic_info[control];
331 anim->part_counter = 0;
332 anim->active_part_nr = 0;
334 anim->has_base = FALSE;
336 anim->init_delay_counter = 0;
338 anim->state = ANIM_STATE_INACTIVE;
342 for (i = 0; i < num_toons; i++)
344 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
345 int sound = SND_UNDEFINED;
346 int music = MUS_UNDEFINED;
347 int graphic = IMG_TOON_1 + i;
348 int control = graphic;
351 part->anim_nr = anim_nr;
352 part->mode_nr = mode_nr;
355 part->graphic = graphic;
356 part->graphic_info = graphic_info[graphic];
357 part->control_info = graphic_info[control];
359 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
361 part->control_info.init_delay_fixed = 0;
362 part->control_info.init_delay_random = 150;
364 part->control_info.x = ARG_UNDEFINED_VALUE;
365 part->control_info.y = ARG_UNDEFINED_VALUE;
367 part->initial_anim_sync_frame = 0;
369 part->step_delay = 0;
370 part->step_delay_value = graphic_info[control].step_delay;
372 part->state = ANIM_STATE_INACTIVE;
373 part->last_anim_status = -1;
382 void InitGlobalAnimControls()
385 int mode_nr, anim_nr, part_nr;
386 int sound, music, graphic, control;
390 for (m = 0; m < NUM_GAME_MODES; m++)
394 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
401 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
403 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
404 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
406 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
408 // if no base animation parameters defined, use default values
409 if (control == IMG_UNDEFINED)
410 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
413 anim->mode_nr = mode_nr;
414 anim->control_info = graphic_info[control];
417 anim->part_counter = 0;
418 anim->active_part_nr = 0;
420 anim->has_base = FALSE;
422 anim->init_delay_counter = 0;
424 anim->state = ANIM_STATE_INACTIVE;
428 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
430 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
432 sound = global_anim_info[a].sound[p][m];
433 music = global_anim_info[a].music[p][m];
434 graphic = global_anim_info[a].graphic[p][m];
435 control = global_anim_info[ctrl_id].graphic[p][m];
437 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
438 control == IMG_UNDEFINED)
442 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
443 m, a, p, mode_nr, anim_nr, part_nr, control);
447 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
448 m, a, p, mode_nr, anim_nr, part_nr, sound);
452 part->anim_nr = anim_nr;
453 part->mode_nr = mode_nr;
456 part->graphic = graphic;
457 part->graphic_info = graphic_info[graphic];
458 part->control_info = graphic_info[control];
460 part->initial_anim_sync_frame = 0;
462 part->step_delay = 0;
463 part->step_delay_value = graphic_info[control].step_delay;
465 part->state = ANIM_STATE_INACTIVE;
466 part->last_anim_status = -1;
468 if (p < GLOBAL_ANIM_ID_PART_BASE)
476 anim->has_base = TRUE;
480 if (anim->num_parts > 0 || anim->has_base)
490 /* sort all animations according to draw_order and animation number */
491 for (m = 0; m < NUM_GAME_MODES; m++)
493 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
495 /* sort all main animations for this game mode */
496 qsort(ctrl->anim, ctrl->num_anims,
497 sizeof(struct GlobalAnimMainControlInfo),
498 compareGlobalAnimMainControlInfo);
500 for (a = 0; a < ctrl->num_anims; a++)
502 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
504 /* sort all animation parts for this main animation */
505 qsort(anim->part, anim->num_parts,
506 sizeof(struct GlobalAnimPartControlInfo),
507 compareGlobalAnimPartControlInfo);
511 for (i = 0; i < NUM_GAME_MODES; i++)
512 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
513 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
514 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
515 game_mode_anim_classes_list[i].class;
517 for (i = 0; i < NUM_ANIM_CLASSES; i++)
518 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
519 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
520 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
521 anim_class_game_modes_list[i].game_mode;
523 anim_status_last = GAME_MODE_LOADING;
524 anim_classes_last = ANIM_CLASS_NONE;
527 void InitGlobalAnimations()
529 InitGlobalAnimControls();
532 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
534 Bitmap *fade_bitmap =
535 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
536 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
537 int game_mode_anim_action[NUM_GAME_MODES];
543 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
544 drawing_target == DRAW_TO_SCREEN)
547 // always start with reliable default values (no animation actions)
548 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
549 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
551 if (global.anim_status != anim_status_last)
553 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
554 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
555 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
558 if (drawing_target == DRAW_TO_FADE_TARGET)
561 // ---------- part 1 ------------------------------------------------------
562 // start or stop global animations by change of game mode
563 // (special handling of animations for "current screen" and "all screens")
565 // stop animations for last screen
566 game_mode_anim_action[anim_status_last] = ANIM_STOP;
568 // start animations for current screen
569 game_mode_anim_action[global.anim_status] = ANIM_START;
571 // start animations for all screens after loading new artwork set
572 if (anim_status_last == GAME_MODE_LOADING)
573 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
575 // ---------- part 2 ------------------------------------------------------
576 // start or stop global animations by change of animation class
577 // (generic handling of animations for "class of screens")
579 for (i = 0; i < NUM_ANIM_CLASSES; i++)
581 int anim_class_check = (1 << i);
582 int anim_class_game_mode = anim_class_game_modes[i];
583 int anim_class_last = anim_classes_last & anim_class_check;
584 int anim_class_next = anim_classes_next & anim_class_check;
586 // stop animations for changed screen class before fading to new screen
587 if (before_fading && anim_class_last && !anim_class_next)
588 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
590 // start animations for changed screen class after fading to new screen
591 if (after_fading && !anim_class_last && anim_class_next)
592 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
595 if (drawing_target == DRAW_TO_SCREEN)
598 anim_classes_last = anim_classes_next;
600 anim_status_last = global.anim_status;
602 // start or stop animations determined to be started or stopped above
603 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
604 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
605 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
607 else if (drawing_target == DRAW_TO_FADE_TARGET)
609 drawing_to_fading_buffer = TRUE;
611 // start animations determined to be (temporary) started above
612 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
613 if (game_mode_anim_action[mode_nr] == ANIM_START)
614 HandleGlobalAnim(ANIM_START, mode_nr);
618 if (global.anim_status == GAME_MODE_LOADING)
621 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
623 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
626 // when preparing source fading buffer, only draw animations to be stopped
627 if (drawing_target == DRAW_TO_FADE_SOURCE &&
628 game_mode_anim_action[mode_nr] != ANIM_STOP)
631 // when preparing target fading buffer, only draw animations to be started
632 if (drawing_target == DRAW_TO_FADE_TARGET &&
633 game_mode_anim_action[mode_nr] != ANIM_START)
637 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
638 mode_nr != game_status)
642 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
644 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
645 struct GraphicInfo *c = &anim->control_info;
646 int part_first, part_last;
649 if (!(anim->state & ANIM_STATE_RUNNING))
652 part_first = part_last = anim->active_part_nr;
654 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
656 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
659 part_last = num_parts - 1;
662 for (part_nr = part_first; part_nr <= part_last; part_nr++)
664 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
665 struct GraphicInfo *g = &part->graphic_info;
668 int width = g->width;
669 int height = g->height;
677 if (!(part->state & ANIM_STATE_RUNNING))
680 if (part->drawing_stage != drawing_stage)
689 else if (part->x > part->viewport_width - g->width)
690 width -= (part->x - (part->viewport_width - g->width));
698 else if (part->y > part->viewport_height - g->height)
699 height -= (part->y - (part->viewport_height - g->height));
701 if (width <= 0 || height <= 0)
704 dst_x += part->viewport_x;
705 dst_y += part->viewport_y;
707 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
708 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
709 g->anim_mode, g->anim_start_frame,
712 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
718 if (drawing_target == DRAW_TO_SCREEN)
719 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
722 BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
728 if (drawing_target == DRAW_TO_FADE_TARGET)
730 // stop animations determined to be (temporary) started above
731 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
732 if (game_mode_anim_action[mode_nr] == ANIM_START)
733 HandleGlobalAnim(ANIM_STOP, mode_nr);
735 drawing_to_fading_buffer = FALSE;
739 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
741 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
742 ResetGlobalAnim_Clickable();
744 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
746 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
747 ResetGlobalAnim_Clicked();
750 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
756 boolean changed = FALSE;
758 if (part->last_anim_status == global.anim_status)
761 part->last_anim_status = global.anim_status;
763 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
765 if (part->control_info.class == get_hash_from_key("window") ||
766 part->control_info.class == get_hash_from_key("border"))
770 viewport_width = WIN_XSIZE;
771 viewport_height = WIN_YSIZE;
773 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
775 else if (part->control_info.class == get_hash_from_key("door_1"))
779 viewport_width = DXSIZE;
780 viewport_height = DYSIZE;
782 else if (part->control_info.class == get_hash_from_key("door_2"))
786 viewport_width = VXSIZE;
787 viewport_height = VYSIZE;
789 else // default: "playfield"
791 viewport_x = REAL_SX;
792 viewport_y = REAL_SY;
793 viewport_width = FULL_SXSIZE;
794 viewport_height = FULL_SYSIZE;
797 if (viewport_x != part->viewport_x ||
798 viewport_y != part->viewport_y ||
799 viewport_width != part->viewport_width ||
800 viewport_height != part->viewport_height)
802 part->viewport_x = viewport_x;
803 part->viewport_y = viewport_y;
804 part->viewport_width = viewport_width;
805 part->viewport_height = viewport_height;
813 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
815 int sound = part->sound;
817 if (sound == SND_UNDEFINED)
820 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
821 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
824 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
825 if (IS_LOOP_SOUND(sound))
826 PlaySoundLoop(sound);
831 printf("::: PLAY SOUND %d.%d.%d: %d\n",
832 part->anim_nr, part->nr, part->mode_nr, sound);
836 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
838 int sound = part->sound;
840 if (sound == SND_UNDEFINED)
846 printf("::: STOP SOUND %d.%d.%d: %d\n",
847 part->anim_nr, part->nr, part->mode_nr, sound);
851 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
853 int music = part->music;
855 if (music == MUS_UNDEFINED)
858 if (!setup.sound_music)
864 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
865 part->anim_nr, part->nr, part->mode_nr, music);
869 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
871 int music = part->music;
873 if (music == MUS_UNDEFINED)
879 printf("::: STOP MUSIC %d.%d.%d: %d\n",
880 part->anim_nr, part->nr, part->mode_nr, music);
884 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
886 // when drawing animations to fading buffer, do not play sounds or music
887 if (drawing_to_fading_buffer)
890 PlayGlobalAnimSound(part);
891 PlayGlobalAnimMusic(part);
894 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
896 StopGlobalAnimSound(part);
897 StopGlobalAnimMusic(part);
900 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part)
902 struct GraphicInfo *c = &part->control_info;
904 return (c->init_event & ANIM_EVENT_CLICK ||
905 c->anim_event & ANIM_EVENT_CLICK);
908 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
909 int mx, int my, boolean clicked)
911 struct GraphicInfo *g = &part->graphic_info;
912 int part_x = part->viewport_x + part->x;
913 int part_y = part->viewport_y + part->y;
914 int part_width = g->width;
915 int part_height = g->height;
917 // check if mouse click was detected at all
921 // check if mouse click is inside the animation part's viewport
922 if (mx < part->viewport_x ||
923 mx >= part->viewport_x + part->viewport_width ||
924 my < part->viewport_y ||
925 my >= part->viewport_y + part->viewport_height)
928 // check if mouse click is inside the animation part's graphic
930 mx >= part_x + part_width ||
932 my >= part_y + part_height)
938 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
940 struct GraphicInfo *g = &part->graphic_info;
941 struct GraphicInfo *c = &part->control_info;
942 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
944 if (viewport_changed)
945 state |= ANIM_STATE_RESTART;
947 if (state & ANIM_STATE_RESTART)
949 // when drawing animations to fading buffer, only start fixed animations
950 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
951 c->y == ARG_UNDEFINED_VALUE))
952 return ANIM_STATE_INACTIVE;
954 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
956 part->init_delay_counter =
957 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
959 part->anim_delay_counter =
960 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
962 part->init_event_state = c->init_event;
963 part->anim_event_state = c->anim_event;
965 part->initial_anim_sync_frame =
966 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
968 if (c->direction & MV_HORIZONTAL)
970 int pos_bottom = part->viewport_height - g->height;
972 if (c->position == POS_TOP)
974 else if (c->position == POS_UPPER)
975 part->y = GetSimpleRandom(pos_bottom / 2);
976 else if (c->position == POS_MIDDLE)
977 part->y = pos_bottom / 2;
978 else if (c->position == POS_LOWER)
979 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
980 else if (c->position == POS_BOTTOM)
981 part->y = pos_bottom;
983 part->y = GetSimpleRandom(pos_bottom);
985 if (c->direction == MV_RIGHT)
987 part->step_xoffset = c->step_offset;
988 part->x = -g->width + part->step_xoffset;
992 part->step_xoffset = -c->step_offset;
993 part->x = part->viewport_width + part->step_xoffset;
996 part->step_yoffset = 0;
998 else if (c->direction & MV_VERTICAL)
1000 int pos_right = part->viewport_width - g->width;
1002 if (c->position == POS_LEFT)
1004 else if (c->position == POS_RIGHT)
1005 part->x = pos_right;
1007 part->x = GetSimpleRandom(pos_right);
1009 if (c->direction == MV_DOWN)
1011 part->step_yoffset = c->step_offset;
1012 part->y = -g->height + part->step_yoffset;
1016 part->step_yoffset = -c->step_offset;
1017 part->y = part->viewport_height + part->step_yoffset;
1020 part->step_xoffset = 0;
1027 part->step_xoffset = 0;
1028 part->step_yoffset = 0;
1031 if (c->x != ARG_UNDEFINED_VALUE)
1033 if (c->y != ARG_UNDEFINED_VALUE)
1036 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1037 part->step_xoffset = c->step_xoffset;
1038 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1039 part->step_yoffset = c->step_yoffset;
1041 if (part->init_delay_counter == 0 &&
1042 part->init_event_state == ANIM_EVENT_NONE)
1043 PlayGlobalAnimSoundAndMusic(part);
1046 part->clickable = isClickablePart(part);
1048 if (part->clicked &&
1049 part->init_event_state != ANIM_EVENT_NONE)
1051 if (part->initial_anim_sync_frame > 0)
1052 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1054 part->init_delay_counter = 1;
1055 part->init_event_state = ANIM_EVENT_NONE;
1057 part->clicked = FALSE;
1060 if (part->clicked &&
1061 part->anim_event_state != ANIM_EVENT_NONE)
1063 part->anim_delay_counter = 1;
1064 part->anim_event_state = ANIM_EVENT_NONE;
1066 part->clicked = FALSE;
1069 if (part->init_delay_counter > 0)
1071 part->init_delay_counter--;
1073 if (part->init_delay_counter == 0)
1075 part->init_event_state = ANIM_EVENT_NONE;
1077 PlayGlobalAnimSoundAndMusic(part);
1080 return ANIM_STATE_WAITING;
1083 if (part->init_event_state != ANIM_EVENT_NONE)
1084 return ANIM_STATE_WAITING;
1086 // check if moving animation has left the visible screen area
1087 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1088 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1089 (part->y <= -g->height && part->step_yoffset <= 0) ||
1090 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1092 // do not wait for "anim" events for off-screen animations
1093 part->anim_event_state = ANIM_EVENT_NONE;
1095 // do not stop animation before "anim" or "post" counter are finished
1096 if (part->anim_delay_counter == 0 &&
1097 part->post_delay_counter == 0)
1099 StopGlobalAnimSoundAndMusic(part);
1101 part->post_delay_counter =
1102 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1104 if (part->post_delay_counter > 0)
1105 return ANIM_STATE_RUNNING;
1107 // drawing last frame not needed here -- animation not visible anymore
1108 return ANIM_STATE_RESTART;
1112 if (part->anim_delay_counter > 0)
1114 part->anim_delay_counter--;
1116 if (part->anim_delay_counter == 0)
1118 part->anim_event_state = ANIM_EVENT_NONE;
1120 StopGlobalAnimSoundAndMusic(part);
1122 part->post_delay_counter =
1123 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1125 if (part->post_delay_counter > 0)
1126 return ANIM_STATE_RUNNING;
1128 // additional state "RUNNING" required to not skip drawing last frame
1129 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1133 if (part->post_delay_counter > 0)
1135 part->post_delay_counter--;
1137 if (part->post_delay_counter == 0)
1138 return ANIM_STATE_RESTART;
1140 return ANIM_STATE_WAITING;
1143 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1145 return ANIM_STATE_RUNNING;
1149 static unsigned int last_counter = -1;
1150 unsigned int counter = Counter();
1152 printf("::: NEXT ANIM PART [%d, %d]\n",
1153 anim_sync_frame, counter - last_counter);
1155 last_counter = counter;
1159 part->x += part->step_xoffset;
1160 part->y += part->step_yoffset;
1162 return ANIM_STATE_RUNNING;
1165 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1167 struct GlobalAnimPartControlInfo *part;
1168 struct GraphicInfo *c = &anim->control_info;
1169 int state, active_part_nr;
1172 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1173 anim->mode_nr, anim->nr, anim->num_parts);
1174 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1178 printf("::: %s(%d): %d, %d, %d [%d]\n",
1179 (action == ANIM_START ? "ANIM_START" :
1180 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1181 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1183 anim->state & ANIM_STATE_RESTART,
1184 anim->state & ANIM_STATE_WAITING,
1185 anim->state & ANIM_STATE_RUNNING,
1192 anim->state = anim->last_state = ANIM_STATE_RESTART;
1193 anim->active_part_nr = anim->last_active_part_nr = 0;
1194 anim->part_counter = 0;
1199 if (anim->state == ANIM_STATE_INACTIVE)
1202 anim->state = anim->last_state;
1203 anim->active_part_nr = anim->last_active_part_nr;
1208 anim->state = ANIM_STATE_INACTIVE;
1211 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1214 for (i = 0; i < num_parts; i++)
1215 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1224 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1226 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1230 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1231 anim->mode_nr, anim->nr, num_parts);
1234 for (i = 0; i < num_parts; i++)
1236 part = &anim->part[i];
1241 anim->state = ANIM_STATE_RUNNING;
1242 part->state = ANIM_STATE_RESTART;
1247 if (part->state == ANIM_STATE_INACTIVE)
1253 part->state = ANIM_STATE_INACTIVE;
1261 part->state = HandleGlobalAnim_Part(part, part->state);
1263 // when animation mode is "once", stop after animation was played once
1264 if (c->anim_mode & ANIM_ONCE &&
1265 part->state & ANIM_STATE_RESTART)
1266 part->state = ANIM_STATE_INACTIVE;
1269 anim->last_state = anim->state;
1270 anim->last_active_part_nr = anim->active_part_nr;
1275 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1276 anim->active_part_nr = getGlobalAnimationPart(anim);
1278 part = &anim->part[anim->active_part_nr];
1280 part->state = ANIM_STATE_RUNNING;
1282 anim->state = HandleGlobalAnim_Part(part, anim->state);
1284 if (anim->state & ANIM_STATE_RESTART)
1285 anim->part_counter++;
1287 // when animation mode is "once", stop after all animations were played once
1288 if (c->anim_mode & ANIM_ONCE &&
1289 anim->part_counter == anim->num_parts)
1290 anim->state = ANIM_STATE_INACTIVE;
1292 state = anim->state;
1293 active_part_nr = anim->active_part_nr;
1295 // while the animation parts are pausing (waiting or inactive), play the base
1296 // (main) animation; this corresponds to the "boring player animation" logic
1297 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1300 if (anim->state == ANIM_STATE_WAITING ||
1301 anim->state == ANIM_STATE_INACTIVE)
1303 anim->active_part_nr = anim->num_parts; // part nr of base animation
1304 part = &anim->part[anim->active_part_nr];
1306 if (anim->state != anim->last_state)
1307 part->state = ANIM_STATE_RESTART;
1309 anim->state = ANIM_STATE_RUNNING;
1310 part->state = HandleGlobalAnim_Part(part, part->state);
1314 anim->last_state = state;
1315 anim->last_active_part_nr = active_part_nr;
1318 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1323 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1324 ctrl->nr, ctrl->num_anims);
1327 for (i = 0; i < ctrl->num_anims; i++)
1328 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1331 static void HandleGlobalAnim(int action, int game_mode)
1334 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1337 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1340 static void DoAnimationExt()
1345 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1348 // global animations now synchronized with frame delay of screen update
1351 for (i = 0; i < NUM_GAME_MODES; i++)
1352 HandleGlobalAnim(ANIM_CONTINUE, i);
1355 // force screen redraw in next frame to continue drawing global animations
1356 redraw_mask = REDRAW_ALL;
1360 static void InitGlobalAnim_Clickable()
1364 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1366 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1369 for (anim_nr = 0; anim_nr < NUM_GLOBAL_ANIMS_AND_TOONS; anim_nr++)
1371 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1374 for (part_nr = 0; part_nr < NUM_GLOBAL_ANIM_PARTS_AND_TOONS; part_nr++)
1376 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1378 part->clickable = FALSE;
1384 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1386 boolean any_part_clicked = FALSE;
1389 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1391 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1394 for (anim_nr = 0; anim_nr < NUM_GLOBAL_ANIMS_AND_TOONS; anim_nr++)
1396 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1399 for (part_nr = 0; part_nr < NUM_GLOBAL_ANIM_PARTS_AND_TOONS; part_nr++)
1401 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1403 part->clicked = FALSE;
1405 if (part->clickable && isClickedPart(part, mx, my, clicked))
1406 any_part_clicked = part->clicked = TRUE;
1411 return any_part_clicked;
1414 static void ResetGlobalAnim_Clickable()
1416 InitGlobalAnim_Clickable();
1419 static void ResetGlobalAnim_Clicked()
1421 InitGlobalAnim_Clicked(-1, -1, FALSE);
1424 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1426 static boolean click_consumed = FALSE;
1427 static int last_button = 0;
1428 boolean press_event;
1429 boolean release_event;
1430 boolean click_consumed_current = click_consumed;
1432 /* check if button state has changed since last invocation */
1433 press_event = (button != 0 && last_button == 0);
1434 release_event = (button == 0 && last_button != 0);
1435 last_button = button;
1439 click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1440 click_consumed_current = click_consumed;
1444 click_consumed = FALSE;
1446 return click_consumed_current;