added optional button to restart game (door, panel and touch variants)
[rocksndiamonds.git] / src / anim.c
1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
5 //                  Holger Schemel
6 //                  info@artsoft.org
7 //                  https://www.artsoft.org/
8 // ----------------------------------------------------------------------------
9 // anim.c
10 // ============================================================================
11
12 #include "libgame/libgame.h"
13
14 #include "anim.h"
15 #include "main.h"
16 #include "tools.h"
17 #include "files.h"
18 #include "events.h"
19 #include "screens.h"
20
21
22 #define DEBUG_ANIM_DELAY                0
23 #define DEBUG_ANIM_EVENTS               0
24
25
26 // values for global toon animation definition
27 #define NUM_GLOBAL_TOON_ANIMS           1
28 #define NUM_GLOBAL_TOON_PARTS           MAX_NUM_TOONS
29
30 // values for global animation definition (including toons)
31 #define NUM_GLOBAL_ANIMS_AND_TOONS      (NUM_GLOBAL_ANIMS +             \
32                                          NUM_GLOBAL_TOON_ANIMS)
33 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL,  \
34                                             NUM_GLOBAL_TOON_PARTS)
35
36 #define ANIM_CLASS_BIT_TITLE_INITIAL    0
37 #define ANIM_CLASS_BIT_TITLE            1
38 #define ANIM_CLASS_BIT_MAIN             2
39 #define ANIM_CLASS_BIT_NAMES            3
40 #define ANIM_CLASS_BIT_SCORES           4
41 #define ANIM_CLASS_BIT_SCORESONLY       5
42 #define ANIM_CLASS_BIT_SUBMENU          6
43 #define ANIM_CLASS_BIT_MENU             7
44 #define ANIM_CLASS_BIT_TOONS            8
45 #define ANIM_CLASS_BIT_NO_TITLE         9
46
47 #define NUM_ANIM_CLASSES                10
48
49 #define ANIM_CLASS_NONE                 0
50 #define ANIM_CLASS_TITLE_INITIAL        (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
51 #define ANIM_CLASS_TITLE                (1 << ANIM_CLASS_BIT_TITLE)
52 #define ANIM_CLASS_MAIN                 (1 << ANIM_CLASS_BIT_MAIN)
53 #define ANIM_CLASS_NAMES                (1 << ANIM_CLASS_BIT_NAMES)
54 #define ANIM_CLASS_SCORES               (1 << ANIM_CLASS_BIT_SCORES)
55 #define ANIM_CLASS_SCORESONLY           (1 << ANIM_CLASS_BIT_SCORESONLY)
56 #define ANIM_CLASS_SUBMENU              (1 << ANIM_CLASS_BIT_SUBMENU)
57 #define ANIM_CLASS_MENU                 (1 << ANIM_CLASS_BIT_MENU)
58 #define ANIM_CLASS_TOONS                (1 << ANIM_CLASS_BIT_TOONS)
59 #define ANIM_CLASS_NO_TITLE             (1 << ANIM_CLASS_BIT_NO_TITLE)
60
61 #define ANIM_CLASS_TOONS_SCORES         (ANIM_CLASS_TOONS       |       \
62                                          ANIM_CLASS_SCORES      |       \
63                                          ANIM_CLASS_NO_TITLE)
64
65 #define ANIM_CLASS_TOONS_SCORESONLY     (ANIM_CLASS_TOONS       |       \
66                                          ANIM_CLASS_SCORES      |       \
67                                          ANIM_CLASS_SCORESONLY  |       \
68                                          ANIM_CLASS_NO_TITLE)
69
70 #define ANIM_CLASS_TOONS_MENU_MAIN      (ANIM_CLASS_TOONS       |       \
71                                          ANIM_CLASS_MENU        |       \
72                                          ANIM_CLASS_MAIN        |       \
73                                          ANIM_CLASS_NO_TITLE)
74
75 #define ANIM_CLASS_TOONS_MENU_SUBMENU   (ANIM_CLASS_TOONS       |       \
76                                          ANIM_CLASS_MENU        |       \
77                                          ANIM_CLASS_SUBMENU     |       \
78                                          ANIM_CLASS_NO_TITLE)
79
80 #define ANIM_CLASS_TOONS_MENU_SUBMENU_2 (ANIM_CLASS_TOONS       |       \
81                                          ANIM_CLASS_MENU        |       \
82                                          ANIM_CLASS_SUBMENU     |       \
83                                          ANIM_CLASS_NAMES       |       \
84                                          ANIM_CLASS_NO_TITLE)
85
86 // values for global animation states
87 #define ANIM_STATE_INACTIVE             0
88 #define ANIM_STATE_RESTART              (1 << 0)
89 #define ANIM_STATE_WAITING              (1 << 1)
90 #define ANIM_STATE_RUNNING              (1 << 2)
91
92 // values for global animation control
93 #define ANIM_NO_ACTION                  0
94 #define ANIM_START                      1
95 #define ANIM_CONTINUE                   2
96 #define ANIM_STOP                       3
97
98
99 struct GlobalAnimPartControlInfo
100 {
101   int old_nr;           // position before mapping animation parts linearly
102   int old_anim_nr;      // position before mapping animations linearly
103
104   int nr;
105   int anim_nr;
106   int mode_nr;
107
108   boolean is_base;      // animation part is base/main/default animation part
109
110   int sound;
111   int music;
112   int graphic;
113
114   struct GraphicInfo graphic_info;
115   struct GraphicInfo control_info;
116
117   int viewport_x;
118   int viewport_y;
119   int viewport_width;
120   int viewport_height;
121
122   int x, y;
123   int step_xoffset, step_yoffset;
124
125   unsigned int initial_anim_sync_frame;
126   unsigned int anim_random_frame;
127   unsigned int step_delay, step_delay_value;
128
129   int init_delay_counter;
130   int anim_delay_counter;
131   int post_delay_counter;
132
133   boolean init_event_state;
134   boolean anim_event_state;
135
136   boolean triggered;
137   boolean clickable;
138   boolean clicked;
139
140   int drawing_stage;
141
142   int state;
143   int last_anim_status;
144 };
145
146 struct GlobalAnimMainControlInfo
147 {
148   struct GlobalAnimPartControlInfo base;
149   struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
150
151   int nr;
152   int mode_nr;
153
154   struct GraphicInfo control_info;
155
156   int num_parts;        // number of animation parts, but without base part
157   int num_parts_all;    // number of animation parts, including base part
158   int part_counter;
159   int active_part_nr;
160
161   boolean has_base;     // animation has base/main/default animation part
162
163   int last_x, last_y;
164
165   int init_delay_counter;
166
167   int state;
168
169   int last_state, last_active_part_nr;
170 };
171
172 struct GlobalAnimControlInfo
173 {
174   struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
175
176   int nr;
177   int num_anims;
178 };
179
180 struct GameModeAnimClass
181 {
182   int game_mode;
183   int class;
184 } game_mode_anim_classes_list[] =
185 {
186   { GAME_MODE_TITLE_INITIAL_1,          ANIM_CLASS_TITLE_INITIAL        },
187   { GAME_MODE_TITLE_INITIAL_2,          ANIM_CLASS_TITLE_INITIAL        },
188   { GAME_MODE_TITLE_INITIAL_3,          ANIM_CLASS_TITLE_INITIAL        },
189   { GAME_MODE_TITLE_INITIAL_4,          ANIM_CLASS_TITLE_INITIAL        },
190   { GAME_MODE_TITLE_INITIAL_5,          ANIM_CLASS_TITLE_INITIAL        },
191   { GAME_MODE_TITLE_1,                  ANIM_CLASS_TITLE                },
192   { GAME_MODE_TITLE_2,                  ANIM_CLASS_TITLE                },
193   { GAME_MODE_TITLE_3,                  ANIM_CLASS_TITLE                },
194   { GAME_MODE_TITLE_4,                  ANIM_CLASS_TITLE                },
195   { GAME_MODE_TITLE_5,                  ANIM_CLASS_TITLE                },
196   { GAME_MODE_NAMES,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
197   { GAME_MODE_LEVELS,                   ANIM_CLASS_TOONS_MENU_SUBMENU   },
198   { GAME_MODE_LEVELNR,                  ANIM_CLASS_TOONS_MENU_SUBMENU   },
199   { GAME_MODE_INFO,                     ANIM_CLASS_TOONS_MENU_SUBMENU   },
200   { GAME_MODE_SETUP,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
201   { GAME_MODE_PSEUDO_NAMESONLY,         ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
202   { GAME_MODE_PSEUDO_TYPENAMES,         ANIM_CLASS_TOONS_MENU_SUBMENU_2 },
203   { GAME_MODE_PSEUDO_MAINONLY,          ANIM_CLASS_TOONS_MENU_MAIN      },
204   { GAME_MODE_PSEUDO_TYPENAME,          ANIM_CLASS_TOONS_MENU_MAIN      },
205   { GAME_MODE_PSEUDO_SCORESOLD,         ANIM_CLASS_TOONS_SCORESONLY     },
206   { GAME_MODE_PSEUDO_SCORESNEW,         ANIM_CLASS_TOONS_SCORESONLY     },
207   { GAME_MODE_SCOREINFO,                ANIM_CLASS_TOONS_SCORES         },
208   { GAME_MODE_EDITOR,                   ANIM_CLASS_NO_TITLE             },
209   { GAME_MODE_PLAYING,                  ANIM_CLASS_NO_TITLE             },
210
211   { -1,                                 -1                              }
212 };
213
214 struct AnimClassGameMode
215 {
216   int class_bit;
217   int game_mode;
218 } anim_class_game_modes_list[] =
219 {
220   { ANIM_CLASS_BIT_TITLE_INITIAL,       GAME_MODE_TITLE_INITIAL         },
221   { ANIM_CLASS_BIT_TITLE,               GAME_MODE_TITLE                 },
222   { ANIM_CLASS_BIT_MAIN,                GAME_MODE_MAIN                  },
223   { ANIM_CLASS_BIT_NAMES,               GAME_MODE_NAMES                 },
224   { ANIM_CLASS_BIT_SCORES,              GAME_MODE_SCORES                },
225   { ANIM_CLASS_BIT_SCORESONLY,          GAME_MODE_PSEUDO_SCORESONLY     },
226   { ANIM_CLASS_BIT_SUBMENU,             GAME_MODE_PSEUDO_SUBMENU        },
227   { ANIM_CLASS_BIT_MENU,                GAME_MODE_PSEUDO_MENU           },
228   { ANIM_CLASS_BIT_TOONS,               GAME_MODE_PSEUDO_TOONS          },
229   { ANIM_CLASS_BIT_NO_TITLE,            GAME_MODE_PSEUDO_NO_TITLE       },
230
231   { -1,                                 -1                              }
232 };
233
234 // forward declaration for internal use
235 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *, int);
236 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *);
237 static void HandleGlobalAnim(int, int);
238 static void DoAnimationExt(void);
239 static void ResetGlobalAnim_Clickable(void);
240 static void ResetGlobalAnim_Clicked(void);
241
242 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
243
244 static unsigned int anim_sync_frame = 0;
245
246 static int game_mode_anim_classes[NUM_GAME_MODES];
247 static int anim_class_game_modes[NUM_ANIM_CLASSES];
248
249 static int anim_status_last_before_fading = GAME_MODE_DEFAULT;
250 static int anim_status_last = GAME_MODE_DEFAULT;
251 static int anim_classes_last = ANIM_CLASS_NONE;
252
253 static boolean drawing_to_fading_buffer = FALSE;
254
255 static boolean handle_click = FALSE;
256
257
258 // ============================================================================
259 // generic animation frame calculation
260 // ============================================================================
261
262 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
263                       int sync_frame)
264 {
265   int frame = 0;
266
267   if (delay < 1)                        // delay must be at least 1
268     delay = 1;
269
270   sync_frame += start_frame * delay;
271
272   if (mode & ANIM_LOOP)                 // looping animation
273   {
274     frame = (sync_frame % (delay * num_frames)) / delay;
275   }
276   else if (mode & ANIM_LINEAR)          // linear (non-looping) animation
277   {
278     frame = sync_frame / delay;
279
280     if (frame > num_frames - 1)
281       frame = num_frames - 1;
282   }
283   else if (mode & ANIM_PINGPONG)        // oscillate (border frames once)
284   {
285     int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
286
287     frame = (sync_frame % (delay * max_anim_frames)) / delay;
288     frame = (frame < num_frames ? frame : max_anim_frames - frame);
289   }
290   else if (mode & ANIM_PINGPONG2)       // oscillate (border frames twice)
291   {
292     int max_anim_frames = 2 * num_frames;
293
294     frame = (sync_frame % (delay * max_anim_frames)) / delay;
295     frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
296   }
297   else if (mode & ANIM_RANDOM)          // play frames in random order
298   {
299     // note: expect different frames for the same delay cycle!
300
301     if (gfx.anim_random_frame < 0)
302       frame = GetSimpleRandom(num_frames);
303     else
304       frame = gfx.anim_random_frame % num_frames;
305   }
306   else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
307   {
308     frame = sync_frame % num_frames;
309   }
310
311   if (mode & ANIM_REVERSE)              // use reverse animation direction
312     frame = num_frames - frame - 1;
313
314   return frame;
315 }
316
317
318 // ============================================================================
319 // global animation functions
320 // ============================================================================
321
322 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
323 {
324   struct GraphicInfo *c = &anim->control_info;
325   int last_anim_random_frame = gfx.anim_random_frame;
326   int part_nr;
327
328   gfx.anim_random_frame = -1;   // (use simple, ad-hoc random numbers)
329
330   part_nr = getAnimationFrame(anim->num_parts, 1,
331                               c->anim_mode, c->anim_start_frame,
332                               anim->part_counter);
333
334   gfx.anim_random_frame = last_anim_random_frame;
335
336   return part_nr;
337 }
338
339 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
340 {
341   const struct GlobalAnimPartControlInfo *o1 =
342     (struct GlobalAnimPartControlInfo *)obj1;
343   const struct GlobalAnimPartControlInfo *o2 =
344     (struct GlobalAnimPartControlInfo *)obj2;
345   int compare_result;
346
347   if (o1->control_info.draw_order != o2->control_info.draw_order)
348     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
349   else
350     compare_result = o1->nr - o2->nr;
351
352   return compare_result;
353 }
354
355 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
356 {
357   const struct GlobalAnimMainControlInfo *o1 =
358     (struct GlobalAnimMainControlInfo *)obj1;
359   const struct GlobalAnimMainControlInfo *o2 =
360     (struct GlobalAnimMainControlInfo *)obj2;
361   int compare_result;
362
363   if (o1->control_info.draw_order != o2->control_info.draw_order)
364     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
365   else
366     compare_result = o1->nr - o2->nr;
367
368   return compare_result;
369 }
370
371 static void InitToonControls(void)
372 {
373   int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
374   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
375   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
376   int mode_nr, anim_nr, part_nr;
377   int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
378   int num_toons = MAX_NUM_TOONS;
379   int i;
380
381   if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
382     num_toons = global.num_toons;
383
384   mode_nr = mode_nr_toons;
385   anim_nr = ctrl->num_anims;
386
387   anim->nr = anim_nr;
388   anim->mode_nr = mode_nr;
389   anim->control_info = graphic_info[control];
390
391   anim->num_parts = 0;
392   anim->num_parts_all = 0;
393   anim->part_counter = 0;
394   anim->active_part_nr = 0;
395
396   anim->has_base = FALSE;
397
398   anim->last_x = POS_OFFSCREEN;
399   anim->last_y = POS_OFFSCREEN;
400
401   anim->init_delay_counter = 0;
402
403   anim->state = ANIM_STATE_INACTIVE;
404
405   part_nr = 0;
406
407   for (i = 0; i < num_toons; i++)
408   {
409     struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
410     int sound = SND_UNDEFINED;
411     int music = MUS_UNDEFINED;
412     int graphic = IMG_TOON_1 + i;
413     int control = graphic;
414
415     part->nr = part_nr;
416     part->anim_nr = anim_nr;
417     part->mode_nr = mode_nr;
418
419     part->is_base = FALSE;
420
421     part->sound = sound;
422     part->music = music;
423     part->graphic = graphic;
424
425     part->graphic_info = graphic_info[graphic];
426     part->control_info = graphic_info[control];
427
428     part->graphic_info.anim_delay *= part->graphic_info.step_delay;
429
430     part->control_info.init_delay_fixed = 0;
431     part->control_info.init_delay_random = 150;
432
433     part->control_info.x = ARG_UNDEFINED_VALUE;
434     part->control_info.y = ARG_UNDEFINED_VALUE;
435
436     part->initial_anim_sync_frame = 0;
437     part->anim_random_frame = -1;
438
439     part->step_delay = 0;
440     part->step_delay_value = graphic_info[control].step_delay;
441
442     part->state = ANIM_STATE_INACTIVE;
443     part->last_anim_status = -1;
444
445     anim->num_parts++;
446     anim->num_parts_all++;
447
448     part_nr++;
449   }
450
451   ctrl->num_anims++;
452 }
453
454 static void InitGlobalAnimControls(void)
455 {
456   int i, m, a, p;
457   int mode_nr, anim_nr, part_nr;
458   int sound, music, graphic, control;
459
460   anim_sync_frame = 0;
461
462   for (m = 0; m < NUM_GAME_MODES; m++)
463   {
464     mode_nr = m;
465
466     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
467
468     ctrl->nr = mode_nr;
469     ctrl->num_anims = 0;
470
471     anim_nr = 0;
472
473     for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
474     {
475       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
476       int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
477
478       control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
479
480       // if no base animation parameters defined, use default values
481       if (control == IMG_UNDEFINED)
482         control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
483
484       anim->nr = anim_nr;
485       anim->mode_nr = mode_nr;
486       anim->control_info = graphic_info[control];
487
488       anim->num_parts = 0;
489       anim->num_parts_all = 0;
490       anim->part_counter = 0;
491       anim->active_part_nr = 0;
492
493       anim->has_base = FALSE;
494
495       anim->last_x = POS_OFFSCREEN;
496       anim->last_y = POS_OFFSCREEN;
497
498       anim->init_delay_counter = 0;
499
500       anim->state = ANIM_STATE_INACTIVE;
501
502       part_nr = 0;
503
504       for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
505       {
506         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
507
508         sound   = global_anim_info[a].sound[p][m];
509         music   = global_anim_info[a].music[p][m];
510         graphic = global_anim_info[a].graphic[p][m];
511         control = global_anim_info[ctrl_id].graphic[p][m];
512
513         if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
514             control == IMG_UNDEFINED)
515           continue;
516
517 #if 0
518         Debug("anim:InitGlobalAnimControls",
519               "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
520               m, a, p, mode_nr, anim_nr, part_nr, control);
521 #endif
522
523 #if 0
524         Debug("anim:InitGlobalAnimControls",
525               "mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]",
526               m, a, p, mode_nr, anim_nr, part_nr, sound);
527 #endif
528
529         part->old_nr = p;
530         part->old_anim_nr = a;
531
532         part->nr = part_nr;
533         part->anim_nr = anim_nr;
534         part->mode_nr = mode_nr;
535
536         part->sound = sound;
537         part->music = music;
538         part->graphic = graphic;
539
540         part->graphic_info = graphic_info[graphic];
541         part->control_info = graphic_info[control];
542
543         part->initial_anim_sync_frame = 0;
544         part->anim_random_frame = -1;
545
546         part->step_delay = 0;
547         part->step_delay_value = graphic_info[control].step_delay;
548
549         part->state = ANIM_STATE_INACTIVE;
550         part->last_anim_status = -1;
551
552         anim->num_parts_all++;
553
554         if (p < GLOBAL_ANIM_ID_PART_BASE)
555         {
556           part->is_base = FALSE;
557
558           anim->num_parts++;
559           part_nr++;
560         }
561         else
562         {
563           part->is_base = TRUE;
564
565           anim->base = *part;
566           anim->has_base = TRUE;
567         }
568
569         // apply special settings for pointer-style animations
570         if (part->control_info.class == get_hash_from_key("pointer"))
571         {
572           // force animation to be on top (must set anim and part control)
573           if (anim->control_info.draw_order == 0)
574             anim->control_info.draw_order = 1000000;
575           if (part->control_info.draw_order == 0)
576             part->control_info.draw_order = 1000000;
577
578           // force animation to pass-through clicks (must set part control)
579           if (part->control_info.style == STYLE_DEFAULT)
580             part->control_info.style |= STYLE_PASSTHROUGH;
581         }
582       }
583
584       if (anim->num_parts > 0 || anim->has_base)
585       {
586         ctrl->num_anims++;
587         anim_nr++;
588       }
589     }
590   }
591
592   InitToonControls();
593
594   // sort all animations according to draw_order and animation number
595   for (m = 0; m < NUM_GAME_MODES; m++)
596   {
597     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
598
599     // sort all main animations for this game mode
600     qsort(ctrl->anim, ctrl->num_anims,
601           sizeof(struct GlobalAnimMainControlInfo),
602           compareGlobalAnimMainControlInfo);
603
604     for (a = 0; a < ctrl->num_anims; a++)
605     {
606       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
607
608       // sort all animation parts for this main animation
609       qsort(anim->part, anim->num_parts,
610             sizeof(struct GlobalAnimPartControlInfo),
611             compareGlobalAnimPartControlInfo);
612     }
613   }
614
615   for (i = 0; i < NUM_GAME_MODES; i++)
616     game_mode_anim_classes[i] = ANIM_CLASS_NONE;
617   for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
618     game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
619       game_mode_anim_classes_list[i].class;
620
621   for (i = 0; i < NUM_ANIM_CLASSES; i++)
622     anim_class_game_modes[i] = GAME_MODE_DEFAULT;
623   for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
624     anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
625       anim_class_game_modes_list[i].game_mode;
626
627   anim_status_last_before_fading = GAME_MODE_LOADING;
628   anim_status_last = GAME_MODE_LOADING;
629   anim_classes_last = ANIM_CLASS_NONE;
630 }
631
632 void InitGlobalAnimations(void)
633 {
634   InitGlobalAnimControls();
635 }
636
637 static void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
638 {
639   Bitmap *fade_bitmap =
640     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
641      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
642   int game_mode_anim_action[NUM_GAME_MODES];
643   int mode_nr;
644
645   if (!setup.toons)
646     return;
647
648   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
649       drawing_target == DRAW_TO_SCREEN)
650     DoAnimationExt();
651
652   // always start with reliable default values (no animation actions)
653   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
654     game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
655
656   if (global.anim_status != anim_status_last)
657   {
658     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
659     boolean after_fading  = (anim_status_last   == GAME_MODE_PSEUDO_FADING);
660     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
661     int i;
662
663     if (drawing_target == DRAW_TO_FADE_TARGET)
664       after_fading = TRUE;
665
666     // special case: changing from/to these screens is done without fading
667     if (global.anim_status == GAME_MODE_PSEUDO_TYPENAME  ||
668         global.anim_status == GAME_MODE_PSEUDO_TYPENAMES ||
669         anim_status_last   == GAME_MODE_PSEUDO_TYPENAME  ||
670         anim_status_last   == GAME_MODE_PSEUDO_TYPENAMES)
671       after_fading = TRUE;
672
673     // ---------- part 1 ------------------------------------------------------
674     // start or stop global animations by change of game mode
675     // (special handling of animations for "current screen" and "all screens")
676
677     if (global.anim_status_next != anim_status_last_before_fading)
678     {
679       // stop animations for last screen before fading to new screen
680       game_mode_anim_action[anim_status_last] = ANIM_STOP;
681
682       // start animations for current screen after fading to new screen
683       game_mode_anim_action[global.anim_status] = ANIM_START;
684     }
685
686     // start animations for all screens after loading new artwork set
687     if (anim_status_last == GAME_MODE_LOADING)
688       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
689
690     // ---------- part 2 ------------------------------------------------------
691     // start or stop global animations by change of animation class
692     // (generic handling of animations for "class of screens")
693
694     for (i = 0; i < NUM_ANIM_CLASSES; i++)
695     {
696       int anim_class_check = (1 << i);
697       int anim_class_game_mode = anim_class_game_modes[i];
698       int anim_class_last = anim_classes_last & anim_class_check;
699       int anim_class_next = anim_classes_next & anim_class_check;
700
701       // stop animations for changed screen class before fading to new screen
702       if (before_fading && anim_class_last && !anim_class_next)
703         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
704
705       // start animations for changed screen class after fading to new screen
706       if (after_fading && !anim_class_last && anim_class_next)
707         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
708     }
709
710     if (drawing_target == DRAW_TO_SCREEN)
711     {
712       if (after_fading)
713       {
714         anim_classes_last = anim_classes_next;
715         anim_status_last_before_fading = global.anim_status;
716       }
717
718       anim_status_last = global.anim_status;
719
720       // start or stop animations determined to be started or stopped above
721       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
722         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
723           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
724     }
725     else if (drawing_target == DRAW_TO_FADE_TARGET)
726     {
727       drawing_to_fading_buffer = TRUE;
728
729       // start animations determined to be (temporary) started above
730       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
731         if (game_mode_anim_action[mode_nr] == ANIM_START)
732           HandleGlobalAnim(ANIM_START, mode_nr);
733     }
734   }
735
736   if (global.anim_status == GAME_MODE_LOADING)
737     return;
738
739   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
740   {
741     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
742     int anim_nr;
743
744     // when preparing source fading buffer, only draw animations to be stopped
745     if (drawing_target == DRAW_TO_FADE_SOURCE &&
746         game_mode_anim_action[mode_nr] != ANIM_STOP)
747       continue;
748
749     // when preparing target fading buffer, only draw animations to be started
750     if (drawing_target == DRAW_TO_FADE_TARGET &&
751         game_mode_anim_action[mode_nr] != ANIM_START)
752       continue;
753
754 #if 0
755     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
756         mode_nr != game_status)
757       continue;
758 #endif
759
760     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
761     {
762       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
763       struct GraphicInfo *c = &anim->control_info;
764       int part_first, part_last;
765       int part_nr;
766
767       if (!(anim->state & ANIM_STATE_RUNNING))
768         continue;
769
770       part_first = part_last = anim->active_part_nr;
771
772       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
773       {
774         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
775
776         part_first = 0;
777         part_last = num_parts - 1;
778       }
779
780       for (part_nr = part_first; part_nr <= part_last; part_nr++)
781       {
782         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
783         struct GraphicInfo *g = &part->graphic_info;
784         Bitmap *src_bitmap;
785         int src_x, src_y;
786         int width  = g->width;
787         int height = g->height;
788         int dst_x = part->x;
789         int dst_y = part->y;
790         int cut_x = 0;
791         int cut_y = 0;
792         int sync_frame;
793         int frame;
794         void (*blit_bitmap)(Bitmap *, Bitmap *, int, int, int, int, int, int) =
795           (g->draw_masked ? BlitBitmapMasked : BlitBitmap);
796         void (*blit_screen)(Bitmap *, int, int, int, int, int, int) =
797           (g->draw_masked ? BlitToScreenMasked : BlitToScreen);
798         int last_anim_random_frame = gfx.anim_random_frame;
799
800         if (!(part->state & ANIM_STATE_RUNNING))
801           continue;
802
803         if (part->drawing_stage != drawing_stage)
804           continue;
805
806         if (part->x < 0)
807         {
808           dst_x = 0;
809           width += part->x;
810           cut_x = -part->x;
811         }
812         else if (part->x > part->viewport_width - g->width)
813           width -= (part->x - (part->viewport_width - g->width));
814
815         if (part->y < 0)
816         {
817           dst_y = 0;
818           height += part->y;
819           cut_y = -part->y;
820         }
821         else if (part->y > part->viewport_height - g->height)
822           height -= (part->y - (part->viewport_height - g->height));
823
824         if (width <= 0 || height <= 0)
825           continue;
826
827         dst_x += part->viewport_x;
828         dst_y += part->viewport_y;
829
830         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
831
832         // re-initialize random animation frame after animation delay
833         if (g->anim_mode == ANIM_RANDOM &&
834             sync_frame % g->anim_delay == 0 &&
835             sync_frame > 0)
836           part->anim_random_frame = GetSimpleRandom(g->anim_frames);
837
838         gfx.anim_random_frame = part->anim_random_frame;
839
840         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
841                                   g->anim_mode, g->anim_start_frame,
842                                   sync_frame);
843
844         gfx.anim_random_frame = last_anim_random_frame;
845
846         getGlobalAnimGraphicSource(part->graphic, frame, &src_bitmap,
847                                    &src_x, &src_y);
848
849         src_x += cut_x;
850         src_y += cut_y;
851
852         if (drawing_target == DRAW_TO_SCREEN)
853           blit_screen(src_bitmap, src_x, src_y, width, height,
854                       dst_x, dst_y);
855         else
856           blit_bitmap(src_bitmap, fade_bitmap, src_x, src_y, width, height,
857                       dst_x, dst_y);
858       }
859     }
860   }
861
862   if (drawing_target == DRAW_TO_FADE_TARGET)
863   {
864     // stop animations determined to be (temporary) started above
865     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
866       if (game_mode_anim_action[mode_nr] == ANIM_START)
867         HandleGlobalAnim(ANIM_STOP, mode_nr);
868
869     drawing_to_fading_buffer = FALSE;
870   }
871 }
872
873 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
874 {
875   int last_cursor_mode_override = gfx.cursor_mode_override;
876
877   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
878   {
879     ResetGlobalAnim_Clickable();
880
881     gfx.cursor_mode_override = CURSOR_UNDEFINED;
882   }
883
884   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
885
886   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
887   {
888     ResetGlobalAnim_Clicked();
889   }
890
891   DrawEnvelopeRequestToScreen(drawing_target, drawing_stage);
892
893   if (gfx.cursor_mode_override != last_cursor_mode_override)
894     SetMouseCursor(gfx.cursor_mode);
895 }
896
897 static boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
898 {
899   int viewport_x;
900   int viewport_y;
901   int viewport_width;
902   int viewport_height;
903   boolean changed = FALSE;
904
905   if (part->last_anim_status == global.anim_status &&
906       part->control_info.class != get_hash_from_key("pointer"))
907     return FALSE;
908
909   part->last_anim_status = global.anim_status;
910
911   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
912
913   if (part->control_info.class == get_hash_from_key("window") ||
914       part->control_info.class == get_hash_from_key("border"))
915   {
916     viewport_x = 0;
917     viewport_y = 0;
918     viewport_width  = WIN_XSIZE;
919     viewport_height = WIN_YSIZE;
920
921     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
922   }
923   else if (part->control_info.class == get_hash_from_key("pointer"))
924   {
925     int mx = MIN(MAX(0, gfx.mouse_x), WIN_XSIZE - 1);
926     int my = MIN(MAX(0, gfx.mouse_y), WIN_YSIZE - 1);
927
928     // prevent displaying off-screen custom mouse cursor in upper left corner
929     if (gfx.mouse_x == POS_OFFSCREEN &&
930         gfx.mouse_y == POS_OFFSCREEN)
931       mx = my = POS_OFFSCREEN;
932
933     viewport_x = mx - part->control_info.x;
934     viewport_y = my - part->control_info.y;
935     viewport_width  = part->graphic_info.width;
936     viewport_height = part->graphic_info.height;
937
938     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
939
940     // do not use global animation mouse pointer when reloading artwork
941     if (global.anim_status != GAME_MODE_LOADING)
942       gfx.cursor_mode_override = CURSOR_NONE;
943   }
944   else if (part->control_info.class == get_hash_from_key("door_1"))
945   {
946     viewport_x = DX;
947     viewport_y = DY;
948     viewport_width  = DXSIZE;
949     viewport_height = DYSIZE;
950   }
951   else if (part->control_info.class == get_hash_from_key("door_2"))
952   {
953     if (part->mode_nr == GAME_MODE_EDITOR)
954     {
955       viewport_x = EX;
956       viewport_y = EY;
957       viewport_width  = EXSIZE;
958       viewport_height = EYSIZE;
959     }
960     else
961     {
962       viewport_x = VX;
963       viewport_y = VY;
964       viewport_width  = VXSIZE;
965       viewport_height = VYSIZE;
966     }
967   }
968   else          // default: "playfield"
969   {
970     viewport_x = REAL_SX;
971     viewport_y = REAL_SY;
972     viewport_width  = FULL_SXSIZE;
973     viewport_height = FULL_SYSIZE;
974   }
975
976   if (viewport_x != part->viewport_x ||
977       viewport_y != part->viewport_y ||
978       viewport_width  != part->viewport_width ||
979       viewport_height != part->viewport_height)
980   {
981     part->viewport_x = viewport_x;
982     part->viewport_y = viewport_y;
983     part->viewport_width  = viewport_width;
984     part->viewport_height = viewport_height;
985
986     if (part->control_info.class != get_hash_from_key("pointer"))
987       changed = TRUE;
988   }
989
990   return changed;
991 }
992
993 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
994 {
995   int sound = part->sound;
996
997   if (sound == SND_UNDEFINED)
998     return;
999
1000   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
1001       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
1002     return;
1003
1004   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
1005   if (IS_LOOP_SOUND(sound))
1006     PlaySoundLoop(sound);
1007   else
1008     PlaySound(sound);
1009
1010 #if 0
1011   Debug("anim:PlayGlobalAnimSound", "PLAY SOUND %d.%d.%d: %d",
1012          part->anim_nr, part->nr, part->mode_nr, sound);
1013 #endif
1014 }
1015
1016 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
1017 {
1018   int sound = part->sound;
1019
1020   if (sound == SND_UNDEFINED)
1021     return;
1022
1023   StopSound(sound);
1024
1025 #if 0
1026   Debug("anim:StopGlobalAnimSound", "STOP SOUND %d.%d.%d: %d",
1027         part->anim_nr, part->nr, part->mode_nr, sound);
1028 #endif
1029 }
1030
1031 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1032 {
1033   int music = part->music;
1034
1035   if (music == MUS_UNDEFINED)
1036     return;
1037
1038   if (!setup.sound_music)
1039     return;
1040
1041   if (IS_LOOP_MUSIC(music))
1042     PlayMusicLoop(music);
1043   else
1044     PlayMusic(music);
1045
1046 #if 0
1047   Debug("anim:PlayGlobalAnimMusic", "PLAY MUSIC %d.%d.%d: %d",
1048         part->anim_nr, part->nr, part->mode_nr, music);
1049 #endif
1050 }
1051
1052 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
1053 {
1054   int music = part->music;
1055
1056   if (music == MUS_UNDEFINED)
1057     return;
1058
1059   StopMusic();
1060
1061 #if 0
1062   Debug("anim:StopGlobalAnimMusic", "STOP MUSIC %d.%d.%d: %d",
1063         part->anim_nr, part->nr, part->mode_nr, music);
1064 #endif
1065 }
1066
1067 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1068 {
1069   // when drawing animations to fading buffer, do not play sounds or music
1070   if (drawing_to_fading_buffer)
1071     return;
1072
1073   PlayGlobalAnimSound(part);
1074   PlayGlobalAnimMusic(part);
1075 }
1076
1077 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
1078 {
1079   StopGlobalAnimSound(part);
1080   StopGlobalAnimMusic(part);
1081 }
1082
1083 static void PlayGlobalAnimSoundIfLoop(struct GlobalAnimPartControlInfo *part)
1084 {
1085   // when drawing animations to fading buffer, do not play sounds
1086   if (drawing_to_fading_buffer)
1087     return;
1088
1089   // loop sounds only expire when playing
1090   if (game_status != GAME_MODE_PLAYING)
1091     return;
1092
1093   // check if any sound is defined for this animation part
1094   if (part->sound == SND_UNDEFINED)
1095     return;
1096
1097   // normal (non-loop) sounds do not expire when playing
1098   if (!IS_LOOP_SOUND(part->sound))
1099     return;
1100
1101   // prevent expiring loop sounds when playing
1102   PlayGlobalAnimSound(part);
1103 }
1104
1105 static boolean checkGlobalAnimEvent(int anim_event, int mask)
1106 {
1107   int mask_anim_only = mask & ~ANIM_EVENT_PART_MASK;
1108
1109   if (mask & ANIM_EVENT_ANY)
1110     return (anim_event & ANIM_EVENT_ANY);
1111   else if (mask & ANIM_EVENT_SELF)
1112     return (anim_event & ANIM_EVENT_SELF);
1113   else if (mask & ANIM_EVENT_UNCLICK_ANY)
1114     return (anim_event & ANIM_EVENT_UNCLICK_ANY);
1115   else
1116     return (anim_event == mask ||
1117             anim_event == mask_anim_only);
1118 }
1119
1120 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part, int mask)
1121 {
1122   struct GraphicInfo *c = &part->control_info;
1123   int num_init_events = GetGlobalAnimEventValueCount(c->init_event);
1124   int num_anim_events = GetGlobalAnimEventValueCount(c->anim_event);
1125   int i;
1126
1127   for (i = 0; i < num_init_events; i++)
1128   {
1129     int init_event = GetGlobalAnimEventValue(c->init_event, i);
1130
1131     if (checkGlobalAnimEvent(init_event, mask))
1132       return TRUE;
1133   }
1134
1135   for (i = 0; i < num_anim_events; i++)
1136   {
1137     int anim_event = GetGlobalAnimEventValue(c->anim_event, i);
1138
1139     if (checkGlobalAnimEvent(anim_event, mask))
1140       return TRUE;
1141   }
1142
1143   return FALSE;
1144 }
1145
1146 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
1147                              int mx, int my, boolean clicked)
1148 {
1149   struct GraphicInfo *g = &part->graphic_info;
1150   int part_x = part->viewport_x + part->x;
1151   int part_y = part->viewport_y + part->y;
1152   int part_width  = g->width;
1153   int part_height = g->height;
1154
1155   // check if mouse click was detected at all
1156   if (!clicked)
1157     return FALSE;
1158
1159   // check if mouse click is inside the animation part's viewport
1160   if (mx <  part->viewport_x ||
1161       mx >= part->viewport_x + part->viewport_width ||
1162       my <  part->viewport_y ||
1163       my >= part->viewport_y + part->viewport_height)
1164     return FALSE;
1165
1166   // check if mouse click is inside the animation part's graphic
1167   if (mx <  part_x ||
1168       mx >= part_x + part_width ||
1169       my <  part_y ||
1170       my >= part_y + part_height)
1171     return FALSE;
1172
1173   return TRUE;
1174 }
1175
1176 static boolean clickBlocked(struct GlobalAnimPartControlInfo *part)
1177 {
1178   return (part->control_info.style & STYLE_BLOCK ? TRUE : FALSE);
1179 }
1180
1181 static boolean clickConsumed(struct GlobalAnimPartControlInfo *part)
1182 {
1183   return (part->control_info.style & STYLE_PASSTHROUGH ? FALSE : TRUE);
1184 }
1185
1186 static void InitGlobalAnim_Triggered(struct GlobalAnimPartControlInfo *part,
1187                                      boolean *click_consumed,
1188                                      boolean *any_event_action,
1189                                      int event_value, char *info_text)
1190 {
1191   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1192
1193   int gic_anim_nr = part->old_anim_nr + 1;      // X as in "anim_X"
1194   int gic_part_nr = part->old_nr + 1;           // Y as in "part_Y"
1195   int mask = event_value | (gic_anim_nr << ANIM_EVENT_ANIM_BIT);
1196
1197   if (!part->is_base)
1198     mask |= gic_part_nr << ANIM_EVENT_PART_BIT;
1199
1200   int anim2_nr;
1201
1202   for (anim2_nr = 0; anim2_nr < ctrl->num_anims; anim2_nr++)
1203   {
1204     struct GlobalAnimMainControlInfo *anim2 = &ctrl->anim[anim2_nr];
1205     int part2_nr;
1206
1207     for (part2_nr = 0; part2_nr < anim2->num_parts_all; part2_nr++)
1208     {
1209       struct GlobalAnimPartControlInfo *part2 = &anim2->part[part2_nr];
1210
1211       if (!(part2->state & ANIM_STATE_RUNNING))
1212         continue;
1213
1214       if (isClickablePart(part2, mask))
1215       {
1216         part2->triggered = TRUE;
1217         *click_consumed |= clickConsumed(part);         // click was on "part"!
1218
1219 #if DEBUG_ANIM_EVENTS
1220         Debug("anim:InitGlobalAnim_Triggered",
1221               "%d.%d TRIGGERED BY %s OF %d.%d",
1222               part2->old_anim_nr + 1, part2->old_nr + 1, info_text,
1223               part->old_anim_nr + 1, part->old_nr + 1);
1224 #endif
1225 #if 0
1226         Debug("anim:InitGlobalAnim_Triggered",
1227               "%d.%d TRIGGER CLICKED [%d]", anim2_nr, part2_nr,
1228               part2->control_info.anim_event_action);
1229 #endif
1230
1231         // after executing event action, ignore any further actions
1232         if (!*any_event_action && DoGlobalAnim_EventAction(part2))
1233           *any_event_action = TRUE;
1234       }
1235
1236 #if 0
1237       struct GraphicInfo *c = &part2->control_info;
1238
1239       if (isClickablePart(part2, mask))
1240         Debug("anim:InitGlobalAnim_Triggered",
1241               "%d.%d: 0x%08x, 0x%08x [0x%08x] <--- TRIGGERED BY %d.%d",
1242               anim2_nr, part2_nr, c->init_event, c->anim_event, mask,
1243               anim_nr, part_nr);
1244       else
1245         Debug("anim:InitGlobalAnim_Triggered",
1246               "%d.%d: 0x%08x, 0x%08x [0x%08x]",
1247               anim2_nr, part2_nr, c->init_event, c->anim_event, mask);
1248 #endif
1249     }
1250   }
1251 }
1252
1253 static void HandleGlobalAnimDelay(struct GlobalAnimPartControlInfo *part,
1254                                   int delay_type, char *info_text)
1255 {
1256 #if DEBUG_ANIM_DELAY
1257   Debug("anim:HandleGlobalAnimDelay", "%d.%d %s",
1258         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1259 #endif
1260
1261   DoGlobalAnim_DelayAction(part, delay_type);
1262 }
1263
1264 static void HandleGlobalAnimEvent(struct GlobalAnimPartControlInfo *part,
1265                                   int event_value, char *info_text)
1266 {
1267 #if DEBUG_ANIM_EVENTS
1268   Debug("anim:HandleGlobalAnimEvent", "%d.%d %s",
1269         part->old_anim_nr + 1, part->old_nr + 1, info_text);
1270 #endif
1271
1272   boolean click_consumed = FALSE;
1273   boolean any_event_action = FALSE;
1274
1275   // check if this event is defined to trigger other animations
1276   InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1277                            event_value, info_text);
1278 }
1279
1280 static int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
1281                                  int state)
1282 {
1283   if (handle_click && !part->clicked)
1284     return state;
1285
1286   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[part->mode_nr];
1287   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[part->anim_nr];
1288   struct GraphicInfo *g = &part->graphic_info;
1289   struct GraphicInfo *c = &part->control_info;
1290   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
1291
1292   if (viewport_changed)
1293     state |= ANIM_STATE_RESTART;
1294
1295   if (state & ANIM_STATE_RESTART)
1296   {
1297     // when drawing animations to fading buffer, only start fixed animations
1298     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
1299                                      c->y == ARG_UNDEFINED_VALUE))
1300       return ANIM_STATE_INACTIVE;
1301
1302     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
1303
1304     part->init_delay_counter =
1305       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
1306
1307     part->anim_delay_counter =
1308       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
1309
1310     part->post_delay_counter = 0;
1311
1312     part->init_event_state = (c->init_event != ANIM_EVENT_UNDEFINED);
1313     part->anim_event_state = (c->anim_event != ANIM_EVENT_UNDEFINED);
1314
1315     part->initial_anim_sync_frame =
1316       (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
1317
1318     // do not re-initialize random animation frame after fade-in
1319     if (part->anim_random_frame == -1)
1320       part->anim_random_frame = GetSimpleRandom(g->anim_frames);
1321
1322     if (c->direction & MV_HORIZONTAL)
1323     {
1324       int pos_bottom = part->viewport_height - g->height;
1325
1326       if (c->position == POS_TOP)
1327         part->y = 0;
1328       else if (c->position == POS_UPPER)
1329         part->y = GetSimpleRandom(pos_bottom / 2);
1330       else if (c->position == POS_MIDDLE)
1331         part->y = pos_bottom / 2;
1332       else if (c->position == POS_LOWER)
1333         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
1334       else if (c->position == POS_BOTTOM)
1335         part->y = pos_bottom;
1336       else
1337         part->y = GetSimpleRandom(pos_bottom);
1338
1339       if (c->direction == MV_RIGHT)
1340       {
1341         part->step_xoffset = c->step_offset;
1342         part->x = -g->width + part->step_xoffset;
1343       }
1344       else
1345       {
1346         part->step_xoffset = -c->step_offset;
1347         part->x = part->viewport_width + part->step_xoffset;
1348       }
1349
1350       part->step_yoffset = 0;
1351     }
1352     else if (c->direction & MV_VERTICAL)
1353     {
1354       int pos_right = part->viewport_width - g->width;
1355
1356       if (c->position == POS_LEFT)
1357         part->x = 0;
1358       else if (c->position == POS_RIGHT)
1359         part->x = pos_right;
1360       else
1361         part->x = GetSimpleRandom(pos_right);
1362
1363       if (c->direction == MV_DOWN)
1364       {
1365         part->step_yoffset = c->step_offset;
1366         part->y = -g->height + part->step_yoffset;
1367       }
1368       else
1369       {
1370         part->step_yoffset = -c->step_offset;
1371         part->y = part->viewport_height + part->step_yoffset;
1372       }
1373
1374       part->step_xoffset = 0;
1375     }
1376     else
1377     {
1378       part->x = 0;
1379       part->y = 0;
1380
1381       part->step_xoffset = 0;
1382       part->step_yoffset = 0;
1383     }
1384
1385     if (part->control_info.class != get_hash_from_key("pointer"))
1386     {
1387       if (c->x != ARG_UNDEFINED_VALUE)
1388         part->x = c->x;
1389       if (c->y != ARG_UNDEFINED_VALUE)
1390         part->y = c->y;
1391     }
1392
1393     if (c->position == POS_LAST &&
1394         anim->last_x > -g->width  && anim->last_x < part->viewport_width &&
1395         anim->last_y > -g->height && anim->last_y < part->viewport_height)
1396     {
1397       part->x = anim->last_x;
1398       part->y = anim->last_y;
1399     }
1400
1401     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1402       part->step_xoffset = c->step_xoffset;
1403     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1404       part->step_yoffset = c->step_yoffset;
1405
1406     if (part->init_delay_counter == 0 &&
1407         !part->init_event_state)
1408     {
1409       PlayGlobalAnimSoundAndMusic(part);
1410
1411       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1412       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1413     }
1414     else
1415     {
1416       HandleGlobalAnimEvent(part, ANIM_EVENT_INIT, "START [INIT_DELAY/EVENT]");
1417     }
1418   }
1419
1420   if (part->clicked &&
1421       part->init_event_state)
1422   {
1423     if (part->initial_anim_sync_frame > 0)
1424       part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1425
1426     part->init_delay_counter = 1;
1427     part->init_event_state = FALSE;
1428
1429     part->clicked = FALSE;
1430   }
1431
1432   if (part->clicked &&
1433       part->anim_event_state)
1434   {
1435     part->anim_delay_counter = 1;
1436     part->anim_event_state = FALSE;
1437
1438     part->clicked = FALSE;
1439   }
1440
1441   if (part->init_delay_counter > 0)
1442   {
1443     part->init_delay_counter--;
1444
1445     if (part->init_delay_counter == 0)
1446     {
1447       part->init_event_state = FALSE;
1448
1449       PlayGlobalAnimSoundAndMusic(part);
1450
1451       HandleGlobalAnimDelay(part, ANIM_DELAY_INIT,  "START [INIT_DELAY]");
1452       HandleGlobalAnimEvent(part, ANIM_EVENT_START, "START [ANIM]");
1453     }
1454
1455     return ANIM_STATE_WAITING;
1456   }
1457
1458   if (part->init_event_state)
1459     return ANIM_STATE_WAITING;
1460
1461   // animation part is now running/visible and therefore clickable
1462   part->clickable = TRUE;
1463
1464   // check if moving animation has left the visible screen area
1465   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1466       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1467       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1468       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1469   {
1470     // do not wait for "anim" events for off-screen animations
1471     part->anim_event_state = FALSE;
1472
1473     // do not stop animation before "anim" or "post" counter are finished
1474     if (part->anim_delay_counter == 0 &&
1475         part->post_delay_counter == 0)
1476     {
1477       StopGlobalAnimSoundAndMusic(part);
1478
1479       HandleGlobalAnimEvent(part, ANIM_EVENT_END, "END [ANIM/OFF-SCREEN]");
1480
1481       part->post_delay_counter =
1482         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1483
1484       if (part->post_delay_counter > 0)
1485         return ANIM_STATE_RUNNING;
1486
1487       // drawing last frame not needed here -- animation not visible anymore
1488       return ANIM_STATE_RESTART;
1489     }
1490   }
1491
1492   if (part->anim_delay_counter > 0)
1493   {
1494     part->anim_delay_counter--;
1495
1496     if (part->anim_delay_counter == 0)
1497     {
1498       part->anim_event_state = FALSE;
1499
1500       StopGlobalAnimSoundAndMusic(part);
1501
1502       HandleGlobalAnimDelay(part, ANIM_DELAY_ANIM, "END [ANIM_DELAY]");
1503       HandleGlobalAnimEvent(part, ANIM_EVENT_END,  "END [ANIM_DELAY/EVENT]");
1504
1505       part->post_delay_counter =
1506         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1507
1508       if (part->post_delay_counter > 0)
1509         return ANIM_STATE_RUNNING;
1510
1511       // additional state "RUNNING" required to not skip drawing last frame
1512       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1513     }
1514   }
1515
1516   if (part->post_delay_counter > 0)
1517   {
1518     part->post_delay_counter--;
1519
1520     if (part->post_delay_counter == 0)
1521     {
1522       HandleGlobalAnimDelay(part, ANIM_DELAY_POST, "END [POST_DELAY]");
1523       HandleGlobalAnimEvent(part, ANIM_EVENT_POST, "END [POST_DELAY]");
1524
1525       return ANIM_STATE_RESTART;
1526     }
1527
1528     return ANIM_STATE_WAITING;
1529   }
1530
1531   // special case to prevent expiring loop sounds when playing
1532   PlayGlobalAnimSoundIfLoop(part);
1533
1534   if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1535                        anim_sync_frame))
1536     return ANIM_STATE_RUNNING;
1537
1538 #if 0
1539   {
1540     static unsigned int last_counter = -1;
1541     unsigned int counter = Counter();
1542
1543     Debug("anim:HandleGlobalAnim_Part", "NEXT ANIM PART [%d, %d]",
1544           anim_sync_frame, counter - last_counter);
1545
1546     last_counter = counter;
1547   }
1548 #endif
1549
1550   part->x += part->step_xoffset;
1551   part->y += part->step_yoffset;
1552
1553   anim->last_x = part->x;
1554   anim->last_y = part->y;
1555
1556   return ANIM_STATE_RUNNING;
1557 }
1558
1559 static void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim,
1560                                   int action)
1561 {
1562   struct GlobalAnimPartControlInfo *part;
1563   struct GraphicInfo *c = &anim->control_info;
1564   int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1565   int state, active_part_nr;
1566   int i;
1567
1568 #if 0
1569   Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1570          anim->mode_nr, anim->nr, anim->num_parts);
1571   Debug("anim:HandleGlobalAnim_Main",
1572         "%d, %d, %d", global.num_toons, setup.toons, num_toons);
1573 #endif
1574
1575 #if 0
1576   Debug("anim:HandleGlobalAnim_Main", "%s(%d): %d, %d, %d [%d]",
1577         (action == ANIM_START ? "ANIM_START" :
1578          action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1579          action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1580         anim->nr,
1581         anim->state & ANIM_STATE_RESTART,
1582         anim->state & ANIM_STATE_WAITING,
1583         anim->state & ANIM_STATE_RUNNING,
1584         anim->num_parts);
1585 #endif
1586
1587   switch (action)
1588   {
1589     case ANIM_START:
1590       anim->state = anim->last_state = ANIM_STATE_RESTART;
1591       anim->active_part_nr = anim->last_active_part_nr = 0;
1592       anim->part_counter = 0;
1593
1594       break;
1595
1596     case ANIM_CONTINUE:
1597       if (anim->state == ANIM_STATE_INACTIVE)
1598         return;
1599
1600       anim->state = anim->last_state;
1601       anim->active_part_nr = anim->last_active_part_nr;
1602
1603       break;
1604
1605     case ANIM_STOP:
1606       anim->state = ANIM_STATE_INACTIVE;
1607
1608       for (i = 0; i < num_parts; i++)
1609         StopGlobalAnimSoundAndMusic(&anim->part[i]);
1610
1611       return;
1612
1613     default:
1614       break;
1615   }
1616
1617   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1618   {
1619 #if 0
1620     Debug("anim:HandleGlobalAnim_Main", "%d, %d => %d",
1621           anim->mode_nr, anim->nr, num_parts);
1622 #endif
1623
1624     for (i = 0; i < num_parts; i++)
1625     {
1626       part = &anim->part[i];
1627
1628       switch (action)
1629       {
1630         case ANIM_START:
1631           anim->state = ANIM_STATE_RUNNING;
1632           part->state = ANIM_STATE_RESTART;
1633
1634           break;
1635
1636         case ANIM_CONTINUE:
1637           if (part->state == ANIM_STATE_INACTIVE)
1638             continue;
1639
1640           break;
1641
1642         case ANIM_STOP:
1643           part->state = ANIM_STATE_INACTIVE;
1644
1645           continue;
1646
1647         default:
1648           break;
1649       }
1650
1651       part->state = HandleGlobalAnim_Part(part, part->state);
1652
1653       // when animation mode is "once", stop after animation was played once
1654       if (c->anim_mode & ANIM_ONCE &&
1655           part->state & ANIM_STATE_RESTART)
1656         part->state = ANIM_STATE_INACTIVE;
1657     }
1658
1659     anim->last_state = anim->state;
1660     anim->last_active_part_nr = anim->active_part_nr;
1661
1662     return;
1663   }
1664
1665   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1666     anim->active_part_nr = getGlobalAnimationPart(anim);
1667
1668   part = &anim->part[anim->active_part_nr];
1669
1670   // first set all animation parts to "inactive", ...
1671   for (i = 0; i < num_parts; i++)
1672     anim->part[i].state = ANIM_STATE_INACTIVE;
1673
1674   // ... then set current animation parts to "running"
1675   part->state = ANIM_STATE_RUNNING;
1676
1677   anim->state = HandleGlobalAnim_Part(part, anim->state);
1678
1679   if (anim->state & ANIM_STATE_RESTART)
1680     anim->part_counter++;
1681
1682   // when animation mode is "once", stop after all animations were played once
1683   if (c->anim_mode & ANIM_ONCE &&
1684       anim->part_counter == anim->num_parts)
1685     anim->state = ANIM_STATE_INACTIVE;
1686
1687   state = anim->state;
1688   active_part_nr = anim->active_part_nr;
1689
1690   // while the animation parts are pausing (waiting or inactive), play the base
1691   // (main) animation; this corresponds to the "boring player animation" logic
1692   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1693   if (anim->has_base)
1694   {
1695     if (anim->state == ANIM_STATE_WAITING ||
1696         anim->state == ANIM_STATE_INACTIVE)
1697     {
1698       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1699       part = &anim->part[anim->active_part_nr];
1700
1701       if (anim->state != anim->last_state)
1702         part->state = ANIM_STATE_RESTART;
1703
1704       anim->state = ANIM_STATE_RUNNING;
1705       part->state = HandleGlobalAnim_Part(part, part->state);
1706     }
1707   }
1708
1709   anim->last_state = state;
1710   anim->last_active_part_nr = active_part_nr;
1711 }
1712
1713 static void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1714 {
1715   int i;
1716
1717 #if 0
1718   Debug("anim:HandleGlobalAnim_Mode", "%d => %d", ctrl->nr, ctrl->num_anims);
1719 #endif
1720
1721   for (i = 0; i < ctrl->num_anims; i++)
1722     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1723 }
1724
1725 static void HandleGlobalAnim(int action, int game_mode)
1726 {
1727 #if 0
1728   Debug("anim:HandleGlobalAnim", "mode == %d", game_status);
1729 #endif
1730
1731   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1732 }
1733
1734 static void DoAnimationExt(void)
1735 {
1736   int i;
1737
1738 #if 0
1739   Debug("anim:DoAnimationExt", "%d, %d", anim_sync_frame, Counter());
1740 #endif
1741
1742   // global animations now synchronized with frame delay of screen update
1743   anim_sync_frame++;
1744
1745   for (i = 0; i < NUM_GAME_MODES; i++)
1746     HandleGlobalAnim(ANIM_CONTINUE, i);
1747
1748 #if 0
1749   // force screen redraw in next frame to continue drawing global animations
1750   redraw_mask = REDRAW_ALL;
1751 #endif
1752 }
1753
1754 static void DoGlobalAnim_DelayAction(struct GlobalAnimPartControlInfo *part,
1755                                      int delay_type)
1756 {
1757   int delay_action =
1758     (delay_type == ANIM_DELAY_INIT ? part->control_info.init_delay_action :
1759      delay_type == ANIM_DELAY_ANIM ? part->control_info.anim_delay_action :
1760      delay_type == ANIM_DELAY_POST ? part->control_info.post_delay_action :
1761      ANIM_DELAY_ACTION_NONE);
1762
1763   if (delay_action == ANIM_DELAY_ACTION_NONE)
1764     return;
1765
1766   PushUserEvent(USEREVENT_ANIM_DELAY_ACTION, delay_action, 0);
1767 }
1768
1769 static boolean DoGlobalAnim_EventAction(struct GlobalAnimPartControlInfo *part)
1770 {
1771   int event_action = (part->init_event_state ?
1772                       part->control_info.init_event_action :
1773                       part->control_info.anim_event_action);
1774
1775   if (event_action == ANIM_EVENT_ACTION_NONE)
1776     return FALSE;
1777
1778   if (event_action < MAX_IMAGE_FILES)
1779     PushUserEvent(USEREVENT_ANIM_EVENT_ACTION, event_action, 0);
1780   else
1781     OpenURLFromHash(anim_url_hash, event_action);
1782
1783   // check if further actions are allowed to be executed
1784   if (part->control_info.style & STYLE_MULTIPLE_ACTIONS)
1785     return FALSE;
1786
1787   return TRUE;
1788 }
1789
1790 static void InitGlobalAnim_Clickable(void)
1791 {
1792   int mode_nr;
1793
1794   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1795   {
1796     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1797     int anim_nr;
1798
1799     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
1800     {
1801       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1802       int part_nr;
1803
1804       for (part_nr = 0; part_nr < anim->num_parts_all; part_nr++)
1805       {
1806         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1807
1808         if (part->triggered)
1809           part->clicked = TRUE;
1810
1811         part->triggered = FALSE;
1812         part->clickable = FALSE;
1813       }
1814     }
1815   }
1816 }
1817
1818 #define ANIM_CLICKED_RESET      0
1819 #define ANIM_CLICKED_PRESSED    1
1820 #define ANIM_CLICKED_RELEASED   2
1821
1822 static boolean InitGlobalAnim_Clicked(int mx, int my, int clicked_event)
1823 {
1824   boolean click_consumed = FALSE;
1825   boolean anything_clicked = FALSE;
1826   boolean any_part_clicked = FALSE;
1827   boolean any_event_action = FALSE;
1828   int mode_nr;
1829   int i;
1830
1831   // check game modes in reverse draw order (to stop when clicked)
1832   for (mode_nr = NUM_GAME_MODES - 1; mode_nr >= 0; mode_nr--)
1833   {
1834     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1835     int anim_nr;
1836
1837     // check animations in reverse draw order (to stop when clicked)
1838     for (anim_nr = ctrl->num_anims - 1; anim_nr >= 0; anim_nr--)
1839     {
1840       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1841       int part_nr;
1842
1843       // check animation parts in reverse draw order (to stop when clicked)
1844       for (part_nr = anim->num_parts_all - 1; part_nr >= 0; part_nr--)
1845       {
1846         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1847
1848         if (clicked_event == ANIM_CLICKED_RESET)
1849         {
1850           part->clicked = FALSE;
1851
1852           continue;
1853         }
1854
1855         if (!part->clickable)
1856           continue;
1857
1858         if (!(part->state & ANIM_STATE_RUNNING))
1859           continue;
1860
1861         // always handle "any" click events (clicking anywhere on screen) ...
1862         if (clicked_event == ANIM_CLICKED_PRESSED &&
1863             isClickablePart(part, ANIM_EVENT_ANY))
1864         {
1865 #if DEBUG_ANIM_EVENTS
1866           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY ANY",
1867                 part->old_anim_nr + 1, part->old_nr + 1);
1868 #endif
1869
1870           anything_clicked = part->clicked = TRUE;
1871           click_consumed |= clickConsumed(part);
1872         }
1873
1874         // always handle "unclick:any" events (releasing anywhere on screen) ...
1875         if (clicked_event == ANIM_CLICKED_RELEASED &&
1876             isClickablePart(part, ANIM_EVENT_UNCLICK_ANY))
1877         {
1878 #if DEBUG_ANIM_EVENTS
1879           Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY UNCLICK:ANY",
1880                 part->old_anim_nr + 1, part->old_nr + 1);
1881 #endif
1882
1883           anything_clicked = part->clicked = TRUE;
1884           click_consumed |= clickConsumed(part);
1885         }
1886
1887         // ... but only handle the first (topmost) clickable animation
1888         if (any_part_clicked)
1889           continue;
1890
1891         if (clicked_event == ANIM_CLICKED_PRESSED &&
1892             isClickedPart(part, mx, my, TRUE))
1893         {
1894 #if 0
1895           Debug("anim:InitGlobalAnim_Clicked", "%d.%d CLICKED [%d]",
1896                 anim_nr, part_nr, part->control_info.anim_event_action);
1897 #endif
1898
1899           // after executing event action, ignore any further actions
1900           if (!any_event_action && DoGlobalAnim_EventAction(part))
1901             any_event_action = TRUE;
1902
1903           // determine if mouse clicks should be blocked from other animations
1904           any_part_clicked |= clickConsumed(part);
1905
1906           if (isClickablePart(part, ANIM_EVENT_SELF))
1907           {
1908 #if DEBUG_ANIM_EVENTS
1909             Debug("anim:InitGlobalAnim_Clicked", "%d.%d TRIGGERED BY SELF",
1910                   part->old_anim_nr + 1, part->old_nr + 1);
1911 #endif
1912
1913             anything_clicked = part->clicked = TRUE;
1914             click_consumed |= clickConsumed(part);
1915           }
1916
1917           // determine if mouse clicks should be blocked by this animation
1918           click_consumed |= clickBlocked(part);
1919
1920           // check if this click is defined to trigger other animations
1921           InitGlobalAnim_Triggered(part, &click_consumed, &any_event_action,
1922                                    ANIM_EVENT_CLICK, "CLICK");
1923         }
1924       }
1925     }
1926   }
1927
1928   if (anything_clicked)
1929   {
1930     handle_click = TRUE;
1931
1932     for (i = 0; i < NUM_GAME_MODES; i++)
1933       HandleGlobalAnim(ANIM_CONTINUE, i);
1934
1935     handle_click = FALSE;
1936
1937     // prevent ignoring release event if processed within same game frame
1938     StopProcessingEvents();
1939   }
1940
1941   return (click_consumed || any_event_action);
1942 }
1943
1944 static void ResetGlobalAnim_Clickable(void)
1945 {
1946   InitGlobalAnim_Clickable();
1947 }
1948
1949 static void ResetGlobalAnim_Clicked(void)
1950 {
1951   InitGlobalAnim_Clicked(-1, -1, ANIM_CLICKED_RESET);
1952 }
1953
1954 boolean HandleGlobalAnimClicks(int mx, int my, int button, boolean force_click)
1955 {
1956   static boolean click_consumed = FALSE;
1957   static int last_button = 0;
1958   boolean press_event;
1959   boolean release_event;
1960   boolean click_consumed_current = click_consumed;
1961
1962   if (button != 0 && force_click)
1963     last_button = 0;
1964
1965   // check if button state has changed since last invocation
1966   press_event   = (button != 0 && last_button == 0);
1967   release_event = (button == 0 && last_button != 0);
1968   last_button = button;
1969
1970   if (press_event)
1971   {
1972     click_consumed = InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_PRESSED);
1973     click_consumed_current = click_consumed;
1974   }
1975
1976   if (release_event)
1977   {
1978     InitGlobalAnim_Clicked(mx, my, ANIM_CLICKED_RELEASED);
1979     click_consumed = FALSE;
1980   }
1981
1982   return click_consumed_current;
1983 }