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
69 int old_nr; // position before mapping animation parts linearly
70 int old_anim_nr; // position before mapping animations linearly
76 boolean is_base; // animation part is base/main/default animation part
82 struct GraphicInfo graphic_info;
83 struct GraphicInfo control_info;
91 int step_xoffset, step_yoffset;
93 unsigned int initial_anim_sync_frame;
94 unsigned int step_delay, step_delay_value;
96 int init_delay_counter;
97 int anim_delay_counter;
98 int post_delay_counter;
100 boolean init_event_state;
101 boolean anim_event_state;
109 int last_anim_status;
112 struct GlobalAnimMainControlInfo
114 struct GlobalAnimPartControlInfo base;
115 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
120 struct GraphicInfo control_info;
122 int num_parts; // number of animation parts, but without base part
123 int num_parts_all; // number of animation parts, including base part
127 boolean has_base; // animation has base/main/default animation part
129 int init_delay_counter;
133 int last_state, last_active_part_nr;
136 struct GlobalAnimControlInfo
138 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
144 struct GameModeAnimClass
148 } game_mode_anim_classes_list[] =
150 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
151 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
152 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
153 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
154 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
155 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
156 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
157 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
158 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
159 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
160 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
161 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
162 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
163 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
164 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
165 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
166 { GAME_MODE_SCORES, ANIM_CLASS_TOONS },
171 struct AnimClassGameMode
175 } anim_class_game_modes_list[] =
177 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
178 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
179 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
180 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
181 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
182 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
187 /* forward declaration for internal use */
188 static void HandleGlobalAnim(int, int);
189 static void DoAnimationExt(void);
190 static void ResetGlobalAnim_Clickable();
191 static void ResetGlobalAnim_Clicked();
193 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
195 static unsigned int anim_sync_frame = 0;
197 static int game_mode_anim_classes[NUM_GAME_MODES];
198 static int anim_class_game_modes[NUM_ANIM_CLASSES];
200 static int anim_status_last = GAME_MODE_DEFAULT;
201 static int anim_classes_last = ANIM_CLASS_NONE;
203 static boolean drawing_to_fading_buffer = FALSE;
206 /* ========================================================================= */
207 /* generic animation frame calculation */
208 /* ========================================================================= */
210 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
215 sync_frame += start_frame * delay;
217 if (mode & ANIM_LOOP) /* looping animation */
219 frame = (sync_frame % (delay * num_frames)) / delay;
221 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
223 frame = sync_frame / delay;
225 if (frame > num_frames - 1)
226 frame = num_frames - 1;
228 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
230 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
232 frame = (sync_frame % (delay * max_anim_frames)) / delay;
233 frame = (frame < num_frames ? frame : max_anim_frames - frame);
235 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
237 int max_anim_frames = 2 * num_frames;
239 frame = (sync_frame % (delay * max_anim_frames)) / delay;
240 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
242 else if (mode & ANIM_RANDOM) /* play frames in random order */
244 /* note: expect different frames for the same delay cycle! */
246 if (gfx.anim_random_frame < 0)
247 frame = GetSimpleRandom(num_frames);
249 frame = gfx.anim_random_frame % num_frames;
251 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
253 frame = sync_frame % num_frames;
256 if (mode & ANIM_REVERSE) /* use reverse animation direction */
257 frame = num_frames - frame - 1;
263 /* ========================================================================= */
264 /* global animation functions */
265 /* ========================================================================= */
267 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
269 struct GraphicInfo *c = &anim->control_info;
270 int last_anim_random_frame = gfx.anim_random_frame;
273 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
275 part_nr = getAnimationFrame(anim->num_parts, 1,
276 c->anim_mode, c->anim_start_frame,
279 gfx.anim_random_frame = last_anim_random_frame;
284 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
286 const struct GlobalAnimPartControlInfo *o1 =
287 (struct GlobalAnimPartControlInfo *)obj1;
288 const struct GlobalAnimPartControlInfo *o2 =
289 (struct GlobalAnimPartControlInfo *)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 int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
302 const struct GlobalAnimMainControlInfo *o1 =
303 (struct GlobalAnimMainControlInfo *)obj1;
304 const struct GlobalAnimMainControlInfo *o2 =
305 (struct GlobalAnimMainControlInfo *)obj2;
308 if (o1->control_info.draw_order != o2->control_info.draw_order)
309 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
311 compare_result = o1->nr - o2->nr;
313 return compare_result;
316 static void InitToonControls()
318 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
319 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
320 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
321 int mode_nr, anim_nr, part_nr;
322 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
323 int num_toons = MAX_NUM_TOONS;
326 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
327 num_toons = global.num_toons;
329 mode_nr = mode_nr_toons;
330 anim_nr = ctrl->num_anims;
333 anim->mode_nr = mode_nr;
334 anim->control_info = graphic_info[control];
337 anim->num_parts_all = 0;
338 anim->part_counter = 0;
339 anim->active_part_nr = 0;
341 anim->has_base = FALSE;
343 anim->init_delay_counter = 0;
345 anim->state = ANIM_STATE_INACTIVE;
349 for (i = 0; i < num_toons; i++)
351 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
352 int sound = SND_UNDEFINED;
353 int music = MUS_UNDEFINED;
354 int graphic = IMG_TOON_1 + i;
355 int control = graphic;
358 part->anim_nr = anim_nr;
359 part->mode_nr = mode_nr;
361 part->is_base = FALSE;
365 part->graphic = graphic;
367 part->graphic_info = graphic_info[graphic];
368 part->control_info = graphic_info[control];
370 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
372 part->control_info.init_delay_fixed = 0;
373 part->control_info.init_delay_random = 150;
375 part->control_info.x = ARG_UNDEFINED_VALUE;
376 part->control_info.y = ARG_UNDEFINED_VALUE;
378 part->initial_anim_sync_frame = 0;
380 part->step_delay = 0;
381 part->step_delay_value = graphic_info[control].step_delay;
383 part->state = ANIM_STATE_INACTIVE;
384 part->last_anim_status = -1;
387 anim->num_parts_all++;
395 void InitGlobalAnimControls()
398 int mode_nr, anim_nr, part_nr;
399 int sound, music, graphic, control;
403 for (m = 0; m < NUM_GAME_MODES; m++)
407 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
414 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
416 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
417 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
419 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
421 // if no base animation parameters defined, use default values
422 if (control == IMG_UNDEFINED)
423 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
426 anim->mode_nr = mode_nr;
427 anim->control_info = graphic_info[control];
430 anim->num_parts_all = 0;
431 anim->part_counter = 0;
432 anim->active_part_nr = 0;
434 anim->has_base = FALSE;
436 anim->init_delay_counter = 0;
438 anim->state = ANIM_STATE_INACTIVE;
442 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
444 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
446 sound = global_anim_info[a].sound[p][m];
447 music = global_anim_info[a].music[p][m];
448 graphic = global_anim_info[a].graphic[p][m];
449 control = global_anim_info[ctrl_id].graphic[p][m];
451 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
452 control == IMG_UNDEFINED)
456 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
457 m, a, p, mode_nr, anim_nr, part_nr, control);
461 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
462 m, a, p, mode_nr, anim_nr, part_nr, sound);
466 part->old_anim_nr = a;
469 part->anim_nr = anim_nr;
470 part->mode_nr = mode_nr;
474 part->graphic = graphic;
476 part->graphic_info = graphic_info[graphic];
477 part->control_info = graphic_info[control];
479 part->initial_anim_sync_frame = 0;
481 part->step_delay = 0;
482 part->step_delay_value = graphic_info[control].step_delay;
484 part->state = ANIM_STATE_INACTIVE;
485 part->last_anim_status = -1;
487 anim->num_parts_all++;
489 if (p < GLOBAL_ANIM_ID_PART_BASE)
491 part->is_base = FALSE;
498 part->is_base = TRUE;
501 anim->has_base = TRUE;
505 if (anim->num_parts > 0 || anim->has_base)
515 /* sort all animations according to draw_order and animation number */
516 for (m = 0; m < NUM_GAME_MODES; m++)
518 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
520 /* sort all main animations for this game mode */
521 qsort(ctrl->anim, ctrl->num_anims,
522 sizeof(struct GlobalAnimMainControlInfo),
523 compareGlobalAnimMainControlInfo);
525 for (a = 0; a < ctrl->num_anims; a++)
527 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
529 /* sort all animation parts for this main animation */
530 qsort(anim->part, anim->num_parts,
531 sizeof(struct GlobalAnimPartControlInfo),
532 compareGlobalAnimPartControlInfo);
536 for (i = 0; i < NUM_GAME_MODES; i++)
537 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
538 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
539 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
540 game_mode_anim_classes_list[i].class;
542 for (i = 0; i < NUM_ANIM_CLASSES; i++)
543 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
544 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
545 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
546 anim_class_game_modes_list[i].game_mode;
548 anim_status_last = GAME_MODE_LOADING;
549 anim_classes_last = ANIM_CLASS_NONE;
552 void InitGlobalAnimations()
554 InitGlobalAnimControls();
557 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
559 Bitmap *fade_bitmap =
560 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
561 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
562 int game_mode_anim_action[NUM_GAME_MODES];
568 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
569 drawing_target == DRAW_TO_SCREEN)
572 // always start with reliable default values (no animation actions)
573 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
574 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
576 if (global.anim_status != anim_status_last)
578 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
579 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
580 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
583 if (drawing_target == DRAW_TO_FADE_TARGET)
586 // ---------- part 1 ------------------------------------------------------
587 // start or stop global animations by change of game mode
588 // (special handling of animations for "current screen" and "all screens")
590 // stop animations for last screen
591 game_mode_anim_action[anim_status_last] = ANIM_STOP;
593 // start animations for current screen
594 game_mode_anim_action[global.anim_status] = ANIM_START;
596 // start animations for all screens after loading new artwork set
597 if (anim_status_last == GAME_MODE_LOADING)
598 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
600 // ---------- part 2 ------------------------------------------------------
601 // start or stop global animations by change of animation class
602 // (generic handling of animations for "class of screens")
604 for (i = 0; i < NUM_ANIM_CLASSES; i++)
606 int anim_class_check = (1 << i);
607 int anim_class_game_mode = anim_class_game_modes[i];
608 int anim_class_last = anim_classes_last & anim_class_check;
609 int anim_class_next = anim_classes_next & anim_class_check;
611 // stop animations for changed screen class before fading to new screen
612 if (before_fading && anim_class_last && !anim_class_next)
613 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
615 // start animations for changed screen class after fading to new screen
616 if (after_fading && !anim_class_last && anim_class_next)
617 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
620 if (drawing_target == DRAW_TO_SCREEN)
623 anim_classes_last = anim_classes_next;
625 anim_status_last = global.anim_status;
627 // start or stop animations determined to be started or stopped above
628 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
629 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
630 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
632 else if (drawing_target == DRAW_TO_FADE_TARGET)
634 drawing_to_fading_buffer = TRUE;
636 // start animations determined to be (temporary) started above
637 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
638 if (game_mode_anim_action[mode_nr] == ANIM_START)
639 HandleGlobalAnim(ANIM_START, mode_nr);
643 if (global.anim_status == GAME_MODE_LOADING)
646 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
648 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
651 // when preparing source fading buffer, only draw animations to be stopped
652 if (drawing_target == DRAW_TO_FADE_SOURCE &&
653 game_mode_anim_action[mode_nr] != ANIM_STOP)
656 // when preparing target fading buffer, only draw animations to be started
657 if (drawing_target == DRAW_TO_FADE_TARGET &&
658 game_mode_anim_action[mode_nr] != ANIM_START)
662 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
663 mode_nr != game_status)
667 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
669 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
670 struct GraphicInfo *c = &anim->control_info;
671 int part_first, part_last;
674 if (!(anim->state & ANIM_STATE_RUNNING))
677 part_first = part_last = anim->active_part_nr;
679 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
681 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
684 part_last = num_parts - 1;
687 for (part_nr = part_first; part_nr <= part_last; part_nr++)
689 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
690 struct GraphicInfo *g = &part->graphic_info;
693 int width = g->width;
694 int height = g->height;
701 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
702 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
703 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
704 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
706 if (!(part->state & ANIM_STATE_RUNNING))
709 if (part->drawing_stage != drawing_stage)
718 else if (part->x > part->viewport_width - g->width)
719 width -= (part->x - (part->viewport_width - g->width));
727 else if (part->y > part->viewport_height - g->height)
728 height -= (part->y - (part->viewport_height - g->height));
730 if (width <= 0 || height <= 0)
733 dst_x += part->viewport_x;
734 dst_y += part->viewport_y;
736 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
737 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
738 g->anim_mode, g->anim_start_frame,
741 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
747 if (drawing_target == DRAW_TO_SCREEN)
748 blit_screen(src_bitmap, src_x, src_y, width, height,
751 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
757 if (drawing_target == DRAW_TO_FADE_TARGET)
759 // stop animations determined to be (temporary) started above
760 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
761 if (game_mode_anim_action[mode_nr] == ANIM_START)
762 HandleGlobalAnim(ANIM_STOP, mode_nr);
764 drawing_to_fading_buffer = FALSE;
768 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
770 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
771 ResetGlobalAnim_Clickable();
773 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
775 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
776 ResetGlobalAnim_Clicked();
779 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
785 boolean changed = FALSE;
787 if (part->last_anim_status == global.anim_status)
790 part->last_anim_status = global.anim_status;
792 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
794 if (part->control_info.class == get_hash_from_key("window") ||
795 part->control_info.class == get_hash_from_key("border"))
799 viewport_width = WIN_XSIZE;
800 viewport_height = WIN_YSIZE;
802 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
804 else if (part->control_info.class == get_hash_from_key("door_1"))
808 viewport_width = DXSIZE;
809 viewport_height = DYSIZE;
811 else if (part->control_info.class == get_hash_from_key("door_2"))
815 viewport_width = VXSIZE;
816 viewport_height = VYSIZE;
818 else // default: "playfield"
820 viewport_x = REAL_SX;
821 viewport_y = REAL_SY;
822 viewport_width = FULL_SXSIZE;
823 viewport_height = FULL_SYSIZE;
826 if (viewport_x != part->viewport_x ||
827 viewport_y != part->viewport_y ||
828 viewport_width != part->viewport_width ||
829 viewport_height != part->viewport_height)
831 part->viewport_x = viewport_x;
832 part->viewport_y = viewport_y;
833 part->viewport_width = viewport_width;
834 part->viewport_height = viewport_height;
842 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
844 int sound = part->sound;
846 if (sound == SND_UNDEFINED)
849 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
850 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
853 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
854 if (IS_LOOP_SOUND(sound))
855 PlaySoundLoop(sound);
860 printf("::: PLAY SOUND %d.%d.%d: %d\n",
861 part->anim_nr, part->nr, part->mode_nr, sound);
865 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
867 int sound = part->sound;
869 if (sound == SND_UNDEFINED)
875 printf("::: STOP SOUND %d.%d.%d: %d\n",
876 part->anim_nr, part->nr, part->mode_nr, sound);
880 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
882 int music = part->music;
884 if (music == MUS_UNDEFINED)
887 if (!setup.sound_music)
893 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
894 part->anim_nr, part->nr, part->mode_nr, music);
898 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
900 int music = part->music;
902 if (music == MUS_UNDEFINED)
908 printf("::: STOP MUSIC %d.%d.%d: %d\n",
909 part->anim_nr, part->nr, part->mode_nr, music);
913 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
915 // when drawing animations to fading buffer, do not play sounds or music
916 if (drawing_to_fading_buffer)
919 PlayGlobalAnimSound(part);
920 PlayGlobalAnimMusic(part);
923 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
925 StopGlobalAnimSound(part);
926 StopGlobalAnimMusic(part);
929 static boolean matchesAnimEventMask(int bits, int mask)
931 return (bits & (mask & ANIM_EVENT_CLICK_ANIM_ALL) &&
932 bits & (mask & ANIM_EVENT_CLICK_PART_ALL));
935 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
937 struct GraphicInfo *c = &part->control_info;
939 boolean clickable_self = FALSE;
940 boolean clickable_triggered = FALSE;
942 if (mask & ANIM_EVENT_CLICK_SELF)
943 clickable_self = (c->init_event & ANIM_EVENT_CLICK_SELF ||
944 c->anim_event & ANIM_EVENT_CLICK_SELF);
946 clickable_triggered = (matchesAnimEventMask(c->init_event, mask) ||
947 matchesAnimEventMask(c->anim_event, mask));
949 return (clickable_self || clickable_triggered);
952 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
953 int mx, int my, boolean clicked)
955 struct GraphicInfo *g = &part->graphic_info;
956 int part_x = part->viewport_x + part->x;
957 int part_y = part->viewport_y + part->y;
958 int part_width = g->width;
959 int part_height = g->height;
961 // check if mouse click was detected at all
965 // check if mouse click is inside the animation part's viewport
966 if (mx < part->viewport_x ||
967 mx >= part->viewport_x + part->viewport_width ||
968 my < part->viewport_y ||
969 my >= part->viewport_y + part->viewport_height)
972 // check if mouse click is inside the animation part's graphic
974 mx >= part_x + part_width ||
976 my >= part_y + part_height)
982 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
984 struct GraphicInfo *g = &part->graphic_info;
985 struct GraphicInfo *c = &part->control_info;
986 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
988 if (viewport_changed)
989 state |= ANIM_STATE_RESTART;
991 if (state & ANIM_STATE_RESTART)
993 // when drawing animations to fading buffer, only start fixed animations
994 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
995 c->y == ARG_UNDEFINED_VALUE))
996 return ANIM_STATE_INACTIVE;
998 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1000 part->init_delay_counter =
1001 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1003 part->anim_delay_counter =
1004 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1006 part->init_event_state = c->init_event;
1007 part->anim_event_state = c->anim_event;
1009 part->initial_anim_sync_frame =
1010 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1012 if (c->direction & MV_HORIZONTAL)
1014 int pos_bottom = part->viewport_height - g->height;
1016 if (c->position == POS_TOP)
1018 else if (c->position == POS_UPPER)
1019 part->y = GetSimpleRandom(pos_bottom / 2);
1020 else if (c->position == POS_MIDDLE)
1021 part->y = pos_bottom / 2;
1022 else if (c->position == POS_LOWER)
1023 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1024 else if (c->position == POS_BOTTOM)
1025 part->y = pos_bottom;
1027 part->y = GetSimpleRandom(pos_bottom);
1029 if (c->direction == MV_RIGHT)
1031 part->step_xoffset = c->step_offset;
1032 part->x = -g->width + part->step_xoffset;
1036 part->step_xoffset = -c->step_offset;
1037 part->x = part->viewport_width + part->step_xoffset;
1040 part->step_yoffset = 0;
1042 else if (c->direction & MV_VERTICAL)
1044 int pos_right = part->viewport_width - g->width;
1046 if (c->position == POS_LEFT)
1048 else if (c->position == POS_RIGHT)
1049 part->x = pos_right;
1051 part->x = GetSimpleRandom(pos_right);
1053 if (c->direction == MV_DOWN)
1055 part->step_yoffset = c->step_offset;
1056 part->y = -g->height + part->step_yoffset;
1060 part->step_yoffset = -c->step_offset;
1061 part->y = part->viewport_height + part->step_yoffset;
1064 part->step_xoffset = 0;
1071 part->step_xoffset = 0;
1072 part->step_yoffset = 0;
1075 if (c->x != ARG_UNDEFINED_VALUE)
1077 if (c->y != ARG_UNDEFINED_VALUE)
1080 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1081 part->step_xoffset = c->step_xoffset;
1082 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1083 part->step_yoffset = c->step_yoffset;
1085 if (part->init_delay_counter == 0 &&
1086 part->init_event_state == ANIM_EVENT_NONE)
1087 PlayGlobalAnimSoundAndMusic(part);
1090 if (part->clicked &&
1091 part->init_event_state != ANIM_EVENT_NONE)
1093 if (part->initial_anim_sync_frame > 0)
1094 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1096 part->init_delay_counter = 1;
1097 part->init_event_state = ANIM_EVENT_NONE;
1099 part->clicked = FALSE;
1102 if (part->clicked &&
1103 part->anim_event_state != ANIM_EVENT_NONE)
1105 part->anim_delay_counter = 1;
1106 part->anim_event_state = ANIM_EVENT_NONE;
1108 part->clicked = FALSE;
1111 if (part->init_delay_counter > 0)
1113 part->init_delay_counter--;
1115 if (part->init_delay_counter == 0)
1117 part->init_event_state = ANIM_EVENT_NONE;
1119 PlayGlobalAnimSoundAndMusic(part);
1122 return ANIM_STATE_WAITING;
1125 if (part->init_event_state != ANIM_EVENT_NONE)
1126 return ANIM_STATE_WAITING;
1128 // animation part is now running/visible and therefore clickable
1129 part->clickable = TRUE;
1131 // check if moving animation has left the visible screen area
1132 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1133 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1134 (part->y <= -g->height && part->step_yoffset <= 0) ||
1135 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1137 // do not wait for "anim" events for off-screen animations
1138 part->anim_event_state = ANIM_EVENT_NONE;
1140 // do not stop animation before "anim" or "post" counter are finished
1141 if (part->anim_delay_counter == 0 &&
1142 part->post_delay_counter == 0)
1144 StopGlobalAnimSoundAndMusic(part);
1146 part->post_delay_counter =
1147 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1149 if (part->post_delay_counter > 0)
1150 return ANIM_STATE_RUNNING;
1152 // drawing last frame not needed here -- animation not visible anymore
1153 return ANIM_STATE_RESTART;
1157 if (part->anim_delay_counter > 0)
1159 part->anim_delay_counter--;
1161 if (part->anim_delay_counter == 0)
1163 part->anim_event_state = ANIM_EVENT_NONE;
1165 StopGlobalAnimSoundAndMusic(part);
1167 part->post_delay_counter =
1168 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1170 if (part->post_delay_counter > 0)
1171 return ANIM_STATE_RUNNING;
1173 // additional state "RUNNING" required to not skip drawing last frame
1174 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1178 if (part->post_delay_counter > 0)
1180 part->post_delay_counter--;
1182 if (part->post_delay_counter == 0)
1183 return ANIM_STATE_RESTART;
1185 return ANIM_STATE_WAITING;
1188 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1190 return ANIM_STATE_RUNNING;
1194 static unsigned int last_counter = -1;
1195 unsigned int counter = Counter();
1197 printf("::: NEXT ANIM PART [%d, %d]\n",
1198 anim_sync_frame, counter - last_counter);
1200 last_counter = counter;
1204 part->x += part->step_xoffset;
1205 part->y += part->step_yoffset;
1207 return ANIM_STATE_RUNNING;
1210 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1212 struct GlobalAnimPartControlInfo *part;
1213 struct GraphicInfo *c = &anim->control_info;
1214 int state, active_part_nr;
1217 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1218 anim->mode_nr, anim->nr, anim->num_parts);
1219 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1223 printf("::: %s(%d): %d, %d, %d [%d]\n",
1224 (action == ANIM_START ? "ANIM_START" :
1225 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1226 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1228 anim->state & ANIM_STATE_RESTART,
1229 anim->state & ANIM_STATE_WAITING,
1230 anim->state & ANIM_STATE_RUNNING,
1237 anim->state = anim->last_state = ANIM_STATE_RESTART;
1238 anim->active_part_nr = anim->last_active_part_nr = 0;
1239 anim->part_counter = 0;
1244 if (anim->state == ANIM_STATE_INACTIVE)
1247 anim->state = anim->last_state;
1248 anim->active_part_nr = anim->last_active_part_nr;
1253 anim->state = ANIM_STATE_INACTIVE;
1256 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1259 for (i = 0; i < num_parts; i++)
1260 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1269 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1271 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1275 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1276 anim->mode_nr, anim->nr, num_parts);
1279 for (i = 0; i < num_parts; i++)
1281 part = &anim->part[i];
1286 anim->state = ANIM_STATE_RUNNING;
1287 part->state = ANIM_STATE_RESTART;
1292 if (part->state == ANIM_STATE_INACTIVE)
1298 part->state = ANIM_STATE_INACTIVE;
1306 part->state = HandleGlobalAnim_Part(part, part->state);
1308 // when animation mode is "once", stop after animation was played once
1309 if (c->anim_mode & ANIM_ONCE &&
1310 part->state & ANIM_STATE_RESTART)
1311 part->state = ANIM_STATE_INACTIVE;
1314 anim->last_state = anim->state;
1315 anim->last_active_part_nr = anim->active_part_nr;
1320 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1321 anim->active_part_nr = getGlobalAnimationPart(anim);
1323 part = &anim->part[anim->active_part_nr];
1325 part->state = ANIM_STATE_RUNNING;
1327 anim->state = HandleGlobalAnim_Part(part, anim->state);
1329 if (anim->state & ANIM_STATE_RESTART)
1330 anim->part_counter++;
1332 // when animation mode is "once", stop after all animations were played once
1333 if (c->anim_mode & ANIM_ONCE &&
1334 anim->part_counter == anim->num_parts)
1335 anim->state = ANIM_STATE_INACTIVE;
1337 state = anim->state;
1338 active_part_nr = anim->active_part_nr;
1340 // while the animation parts are pausing (waiting or inactive), play the base
1341 // (main) animation; this corresponds to the "boring player animation" logic
1342 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1345 if (anim->state == ANIM_STATE_WAITING ||
1346 anim->state == ANIM_STATE_INACTIVE)
1348 anim->active_part_nr = anim->num_parts; // part nr of base animation
1349 part = &anim->part[anim->active_part_nr];
1351 if (anim->state != anim->last_state)
1352 part->state = ANIM_STATE_RESTART;
1354 anim->state = ANIM_STATE_RUNNING;
1355 part->state = HandleGlobalAnim_Part(part, part->state);
1359 anim->last_state = state;
1360 anim->last_active_part_nr = active_part_nr;
1363 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1368 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1369 ctrl->nr, ctrl->num_anims);
1372 for (i = 0; i < ctrl->num_anims; i++)
1373 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1376 static void HandleGlobalAnim(int action, int game_mode)
1379 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1382 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1385 static void DoAnimationExt()
1390 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1393 // global animations now synchronized with frame delay of screen update
1396 for (i = 0; i < NUM_GAME_MODES; i++)
1397 HandleGlobalAnim(ANIM_CONTINUE, i);
1400 // force screen redraw in next frame to continue drawing global animations
1401 redraw_mask = REDRAW_ALL;
1405 static void InitGlobalAnim_Clickable()
1409 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1411 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1414 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1416 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1419 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1421 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1423 part->clickable = FALSE;
1429 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1431 boolean any_part_clicked = FALSE;
1434 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1436 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1439 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1441 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1444 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1446 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1450 part->clicked = FALSE;
1455 if (part->clickable &&
1456 isClickedPart(part, mx, my, clicked))
1459 printf("::: %d.%d CLICKED\n", anim_nr, part_nr);
1462 if (isClickablePart(part, ANIM_EVENT_CLICK_SELF))
1463 any_part_clicked = part->clicked = TRUE;
1465 // check if this click is defined to trigger other animations
1466 int old_anim_nr = part->old_anim_nr;
1467 int old_part_nr = part->old_nr;
1468 int mask = ANIM_EVENT_CLICK_ANIM_1 << old_anim_nr;
1471 mask |= ANIM_EVENT_CLICK_PART_ALL;
1473 mask |= ANIM_EVENT_CLICK_PART_1 << old_part_nr;
1477 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1479 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1482 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1484 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1486 if (isClickablePart(part2, mask))
1487 any_part_clicked = part2->clicked = TRUE;
1490 struct GraphicInfo *c = &part2->control_info;
1492 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1493 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1495 if (isClickablePart(part2, mask))
1496 printf(" <--- TRIGGERED BY %d.%d",
1508 return any_part_clicked;
1511 static void ResetGlobalAnim_Clickable()
1513 InitGlobalAnim_Clickable();
1516 static void ResetGlobalAnim_Clicked()
1518 InitGlobalAnim_Clicked(-1, -1, FALSE);
1521 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1523 static boolean click_consumed = FALSE;
1524 static int last_button = 0;
1525 boolean press_event;
1526 boolean release_event;
1527 boolean click_consumed_current = click_consumed;
1529 /* check if button state has changed since last invocation */
1530 press_event = (button != 0 && last_button == 0);
1531 release_event = (button == 0 && last_button != 0);
1532 last_button = button;
1536 click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1537 click_consumed_current = click_consumed;
1541 click_consumed = FALSE;
1543 return click_consumed_current;