changed click events for global animations to ignore 'consumed' clicks
[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 //                  http://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
18
19 /* values for global toon animation definition */
20 #define NUM_GLOBAL_TOON_ANIMS           1
21 #define NUM_GLOBAL_TOON_PARTS           MAX_NUM_TOONS
22
23 /* values for global animation definition (including toons) */
24 #define NUM_GLOBAL_ANIMS_AND_TOONS      (NUM_GLOBAL_ANIMS +             \
25                                          NUM_GLOBAL_TOON_ANIMS)
26 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL,  \
27                                             NUM_GLOBAL_TOON_PARTS)
28
29 #define ANIM_CLASS_BIT_TITLE_INITIAL    0
30 #define ANIM_CLASS_BIT_TITLE            1
31 #define ANIM_CLASS_BIT_MAIN             2
32 #define ANIM_CLASS_BIT_SUBMENU          3
33 #define ANIM_CLASS_BIT_MENU             4
34 #define ANIM_CLASS_BIT_TOONS            5
35
36 #define NUM_ANIM_CLASSES                6
37
38 #define ANIM_CLASS_NONE                 0
39 #define ANIM_CLASS_TITLE_INITIAL        (1 << ANIM_CLASS_BIT_TITLE_INITIAL)
40 #define ANIM_CLASS_TITLE                (1 << ANIM_CLASS_BIT_TITLE)
41 #define ANIM_CLASS_MAIN                 (1 << ANIM_CLASS_BIT_MAIN)
42 #define ANIM_CLASS_SUBMENU              (1 << ANIM_CLASS_BIT_SUBMENU)
43 #define ANIM_CLASS_MENU                 (1 << ANIM_CLASS_BIT_MENU)
44 #define ANIM_CLASS_TOONS                (1 << ANIM_CLASS_BIT_TOONS)
45
46 #define ANIM_CLASS_TOONS_MENU_MAIN      (ANIM_CLASS_TOONS |     \
47                                          ANIM_CLASS_MENU  |     \
48                                          ANIM_CLASS_MAIN)
49
50 #define ANIM_CLASS_TOONS_MENU_SUBMENU   (ANIM_CLASS_TOONS |     \
51                                          ANIM_CLASS_MENU  |     \
52                                          ANIM_CLASS_SUBMENU)
53
54 /* values for global animation states */
55 #define ANIM_STATE_INACTIVE             0
56 #define ANIM_STATE_RESTART              (1 << 0)
57 #define ANIM_STATE_WAITING              (1 << 1)
58 #define ANIM_STATE_RUNNING              (1 << 2)
59
60 /* values for global animation control */
61 #define ANIM_NO_ACTION                  0
62 #define ANIM_START                      1
63 #define ANIM_CONTINUE                   2
64 #define ANIM_STOP                       3
65
66
67 struct GlobalAnimPartControlInfo
68 {
69   int nr;
70   int anim_nr;
71   int mode_nr;
72
73   int sound;
74   int music;
75   int graphic;
76
77   struct GraphicInfo graphic_info;
78   struct GraphicInfo control_info;
79
80   int viewport_x;
81   int viewport_y;
82   int viewport_width;
83   int viewport_height;
84
85   int x, y;
86   int step_xoffset, step_yoffset;
87
88   unsigned int initial_anim_sync_frame;
89   unsigned int step_delay, step_delay_value;
90
91   int init_delay_counter;
92   int anim_delay_counter;
93   int post_delay_counter;
94
95   boolean init_event_state;
96   boolean anim_event_state;
97
98   boolean clickable;
99   boolean clicked;
100
101   int drawing_stage;
102
103   int state;
104   int last_anim_status;
105 };
106
107 struct GlobalAnimMainControlInfo
108 {
109   struct GlobalAnimPartControlInfo base;
110   struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
111
112   int nr;
113   int mode_nr;
114
115   struct GraphicInfo control_info;
116
117   int num_parts;
118   int part_counter;
119   int active_part_nr;
120
121   boolean has_base;
122
123   int init_delay_counter;
124
125   int state;
126
127   int last_state, last_active_part_nr;
128 };
129
130 struct GlobalAnimControlInfo
131 {
132   struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
133
134   int nr;
135   int num_anims;
136 };
137
138 struct GameModeAnimClass
139 {
140   int game_mode;
141   int class;
142 } game_mode_anim_classes_list[] =
143 {
144   { GAME_MODE_TITLE_INITIAL_1,          ANIM_CLASS_TITLE_INITIAL        },
145   { GAME_MODE_TITLE_INITIAL_2,          ANIM_CLASS_TITLE_INITIAL        },
146   { GAME_MODE_TITLE_INITIAL_3,          ANIM_CLASS_TITLE_INITIAL        },
147   { GAME_MODE_TITLE_INITIAL_4,          ANIM_CLASS_TITLE_INITIAL        },
148   { GAME_MODE_TITLE_INITIAL_5,          ANIM_CLASS_TITLE_INITIAL        },
149   { GAME_MODE_TITLE_1,                  ANIM_CLASS_TITLE                },
150   { GAME_MODE_TITLE_2,                  ANIM_CLASS_TITLE                },
151   { GAME_MODE_TITLE_3,                  ANIM_CLASS_TITLE                },
152   { GAME_MODE_TITLE_4,                  ANIM_CLASS_TITLE                },
153   { GAME_MODE_TITLE_5,                  ANIM_CLASS_TITLE                },
154   { GAME_MODE_LEVELS,                   ANIM_CLASS_TOONS_MENU_SUBMENU   },
155   { GAME_MODE_LEVELNR,                  ANIM_CLASS_TOONS_MENU_SUBMENU   },
156   { GAME_MODE_INFO,                     ANIM_CLASS_TOONS_MENU_SUBMENU   },
157   { GAME_MODE_SETUP,                    ANIM_CLASS_TOONS_MENU_SUBMENU   },
158   { GAME_MODE_PSEUDO_MAINONLY,          ANIM_CLASS_TOONS_MENU_MAIN      },
159   { GAME_MODE_PSEUDO_TYPENAME,          ANIM_CLASS_TOONS_MENU_MAIN      },
160   { GAME_MODE_SCORES,                   ANIM_CLASS_TOONS                },
161
162   { -1,                                 -1                              }
163 };
164
165 struct AnimClassGameMode
166 {
167   int class_bit;
168   int game_mode;
169 } anim_class_game_modes_list[] =
170 {
171   { ANIM_CLASS_BIT_TITLE_INITIAL,       GAME_MODE_TITLE_INITIAL         },
172   { ANIM_CLASS_BIT_TITLE,               GAME_MODE_TITLE                 },
173   { ANIM_CLASS_BIT_MAIN,                GAME_MODE_MAIN                  },
174   { ANIM_CLASS_BIT_SUBMENU,             GAME_MODE_PSEUDO_SUBMENU        },
175   { ANIM_CLASS_BIT_MENU,                GAME_MODE_PSEUDO_MENU           },
176   { ANIM_CLASS_BIT_TOONS,               GAME_MODE_PSEUDO_TOONS          },
177
178   { -1,                                 -1                              }
179 };
180
181 /* forward declaration for internal use */
182 static void HandleGlobalAnim(int, int);
183 static void DoAnimationExt(void);
184 static void ResetGlobalAnim_Clickable();
185 static void ResetGlobalAnim_Clicked();
186
187 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
188
189 static unsigned int anim_sync_frame = 0;
190
191 static int game_mode_anim_classes[NUM_GAME_MODES];
192 static int anim_class_game_modes[NUM_ANIM_CLASSES];
193
194 static int anim_status_last = GAME_MODE_DEFAULT;
195 static int anim_classes_last = ANIM_CLASS_NONE;
196
197 static boolean drawing_to_fading_buffer = FALSE;
198
199
200 /* ========================================================================= */
201 /* generic animation frame calculation                                       */
202 /* ========================================================================= */
203
204 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
205                       int sync_frame)
206 {
207   int frame = 0;
208
209   sync_frame += start_frame * delay;
210
211   if (mode & ANIM_LOOP)                 /* looping animation */
212   {
213     frame = (sync_frame % (delay * num_frames)) / delay;
214   }
215   else if (mode & ANIM_LINEAR)          /* linear (non-looping) animation */
216   {
217     frame = sync_frame / delay;
218
219     if (frame > num_frames - 1)
220       frame = num_frames - 1;
221   }
222   else if (mode & ANIM_PINGPONG)        /* oscillate (border frames once) */
223   {
224     int max_anim_frames = (num_frames > 1 ? 2 * num_frames - 2 : 1);
225
226     frame = (sync_frame % (delay * max_anim_frames)) / delay;
227     frame = (frame < num_frames ? frame : max_anim_frames - frame);
228   }
229   else if (mode & ANIM_PINGPONG2)       /* oscillate (border frames twice) */
230   {
231     int max_anim_frames = 2 * num_frames;
232
233     frame = (sync_frame % (delay * max_anim_frames)) / delay;
234     frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
235   }
236   else if (mode & ANIM_RANDOM)          /* play frames in random order */
237   {
238     /* note: expect different frames for the same delay cycle! */
239
240     if (gfx.anim_random_frame < 0)
241       frame = GetSimpleRandom(num_frames);
242     else
243       frame = gfx.anim_random_frame % num_frames;
244   }
245   else if (mode & (ANIM_CE_VALUE | ANIM_CE_SCORE | ANIM_CE_DELAY))
246   {
247     frame = sync_frame % num_frames;
248   }
249
250   if (mode & ANIM_REVERSE)              /* use reverse animation direction */
251     frame = num_frames - frame - 1;
252
253   return frame;
254 }
255
256
257 /* ========================================================================= */
258 /* global animation functions                                                */
259 /* ========================================================================= */
260
261 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
262 {
263   struct GraphicInfo *c = &anim->control_info;
264   int last_anim_random_frame = gfx.anim_random_frame;
265   int part_nr;
266
267   gfx.anim_random_frame = -1;   // (use simple, ad-hoc random numbers)
268
269   part_nr = getAnimationFrame(anim->num_parts, 1,
270                               c->anim_mode, c->anim_start_frame,
271                               anim->part_counter);
272
273   gfx.anim_random_frame = last_anim_random_frame;
274
275   return part_nr;
276 }
277
278 static int compareGlobalAnimPartControlInfo(const void *obj1, const void *obj2)
279 {
280   const struct GlobalAnimPartControlInfo *o1 =
281     (struct GlobalAnimPartControlInfo *)obj1;
282   const struct GlobalAnimPartControlInfo *o2 =
283     (struct GlobalAnimPartControlInfo *)obj2;
284   int compare_result;
285
286   if (o1->control_info.draw_order != o2->control_info.draw_order)
287     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
288   else
289     compare_result = o1->nr - o2->nr;
290
291   return compare_result;
292 }
293
294 static int compareGlobalAnimMainControlInfo(const void *obj1, const void *obj2)
295 {
296   const struct GlobalAnimMainControlInfo *o1 =
297     (struct GlobalAnimMainControlInfo *)obj1;
298   const struct GlobalAnimMainControlInfo *o2 =
299     (struct GlobalAnimMainControlInfo *)obj2;
300   int compare_result;
301
302   if (o1->control_info.draw_order != o2->control_info.draw_order)
303     compare_result = o1->control_info.draw_order - o2->control_info.draw_order;
304   else
305     compare_result = o1->nr - o2->nr;
306
307   return compare_result;
308 }
309
310 static void InitToonControls()
311 {
312   int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
313   struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
314   struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
315   int mode_nr, anim_nr, part_nr;
316   int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
317   int num_toons = MAX_NUM_TOONS;
318   int i;
319
320   if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
321     num_toons = global.num_toons;
322
323   mode_nr = mode_nr_toons;
324   anim_nr = ctrl->num_anims;
325
326   anim->nr = anim_nr;
327   anim->mode_nr = mode_nr;
328   anim->control_info = graphic_info[control];
329
330   anim->num_parts = 0;
331   anim->part_counter = 0;
332   anim->active_part_nr = 0;
333
334   anim->has_base = FALSE;
335
336   anim->init_delay_counter = 0;
337
338   anim->state = ANIM_STATE_INACTIVE;
339
340   part_nr = 0;
341
342   for (i = 0; i < num_toons; i++)
343   {
344     struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
345     int sound = SND_UNDEFINED;
346     int music = MUS_UNDEFINED;
347     int graphic = IMG_TOON_1 + i;
348     int control = graphic;
349
350     part->nr = part_nr;
351     part->anim_nr = anim_nr;
352     part->mode_nr = mode_nr;
353     part->sound = sound;
354     part->music = music;
355     part->graphic = graphic;
356     part->graphic_info = graphic_info[graphic];
357     part->control_info = graphic_info[control];
358
359     part->graphic_info.anim_delay *= part->graphic_info.step_delay;
360
361     part->control_info.init_delay_fixed = 0;
362     part->control_info.init_delay_random = 150;
363
364     part->control_info.x = ARG_UNDEFINED_VALUE;
365     part->control_info.y = ARG_UNDEFINED_VALUE;
366
367     part->initial_anim_sync_frame = 0;
368
369     part->step_delay = 0;
370     part->step_delay_value = graphic_info[control].step_delay;
371
372     part->state = ANIM_STATE_INACTIVE;
373     part->last_anim_status = -1;
374
375     anim->num_parts++;
376     part_nr++;
377   }
378
379   ctrl->num_anims++;
380 }
381
382 void InitGlobalAnimControls()
383 {
384   int i, m, a, p;
385   int mode_nr, anim_nr, part_nr;
386   int sound, music, graphic, control;
387
388   anim_sync_frame = 0;
389
390   for (m = 0; m < NUM_GAME_MODES; m++)
391   {
392     mode_nr = m;
393
394     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
395
396     ctrl->nr = mode_nr;
397     ctrl->num_anims = 0;
398
399     anim_nr = 0;
400
401     for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
402     {
403       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
404       int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
405
406       control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
407
408       // if no base animation parameters defined, use default values
409       if (control == IMG_UNDEFINED)
410         control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
411
412       anim->nr = anim_nr;
413       anim->mode_nr = mode_nr;
414       anim->control_info = graphic_info[control];
415
416       anim->num_parts = 0;
417       anim->part_counter = 0;
418       anim->active_part_nr = 0;
419
420       anim->has_base = FALSE;
421
422       anim->init_delay_counter = 0;
423
424       anim->state = ANIM_STATE_INACTIVE;
425
426       part_nr = 0;
427
428       for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
429       {
430         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
431
432         sound   = global_anim_info[a].sound[p][m];
433         music   = global_anim_info[a].music[p][m];
434         graphic = global_anim_info[a].graphic[p][m];
435         control = global_anim_info[ctrl_id].graphic[p][m];
436
437         if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
438             control == IMG_UNDEFINED)
439           continue;
440
441 #if 0
442         printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
443                m, a, p, mode_nr, anim_nr, part_nr, control);
444 #endif
445
446 #if 0
447         printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
448                m, a, p, mode_nr, anim_nr, part_nr, sound);
449 #endif
450
451         part->nr = part_nr;
452         part->anim_nr = anim_nr;
453         part->mode_nr = mode_nr;
454         part->sound = sound;
455         part->music = music;
456         part->graphic = graphic;
457         part->graphic_info = graphic_info[graphic];
458         part->control_info = graphic_info[control];
459
460         part->initial_anim_sync_frame = 0;
461
462         part->step_delay = 0;
463         part->step_delay_value = graphic_info[control].step_delay;
464
465         part->state = ANIM_STATE_INACTIVE;
466         part->last_anim_status = -1;
467
468         if (p < GLOBAL_ANIM_ID_PART_BASE)
469         {
470           anim->num_parts++;
471           part_nr++;
472         }
473         else
474         {
475           anim->base = *part;
476           anim->has_base = TRUE;
477         }
478       }
479
480       if (anim->num_parts > 0 || anim->has_base)
481       {
482         ctrl->num_anims++;
483         anim_nr++;
484       }
485     }
486   }
487
488   InitToonControls();
489
490   /* sort all animations according to draw_order and animation number */
491   for (m = 0; m < NUM_GAME_MODES; m++)
492   {
493     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[m];
494
495     /* sort all main animations for this game mode */
496     qsort(ctrl->anim, ctrl->num_anims,
497           sizeof(struct GlobalAnimMainControlInfo),
498           compareGlobalAnimMainControlInfo);
499
500     for (a = 0; a < ctrl->num_anims; a++)
501     {
502       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[a];
503
504       /* sort all animation parts for this main animation */
505       qsort(anim->part, anim->num_parts,
506             sizeof(struct GlobalAnimPartControlInfo),
507             compareGlobalAnimPartControlInfo);
508     }
509   }
510
511   for (i = 0; i < NUM_GAME_MODES; i++)
512     game_mode_anim_classes[i] = ANIM_CLASS_NONE;
513   for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
514     game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
515       game_mode_anim_classes_list[i].class;
516
517   for (i = 0; i < NUM_ANIM_CLASSES; i++)
518     anim_class_game_modes[i] = GAME_MODE_DEFAULT;
519   for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
520     anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
521       anim_class_game_modes_list[i].game_mode;
522
523   anim_status_last = GAME_MODE_LOADING;
524   anim_classes_last = ANIM_CLASS_NONE;
525 }
526
527 void InitGlobalAnimations()
528 {
529   InitGlobalAnimControls();
530 }
531
532 void DrawGlobalAnimationsExt(int drawing_target, int drawing_stage)
533 {
534   Bitmap *fade_bitmap =
535     (drawing_target == DRAW_TO_FADE_SOURCE ? gfx.fade_bitmap_source :
536      drawing_target == DRAW_TO_FADE_TARGET ? gfx.fade_bitmap_target : NULL);
537   int game_mode_anim_action[NUM_GAME_MODES];
538   int mode_nr;
539
540   if (!setup.toons)
541     return;
542
543   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1 &&
544       drawing_target == DRAW_TO_SCREEN)
545     DoAnimationExt();
546
547   // always start with reliable default values (no animation actions)
548   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
549     game_mode_anim_action[mode_nr] = ANIM_NO_ACTION;
550
551   if (global.anim_status != anim_status_last)
552   {
553     boolean before_fading = (global.anim_status == GAME_MODE_PSEUDO_FADING);
554     boolean after_fading  = (anim_status_last   == GAME_MODE_PSEUDO_FADING);
555     int anim_classes_next = game_mode_anim_classes[global.anim_status_next];
556     int i;
557
558     if (drawing_target == DRAW_TO_FADE_TARGET)
559       after_fading = TRUE;
560
561     // ---------- part 1 ------------------------------------------------------
562     // start or stop global animations by change of game mode
563     // (special handling of animations for "current screen" and "all screens")
564
565     // stop animations for last screen
566     game_mode_anim_action[anim_status_last] = ANIM_STOP;
567
568     // start animations for current screen
569     game_mode_anim_action[global.anim_status] = ANIM_START;
570
571     // start animations for all screens after loading new artwork set
572     if (anim_status_last == GAME_MODE_LOADING)
573       game_mode_anim_action[GAME_MODE_DEFAULT] = ANIM_START;
574
575     // ---------- part 2 ------------------------------------------------------
576     // start or stop global animations by change of animation class
577     // (generic handling of animations for "class of screens")
578
579     for (i = 0; i < NUM_ANIM_CLASSES; i++)
580     {
581       int anim_class_check = (1 << i);
582       int anim_class_game_mode = anim_class_game_modes[i];
583       int anim_class_last = anim_classes_last & anim_class_check;
584       int anim_class_next = anim_classes_next & anim_class_check;
585
586       // stop animations for changed screen class before fading to new screen
587       if (before_fading && anim_class_last && !anim_class_next)
588         game_mode_anim_action[anim_class_game_mode] = ANIM_STOP;
589
590       // start animations for changed screen class after fading to new screen
591       if (after_fading && !anim_class_last && anim_class_next)
592         game_mode_anim_action[anim_class_game_mode] = ANIM_START;
593     }
594
595     if (drawing_target == DRAW_TO_SCREEN)
596     {
597       if (after_fading)
598         anim_classes_last = anim_classes_next;
599
600       anim_status_last = global.anim_status;
601
602       // start or stop animations determined to be started or stopped above
603       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
604         if (game_mode_anim_action[mode_nr] != ANIM_NO_ACTION)
605           HandleGlobalAnim(game_mode_anim_action[mode_nr], mode_nr);
606     }
607     else if (drawing_target == DRAW_TO_FADE_TARGET)
608     {
609       drawing_to_fading_buffer = TRUE;
610
611       // start animations determined to be (temporary) started above
612       for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
613         if (game_mode_anim_action[mode_nr] == ANIM_START)
614           HandleGlobalAnim(ANIM_START, mode_nr);
615     }
616   }
617
618   if (global.anim_status == GAME_MODE_LOADING)
619     return;
620
621   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
622   {
623     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
624     int anim_nr;
625
626     // when preparing source fading buffer, only draw animations to be stopped
627     if (drawing_target == DRAW_TO_FADE_SOURCE &&
628         game_mode_anim_action[mode_nr] != ANIM_STOP)
629       continue;
630
631     // when preparing target fading buffer, only draw animations to be started
632     if (drawing_target == DRAW_TO_FADE_TARGET &&
633         game_mode_anim_action[mode_nr] != ANIM_START)
634       continue;
635
636 #if 0
637     if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
638         mode_nr != game_status)
639       continue;
640 #endif
641
642     for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
643     {
644       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
645       struct GraphicInfo *c = &anim->control_info;
646       int part_first, part_last;
647       int part_nr;
648
649       if (!(anim->state & ANIM_STATE_RUNNING))
650         continue;
651
652       part_first = part_last = anim->active_part_nr;
653
654       if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
655       {
656         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
657
658         part_first = 0;
659         part_last = num_parts - 1;
660       }
661
662       for (part_nr = part_first; part_nr <= part_last; part_nr++)
663       {
664         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
665         struct GraphicInfo *g = &part->graphic_info;
666         Bitmap *src_bitmap;
667         int src_x, src_y;
668         int width  = g->width;
669         int height = g->height;
670         int dst_x = part->x;
671         int dst_y = part->y;
672         int cut_x = 0;
673         int cut_y = 0;
674         int sync_frame;
675         int frame;
676
677         if (!(part->state & ANIM_STATE_RUNNING))
678           continue;
679
680         if (part->drawing_stage != drawing_stage)
681           continue;
682
683         if (part->x < 0)
684         {
685           dst_x = 0;
686           width += part->x;
687           cut_x = -part->x;
688         }
689         else if (part->x > part->viewport_width - g->width)
690           width -= (part->x - (part->viewport_width - g->width));
691
692         if (part->y < 0)
693         {
694           dst_y = 0;
695           height += part->y;
696           cut_y = -part->y;
697         }
698         else if (part->y > part->viewport_height - g->height)
699           height -= (part->y - (part->viewport_height - g->height));
700
701         if (width <= 0 || height <= 0)
702           continue;
703
704         dst_x += part->viewport_x;
705         dst_y += part->viewport_y;
706
707         sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
708         frame = getAnimationFrame(g->anim_frames, g->anim_delay,
709                                   g->anim_mode, g->anim_start_frame,
710                                   sync_frame);
711
712         getFixedGraphicSource(part->graphic, frame, &src_bitmap,
713                               &src_x, &src_y);
714
715         src_x += cut_x;
716         src_y += cut_y;
717
718         if (drawing_target == DRAW_TO_SCREEN)
719           BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
720                              dst_x, dst_y);
721         else
722           BlitBitmapMasked(src_bitmap, fade_bitmap, src_x, src_y, width, height,
723                            dst_x, dst_y);
724       }
725     }
726   }
727
728   if (drawing_target == DRAW_TO_FADE_TARGET)
729   {
730     // stop animations determined to be (temporary) started above
731     for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
732       if (game_mode_anim_action[mode_nr] == ANIM_START)
733         HandleGlobalAnim(ANIM_STOP, mode_nr);
734
735     drawing_to_fading_buffer = FALSE;
736   }
737 }
738
739 void DrawGlobalAnimations(int drawing_target, int drawing_stage)
740 {
741   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
742     ResetGlobalAnim_Clickable();
743
744   DrawGlobalAnimationsExt(drawing_target, drawing_stage);
745
746   if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_2)
747     ResetGlobalAnim_Clicked();
748 }
749
750 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
751 {
752   int viewport_x;
753   int viewport_y;
754   int viewport_width;
755   int viewport_height;
756   boolean changed = FALSE;
757
758   if (part->last_anim_status == global.anim_status)
759     return FALSE;
760
761   part->last_anim_status = global.anim_status;
762
763   part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
764
765   if (part->control_info.class == get_hash_from_key("window") ||
766       part->control_info.class == get_hash_from_key("border"))
767   {
768     viewport_x = 0;
769     viewport_y = 0;
770     viewport_width  = WIN_XSIZE;
771     viewport_height = WIN_YSIZE;
772
773     part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
774   }
775   else if (part->control_info.class == get_hash_from_key("door_1"))
776   {
777     viewport_x = DX;
778     viewport_y = DY;
779     viewport_width  = DXSIZE;
780     viewport_height = DYSIZE;
781   }
782   else if (part->control_info.class == get_hash_from_key("door_2"))
783   {
784     viewport_x = VX;
785     viewport_y = VY;
786     viewport_width  = VXSIZE;
787     viewport_height = VYSIZE;
788   }
789   else          // default: "playfield"
790   {
791     viewport_x = REAL_SX;
792     viewport_y = REAL_SY;
793     viewport_width  = FULL_SXSIZE;
794     viewport_height = FULL_SYSIZE;
795   }
796
797   if (viewport_x != part->viewport_x ||
798       viewport_y != part->viewport_y ||
799       viewport_width  != part->viewport_width ||
800       viewport_height != part->viewport_height)
801   {
802     part->viewport_x = viewport_x;
803     part->viewport_y = viewport_y;
804     part->viewport_width  = viewport_width;
805     part->viewport_height = viewport_height;
806
807     changed = TRUE;
808   }
809
810   return changed;
811 }
812
813 static void PlayGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
814 {
815   int sound = part->sound;
816
817   if (sound == SND_UNDEFINED)
818     return;
819
820   if ((!setup.sound_simple && !IS_LOOP_SOUND(sound)) ||
821       (!setup.sound_loops && IS_LOOP_SOUND(sound)))
822     return;
823
824   // !!! TODO: ADD STEREO POSITION FOR MOVING ANIMATIONS !!!
825   if (IS_LOOP_SOUND(sound))
826     PlaySoundLoop(sound);
827   else
828     PlaySound(sound);
829
830 #if 0
831   printf("::: PLAY SOUND %d.%d.%d: %d\n",
832          part->anim_nr, part->nr, part->mode_nr, sound);
833 #endif
834 }
835
836 static void StopGlobalAnimSound(struct GlobalAnimPartControlInfo *part)
837 {
838   int sound = part->sound;
839
840   if (sound == SND_UNDEFINED)
841     return;
842
843   StopSound(sound);
844
845 #if 0
846   printf("::: STOP SOUND %d.%d.%d: %d\n",
847          part->anim_nr, part->nr, part->mode_nr, sound);
848 #endif
849 }
850
851 static void PlayGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
852 {
853   int music = part->music;
854
855   if (music == MUS_UNDEFINED)
856     return;
857
858   if (!setup.sound_music)
859     return;
860
861   PlayMusic(music);
862
863 #if 0
864   printf("::: PLAY MUSIC %d.%d.%d: %d\n",
865          part->anim_nr, part->nr, part->mode_nr, music);
866 #endif
867 }
868
869 static void StopGlobalAnimMusic(struct GlobalAnimPartControlInfo *part)
870 {
871   int music = part->music;
872
873   if (music == MUS_UNDEFINED)
874     return;
875
876   StopMusic();
877
878 #if 0
879   printf("::: STOP MUSIC %d.%d.%d: %d\n",
880          part->anim_nr, part->nr, part->mode_nr, music);
881 #endif
882 }
883
884 static void PlayGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
885 {
886   // when drawing animations to fading buffer, do not play sounds or music
887   if (drawing_to_fading_buffer)
888     return;
889
890   PlayGlobalAnimSound(part);
891   PlayGlobalAnimMusic(part);
892 }
893
894 static void StopGlobalAnimSoundAndMusic(struct GlobalAnimPartControlInfo *part)
895 {
896   StopGlobalAnimSound(part);
897   StopGlobalAnimMusic(part);
898 }
899
900 static boolean isClickablePart(struct GlobalAnimPartControlInfo *part)
901 {
902   struct GraphicInfo *c = &part->control_info;
903
904   return (c->init_event & ANIM_EVENT_CLICK ||
905           c->anim_event & ANIM_EVENT_CLICK);
906 }
907
908 static boolean isClickedPart(struct GlobalAnimPartControlInfo *part,
909                              int mx, int my, boolean clicked)
910 {
911   struct GraphicInfo *g = &part->graphic_info;
912   int part_x = part->viewport_x + part->x;
913   int part_y = part->viewport_y + part->y;
914   int part_width  = g->width;
915   int part_height = g->height;
916
917   // check if mouse click was detected at all
918   if (!clicked)
919     return FALSE;
920
921   // check if mouse click is inside the animation part's viewport
922   if (mx <  part->viewport_x ||
923       mx >= part->viewport_x + part->viewport_width ||
924       my <  part->viewport_y ||
925       my >= part->viewport_y + part->viewport_height)
926     return FALSE;
927
928   // check if mouse click is inside the animation part's graphic
929   if (mx <  part_x ||
930       mx >= part_x + part_width ||
931       my <  part_y ||
932       my >= part_y + part_height)
933     return FALSE;
934
935   return TRUE;
936 }
937
938 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
939 {
940   struct GraphicInfo *g = &part->graphic_info;
941   struct GraphicInfo *c = &part->control_info;
942   boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
943
944   if (viewport_changed)
945     state |= ANIM_STATE_RESTART;
946
947   if (state & ANIM_STATE_RESTART)
948   {
949     // when drawing animations to fading buffer, only start fixed animations
950     if (drawing_to_fading_buffer && (c->x == ARG_UNDEFINED_VALUE ||
951                                      c->y == ARG_UNDEFINED_VALUE))
952       return ANIM_STATE_INACTIVE;
953
954     ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
955
956     part->init_delay_counter =
957       (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
958
959     part->anim_delay_counter =
960       (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
961
962     part->init_event_state = c->init_event;
963     part->anim_event_state = c->anim_event;
964
965     part->initial_anim_sync_frame =
966       (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
967
968     if (c->direction & MV_HORIZONTAL)
969     {
970       int pos_bottom = part->viewport_height - g->height;
971
972       if (c->position == POS_TOP)
973         part->y = 0;
974       else if (c->position == POS_UPPER)
975         part->y = GetSimpleRandom(pos_bottom / 2);
976       else if (c->position == POS_MIDDLE)
977         part->y = pos_bottom / 2;
978       else if (c->position == POS_LOWER)
979         part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
980       else if (c->position == POS_BOTTOM)
981         part->y = pos_bottom;
982       else
983         part->y = GetSimpleRandom(pos_bottom);
984
985       if (c->direction == MV_RIGHT)
986       {
987         part->step_xoffset = c->step_offset;
988         part->x = -g->width + part->step_xoffset;
989       }
990       else
991       {
992         part->step_xoffset = -c->step_offset;
993         part->x = part->viewport_width + part->step_xoffset;
994       }
995
996       part->step_yoffset = 0;
997     }
998     else if (c->direction & MV_VERTICAL)
999     {
1000       int pos_right = part->viewport_width - g->width;
1001
1002       if (c->position == POS_LEFT)
1003         part->x = 0;
1004       else if (c->position == POS_RIGHT)
1005         part->x = pos_right;
1006       else
1007         part->x = GetSimpleRandom(pos_right);
1008
1009       if (c->direction == MV_DOWN)
1010       {
1011         part->step_yoffset = c->step_offset;
1012         part->y = -g->height + part->step_yoffset;
1013       }
1014       else
1015       {
1016         part->step_yoffset = -c->step_offset;
1017         part->y = part->viewport_height + part->step_yoffset;
1018       }
1019
1020       part->step_xoffset = 0;
1021     }
1022     else
1023     {
1024       part->x = 0;
1025       part->y = 0;
1026
1027       part->step_xoffset = 0;
1028       part->step_yoffset = 0;
1029     }
1030
1031     if (c->x != ARG_UNDEFINED_VALUE)
1032       part->x = c->x;
1033     if (c->y != ARG_UNDEFINED_VALUE)
1034       part->y = c->y;
1035
1036     if (c->step_xoffset != ARG_UNDEFINED_VALUE)
1037       part->step_xoffset = c->step_xoffset;
1038     if (c->step_yoffset != ARG_UNDEFINED_VALUE)
1039       part->step_yoffset = c->step_yoffset;
1040
1041     if (part->init_delay_counter == 0 &&
1042         part->init_event_state == ANIM_EVENT_NONE)
1043       PlayGlobalAnimSoundAndMusic(part);
1044   }
1045
1046   part->clickable = isClickablePart(part);
1047
1048   if (part->clicked &&
1049       part->init_event_state != ANIM_EVENT_NONE)
1050   {
1051     if (part->initial_anim_sync_frame > 0)
1052       part->initial_anim_sync_frame -= part->init_delay_counter - 1;
1053
1054     part->init_delay_counter = 1;
1055     part->init_event_state = ANIM_EVENT_NONE;
1056
1057     part->clicked = FALSE;
1058   }
1059
1060   if (part->clicked &&
1061       part->anim_event_state != ANIM_EVENT_NONE)
1062   {
1063     part->anim_delay_counter = 1;
1064     part->anim_event_state = ANIM_EVENT_NONE;
1065
1066     part->clicked = FALSE;
1067   }
1068
1069   if (part->init_delay_counter > 0)
1070   {
1071     part->init_delay_counter--;
1072
1073     if (part->init_delay_counter == 0)
1074     {
1075       part->init_event_state = ANIM_EVENT_NONE;
1076
1077       PlayGlobalAnimSoundAndMusic(part);
1078     }
1079
1080     return ANIM_STATE_WAITING;
1081   }
1082
1083   if (part->init_event_state != ANIM_EVENT_NONE)
1084     return ANIM_STATE_WAITING;
1085
1086   // check if moving animation has left the visible screen area
1087   if ((part->x <= -g->width              && part->step_xoffset <= 0) ||
1088       (part->x >=  part->viewport_width  && part->step_xoffset >= 0) ||
1089       (part->y <= -g->height             && part->step_yoffset <= 0) ||
1090       (part->y >=  part->viewport_height && part->step_yoffset >= 0))
1091   {
1092     // do not wait for "anim" events for off-screen animations
1093     part->anim_event_state = ANIM_EVENT_NONE;
1094
1095     // do not stop animation before "anim" or "post" counter are finished
1096     if (part->anim_delay_counter == 0 &&
1097         part->post_delay_counter == 0)
1098     {
1099       StopGlobalAnimSoundAndMusic(part);
1100
1101       part->post_delay_counter =
1102         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1103
1104       if (part->post_delay_counter > 0)
1105         return ANIM_STATE_RUNNING;
1106
1107       // drawing last frame not needed here -- animation not visible anymore
1108       return ANIM_STATE_RESTART;
1109     }
1110   }
1111
1112   if (part->anim_delay_counter > 0)
1113   {
1114     part->anim_delay_counter--;
1115
1116     if (part->anim_delay_counter == 0)
1117     {
1118       part->anim_event_state = ANIM_EVENT_NONE;
1119
1120       StopGlobalAnimSoundAndMusic(part);
1121
1122       part->post_delay_counter =
1123         (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
1124
1125       if (part->post_delay_counter > 0)
1126         return ANIM_STATE_RUNNING;
1127
1128       // additional state "RUNNING" required to not skip drawing last frame
1129       return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
1130     }
1131   }
1132
1133   if (part->post_delay_counter > 0)
1134   {
1135     part->post_delay_counter--;
1136
1137     if (part->post_delay_counter == 0)
1138       return ANIM_STATE_RESTART;
1139
1140     return ANIM_STATE_WAITING;
1141   }
1142
1143   if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
1144                        anim_sync_frame))
1145     return ANIM_STATE_RUNNING;
1146
1147 #if 0
1148   {
1149     static unsigned int last_counter = -1;
1150     unsigned int counter = Counter();
1151
1152     printf("::: NEXT ANIM PART [%d, %d]\n",
1153            anim_sync_frame, counter - last_counter);
1154
1155     last_counter = counter;
1156   }
1157 #endif
1158
1159   part->x += part->step_xoffset;
1160   part->y += part->step_yoffset;
1161
1162   return ANIM_STATE_RUNNING;
1163 }
1164
1165 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
1166 {
1167   struct GlobalAnimPartControlInfo *part;
1168   struct GraphicInfo *c = &anim->control_info;
1169   int state, active_part_nr;
1170
1171 #if 0
1172   printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1173          anim->mode_nr, anim->nr, anim->num_parts);
1174   printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
1175 #endif
1176
1177 #if 0
1178   printf("::: %s(%d): %d, %d, %d [%d]\n",
1179          (action == ANIM_START ? "ANIM_START" :
1180           action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
1181           action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
1182          anim->nr,
1183          anim->state & ANIM_STATE_RESTART,
1184          anim->state & ANIM_STATE_WAITING,
1185          anim->state & ANIM_STATE_RUNNING,
1186          anim->num_parts);
1187 #endif
1188
1189   switch (action)
1190   {
1191     case ANIM_START:
1192       anim->state = anim->last_state = ANIM_STATE_RESTART;
1193       anim->active_part_nr = anim->last_active_part_nr = 0;
1194       anim->part_counter = 0;
1195
1196       break;
1197
1198     case ANIM_CONTINUE:
1199       if (anim->state == ANIM_STATE_INACTIVE)
1200         return;
1201
1202       anim->state = anim->last_state;
1203       anim->active_part_nr = anim->last_active_part_nr;
1204
1205       break;
1206
1207     case ANIM_STOP:
1208       anim->state = ANIM_STATE_INACTIVE;
1209
1210       {
1211         int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1212         int i;
1213
1214         for (i = 0; i < num_parts; i++)
1215           StopGlobalAnimSoundAndMusic(&anim->part[i]);
1216       }
1217
1218       return;
1219
1220     default:
1221       break;
1222   }
1223
1224   if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
1225   {
1226     int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
1227     int i;
1228
1229 #if 0
1230     printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
1231            anim->mode_nr, anim->nr, num_parts);
1232 #endif
1233
1234     for (i = 0; i < num_parts; i++)
1235     {
1236       part = &anim->part[i];
1237
1238       switch (action)
1239       {
1240         case ANIM_START:
1241           anim->state = ANIM_STATE_RUNNING;
1242           part->state = ANIM_STATE_RESTART;
1243
1244           break;
1245
1246         case ANIM_CONTINUE:
1247           if (part->state == ANIM_STATE_INACTIVE)
1248             continue;
1249
1250           break;
1251
1252         case ANIM_STOP:
1253           part->state = ANIM_STATE_INACTIVE;
1254
1255           continue;
1256
1257         default:
1258           break;
1259       }
1260
1261       part->state = HandleGlobalAnim_Part(part, part->state);
1262
1263       // when animation mode is "once", stop after animation was played once
1264       if (c->anim_mode & ANIM_ONCE &&
1265           part->state & ANIM_STATE_RESTART)
1266         part->state = ANIM_STATE_INACTIVE;
1267     }
1268
1269     anim->last_state = anim->state;
1270     anim->last_active_part_nr = anim->active_part_nr;
1271
1272     return;
1273   }
1274
1275   if (anim->state & ANIM_STATE_RESTART)         // directly after restart
1276     anim->active_part_nr = getGlobalAnimationPart(anim);
1277
1278   part = &anim->part[anim->active_part_nr];
1279
1280   part->state = ANIM_STATE_RUNNING;
1281
1282   anim->state = HandleGlobalAnim_Part(part, anim->state);
1283
1284   if (anim->state & ANIM_STATE_RESTART)
1285     anim->part_counter++;
1286
1287   // when animation mode is "once", stop after all animations were played once
1288   if (c->anim_mode & ANIM_ONCE &&
1289       anim->part_counter == anim->num_parts)
1290     anim->state = ANIM_STATE_INACTIVE;
1291
1292   state = anim->state;
1293   active_part_nr = anim->active_part_nr;
1294
1295   // while the animation parts are pausing (waiting or inactive), play the base
1296   // (main) animation; this corresponds to the "boring player animation" logic
1297   // (!!! KLUDGE WARNING: I THINK THIS IS A MESS THAT SHOULD BE CLEANED UP !!!)
1298   if (anim->has_base)
1299   {
1300     if (anim->state == ANIM_STATE_WAITING ||
1301         anim->state == ANIM_STATE_INACTIVE)
1302     {
1303       anim->active_part_nr = anim->num_parts;   // part nr of base animation
1304       part = &anim->part[anim->active_part_nr];
1305
1306       if (anim->state != anim->last_state)
1307         part->state = ANIM_STATE_RESTART;
1308
1309       anim->state = ANIM_STATE_RUNNING;
1310       part->state = HandleGlobalAnim_Part(part, part->state);
1311     }
1312   }
1313
1314   anim->last_state = state;
1315   anim->last_active_part_nr = active_part_nr;
1316 }
1317
1318 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
1319 {
1320   int i;
1321
1322 #if 0
1323   printf("::: HandleGlobalAnim_Mode: %d => %d\n",
1324          ctrl->nr, ctrl->num_anims);
1325 #endif
1326
1327   for (i = 0; i < ctrl->num_anims; i++)
1328     HandleGlobalAnim_Main(&ctrl->anim[i], action);
1329 }
1330
1331 static void HandleGlobalAnim(int action, int game_mode)
1332 {
1333 #if 0
1334   printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
1335 #endif
1336
1337   HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
1338 }
1339
1340 static void DoAnimationExt()
1341 {
1342   int i;
1343
1344 #if 0
1345   printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
1346 #endif
1347
1348   // global animations now synchronized with frame delay of screen update
1349   anim_sync_frame++;
1350
1351   for (i = 0; i < NUM_GAME_MODES; i++)
1352     HandleGlobalAnim(ANIM_CONTINUE, i);
1353
1354 #if 0
1355   // force screen redraw in next frame to continue drawing global animations
1356   redraw_mask = REDRAW_ALL;
1357 #endif
1358 }
1359
1360 static void InitGlobalAnim_Clickable()
1361 {
1362   int mode_nr;
1363
1364   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1365   {
1366     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1367     int anim_nr;
1368
1369     for (anim_nr = 0; anim_nr < NUM_GLOBAL_ANIMS_AND_TOONS; anim_nr++)
1370     {
1371       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1372       int part_nr;
1373
1374       for (part_nr = 0; part_nr < NUM_GLOBAL_ANIM_PARTS_AND_TOONS; part_nr++)
1375       {
1376         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1377
1378         part->clickable = FALSE;
1379       }
1380     }
1381   }
1382 }
1383
1384 static boolean InitGlobalAnim_Clicked(int mx, int my, boolean clicked)
1385 {
1386   boolean any_part_clicked = FALSE;
1387   int mode_nr;
1388
1389   for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
1390   {
1391     struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
1392     int anim_nr;
1393
1394     for (anim_nr = 0; anim_nr < NUM_GLOBAL_ANIMS_AND_TOONS; anim_nr++)
1395     {
1396       struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
1397       int part_nr;
1398
1399       for (part_nr = 0; part_nr < NUM_GLOBAL_ANIM_PARTS_AND_TOONS; part_nr++)
1400       {
1401         struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
1402
1403         part->clicked = FALSE;
1404
1405         if (part->clickable && isClickedPart(part, mx, my, clicked))
1406           any_part_clicked = part->clicked = TRUE;
1407       }
1408     }
1409   }
1410
1411   return any_part_clicked;
1412 }
1413
1414 static void ResetGlobalAnim_Clickable()
1415 {
1416   InitGlobalAnim_Clickable();
1417 }
1418
1419 static void ResetGlobalAnim_Clicked()
1420 {
1421   InitGlobalAnim_Clicked(-1, -1, FALSE);
1422 }
1423
1424 boolean HandleGlobalAnimClicks(int mx, int my, int button)
1425 {
1426   static boolean click_consumed = FALSE;
1427   static int last_button = 0;
1428   boolean press_event;
1429   boolean release_event;
1430   boolean click_consumed_current = click_consumed;
1431
1432   /* check if button state has changed since last invocation */
1433   press_event   = (button != 0 && last_button == 0);
1434   release_event = (button == 0 && last_button != 0);
1435   last_button = button;
1436
1437   if (press_event)
1438   {
1439     click_consumed = InitGlobalAnim_Clicked(mx, my, TRUE);
1440     click_consumed_current = click_consumed;
1441   }
1442
1443   if (release_event)
1444     click_consumed = FALSE;
1445
1446   return click_consumed_current;
1447 }