1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
12 #include "libgame/libgame.h"
23 #define DEBUG_ANIM_DELAY 0
24 #define DEBUG_ANIM_EVENTS 0
27 // values for global toon animation definition
28 #define NUM_GLOBAL_TOON_ANIMS 1
29 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
31 // values for global animation definition (including toons)
32 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
33 NUM_GLOBAL_TOON_ANIMS)
34 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
35 NUM_GLOBAL_TOON_PARTS)
37 #define MAX_GLOBAL_ANIM_LIST (NUM_GAME_MODES * \
38 NUM_GLOBAL_ANIMS_AND_TOONS * \
39 NUM_GLOBAL_ANIM_PARTS_AND_TOONS)
41 #define ANIM_CLASS_BIT_TITLE_INITIAL 0
42 #define ANIM_CLASS_BIT_TITLE 1
43 #define ANIM_CLASS_BIT_MAIN 2
44 #define ANIM_CLASS_BIT_NAMES 3
45 #define ANIM_CLASS_BIT_SCORES 4
46 #define ANIM_CLASS_BIT_SCORESONLY 5
47 #define ANIM_CLASS_BIT_SUBMENU 6
48 #define ANIM_CLASS_BIT_MENU 7
49 #define ANIM_CLASS_BIT_TOONS 8
50 #define ANIM_CLASS_BIT_NO_TITLE 9
52 #define NUM_ANIM_CLASSES 10
54 #define ANIM_CLASS_NONE 0
55 #define ANIM_CLASS_TITLE_INITIAL (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
56 #define ANIM_CLASS_TITLE (1 << ANIM_CLASS_BIT_TITLE)
57 #define ANIM_CLASS_MAIN (1 << ANIM_CLASS_BIT_MAIN)
58 #define ANIM_CLASS_NAMES (1 << ANIM_CLASS_BIT_NAMES)
59 #define ANIM_CLASS_SCORES (1 << ANIM_CLASS_BIT_SCORES)
60 #define ANIM_CLASS_SCORESONLY (1 << ANIM_CLASS_BIT_SCORESONLY)
61 #define ANIM_CLASS_SUBMENU (1 << ANIM_CLASS_BIT_SUBMENU)
62 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
63 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
64 #define ANIM_CLASS_NO_TITLE (1 << ANIM_CLASS_BIT_NO_TITLE)
66 #define ANIM_CLASS_TOONS_SCORES (ANIM_CLASS_TOONS | \
70 #define ANIM_CLASS_TOONS_SCORESONLY (ANIM_CLASS_TOONS | \
72 ANIM_CLASS_SCORESONLY | \
75 #define ANIM_CLASS_TOONS_MENU_MAIN (ANIM_CLASS_TOONS | \
80 #define ANIM_CLASS_TOONS_MENU_SUBMENU (ANIM_CLASS_TOONS | \
82 ANIM_CLASS_SUBMENU | \
85 #define ANIM_CLASS_TOONS_MENU_SUBMENU_2 (ANIM_CLASS_TOONS | \
87 ANIM_CLASS_SUBMENU | \
91 // values for global animation states
92 #define ANIM_STATE_INACTIVE 0
93 #define ANIM_STATE_RESTART (1 << 0)
94 #define ANIM_STATE_WAITING (1 << 1)
95 #define ANIM_STATE_RUNNING (1 << 2)
97 // values for global animation control
98 #define ANIM_NO_ACTION 0
100 #define ANIM_CONTINUE 2
104 struct GlobalAnimPartControlInfo
106 int old_nr; // position before mapping animation parts linearly
107 int old_anim_nr; // position before mapping animations linearly
113 boolean is_base; // animation part is base/main/default animation part
119 struct GraphicInfo graphic_info;
120 struct GraphicInfo control_info;
122 boolean class_playfield_or_door;
130 int step_xoffset, step_yoffset;
133 int tile_xoffset, tile_yoffset;
135 unsigned int initial_anim_sync_frame;
136 unsigned int anim_random_frame;
138 DelayCounter step_delay;
140 int init_delay_counter;
141 int anim_delay_counter;
142 int post_delay_counter;
144 int fade_delay_counter;
147 boolean init_event_state;
148 boolean anim_event_state;
157 int last_anim_status;
160 struct GlobalAnimMainControlInfo
162 struct GlobalAnimPartControlInfo base;
163 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
168 struct GraphicInfo control_info;
170 int num_parts; // number of animation parts, but without base part
171 int num_parts_all; // number of animation parts, including base part
175 boolean has_base; // animation has base/main/default animation part
179 int init_delay_counter;
183 int last_state, last_active_part_nr;
186 struct GlobalAnimControlInfo
188 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
194 struct GameModeAnimClass
198 } game_mode_anim_classes_list[] =
200 { GAME_MODE_TITLE_INITIAL_1, ANIM_CLASS_TITLE_INITIAL },
201 { GAME_MODE_TITLE_INITIAL_2, ANIM_CLASS_TITLE_INITIAL },
202 { GAME_MODE_TITLE_INITIAL_3, ANIM_CLASS_TITLE_INITIAL },
203 { GAME_MODE_TITLE_INITIAL_4, ANIM_CLASS_TITLE_INITIAL },
204 { GAME_MODE_TITLE_INITIAL_5, ANIM_CLASS_TITLE_INITIAL },
205 { GAME_MODE_TITLE_1, ANIM_CLASS_TITLE },
206 { GAME_MODE_TITLE_2, ANIM_CLASS_TITLE },
207 { GAME_MODE_TITLE_3, ANIM_CLASS_TITLE },
208 { GAME_MODE_TITLE_4, ANIM_CLASS_TITLE },
209 { GAME_MODE_TITLE_5, ANIM_CLASS_TITLE },
210 { GAME_MODE_NAMES, ANIM_CLASS_TOONS_MENU_SUBMENU },
211 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS_MENU_SUBMENU },
212 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS_MENU_SUBMENU },
213 { GAME_MODE_INFO, ANIM_CLASS_TOONS_MENU_SUBMENU },
214 { GAME_MODE_SETUP, ANIM_CLASS_TOONS_MENU_SUBMENU },
215 { GAME_MODE_PSEUDO_NAMESONLY, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
216 { GAME_MODE_PSEUDO_TYPENAMES, ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
217 { GAME_MODE_PSEUDO_MAINONLY, ANIM_CLASS_TOONS_MENU_MAIN },
218 { GAME_MODE_PSEUDO_TYPENAME, ANIM_CLASS_TOONS_MENU_MAIN },
219 { GAME_MODE_PSEUDO_SCORESOLD, ANIM_CLASS_TOONS_SCORESONLY },
220 { GAME_MODE_PSEUDO_SCORESNEW, ANIM_CLASS_TOONS_SCORESONLY },
221 { GAME_MODE_SCOREINFO, ANIM_CLASS_TOONS_SCORES },
222 { GAME_MODE_EDITOR, ANIM_CLASS_NO_TITLE },
223 { GAME_MODE_PLAYING, ANIM_CLASS_NO_TITLE },
228 struct AnimClassGameMode
232 } anim_class_game_modes_list[] =
234 { ANIM_CLASS_BIT_TITLE_INITIAL, GAME_MODE_TITLE_INITIAL },
235 { ANIM_CLASS_BIT_TITLE, GAME_MODE_TITLE },
236 { ANIM_CLASS_BIT_MAIN, GAME_MODE_MAIN },
237 { ANIM_CLASS_BIT_NAMES, GAME_MODE_NAMES },
238 { ANIM_CLASS_BIT_SCORES, GAME_MODE_SCORES },
239 { ANIM_CLASS_BIT_SCORESONLY, GAME_MODE_PSEUDO_SCORESONLY },
240 { ANIM_CLASS_BIT_SUBMENU, GAME_MODE_PSEUDO_SUBMENU },
241 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
242 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
243 { ANIM_CLASS_BIT_NO_TITLE, GAME_MODE_PSEUDO_NO_TITLE },
248 // forward declaration for internal use
249 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
250 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
251 static void HandleGlobalAnim(int, int);
252 static void DoAnimationExt(void);
253 static void ResetGlobalAnim_Clickable(void);
254 static void ResetGlobalAnim_Clicked(void);
256 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
257 static struct GlobalAnimPartControlInfo *global_anim_list[MAX_GLOBAL_ANIM_LIST];
258 static int num_global_anim_list = 0;
260 static unsigned int anim_sync_frame = 0;
262 static int game_mode_anim_classes[NUM_GAME_MODES];
263 static int anim_class_game_modes[NUM_ANIM_CLASSES];
265 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
266 static int anim_status_last = GAME_MODE_DEFAULT;
267 static int anim_classes_last = ANIM_CLASS_NONE;
269 static boolean drawing_to_fading_buffer = FALSE;
271 static boolean handle_click = FALSE;
274 // ============================================================================
275 // generic animation frame calculation
276 // ============================================================================
278 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
283 if (delay < 1) // delay must be at least 1
286 sync_frame += start_frame * delay;
288 if (mode & ANIM_LOOP) // looping animation
290 frame = (sync_frame % (delay * num_frames)) / delay;
292 else if (mode & ANIM_LINEAR) // linear (non-looping) animation
294 frame = sync_frame / delay;
296 if (frame > num_frames - 1)
297 frame = num_frames - 1;
299 else if (mode & ANIM_PINGPONG) // oscillate (border frames once)
301 int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
303 frame = (sync_frame % (delay * max_anim_frames)) / delay;
304 frame = (frame < num_frames ? frame : max_anim_frames - frame);
306 else if (mode & ANIM_PINGPONG2) // oscillate (border frames twice)
308 int max_anim_frames = 2 * num_frames;
310 frame = (sync_frame % (delay * max_anim_frames)) / delay;
311 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
313 else if (mode & ANIM_RANDOM) // play frames in random order
315 // note: expect different frames for the same delay cycle!
317 if (gfx.anim_random_frame < 0)
318 frame = GetSimpleRandom(num_frames);
320 frame = gfx.anim_random_frame % num_frames;
322 else if (mode & ANIM_LEVEL_NR) // play frames by level number
324 int level_pos = level_nr - gfx.anim_first_level;
326 frame = level_pos % num_frames;
328 else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
330 frame = sync_frame % num_frames;
333 if (mode & ANIM_REVERSE) // use reverse animation direction
334 frame = num_frames - frame - 1;
340 // ============================================================================
341 // global animation functions
342 // ============================================================================
344 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
346 struct GraphicInfo *c = &anim->control_info;
347 int last_anim_random_frame = gfx.anim_random_frame;
350 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
352 part_nr = getAnimationFrame(anim->num_parts, 1,
353 c->anim_mode, c->anim_start_frame,
356 gfx.anim_random_frame = last_anim_random_frame;
361 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
363 const struct GlobalAnimPartControlInfo *o1 =
364 *(struct GlobalAnimPartControlInfo **)obj1;
365 const struct GlobalAnimPartControlInfo *o2 =
366 *(struct GlobalAnimPartControlInfo **)obj2;
369 if (o1->control_info.draw_order != o2->control_info.draw_order)
370 compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
371 else if (o1->mode_nr != o2->mode_nr)
372 compare_result = o1->mode_nr - o2->mode_nr;
373 else if (o1->anim_nr != o2->anim_nr)
374 compare_result = o1->anim_nr - o2->anim_nr;
376 compare_result = o1->nr - o2->nr;
378 return compare_result;
381 static boolean isPausedOnPlayfieldOrDoor(struct GlobalAnimPartControlInfo *part)
383 // only pause playfield and door animations when playing
384 if (game_status != GAME_MODE_PLAYING)
387 // do not pause animations when game ended (and engine is running)
388 if (checkGameEnded())
391 // only pause animations on playfield and doors
392 if (!part->class_playfield_or_door)
395 // only pause animations when engine is paused or request dialog is active
396 if (!tape.pausing && !game.request_active)
402 static void InitToonControls(void)
404 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
405 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
406 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
407 int mode_nr, anim_nr, part_nr;
408 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
409 int num_toons = MAX_NUM_TOONS;
412 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
413 num_toons = global.num_toons;
415 mode_nr = mode_nr_toons;
416 anim_nr = ctrl->num_anims;
419 anim->mode_nr = mode_nr;
420 anim->control_info = graphic_info[control];
423 anim->num_parts_all = 0;
424 anim->part_counter = 0;
425 anim->active_part_nr = 0;
427 anim->has_base = FALSE;
429 anim->last_x = POS_OFFSCREEN;
430 anim->last_y = POS_OFFSCREEN;
432 anim->init_delay_counter = 0;
434 anim->state = ANIM_STATE_INACTIVE;
438 for (i = 0; i < num_toons; i++)
440 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
441 int sound = SND_UNDEFINED;
442 int music = MUS_UNDEFINED;
443 int graphic = IMG_TOON_1 + i;
448 part->anim_nr = anim_nr;
449 part->mode_nr = mode_nr;
451 part->is_base = FALSE;
455 part->graphic = graphic;
457 part->graphic_info = graphic_info[graphic];
458 part->control_info = graphic_info[control];
460 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
462 part->control_info.init_delay_fixed = 0;
463 part->control_info.init_delay_random = 150;
465 part->control_info.x = ARG_UNDEFINED_VALUE;
466 part->control_info.y = ARG_UNDEFINED_VALUE;
468 part->initial_anim_sync_frame = 0;
469 part->anim_random_frame = -1;
471 part->step_delay.count = 0;
472 part->step_delay.value = graphic_info[control].step_delay;
474 part->state = ANIM_STATE_INACTIVE;
475 part->last_anim_status = -1;
478 anim->num_parts_all++;
486 static void InitGlobalAnimControls(void)
489 int mode_nr, anim_nr, part_nr;
490 int sound, music, graphic, control;
494 for (m = 0; m < NUM_GAME_MODES; m++)
498 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
505 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
507 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
508 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
510 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
512 // if no base animation parameters defined, use default values
513 if (control == IMG_UNDEFINED)
514 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
517 anim->mode_nr = mode_nr;
518 anim->control_info = graphic_info[control];
521 anim->num_parts_all = 0;
522 anim->part_counter = 0;
523 anim->active_part_nr = 0;
525 anim->has_base = FALSE;
527 anim->last_x = POS_OFFSCREEN;
528 anim->last_y = POS_OFFSCREEN;
530 anim->init_delay_counter = 0;
532 anim->state = ANIM_STATE_INACTIVE;
534 // if draw order is undefined, set to default value "0"
535 if (anim->control_info.draw_order == ARG_UNDEFINED_VALUE)
536 anim->control_info.draw_order = 0;
540 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
542 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
544 sound = global_anim_info[a].sound[p][m];
545 music = global_anim_info[a].music[p][m];
546 graphic = global_anim_info[a].graphic[p][m];
547 control = global_anim_info[ctrl_id].graphic[p][m];
549 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
550 control == IMG_UNDEFINED)
554 Debug("anim:InitGlobalAnimControls",
555 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
556 m, a, p, mode_nr, anim_nr, part_nr, control);
560 Debug("anim:InitGlobalAnimControls",
561 "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
562 m, a, p, mode_nr, anim_nr, part_nr, sound);
566 part->old_anim_nr = a;
569 part->anim_nr = anim_nr;
570 part->mode_nr = mode_nr;
574 part->graphic = graphic;
576 part->graphic_info = graphic_info[graphic];
577 part->control_info = graphic_info[control];
579 part->initial_anim_sync_frame = 0;
580 part->anim_random_frame = -1;
582 part->step_delay.count = 0;
583 part->step_delay.value = graphic_info[control].step_delay;
585 part->state = ANIM_STATE_INACTIVE;
586 part->last_anim_status = -1;
588 anim->num_parts_all++;
590 if (p < GLOBAL_ANIM_ID_PART_BASE)
592 part->is_base = FALSE;
599 part->is_base = TRUE;
602 anim->has_base = TRUE;
605 // force pointer-style animations to pass-through clicks
606 if (part->control_info.class == get_hash_from_key("pointer") &&
607 part->control_info.style == STYLE_DEFAULT)
608 part->control_info.style |= STYLE_PASSTHROUGH;
610 // if draw order is undefined, inherit it from main animation
611 if (part->control_info.draw_order == ARG_UNDEFINED_VALUE)
612 part->control_info.draw_order = anim->control_info.draw_order;
615 if (anim->num_parts > 0 || anim->has_base)
625 // create list of all animation parts
626 num_global_anim_list = 0;
627 for (m = 0; m < NUM_GAME_MODES; m++)
628 for (a = 0; a < global_anim_ctrl[m].num_anims; a++)
629 for (p = 0; p < global_anim_ctrl[m].anim[a].num_parts_all; p++)
630 global_anim_list[num_global_anim_list++] =
631 &global_anim_ctrl[m].anim[a].part[p];
633 // sort list of all animation parts according to draw_order and number
634 qsort(global_anim_list, num_global_anim_list,
635 sizeof(struct GlobalAnimPartControlInfo *),
636 compareGlobalAnimPartControlInfo);
638 for (i = 0; i < NUM_GAME_MODES; i++)
639 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
640 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
641 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
642 game_mode_anim_classes_list[i].class;
644 for (i = 0; i < NUM_ANIM_CLASSES; i++)
645 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
646 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
647 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
648 anim_class_game_modes_list[i].game_mode;
650 anim_status_last_before_fading = GAME_MODE_LOADING;
651 anim_status_last = GAME_MODE_LOADING;
652 anim_classes_last = ANIM_CLASS_NONE;
655 static void SetGlobalAnimEventsForCustomElements(int list_pos)
657 int num_events = GetGlobalAnimEventValueCount(list_pos);
660 for (i = 0; i < num_events; i++)
662 int event = GetGlobalAnimEventValue(list_pos, i);
664 if (event & ANIM_EVENT_CE_CHANGE)
666 int nr = (event >> ANIM_EVENT_CE_BIT) & 0xff;
668 if (nr >= 0 && nr < NUM_CUSTOM_ELEMENTS)
669 element_info[EL_CUSTOM_START + nr].has_anim_event = TRUE;
674 void InitGlobalAnimEventsForCustomElements(void)
679 // custom element events for global animations only relevant while playing
680 m = GAME_MODE_PLAYING;
682 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
684 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
686 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
688 // if no base animation parameters defined, use default values
689 if (control == IMG_UNDEFINED)
690 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
692 SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
693 SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
695 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
697 control = global_anim_info[ctrl_id].graphic[p][m];
699 if (control == IMG_UNDEFINED)
702 SetGlobalAnimEventsForCustomElements(graphic_info[control].init_event);
703 SetGlobalAnimEventsForCustomElements(graphic_info[control].anim_event);
708 void InitGlobalAnimations(void)
710 InitGlobalAnimControls();
713 static void BlitGlobalAnimation(struct GlobalAnimPartControlInfo *part,
714 Bitmap *src_bitmap, int src_x0, int src_y0,
717 struct GraphicInfo *g = &part->graphic_info;
718 struct GraphicInfo *c = &part->control_info;
719 void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
720 (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
721 void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
722 (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
723 Bitmap *fade_bitmap =
724 (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
725 drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
726 int alpha = (c->fade_mode & FADE_MODE_FADE ? part->fade_alpha : g->alpha);
729 for (y = 0; y < c->stacked_yfactor; y++)
731 for (x = 0; x < c->stacked_xfactor; x++)
735 int dst_x = part->x + x * (g->width + c->stacked_xoffset);
736 int dst_y = part->y + y * (g->height + c->stacked_yoffset);
739 int width = g->width;
740 int height = g->height;
748 else if (dst_x > part->viewport_width - g->width)
750 width -= (dst_x - (part->viewport_width - g->width));
759 else if (dst_y > part->viewport_height - g->height)
761 height -= (dst_y - (part->viewport_height - g->height));
764 if (width <= 0 || height <= 0)
770 dst_x += part->viewport_x;
771 dst_y += part->viewport_y;
773 SetBitmapAlphaNextBlit(src_bitmap, alpha);
775 if (drawing_target == DRAW_TO_SCREEN)
776 blit_screen(src_bitmap, src_x, src_y, width, height,
779 blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
785 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
787 int game_mode_anim_action[NUM_GAME_MODES];
794 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
795 drawing_target == DRAW_TO_SCREEN)
798 // always start with reliable default values (no animation actions)
799 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
800 game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
802 if (global.anim_status != anim_status_last)
804 boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
805 boolean after_fading = (anim_status_last == GAME_MODE_PSEUDO_FADING);
806 int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
809 if (drawing_target == DRAW_TO_FADE_TARGET)
812 // special case: changing from/to these screens is done without fading
813 if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME ||
814 global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
815 anim_status_last == GAME_MODE_PSEUDO_TYPENAME ||
816 anim_status_last == GAME_MODE_PSEUDO_TYPENAMES)
819 // ---------- part 1 ------------------------------------------------------
820 // start or stop global animations by change of game mode
821 // (special handling of animations for "current screen" and "all screens")
823 if (global.anim_status_next != anim_status_last_before_fading)
825 // stop animations for last screen before fading to new screen
826 game_mode_anim_action[anim_status_last] = ANIM_STOP;
828 // start animations for current screen after fading to new screen
829 game_mode_anim_action[global.anim_status] = ANIM_START;
832 // start animations for all screens after loading new artwork set
833 if (anim_status_last == GAME_MODE_LOADING)
834 game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
836 // ---------- part 2 ------------------------------------------------------
837 // start or stop global animations by change of animation class
838 // (generic handling of animations for "class of screens")
840 for (i = 0; i < NUM_ANIM_CLASSES; i++)
842 int anim_class_check = (1 << i);
843 int anim_class_game_mode = anim_class_game_modes[i];
844 int anim_class_last = anim_classes_last & anim_class_check;
845 int anim_class_next = anim_classes_next & anim_class_check;
847 // stop animations for changed screen class before fading to new screen
848 if (before_fading && anim_class_last && !anim_class_next)
849 game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
851 // start animations for changed screen class after fading to new screen
852 if (after_fading && !anim_class_last && anim_class_next)
853 game_mode_anim_action[anim_class_game_mode] = ANIM_START;
856 if (drawing_target == DRAW_TO_SCREEN)
860 anim_classes_last = anim_classes_next;
861 anim_status_last_before_fading = global.anim_status;
864 anim_status_last = global.anim_status;
866 // start or stop animations determined to be started or stopped above
867 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
868 if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
869 HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
871 else if (drawing_target == DRAW_TO_FADE_TARGET)
873 drawing_to_fading_buffer = TRUE;
875 // start animations determined to be (temporary) started above
876 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
877 if (game_mode_anim_action[mode_nr] == ANIM_START)
878 HandleGlobalAnim(ANIM_START, mode_nr);
882 // when restarting global animations, do not redraw them, but stop here
883 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_RESTART)
886 if (global.anim_status == GAME_MODE_LOADING)
889 for (i = 0; i < num_global_anim_list; i++)
891 struct GlobalAnimPartControlInfo *part = global_anim_list[i];
892 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
893 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
894 struct GraphicInfo *g = &part->graphic_info;
899 int last_anim_random_frame = gfx.anim_random_frame;
901 // when preparing source fading buffer, only draw animations to be stopped
902 if (drawing_target == DRAW_TO_FADE_SOURCE &&
903 game_mode_anim_action[part->mode_nr] != ANIM_STOP)
906 // when preparing target fading buffer, only draw animations to be started
907 if (drawing_target == DRAW_TO_FADE_TARGET &&
908 game_mode_anim_action[part->mode_nr] != ANIM_START)
911 if (!(anim->state & ANIM_STATE_RUNNING))
914 if (!(part->state & ANIM_STATE_RUNNING))
917 if (part->drawing_stage != drawing_stage)
920 // if game is paused, also pause playfield and door animations
921 if (isPausedOnPlayfieldOrDoor(part))
922 part->initial_anim_sync_frame++;
924 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
926 // re-initialize random animation frame after animation delay
927 if (g->anim_mode == ANIM_RANDOM &&
928 sync_frame % g->anim_delay == 0 &&
930 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
932 gfx.anim_random_frame = part->anim_random_frame;
934 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
935 g->anim_mode, g->anim_start_frame,
938 gfx.anim_random_frame = last_anim_random_frame;
940 getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
943 BlitGlobalAnimation(part, src_bitmap, src_x, src_y, drawing_target);
946 if (drawing_target == DRAW_TO_FADE_TARGET)
948 // stop animations determined to be (temporary) started above
949 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
950 if (game_mode_anim_action[mode_nr] == ANIM_START)
951 HandleGlobalAnim(ANIM_STOP, mode_nr);
953 drawing_to_fading_buffer = FALSE;
957 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
959 int last_cursor_mode_override = gfx.cursor_mode_override;
961 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
963 ResetGlobalAnim_Clickable();
965 gfx.cursor_mode_override = CURSOR_UNDEFINED;
968 DrawGlobalAnimationsExt(drawing_target, drawing_stage);
970 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
972 ResetGlobalAnim_Clicked();
975 if (gfx.cursor_mode_override != last_cursor_mode_override)
976 SetMouseCursor(gfx.cursor_mode);
979 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
985 boolean changed = FALSE;
987 if (part->last_anim_status == global.anim_status &&
988 part->control_info.class != get_hash_from_key("pointer"))
991 part->last_anim_status = global.anim_status;
993 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
995 part->class_playfield_or_door = FALSE;
997 if (part->control_info.class == get_hash_from_key("window") ||
998 part->control_info.class == get_hash_from_key("border"))
1002 viewport_width = WIN_XSIZE;
1003 viewport_height = WIN_YSIZE;
1005 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
1007 else if (part->control_info.class == get_hash_from_key("pointer"))
1009 int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
1010 int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
1012 // prevent displaying off-screen custom mouse cursor in upper left corner
1013 if (gfx.mouse_x == POS_OFFSCREEN &&
1014 gfx.mouse_y == POS_OFFSCREEN)
1015 mx = my = POS_OFFSCREEN;
1017 viewport_x = mx - part->control_info.x;
1018 viewport_y = my - part->control_info.y;
1019 viewport_width = part->graphic_info.width;
1020 viewport_height = part->graphic_info.height;
1022 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_3;
1024 // do not use global animation mouse pointer when reloading artwork
1025 if (global.anim_status != GAME_MODE_LOADING)
1026 gfx.cursor_mode_override = CURSOR_NONE;
1028 else if (part->control_info.class == get_hash_from_key("door_1"))
1032 viewport_width = DXSIZE;
1033 viewport_height = DYSIZE;
1035 part->class_playfield_or_door = TRUE;
1037 else if (part->control_info.class == get_hash_from_key("door_2"))
1039 if (part->mode_nr == GAME_MODE_EDITOR)
1043 viewport_width = EXSIZE;
1044 viewport_height = EYSIZE;
1050 viewport_width = VXSIZE;
1051 viewport_height = VYSIZE;
1054 part->class_playfield_or_door = TRUE;
1056 else // default: "playfield"
1058 viewport_x = REAL_SX;
1059 viewport_y = REAL_SY;
1060 viewport_width = FULL_SXSIZE;
1061 viewport_height = FULL_SYSIZE;
1063 part->class_playfield_or_door = TRUE;
1066 if (viewport_x != part->viewport_x ||
1067 viewport_y != part->viewport_y ||
1068 viewport_width != part->viewport_width ||
1069 viewport_height != part->viewport_height)
1071 part->viewport_x = viewport_x;
1072 part->viewport_y = viewport_y;
1073 part->viewport_width = viewport_width;
1074 part->viewport_height = viewport_height;
1076 if (part->control_info.class != get_hash_from_key("pointer"))
1083 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1085 int sound = part->sound;
1087 if (sound == SND_UNDEFINED)
1090 if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1091 (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1094 // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1095 if (IS_LOOP_SOUND(sound))
1096 PlaySoundLoop(sound);
1101 Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1102 part->anim_nr, part->nr, part->mode_nr, sound);
1106 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1108 int sound = part->sound;
1110 if (sound == SND_UNDEFINED)
1116 Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1117 part->anim_nr, part->nr, part->mode_nr, sound);
1121 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1123 int music = part->music;
1125 if (music == MUS_UNDEFINED)
1128 if (!setup.sound_music)
1131 if (IS_LOOP_MUSIC(music))
1132 PlayMusicLoop(music);
1137 Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1138 part->anim_nr, part->nr, part->mode_nr, music);
1142 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1144 int music = part->music;
1146 if (music == MUS_UNDEFINED)
1149 char *anim_music = getMusicInfoEntryFilename(music);
1150 char *curr_music = getCurrentlyPlayingMusicFilename();
1152 // do not stop music if global anim music differs from current music
1153 if (!strEqual(curr_music, anim_music))
1159 Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1160 part->anim_nr, part->nr, part->mode_nr, music);
1164 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1166 // when drawing animations to fading buffer, do not play sounds or music
1167 if (drawing_to_fading_buffer)
1170 PlayGlobalAnimSound(part);
1171 PlayGlobalAnimMusic(part);
1174 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1176 StopGlobalAnimSound(part);
1177 StopGlobalAnimMusic(part);
1180 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1182 // when drawing animations to fading buffer, do not play sounds
1183 if (drawing_to_fading_buffer)
1186 // loop sounds only expire when playing
1187 if (game_status != GAME_MODE_PLAYING)
1190 // check if any sound is defined for this animation part
1191 if (part->sound == SND_UNDEFINED)
1194 // normal (non-loop) sounds do not expire when playing
1195 if (!IS_LOOP_SOUND(part->sound))
1198 // prevent expiring loop sounds when playing
1199 PlayGlobalAnimSound(part);
1202 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1204 int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1205 int mask_ce_only = mask & ~ANIM_EVENT_PAGE_MASK;
1207 if (mask & ANIM_EVENT_ANY)
1208 return (anim_event & ANIM_EVENT_ANY);
1209 else if (mask & ANIM_EVENT_SELF)
1210 return (anim_event & ANIM_EVENT_SELF);
1211 else if (mask & ANIM_EVENT_UNCLICK_ANY)
1212 return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1213 else if (mask & ANIM_EVENT_CE_CHANGE)
1214 return (anim_event == mask ||
1215 anim_event == mask_ce_only);
1217 return (anim_event == mask ||
1218 anim_event == mask_anim_only);
1221 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1223 struct GraphicInfo *c = &part->control_info;
1226 if (part->init_event_state)
1228 int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1230 for (i = 0; i < num_init_events; i++)
1232 int init_event = GetGlobalAnimEventValue(c->init_event, i);
1234 if (checkGlobalAnimEvent(init_event, mask))
1238 else if (part->anim_event_state)
1240 int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1242 for (i = 0; i < num_anim_events; i++)
1244 int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1246 if (checkGlobalAnimEvent(anim_event, mask))
1254 static boolean isInsidePartStacked(struct GlobalAnimPartControlInfo *part,
1257 struct GraphicInfo *g = &part->graphic_info;
1258 struct GraphicInfo *c = &part->control_info;
1259 int part_x = part->viewport_x + part->x;
1260 int part_y = part->viewport_y + part->y;
1261 int part_width = g->width;
1262 int part_height = g->height;
1265 for (y = 0; y < c->stacked_yfactor; y++)
1267 for (x = 0; x < c->stacked_xfactor; x++)
1269 int part_stacked_x = part_x + x * (part_width + c->stacked_xoffset);
1270 int part_stacked_y = part_y + y * (part_height + c->stacked_yoffset);
1272 if (mx >= part_stacked_x &&
1273 mx < part_stacked_x + part_width &&
1274 my >= part_stacked_y &&
1275 my < part_stacked_y + part_height)
1283 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1284 int mx, int my, boolean clicked)
1286 // check if mouse click was detected at all
1290 // check if mouse click is outside the animation part's viewport
1291 if (mx < part->viewport_x ||
1292 mx >= part->viewport_x + part->viewport_width ||
1293 my < part->viewport_y ||
1294 my >= part->viewport_y + part->viewport_height)
1297 // check if mouse click is inside the animation part's (stacked) graphic
1298 if (isInsidePartStacked(part, mx, my))
1304 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1306 return ((part->control_info.style & STYLE_BLOCK) ? TRUE : FALSE);
1309 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1311 return ((part->control_info.style & STYLE_PASSTHROUGH) ? FALSE : TRUE);
1314 static void SetGlobalAnimPartTileXY(struct GlobalAnimPartControlInfo *part)
1316 // calculate playfield position (with scrolling) for related CE tile
1317 // (do not use FX/FY, which are incorrect during envelope requests)
1318 int FX0 = 2 * TILEX_VAR; // same as FX during DRAW_TO_FIELDBUFFER
1319 int FY0 = 2 * TILEY_VAR; // same as FY during DRAW_TO_FIELDBUFFER
1320 int fx = getFieldbufferOffsetX_RND(ScreenMovDir, ScreenGfxPos);
1321 int fy = getFieldbufferOffsetY_RND(ScreenMovDir, ScreenGfxPos);
1322 int sx = FX0 + SCREENX(part->tile_x) * TILEX_VAR;
1323 int sy = FY0 + SCREENY(part->tile_y) * TILEY_VAR;
1324 int cx = SX - REAL_SX;
1325 int cy = SY - REAL_SY;
1326 int x = sx - fx + cx;
1327 int y = sy - fy + cy;
1329 part->tile_xoffset += part->step_xoffset;
1330 part->tile_yoffset += part->step_yoffset;
1332 part->x = x + part->tile_xoffset;
1333 part->y = y + part->tile_yoffset;
1336 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1337 boolean *click_consumed,
1338 boolean *any_event_action,
1339 int event_value, char *info_text)
1341 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1343 int gic_anim_nr = part->old_anim_nr + 1; // X as in "anim_X"
1344 int gic_part_nr = part->old_nr + 1; // Y as in "part_Y"
1345 int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1348 mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1352 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1354 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1357 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1359 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1361 if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1364 if (isClickablePart(part2, mask))
1366 part2->triggered = TRUE;
1367 *click_consumed |= clickConsumed(part); // click was on "part"!
1369 #if DEBUG_ANIM_EVENTS
1370 Debug("anim:InitGlobalAnim_Triggered",
1371 "%d.%d TRIGGERED BY %s OF %d.%d",
1372 part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1373 part->old_anim_nr + 1, part->old_nr + 1);
1376 Debug("anim:InitGlobalAnim_Triggered",
1377 "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1378 part2->control_info.anim_event_action);
1381 // after executing event action, ignore any further actions
1382 if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1383 *any_event_action = TRUE;
1387 struct GraphicInfo *c = &part2->control_info;
1389 if (isClickablePart(part2, mask))
1390 Debug("anim:InitGlobalAnim_Triggered",
1391 "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1392 anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1395 Debug("anim:InitGlobalAnim_Triggered",
1396 "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1397 anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1403 static void InitGlobalAnim_Triggered_ByCustomElement(int nr, int page,
1408 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_PLAYING];
1410 int event_value = ANIM_EVENT_CE_CHANGE;
1411 int event_bits = (nr << ANIM_EVENT_CE_BIT) | (page << ANIM_EVENT_PAGE_BIT);
1412 int mask = event_value | event_bits;
1415 for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1417 struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1420 for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1422 struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1424 if (!(part2->state & (ANIM_STATE_RUNNING | ANIM_STATE_WAITING)))
1427 if (isClickablePart(part2, mask) && !part2->triggered)
1429 struct GraphicInfo *c = &part2->control_info;
1431 if (c->position == POS_CE ||
1432 c->position == POS_CE_TRIGGER)
1434 // store CE tile and offset position to handle scrolling
1435 part2->tile_x = (c->position == POS_CE_TRIGGER ? trigger_x : x);
1436 part2->tile_y = (c->position == POS_CE_TRIGGER ? trigger_y : y);
1437 part2->tile_xoffset = c->x;
1438 part2->tile_yoffset = c->y;
1440 // set resulting animation position relative to CE tile position
1441 // (but only for ".init_event", not ".anim_event" type events)
1442 if (part2->init_event_state)
1443 SetGlobalAnimPartTileXY(part2);
1445 // restart animation (by using current sync frame)
1446 part2->initial_anim_sync_frame = anim_sync_frame;
1449 part2->triggered = TRUE;
1451 // do not trigger any other animation if CE change event was consumed
1452 if (c->style == STYLE_CONSUME_CE_EVENT)
1456 Debug("anim:InitGlobalAnim_Triggered_ByCustomElement",
1457 "%d.%d TRIGGERED BY CE %d", anim2_nr, part2_nr, nr + 1);
1464 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1465 int delay_type, char *info_text)
1467 #if DEBUG_ANIM_DELAY
1468 Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1469 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1472 DoGlobalAnim_DelayAction(part, delay_type);
1475 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1476 int event_value, char *info_text)
1478 #if DEBUG_ANIM_EVENTS
1479 Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1480 part->old_anim_nr + 1, part->old_nr + 1, info_text);
1483 boolean click_consumed = FALSE;
1484 boolean any_event_action = FALSE;
1486 // check if this event is defined to trigger other animations
1487 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1488 event_value, info_text);
1491 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1494 if (handle_click && !part->clicked)
1497 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1498 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1499 struct GraphicInfo *g = &part->graphic_info;
1500 struct GraphicInfo *c = &part->control_info;
1501 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1502 int alpha = (g->alpha != -1 ? g->alpha : SDL_ALPHA_OPAQUE);
1504 // if game is paused, also pause playfield and door animations
1505 if (isPausedOnPlayfieldOrDoor(part))
1508 if (viewport_changed)
1509 state |= ANIM_STATE_RESTART;
1511 if (state & ANIM_STATE_RESTART)
1513 // when drawing animations to fading buffer, only start fixed animations
1514 if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1515 c->y == ARG_UNDEFINED_VALUE))
1516 return ANIM_STATE_INACTIVE;
1518 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1520 part->init_delay_counter =
1521 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1523 part->anim_delay_counter =
1524 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1526 part->post_delay_counter = 0;
1528 part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1529 part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1531 part->initial_anim_sync_frame =
1532 (g->anim_global_sync || g->anim_global_anim_sync ? 0 :
1533 anim_sync_frame + part->init_delay_counter);
1535 // do not re-initialize random animation frame after fade-in
1536 if (part->anim_random_frame == -1)
1537 part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1539 if (c->fade_mode & FADE_MODE_FADE)
1541 // when fading in screen, first frame is 100 % transparent or opaque
1542 part->fade_delay_counter = c->fade_delay + 1;
1543 part->fade_alpha = (c->fade_mode == FADE_MODE_FADE_IN ? 0 : alpha);
1547 part->fade_delay_counter = 0;
1548 part->fade_alpha = -1;
1551 if (c->direction & MV_HORIZONTAL)
1553 int pos_bottom = part->viewport_height - g->height;
1555 if (c->position == POS_TOP)
1557 else if (c->position == POS_UPPER)
1558 part->y = GetSimpleRandom(pos_bottom / 2);
1559 else if (c->position == POS_MIDDLE)
1560 part->y = pos_bottom / 2;
1561 else if (c->position == POS_LOWER)
1562 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1563 else if (c->position == POS_BOTTOM)
1564 part->y = pos_bottom;
1566 part->y = GetSimpleRandom(pos_bottom);
1568 if (c->direction == MV_RIGHT)
1570 part->step_xoffset = c->step_offset;
1571 part->x = -g->width + part->step_xoffset;
1575 part->step_xoffset = -c->step_offset;
1576 part->x = part->viewport_width + part->step_xoffset;
1579 part->step_yoffset = 0;
1581 else if (c->direction & MV_VERTICAL)
1583 int pos_right = part->viewport_width - g->width;
1585 if (c->position == POS_LEFT)
1587 else if (c->position == POS_RIGHT)
1588 part->x = pos_right;
1590 part->x = GetSimpleRandom(pos_right);
1592 if (c->direction == MV_DOWN)
1594 part->step_yoffset = c->step_offset;
1595 part->y = -g->height + part->step_yoffset;
1599 part->step_yoffset = -c->step_offset;
1600 part->y = part->viewport_height + part->step_yoffset;
1603 part->step_xoffset = 0;
1610 part->step_xoffset = 0;
1611 part->step_yoffset = 0;
1614 if (part->control_info.class != get_hash_from_key("pointer"))
1616 if (c->x != ARG_UNDEFINED_VALUE)
1618 if (c->y != ARG_UNDEFINED_VALUE)
1622 if (c->position == POS_LAST &&
1623 anim->last_x > -g->width && anim->last_x < part->viewport_width &&
1624 anim->last_y > -g->height && anim->last_y < part->viewport_height)
1626 part->x = anim->last_x;
1627 part->y = anim->last_y;
1630 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1631 part->step_xoffset = c->step_xoffset;
1632 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1633 part->step_yoffset = c->step_yoffset;
1635 if (part->init_delay_counter == 0 &&
1636 !part->init_event_state)
1638 PlayGlobalAnimSoundAndMusic(part);
1640 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1641 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1645 HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1649 if (part->clicked &&
1650 part->init_event_state)
1652 if (part->initial_anim_sync_frame > 0)
1653 part->initial_anim_sync_frame = anim_sync_frame;
1655 part->init_delay_counter = 1;
1656 part->init_event_state = FALSE;
1658 part->clicked = FALSE;
1661 if (part->clicked &&
1662 part->anim_event_state)
1664 part->anim_delay_counter = 1;
1665 part->anim_event_state = FALSE;
1667 part->clicked = FALSE;
1670 if (part->init_delay_counter > 0)
1672 part->init_delay_counter--;
1674 if (part->init_delay_counter == 0)
1676 part->init_event_state = FALSE;
1678 PlayGlobalAnimSoundAndMusic(part);
1680 HandleGlobalAnimDelay(part, ANIM_DELAY_INIT, "START [INIT_DELAY]");
1681 HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1683 // continue with state ANIM_STATE_RUNNING (set below)
1687 return ANIM_STATE_WAITING;
1691 if (part->init_event_state)
1692 return ANIM_STATE_WAITING;
1694 // animation part is now running/visible and therefore clickable
1695 part->clickable = TRUE;
1697 // check if moving animation has left the visible screen area
1698 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
1699 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
1700 (part->y <= -g->height && part->step_yoffset <= 0) ||
1701 (part->y >= part->viewport_height && part->step_yoffset >= 0))
1703 // do not wait for "anim" events for off-screen animations
1704 part->anim_event_state = FALSE;
1706 // do not stop animation before "anim" or "post" counter are finished
1707 if (part->anim_delay_counter == 0 &&
1708 part->post_delay_counter == 0)
1710 StopGlobalAnimSoundAndMusic(part);
1712 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1714 part->post_delay_counter =
1715 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1717 if (part->post_delay_counter > 0)
1718 return ANIM_STATE_RUNNING;
1720 // drawing last frame not needed here -- animation not visible anymore
1721 return ANIM_STATE_RESTART;
1725 if (part->anim_delay_counter > 0)
1727 part->anim_delay_counter--;
1729 if (part->anim_delay_counter == 0)
1731 part->anim_event_state = FALSE;
1733 StopGlobalAnimSoundAndMusic(part);
1735 HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1736 HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM_DELAY/EVENT]");
1738 part->post_delay_counter =
1739 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1741 if (part->post_delay_counter > 0)
1742 return ANIM_STATE_RUNNING;
1744 // additional state "RUNNING" required to not skip drawing last frame
1745 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1749 if (part->post_delay_counter > 0)
1751 part->post_delay_counter--;
1753 if (part->post_delay_counter == 0)
1755 HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1756 HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1758 return ANIM_STATE_RESTART;
1761 return ANIM_STATE_WAITING;
1764 if (part->fade_delay_counter > 0)
1766 part->fade_delay_counter--;
1767 part->fade_alpha = alpha * (c->fade_mode == FADE_MODE_FADE_IN ?
1768 c->fade_delay - part->fade_delay_counter :
1769 part->fade_delay_counter) / c->fade_delay;
1772 // special case to prevent expiring loop sounds when playing
1773 PlayGlobalAnimSoundIfLoop(part);
1775 if (!DelayReachedExt(&part->step_delay, anim_sync_frame))
1776 return ANIM_STATE_RUNNING;
1780 static unsigned int last_counter = -1;
1781 unsigned int counter = Counter();
1783 Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1784 anim_sync_frame, counter - last_counter);
1786 last_counter = counter;
1790 if (c->position == POS_CE ||
1791 c->position == POS_CE_TRIGGER)
1793 SetGlobalAnimPartTileXY(part);
1797 part->x += part->step_xoffset;
1798 part->y += part->step_yoffset;
1801 anim->last_x = part->x;
1802 anim->last_y = part->y;
1804 return ANIM_STATE_RUNNING;
1807 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1810 struct GlobalAnimPartControlInfo *part;
1811 struct GraphicInfo *c = &anim->control_info;
1812 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1813 int state, active_part_nr;
1817 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1818 anim->mode_nr, anim->nr, anim->num_parts);
1819 Debug("anim:HandleGlobalAnim_Main",
1820 "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1824 Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1825 (action == ANIM_START ? "ANIM_START" :
1826 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1827 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1829 anim->state & ANIM_STATE_RESTART,
1830 anim->state & ANIM_STATE_WAITING,
1831 anim->state & ANIM_STATE_RUNNING,
1838 anim->state = anim->last_state = ANIM_STATE_RESTART;
1839 anim->active_part_nr = anim->last_active_part_nr = 0;
1840 anim->part_counter = 0;
1845 if (anim->state == ANIM_STATE_INACTIVE)
1848 anim->state = anim->last_state;
1849 anim->active_part_nr = anim->last_active_part_nr;
1854 anim->state = ANIM_STATE_INACTIVE;
1856 for (i = 0; i < num_parts; i++)
1857 StopGlobalAnimSoundAndMusic(&anim->part[i]);
1865 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1868 Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1869 anim->mode_nr, anim->nr, num_parts);
1872 for (i = 0; i < num_parts; i++)
1874 part = &anim->part[i];
1879 anim->state = ANIM_STATE_RUNNING;
1880 part->state = ANIM_STATE_RESTART;
1885 if (part->state == ANIM_STATE_INACTIVE)
1891 part->state = ANIM_STATE_INACTIVE;
1899 part->state = HandleGlobalAnim_Part(part, part->state);
1901 // when animation mode is "once", stop after animation was played once
1902 if (c->anim_mode & ANIM_ONCE &&
1903 part->state & ANIM_STATE_RESTART)
1904 part->state = ANIM_STATE_INACTIVE;
1907 anim->last_state = anim->state;
1908 anim->last_active_part_nr = anim->active_part_nr;
1913 if (anim->state & ANIM_STATE_RESTART) // directly after restart
1914 anim->active_part_nr = getGlobalAnimationPart(anim);
1916 part = &anim->part[anim->active_part_nr];
1918 // first set all animation parts to "inactive", ...
1919 for (i = 0; i < num_parts; i++)
1920 anim->part[i].state = ANIM_STATE_INACTIVE;
1922 // ... then set current animation part to "running" ...
1923 part->state = ANIM_STATE_RUNNING;
1925 // ... unless it is waiting for an initial event
1926 if (part->init_event_state)
1927 part->state = ANIM_STATE_WAITING;
1929 anim->state = HandleGlobalAnim_Part(part, anim->state);
1931 if (anim->state & ANIM_STATE_RESTART)
1932 anim->part_counter++;
1934 // when animation mode is "once", stop after all animations were played once
1935 if (c->anim_mode & ANIM_ONCE &&
1936 anim->part_counter == anim->num_parts)
1937 anim->state = ANIM_STATE_INACTIVE;
1939 state = anim->state;
1940 active_part_nr = anim->active_part_nr;
1942 // while the animation parts are pausing (waiting or inactive), play the base
1943 // (main) animation; this corresponds to the "boring player animation" logic
1944 // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1947 if (anim->state == ANIM_STATE_WAITING ||
1948 anim->state == ANIM_STATE_INACTIVE)
1950 anim->active_part_nr = anim->num_parts; // part nr of base animation
1951 part = &anim->part[anim->active_part_nr];
1953 if (anim->state != anim->last_state)
1954 part->state = ANIM_STATE_RESTART;
1956 anim->state = ANIM_STATE_RUNNING;
1957 part->state = HandleGlobalAnim_Part(part, part->state);
1961 anim->last_state = state;
1962 anim->last_active_part_nr = active_part_nr;
1965 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1970 Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1973 for (i = 0; i < ctrl->num_anims; i++)
1974 HandleGlobalAnim_Main(&ctrl->anim[i], action);
1977 static void HandleGlobalAnim(int action, int game_mode)
1980 Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1983 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1986 static void DoAnimationExt(void)
1991 Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1994 // global animations now synchronized with frame delay of screen update
1997 for (i = 0; i < NUM_GAME_MODES; i++)
1998 HandleGlobalAnim(ANIM_CONTINUE, i);
2001 // force screen redraw in next frame to continue drawing global animations
2002 redraw_mask = REDRAW_ALL;
2006 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
2010 (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
2011 delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
2012 delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
2013 ANIM_DELAY_ACTION_NONE);
2015 if (delay_action == ANIM_DELAY_ACTION_NONE)
2018 PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
2021 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
2023 int event_action = (part->init_event_state ?
2024 part->control_info.init_event_action :
2025 part->control_info.anim_event_action);
2027 if (event_action == ANIM_EVENT_ACTION_NONE)
2030 if (event_action < MAX_IMAGE_FILES)
2031 PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
2033 OpenURLFromHash(anim_url_hash, event_action);
2035 // check if further actions are allowed to be executed
2036 if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
2042 static void InitGlobalAnim_Clickable(void)
2046 for (i = 0; i < num_global_anim_list; i++)
2048 struct GlobalAnimPartControlInfo *part = global_anim_list[i];
2050 if (part->triggered)
2051 part->clicked = TRUE;
2053 part->triggered = FALSE;
2054 part->clickable = FALSE;
2058 #define ANIM_CLICKED_RESET 0
2059 #define ANIM_CLICKED_PRESSED 1
2060 #define ANIM_CLICKED_RELEASED 2
2062 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
2064 boolean click_consumed = FALSE;
2065 boolean anything_clicked = FALSE;
2066 boolean any_part_clicked = FALSE;
2067 boolean any_event_action = FALSE;
2070 // check animation parts in reverse draw order (to stop when clicked)
2071 for (i = num_global_anim_list - 1; i >= 0; i--)
2073 struct GlobalAnimPartControlInfo *part = global_anim_list[i];
2075 // if request dialog is active, only handle pointer-style animations
2076 if (game.request_active &&
2077 part->control_info.class != get_hash_from_key("pointer"))
2080 if (clicked_event == ANIM_CLICKED_RESET)
2082 part->clicked = FALSE;
2087 if (!part->clickable)
2090 if (!(part->state & ANIM_STATE_RUNNING))
2093 // always handle "any" click events (clicking anywhere on screen) ...
2094 if (clicked_event == ANIM_CLICKED_PRESSED &&
2095 isClickablePart(part, ANIM_EVENT_ANY))
2097 #if DEBUG_ANIM_EVENTS
2098 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
2099 part->old_anim_nr + 1, part->old_nr + 1);
2102 anything_clicked = part->clicked = TRUE;
2103 click_consumed |= clickConsumed(part);
2106 // always handle "unclick:any" events (releasing anywhere on screen) ...
2107 if (clicked_event == ANIM_CLICKED_RELEASED &&
2108 isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
2110 #if DEBUG_ANIM_EVENTS
2111 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
2112 part->old_anim_nr + 1, part->old_nr + 1);
2115 anything_clicked = part->clicked = TRUE;
2116 click_consumed |= clickConsumed(part);
2119 // ... but only handle the first (topmost) clickable animation
2120 if (any_part_clicked)
2123 if (clicked_event == ANIM_CLICKED_PRESSED &&
2124 isClickedPart(part, mx, my, TRUE))
2127 Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
2128 anim_nr, part_nr, part->control_info.anim_event_action);
2131 // after executing event action, ignore any further actions
2132 if (!any_event_action && DoGlobalAnim_EventAction(part))
2133 any_event_action = TRUE;
2135 // determine if mouse clicks should be blocked from other animations
2136 any_part_clicked |= clickConsumed(part);
2138 if (isClickablePart(part, ANIM_EVENT_SELF))
2140 #if DEBUG_ANIM_EVENTS
2141 Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
2142 part->old_anim_nr + 1, part->old_nr + 1);
2145 anything_clicked = part->clicked = TRUE;
2146 click_consumed |= clickConsumed(part);
2149 // determine if mouse clicks should be blocked by this animation
2150 click_consumed |= clickBlocked(part);
2152 // check if this click is defined to trigger other animations
2153 InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
2154 ANIM_EVENT_CLICK, "CLICK");
2158 if (anything_clicked)
2160 handle_click = TRUE;
2162 for (i = 0; i < NUM_GAME_MODES; i++)
2163 HandleGlobalAnim(ANIM_CONTINUE, i);
2165 handle_click = FALSE;
2167 // prevent ignoring release event if processed within same game frame
2168 StopProcessingEvents();
2171 return (click_consumed || any_event_action);
2174 static void ResetGlobalAnim_Clickable(void)
2176 InitGlobalAnim_Clickable();
2179 static void ResetGlobalAnim_Clicked(void)
2181 InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
2184 void RestartGlobalAnimsByStatus(int status)
2186 int anim_status_last = global.anim_status;
2188 global.anim_status = status;
2190 // force restarting global animations by changed global animation status
2191 DrawGlobalAnimationsExt(DRAW_TO_SCREEN, DRAW_GLOBAL_ANIM_STAGE_RESTART);
2193 global.anim_status = anim_status_last;
2196 void SetAnimStatusBeforeFading(int status)
2198 anim_status_last_before_fading = status;
2201 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
2203 static boolean click_consumed = FALSE;
2204 static int last_button = 0;
2205 boolean press_event;
2206 boolean release_event;
2207 boolean click_consumed_current = click_consumed;
2209 if (button != 0 && force_click)
2212 // check if button state has changed since last invocation
2213 press_event = (button != 0 && last_button == 0);
2214 release_event = (button == 0 && last_button != 0);
2215 last_button = button;
2219 click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
2220 click_consumed_current = click_consumed;
2225 InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
2226 click_consumed = FALSE;
2229 return click_consumed_current;
2232 int getGlobalAnimSyncFrame(void)
2234 return anim_sync_frame;
2237 void HandleGlobalAnimEventByElementChange(int element, int page, int x, int y,
2238 int trigger_x, int trigger_y)
2240 if (!IS_CUSTOM_ELEMENT(element))
2243 // custom element stored as 0 to 255, change page stored as 1 to 32
2244 InitGlobalAnim_Triggered_ByCustomElement(element - EL_CUSTOM_START, page + 1,
2245 x, y, trigger_x, trigger_y);