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
131 int init_delay_counter;
135 int last_state, last_active_part_nr;
138 struct GlobalAnimControlInfo
140 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
146 struct GameModeAnimClass
150 } game_mode_anim_classes_list[] =
152 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
153 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
154 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
155 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
156 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
157 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
158 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
159 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
160 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
161 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
162 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
163 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
164 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
165 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
166 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
167 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
168 { GAME_MODE_SCORES, ANIM_CLASS_TOONS },
173 struct AnimClassGameMode
177 } anim_class_game_modes_list[] =
179 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
180 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
181 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
182 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
183 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
184 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
189 /* forward declaration for internal use */
190 static void HandleGlobalAnim(int, int);
191 static void DoAnimationExt(void);
192 static void ResetGlobalAnim_Clickable();
193 static void ResetGlobalAnim_Clicked();
195 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
197 static unsigned int anim_sync_frame = 0;
199 static int game_mode_anim_classes[NUM_GAME_MODES];
200 static int anim_class_game_modes[NUM_ANIM_CLASSES];
202 static int anim_status_last = GAME_MODE_DEFAULT;
203 static int anim_classes_last = ANIM_CLASS_NONE;
205 static boolean drawing_to_fading_buffer = FALSE;
208 /* ========================================================================= */
209 /* generic animation frame calculation */
210 /* ========================================================================= */
212 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
217 sync_frame += start_frame * delay;
219 if (mode & ANIM_LOOP) /* looping animation */
221 frame = (sync_frame % (delay * num_frames)) / delay;
223 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
225 frame = sync_frame / delay;
227 if (frame > num_frames - 1)
228 frame = num_frames - 1;
230 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
232 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
234 frame = (sync_frame % (delay * max_anim_frames)) / delay;
235 frame = (frame < num_frames ? frame : max_anim_frames - frame);
237 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
239 int max_anim_frames = 2 * num_frames;
241 frame = (sync_frame % (delay * max_anim_frames)) / delay;
242 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
244 else if (mode & ANIM_RANDOM) /* play frames in random order */
246 /* note: expect different frames for the same delay cycle! */
248 if (gfx.anim_random_frame < 0)
249 frame = GetSimpleRandom(num_frames);
251 frame = gfx.anim_random_frame % num_frames;
253 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
255 frame = sync_frame % num_frames;
258 if (mode & ANIM_REVERSE) /* use reverse animation direction */
259 frame = num_frames - frame - 1;
265 /* ========================================================================= */
266 /* global animation functions */
267 /* ========================================================================= */
269 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
271 struct GraphicInfo *c = &anim->control_info;
272 int last_anim_random_frame = gfx.anim_random_frame;
275 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
277 part_nr = getAnimationFrame(anim->num_parts, 1,
278 c->anim_mode, c->anim_start_frame,
281 gfx.anim_random_frame = last_anim_random_frame;
286 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
288 const struct GlobalAnimPartControlInfo *o1 =
289 (struct GlobalAnimPartControlInfo *)obj1;
290 const struct GlobalAnimPartControlInfo *o2 =
291 (struct GlobalAnimPartControlInfo *)obj2;
294 if (o1->control_info.draw_order != o2->control_info.draw_order)
295 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
297 compare_result = o1->nr - o2->nr;
299 return compare_result;
302 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
304 const struct GlobalAnimMainControlInfo *o1 =
305 (struct GlobalAnimMainControlInfo *)obj1;
306 const struct GlobalAnimMainControlInfo *o2 =
307 (struct GlobalAnimMainControlInfo *)obj2;
310 if (o1->control_info.draw_order != o2->control_info.draw_order)
311 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
313 compare_result = o1->nr - o2->nr;
315 return compare_result;
318 static void InitToonControls()
320 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
321 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
322 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
323 int mode_nr, anim_nr, part_nr;
324 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
325 int num_toons = MAX_NUM_TOONS;
328 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
329 num_toons = global.num_toons;
331 mode_nr = mode_nr_toons;
332 anim_nr = ctrl->num_anims;
335 anim->mode_nr = mode_nr;
336 anim->control_info = graphic_info[control];
339 anim->num_parts_all = 0;
340 anim->part_counter = 0;
341 anim->active_part_nr = 0;
343 anim->has_base = FALSE;
345 anim->last_x = POS_OFFSCREEN;
346 anim->last_y = POS_OFFSCREEN;
348 anim->init_delay_counter = 0;
350 anim->state = ANIM_STATE_INACTIVE;
354 for (i = 0; i < num_toons; i++)
356 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
357 int sound = SND_UNDEFINED;
358 int music = MUS_UNDEFINED;
359 int graphic = IMG_TOON_1 + i;
360 int control = graphic;
363 part->anim_nr = anim_nr;
364 part->mode_nr = mode_nr;
366 part->is_base = FALSE;
370 part->graphic = graphic;
372 part->graphic_info = graphic_info[graphic];
373 part->control_info = graphic_info[control];
375 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
377 part->control_info.init_delay_fixed = 0;
378 part->control_info.init_delay_random = 150;
380 part->control_info.x = ARG_UNDEFINED_VALUE;
381 part->control_info.y = ARG_UNDEFINED_VALUE;
383 part->initial_anim_sync_frame = 0;
385 part->step_delay = 0;
386 part->step_delay_value = graphic_info[control].step_delay;
388 part->state = ANIM_STATE_INACTIVE;
389 part->last_anim_status = -1;
392 anim->num_parts_all++;
400 void InitGlobalAnimControls()
403 int mode_nr, anim_nr, part_nr;
404 int sound, music, graphic, control;
408 for (m = 0; m < NUM_GAME_MODES; m++)
412 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
419 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
421 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
422 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
424 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
426 // if no base animation parameters defined, use default values
427 if (control == IMG_UNDEFINED)
428 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
431 anim->mode_nr = mode_nr;
432 anim->control_info = graphic_info[control];
435 anim->num_parts_all = 0;
436 anim->part_counter = 0;
437 anim->active_part_nr = 0;
439 anim->has_base = FALSE;
441 anim->last_x = POS_OFFSCREEN;
442 anim->last_y = POS_OFFSCREEN;
444 anim->init_delay_counter = 0;
446 anim->state = ANIM_STATE_INACTIVE;
450 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
452 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
454 sound = global_anim_info[a].sound[p][m];
455 music = global_anim_info[a].music[p][m];
456 graphic = global_anim_info[a].graphic[p][m];
457 control = global_anim_info[ctrl_id].graphic[p][m];
459 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
460 control == IMG_UNDEFINED)
464 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
465 m, a, p, mode_nr, anim_nr, part_nr, control);
469 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
470 m, a, p, mode_nr, anim_nr, part_nr, sound);
474 part->old_anim_nr = a;
477 part->anim_nr = anim_nr;
478 part->mode_nr = mode_nr;
482 part->graphic = graphic;
484 part->graphic_info = graphic_info[graphic];
485 part->control_info = graphic_info[control];
487 part->initial_anim_sync_frame = 0;
489 part->step_delay = 0;
490 part->step_delay_value = graphic_info[control].step_delay;
492 part->state = ANIM_STATE_INACTIVE;
493 part->last_anim_status = -1;
495 anim->num_parts_all++;
497 if (p < GLOBAL_ANIM_ID_PART_BASE)
499 part->is_base = FALSE;
506 part->is_base = TRUE;
509 anim->has_base = TRUE;
513 if (anim->num_parts > 0 || anim->has_base)
523 /* sort all animations according to draw_order and animation number */
524 for (m = 0; m < NUM_GAME_MODES; m++)
526 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
528 /* sort all main animations for this game mode */
529 qsort(ctrl->anim, ctrl->num_anims,
530 sizeof(struct GlobalAnimMainControlInfo),
531 compareGlobalAnimMainControlInfo);
533 for (a = 0; a < ctrl->num_anims; a++)
535 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
537 /* sort all animation parts for this main animation */
538 qsort(anim->part, anim->num_parts,
539 sizeof(struct GlobalAnimPartControlInfo),
540 compareGlobalAnimPartControlInfo);
544 for (i = 0; i < NUM_GAME_MODES; i++)
545 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
546 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
547 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
548 game_mode_anim_classes_list[i].class;
550 for (i = 0; i < NUM_ANIM_CLASSES; i++)
551 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
552 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
553 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
554 anim_class_game_modes_list[i].game_mode;
556 anim_status_last = GAME_MODE_LOADING;
557 anim_classes_last = ANIM_CLASS_NONE;
560 void InitGlobalAnimations()
562 InitGlobalAnimControls();
565 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
567 Bitmap *fade_bitmap =
568 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
569 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
570 int game_mode_anim_action[NUM_GAME_MODES];
576 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
577 drawing_target == DRAW_TO_SCREEN)
580 // always start with reliable default values (no animation actions)
581 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
582 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
584 if (global.anim_status != anim_status_last)
586 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
587 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
588 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
591 if (drawing_target == DRAW_TO_FADE_TARGET)
594 // ---------- part 1 ------------------------------------------------------
595 // start or stop global animations by change of game mode
596 // (special handling of animations for "current screen" and "all screens")
598 // stop animations for last screen
599 game_mode_anim_action[anim_status_last] = ANIM_STOP;
601 // start animations for current screen
602 game_mode_anim_action[global.anim_status] = ANIM_START;
604 // start animations for all screens after loading new artwork set
605 if (anim_status_last == GAME_MODE_LOADING)
606 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
608 // ---------- part 2 ------------------------------------------------------
609 // start or stop global animations by change of animation class
610 // (generic handling of animations for "class of screens")
612 for (i = 0; i < NUM_ANIM_CLASSES; i++)
614 int anim_class_check = (1 << i);
615 int anim_class_game_mode = anim_class_game_modes[i];
616 int anim_class_last = anim_classes_last & anim_class_check;
617 int anim_class_next = anim_classes_next & anim_class_check;
619 // stop animations for changed screen class before fading to new screen
620 if (before_fading && anim_class_last && !anim_class_next)
621 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
623 // start animations for changed screen class after fading to new screen
624 if (after_fading && !anim_class_last && anim_class_next)
625 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
628 if (drawing_target == DRAW_TO_SCREEN)
631 anim_classes_last = anim_classes_next;
633 anim_status_last = global.anim_status;
635 // start or stop animations determined to be started or stopped above
636 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
637 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
638 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
640 else if (drawing_target == DRAW_TO_FADE_TARGET)
642 drawing_to_fading_buffer = TRUE;
644 // start animations determined to be (temporary) started above
645 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
646 if (game_mode_anim_action[mode_nr] == ANIM_START)
647 HandleGlobalAnim(ANIM_START, mode_nr);
651 if (global.anim_status == GAME_MODE_LOADING)
654 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
656 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
659 // when preparing source fading buffer, only draw animations to be stopped
660 if (drawing_target == DRAW_TO_FADE_SOURCE &&
661 game_mode_anim_action[mode_nr] != ANIM_STOP)
664 // when preparing target fading buffer, only draw animations to be started
665 if (drawing_target == DRAW_TO_FADE_TARGET &&
666 game_mode_anim_action[mode_nr] != ANIM_START)
670 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
671 mode_nr != game_status)
675 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
677 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
678 struct GraphicInfo *c = &anim->control_info;
679 int part_first, part_last;
682 if (!(anim->state & ANIM_STATE_RUNNING))
685 part_first = part_last = anim->active_part_nr;
687 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
689 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
692 part_last = num_parts - 1;
695 for (part_nr = part_first; part_nr <= part_last; part_nr++)
697 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
698 struct GraphicInfo *g = &part->graphic_info;
701 int width = g->width;
702 int height = g->height;
709 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
710 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
711 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
712 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
714 if (!(part->state & ANIM_STATE_RUNNING))
717 if (part->drawing_stage != drawing_stage)
726 else if (part->x > part->viewport_width - g->width)
727 width -= (part->x - (part->viewport_width - g->width));
735 else if (part->y > part->viewport_height - g->height)
736 height -= (part->y - (part->viewport_height - g->height));
738 if (width <= 0 || height <= 0)
741 dst_x += part->viewport_x;
742 dst_y += part->viewport_y;
744 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
745 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
746 g->anim_mode, g->anim_start_frame,
749 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
755 if (drawing_target == DRAW_TO_SCREEN)
756 blit_screen(src_bitmap, src_x, src_y, width, height,
759 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
765 if (drawing_target == DRAW_TO_FADE_TARGET)
767 // stop animations determined to be (temporary) started above
768 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
769 if (game_mode_anim_action[mode_nr] == ANIM_START)
770 HandleGlobalAnim(ANIM_STOP, mode_nr);
772 drawing_to_fading_buffer = FALSE;
776 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
778 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
779 ResetGlobalAnim_Clickable();
781 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
783 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
784 ResetGlobalAnim_Clicked();
787 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
793 boolean changed = FALSE;
795 if (part->last_anim_status == global.anim_status)
798 part->last_anim_status = global.anim_status;
800 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
802 if (part->control_info.class == get_hash_from_key("window") ||
803 part->control_info.class == get_hash_from_key("border"))
807 viewport_width = WIN_XSIZE;
808 viewport_height = WIN_YSIZE;
810 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
812 else if (part->control_info.class == get_hash_from_key("door_1"))
816 viewport_width = DXSIZE;
817 viewport_height = DYSIZE;
819 else if (part->control_info.class == get_hash_from_key("door_2"))
823 viewport_width = VXSIZE;
824 viewport_height = VYSIZE;
826 else // default: "playfield"
828 viewport_x = REAL_SX;
829 viewport_y = REAL_SY;
830 viewport_width = FULL_SXSIZE;
831 viewport_height = FULL_SYSIZE;
834 if (viewport_x != part->viewport_x ||
835 viewport_y != part->viewport_y ||
836 viewport_width != part->viewport_width ||
837 viewport_height != part->viewport_height)
839 part->viewport_x = viewport_x;
840 part->viewport_y = viewport_y;
841 part->viewport_width = viewport_width;
842 part->viewport_height = viewport_height;
850 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
852 int sound = part->sound;
854 if (sound == SND_UNDEFINED)
857 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
858 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
861 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
862 if (IS_LOOP_SOUND(sound))
863 PlaySoundLoop(sound);
868 printf("::: PLAY SOUND %d.%d.%d: %d\n",
869 part->anim_nr, part->nr, part->mode_nr, sound);
873 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
875 int sound = part->sound;
877 if (sound == SND_UNDEFINED)
883 printf("::: STOP SOUND %d.%d.%d: %d\n",
884 part->anim_nr, part->nr, part->mode_nr, sound);
888 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
890 int music = part->music;
892 if (music == MUS_UNDEFINED)
895 if (!setup.sound_music)
901 printf("::: PLAY MUSIC %d.%d.%d: %d\n",
902 part->anim_nr, part->nr, part->mode_nr, music);
906 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
908 int music = part->music;
910 if (music == MUS_UNDEFINED)
916 printf("::: STOP MUSIC %d.%d.%d: %d\n",
917 part->anim_nr, part->nr, part->mode_nr, music);
921 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
923 // when drawing animations to fading buffer, do not play sounds or music
924 if (drawing_to_fading_buffer)
927 PlayGlobalAnimSound(part);
928 PlayGlobalAnimMusic(part);
931 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
933 StopGlobalAnimSound(part);
934 StopGlobalAnimMusic(part);
937 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
939 struct GraphicInfo *c = &part->control_info;
940 int trigger_mask = ANIM_EVENT_ANIM_MASK | ANIM_EVENT_PART_MASK;
941 int mask_anim_only = mask & ANIM_EVENT_ANIM_MASK;
943 if (mask & ANIM_EVENT_ANY)
944 return (c->init_event & ANIM_EVENT_ANY ||
945 c->anim_event & ANIM_EVENT_ANY);
946 else if (mask & ANIM_EVENT_SELF)
947 return (c->init_event & ANIM_EVENT_SELF ||
948 c->anim_event & ANIM_EVENT_SELF);
950 return ((c->init_event & trigger_mask) == mask ||
951 (c->anim_event & trigger_mask) == mask ||
952 (c->init_event & trigger_mask) == mask_anim_only ||
953 (c->anim_event & trigger_mask) == mask_anim_only);
956 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
957 int mx, int my, boolean clicked)
959 struct GraphicInfo *g = &part->graphic_info;
960 int part_x = part->viewport_x + part->x;
961 int part_y = part->viewport_y + part->y;
962 int part_width = g->width;
963 int part_height = g->height;
965 // check if mouse click was detected at all
969 // check if mouse click is inside the animation part's viewport
970 if (mx < part->viewport_x ||
971 mx >= part->viewport_x + part->viewport_width ||
972 my < part->viewport_y ||
973 my >= part->viewport_y + part->viewport_height)
976 // check if mouse click is inside the animation part's graphic
978 mx >= part_x + part_width ||
980 my >= part_y + part_height)
986 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
988 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
989 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
990 struct GraphicInfo *g = &part->graphic_info;
991 struct GraphicInfo *c = &part->control_info;
992 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
994 if (viewport_changed)
995 state |= ANIM_STATE_RESTART;
997 if (state & ANIM_STATE_RESTART)
999 // when drawing animations to fading buffer, only start fixed animations
1000 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1001 c->y == ARG_UNDEFINED_VALUE))
1002 return ANIM_STATE_INACTIVE;
1004 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1006 part->init_delay_counter =
1007 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1009 part->anim_delay_counter =
1010 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1012 part->init_event_state = c->init_event;
1013 part->anim_event_state = c->anim_event;
1015 part->initial_anim_sync_frame =
1016 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1018 if (c->direction & MV_HORIZONTAL)
1020 int pos_bottom = part->viewport_height - g->height;
1022 if (c->position == POS_TOP)
1024 else if (c->position == POS_UPPER)
1025 part->y = GetSimpleRandom(pos_bottom / 2);
1026 else if (c->position == POS_MIDDLE)
1027 part->y = pos_bottom / 2;
1028 else if (c->position == POS_LOWER)
1029 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1030 else if (c->position == POS_BOTTOM)
1031 part->y = pos_bottom;
1033 part->y = GetSimpleRandom(pos_bottom);
1035 if (c->direction == MV_RIGHT)
1037 part->step_xoffset = c->step_offset;
1038 part->x = -g->width + part->step_xoffset;
1042 part->step_xoffset = -c->step_offset;
1043 part->x = part->viewport_width + part->step_xoffset;
1046 part->step_yoffset = 0;
1048 else if (c->direction & MV_VERTICAL)
1050 int pos_right = part->viewport_width - g->width;
1052 if (c->position == POS_LEFT)
1054 else if (c->position == POS_RIGHT)
1055 part->x = pos_right;
1057 part->x = GetSimpleRandom(pos_right);
1059 if (c->direction == MV_DOWN)
1061 part->step_yoffset = c->step_offset;
1062 part->y = -g->height + part->step_yoffset;
1066 part->step_yoffset = -c->step_offset;
1067 part->y = part->viewport_height + part->step_yoffset;
1070 part->step_xoffset = 0;
1077 part->step_xoffset = 0;
1078 part->step_yoffset = 0;
1081 if (c->x != ARG_UNDEFINED_VALUE)
1083 if (c->y != ARG_UNDEFINED_VALUE)
1086 if (c->position == POS_LAST &&
1087 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1088 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1090 part->x = anim->last_x;
1091 part->y = anim->last_y;
1094 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1095 part->step_xoffset = c->step_xoffset;
1096 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1097 part->step_yoffset = c->step_yoffset;
1099 if (part->init_delay_counter == 0 &&
1100 part->init_event_state == ANIM_EVENT_NONE)
1101 PlayGlobalAnimSoundAndMusic(part);
1104 if (part->clicked &&
1105 part->init_event_state != ANIM_EVENT_NONE)
1107 if (part->initial_anim_sync_frame > 0)
1108 part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1110 part->init_delay_counter = 1;
1111 part->init_event_state = ANIM_EVENT_NONE;
1113 part->clicked = FALSE;
1116 if (part->clicked &&
1117 part->anim_event_state != ANIM_EVENT_NONE)
1119 part->anim_delay_counter = 1;
1120 part->anim_event_state = ANIM_EVENT_NONE;
1122 part->clicked = FALSE;
1125 if (part->init_delay_counter > 0)
1127 part->init_delay_counter--;
1129 if (part->init_delay_counter == 0)
1131 part->init_event_state = ANIM_EVENT_NONE;
1133 PlayGlobalAnimSoundAndMusic(part);
1136 return ANIM_STATE_WAITING;
1139 if (part->init_event_state != ANIM_EVENT_NONE)
1140 return ANIM_STATE_WAITING;
1142 // animation part is now running/visible and therefore clickable
1143 part->clickable = TRUE;
1145 // check if moving animation has left the visible screen area
1146 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1147 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1148 (part->y <= -g->height && part->step_yoffset <= 0) ||
1149 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1151 // do not wait for "anim" events for off-screen animations
1152 part->anim_event_state = ANIM_EVENT_NONE;
1154 // do not stop animation before "anim" or "post" counter are finished
1155 if (part->anim_delay_counter == 0 &&
1156 part->post_delay_counter == 0)
1158 StopGlobalAnimSoundAndMusic(part);
1160 part->post_delay_counter =
1161 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1163 if (part->post_delay_counter > 0)
1164 return ANIM_STATE_RUNNING;
1166 // drawing last frame not needed here -- animation not visible anymore
1167 return ANIM_STATE_RESTART;
1171 if (part->anim_delay_counter > 0)
1173 part->anim_delay_counter--;
1175 if (part->anim_delay_counter == 0)
1177 part->anim_event_state = ANIM_EVENT_NONE;
1179 StopGlobalAnimSoundAndMusic(part);
1181 part->post_delay_counter =
1182 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1184 if (part->post_delay_counter > 0)
1185 return ANIM_STATE_RUNNING;
1187 // additional state "RUNNING" required to not skip drawing last frame
1188 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1192 if (part->post_delay_counter > 0)
1194 part->post_delay_counter--;
1196 if (part->post_delay_counter == 0)
1197 return ANIM_STATE_RESTART;
1199 return ANIM_STATE_WAITING;
1202 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1204 return ANIM_STATE_RUNNING;
1208 static unsigned int last_counter = -1;
1209 unsigned int counter = Counter();
1211 printf("::: NEXT ANIM PART [%d, %d]\n",
1212 anim_sync_frame, counter - last_counter);
1214 last_counter = counter;
1218 part->x += part->step_xoffset;
1219 part->y += part->step_yoffset;
1221 anim->last_x = part->x;
1222 anim->last_y = part->y;
1224 return ANIM_STATE_RUNNING;
1227 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1229 struct GlobalAnimPartControlInfo *part;
1230 struct GraphicInfo *c = &anim->control_info;
1231 int state, active_part_nr;
1234 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1235 anim->mode_nr, anim->nr, anim->num_parts);
1236 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1240 printf("::: %s(%d): %d, %d, %d [%d]\n",
1241 (action == ANIM_START ? "ANIM_START" :
1242 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1243 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1245 anim->state & ANIM_STATE_RESTART,
1246 anim->state & ANIM_STATE_WAITING,
1247 anim->state & ANIM_STATE_RUNNING,
1254 anim->state = anim->last_state = ANIM_STATE_RESTART;
1255 anim->active_part_nr = anim->last_active_part_nr = 0;
1256 anim->part_counter = 0;
1261 if (anim->state == ANIM_STATE_INACTIVE)
1264 anim->state = anim->last_state;
1265 anim->active_part_nr = anim->last_active_part_nr;
1270 anim->state = ANIM_STATE_INACTIVE;
1273 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1276 for (i = 0; i < num_parts; i++)
1277 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1286 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1288 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1292 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1293 anim->mode_nr, anim->nr, num_parts);
1296 for (i = 0; i < num_parts; i++)
1298 part = &anim->part[i];
1303 anim->state = ANIM_STATE_RUNNING;
1304 part->state = ANIM_STATE_RESTART;
1309 if (part->state == ANIM_STATE_INACTIVE)
1315 part->state = ANIM_STATE_INACTIVE;
1323 part->state = HandleGlobalAnim_Part(part, part->state);
1325 // when animation mode is "once", stop after animation was played once
1326 if (c->anim_mode & ANIM_ONCE &&
1327 part->state & ANIM_STATE_RESTART)
1328 part->state = ANIM_STATE_INACTIVE;
1331 anim->last_state = anim->state;
1332 anim->last_active_part_nr = anim->active_part_nr;
1337 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1338 anim->active_part_nr = getGlobalAnimationPart(anim);
1340 part = &anim->part[anim->active_part_nr];
1342 part->state = ANIM_STATE_RUNNING;
1344 anim->state = HandleGlobalAnim_Part(part, anim->state);
1346 if (anim->state & ANIM_STATE_RESTART)
1347 anim->part_counter++;
1349 // when animation mode is "once", stop after all animations were played once
1350 if (c->anim_mode & ANIM_ONCE &&
1351 anim->part_counter == anim->num_parts)
1352 anim->state = ANIM_STATE_INACTIVE;
1354 state = anim->state;
1355 active_part_nr = anim->active_part_nr;
1357 // while the animation parts are pausing (waiting or inactive), play the base
1358 // (main) animation; this corresponds to the "boring player animation" logic
1359 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1362 if (anim->state == ANIM_STATE_WAITING ||
1363 anim->state == ANIM_STATE_INACTIVE)
1365 anim->active_part_nr = anim->num_parts; // part nr of base animation
1366 part = &anim->part[anim->active_part_nr];
1368 if (anim->state != anim->last_state)
1369 part->state = ANIM_STATE_RESTART;
1371 anim->state = ANIM_STATE_RUNNING;
1372 part->state = HandleGlobalAnim_Part(part, part->state);
1376 anim->last_state = state;
1377 anim->last_active_part_nr = active_part_nr;
1380 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1385 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1386 ctrl->nr, ctrl->num_anims);
1389 for (i = 0; i < ctrl->num_anims; i++)
1390 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1393 static void HandleGlobalAnim(int action, int game_mode)
1396 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1399 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1402 static void DoAnimationExt()
1407 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1410 // global animations now synchronized with frame delay of screen update
1413 for (i = 0; i < NUM_GAME_MODES; i++)
1414 HandleGlobalAnim(ANIM_CONTINUE, i);
1417 // force screen redraw in next frame to continue drawing global animations
1418 redraw_mask = REDRAW_ALL;
1422 static void InitGlobalAnim_Clickable()
1426 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1428 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1431 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1433 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1436 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1438 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1440 part->clickable = FALSE;
1446 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1448 boolean any_part_clicked = FALSE;
1451 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1453 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1456 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1458 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1461 for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1463 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1467 part->clicked = FALSE;
1472 if (!part->clickable)
1475 if (isClickablePart(part, ANIM_EVENT_ANY))
1476 any_part_clicked = part->clicked = TRUE;
1478 if (isClickedPart(part, mx, my, clicked))
1481 printf("::: %d.%d CLICKED\n", anim_nr, part_nr);
1484 if (isClickablePart(part, ANIM_EVENT_SELF))
1485 any_part_clicked = part->clicked = TRUE;
1487 // check if this click is defined to trigger other animations
1488 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1489 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1490 int mask = gic_anim_nr << ANIM_EVENT_ANIM_BIT;
1493 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1497 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1499 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1502 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1504 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1506 if (isClickablePart(part2, mask))
1507 any_part_clicked = part2->clicked = TRUE;
1510 struct GraphicInfo *c = &part2->control_info;
1512 printf("::: - %d.%d: 0x%08x, 0x%08x [0x%08x]",
1513 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1515 if (isClickablePart(part2, mask))
1516 printf(" <--- TRIGGERED BY %d.%d",
1528 return any_part_clicked;
1531 static void ResetGlobalAnim_Clickable()
1533 InitGlobalAnim_Clickable();
1536 static void ResetGlobalAnim_Clicked()
1538 InitGlobalAnim_Clicked(-1, -1, FALSE);
1541 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1543 static boolean click_consumed = FALSE;
1544 static int last_button = 0;
1545 boolean press_event;
1546 boolean release_event;
1547 boolean click_consumed_current = click_consumed;
1549 /* check if button state has changed since last invocation */
1550 press_event = (button != 0 && last_button == 0);
1551 release_event = (button == 0 && last_button != 0);
1552 last_button = button;
1556 click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1557 click_consumed_current = click_consumed;
1561 click_consumed = FALSE;
1563 return click_consumed_current;