1 // ============================================================================
2 // Rocks'n'Diamonds - McDuffin Strikes Back!
3 // ----------------------------------------------------------------------------
4 // (c) 1995-2014 by Artsoft Entertainment
7 // http://www.artsoft.org/
8 // ----------------------------------------------------------------------------
10 // ============================================================================
17 /* values for global toon animation definition */
18 #define NUM_GLOBAL_TOON_ANIMS 1
19 #define NUM_GLOBAL_TOON_PARTS MAX_NUM_TOONS
21 /* values for global animation definition (including toons) */
22 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + \
23 NUM_GLOBAL_TOON_ANIMS)
24 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
25 NUM_GLOBAL_TOON_PARTS)
27 #define ANIM_CLASS_BIT_MENU 0
28 #define ANIM_CLASS_BIT_TOONS 1
30 #define NUM_ANIM_CLASSES 2
32 #define ANIM_CLASS_NONE 0
33 #define ANIM_CLASS_MENU (1 << ANIM_CLASS_BIT_MENU)
34 #define ANIM_CLASS_TOONS (1 << ANIM_CLASS_BIT_TOONS)
37 struct GlobalAnimPartControlInfo
44 struct GraphicInfo graphic_info;
45 struct GraphicInfo control_info;
53 int step_xoffset, step_yoffset;
55 unsigned int initial_anim_sync_frame;
56 unsigned int step_delay, step_delay_value;
58 int init_delay_counter;
59 int anim_delay_counter;
60 int post_delay_counter;
68 struct GlobalAnimMainControlInfo
70 struct GlobalAnimPartControlInfo base;
71 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
76 struct GraphicInfo control_info;
84 int init_delay_counter;
89 struct GlobalAnimControlInfo
91 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
97 struct GameModeAnimClass
101 } game_mode_anim_classes_list[] =
103 { GAME_MODE_MAIN, ANIM_CLASS_TOONS | ANIM_CLASS_MENU },
104 { GAME_MODE_LEVELS, ANIM_CLASS_TOONS | ANIM_CLASS_MENU },
105 { GAME_MODE_LEVELNR, ANIM_CLASS_TOONS | ANIM_CLASS_MENU },
106 { GAME_MODE_INFO, ANIM_CLASS_TOONS | ANIM_CLASS_MENU },
107 { GAME_MODE_SETUP, ANIM_CLASS_TOONS | ANIM_CLASS_MENU },
108 { GAME_MODE_SCORES, ANIM_CLASS_TOONS },
113 struct AnimClassGameMode
117 } anim_class_game_modes_list[] =
119 { ANIM_CLASS_BIT_MENU, GAME_MODE_PSEUDO_MENU },
120 { ANIM_CLASS_BIT_TOONS, GAME_MODE_PSEUDO_TOONS },
125 /* forward declaration for internal use */
126 static void HandleGlobalAnim(int, int);
127 static void DoAnimationExt(void);
129 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_GAME_MODES];
130 static struct ToonInfo toons[MAX_NUM_TOONS];
132 static unsigned int anim_sync_frame = 0;
133 static unsigned int anim_sync_frame_delay = 0;
134 static unsigned int anim_sync_frame_delay_value = GAME_FRAME_DELAY;
136 static int game_mode_anim_classes[NUM_GAME_MODES];
137 static int anim_class_game_modes[NUM_ANIM_CLASSES];
139 static int anim_status_last = GAME_MODE_DEFAULT;
140 static int anim_classes_last = ANIM_CLASS_NONE;
143 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
145 struct GraphicInfo *c = &anim->control_info;
146 int last_anim_random_frame = gfx.anim_random_frame;
149 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
151 part_nr = getAnimationFrame(anim->num_parts, 1,
152 c->anim_mode, c->anim_start_frame,
155 gfx.anim_random_frame = last_anim_random_frame;
160 static void PrepareBackbuffer()
162 if (game_status != GAME_MODE_PLAYING)
165 BlitScreenToBitmap(backbuffer);
168 boolean ToonNeedsRedraw()
175 int num_toons = MAX_NUM_TOONS;
178 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
179 num_toons = global.num_toons;
181 for (i = 0; i < num_toons; i++)
183 int graphic = IMG_TOON_1 + i;
184 struct FileInfo *image = getImageListEntryFromImageID(graphic);
186 toons[i].bitmap = graphic_info[graphic].bitmap;
188 toons[i].src_x = graphic_info[graphic].src_x;
189 toons[i].src_y = graphic_info[graphic].src_y;
191 toons[i].width = graphic_info[graphic].width;
192 toons[i].height = graphic_info[graphic].height;
194 toons[i].anim_frames = graphic_info[graphic].anim_frames;
195 toons[i].anim_delay = graphic_info[graphic].anim_delay;
196 toons[i].anim_mode = graphic_info[graphic].anim_mode;
197 toons[i].anim_start_frame = graphic_info[graphic].anim_start_frame;
199 toons[i].step_offset = graphic_info[graphic].step_offset;
200 toons[i].step_delay = graphic_info[graphic].step_delay;
202 toons[i].direction = image->parameter[GFX_ARG_DIRECTION];
203 toons[i].position = image->parameter[GFX_ARG_POSITION];
206 InitToonScreen(bitmap_db_toons,
207 BackToFront, PrepareBackbuffer, ToonNeedsRedraw,
209 REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE,
213 static void InitToonControls()
215 int mode_nr_toons = GAME_MODE_PSEUDO_TOONS;
216 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr_toons];
217 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
218 int mode_nr, anim_nr, part_nr;
219 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
220 int num_toons = MAX_NUM_TOONS;
223 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
224 num_toons = global.num_toons;
226 mode_nr = mode_nr_toons;
227 anim_nr = ctrl->num_anims;
230 anim->mode_nr = mode_nr;
231 anim->control_info = graphic_info[control];
234 anim->part_counter = 0;
235 anim->active_part_nr = 0;
237 anim->has_base = FALSE;
239 anim->init_delay_counter = 0;
241 anim->state = ANIM_STATE_INACTIVE;
245 for (i = 0; i < num_toons; i++)
247 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
248 int graphic = IMG_TOON_1 + i;
249 int control = graphic;
252 part->anim_nr = anim_nr;
253 part->mode_nr = mode_nr;
254 part->graphic = graphic;
255 part->graphic_info = graphic_info[graphic];
256 part->control_info = graphic_info[control];
258 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
260 part->control_info.init_delay_fixed = 0;
261 part->control_info.init_delay_random = 150;
263 part->control_info.x = ARG_UNDEFINED_VALUE;
264 part->control_info.y = ARG_UNDEFINED_VALUE;
266 part->initial_anim_sync_frame = 0;
268 part->step_delay = 0;
269 part->step_delay_value = graphic_info[control].step_delay;
271 part->state = ANIM_STATE_INACTIVE;
272 part->last_anim_status = -1;
281 void InitGlobalAnimControls()
284 int mode_nr, anim_nr, part_nr;
285 int graphic, control;
289 ResetDelayCounter(&anim_sync_frame_delay);
291 for (m = 0; m < NUM_GAME_MODES; m++)
295 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
302 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
304 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
305 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
307 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
309 // if no base animation parameters defined, use default values
310 if (control == IMG_UNDEFINED)
311 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
314 anim->mode_nr = mode_nr;
315 anim->control_info = graphic_info[control];
318 anim->part_counter = 0;
319 anim->active_part_nr = 0;
321 anim->has_base = FALSE;
323 anim->init_delay_counter = 0;
325 anim->state = ANIM_STATE_INACTIVE;
329 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
331 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
333 graphic = global_anim_info[a].graphic[p][m];
334 control = global_anim_info[ctrl_id].graphic[p][m];
336 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
337 control == IMG_UNDEFINED)
341 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
342 m, a, p, mode_nr, anim_nr, part_nr, control);
346 part->anim_nr = anim_nr;
347 part->mode_nr = mode_nr;
348 part->graphic = graphic;
349 part->graphic_info = graphic_info[graphic];
350 part->control_info = graphic_info[control];
352 part->initial_anim_sync_frame = 0;
354 part->step_delay = 0;
355 part->step_delay_value = graphic_info[control].step_delay;
357 part->state = ANIM_STATE_INACTIVE;
358 part->last_anim_status = -1;
360 if (p < GLOBAL_ANIM_ID_PART_BASE)
368 anim->has_base = TRUE;
372 if (anim->num_parts > 0 || anim->has_base)
382 for (i = 0; i < NUM_GAME_MODES; i++)
383 game_mode_anim_classes[i] = ANIM_CLASS_NONE;
384 for (i = 0; game_mode_anim_classes_list[i].game_mode != -1; i++)
385 game_mode_anim_classes[game_mode_anim_classes_list[i].game_mode] =
386 game_mode_anim_classes_list[i].class;
388 for (i = 0; i < NUM_ANIM_CLASSES; i++)
389 anim_class_game_modes[i] = GAME_MODE_DEFAULT;
390 for (i = 0; anim_class_game_modes_list[i].game_mode != -1; i++)
391 anim_class_game_modes[anim_class_game_modes_list[i].class_bit] =
392 anim_class_game_modes_list[i].game_mode;
394 anim_status_last = GAME_MODE_LOADING;
395 anim_classes_last = ANIM_CLASS_NONE;
398 void InitGlobalAnimations()
400 InitGlobalAnimControls();
403 void DrawGlobalAnimExt(int drawing_stage)
405 int anim_classes = game_mode_anim_classes[global.anim_status_next];
409 // start or stop global animations by change of game mode
410 // (special handling of animations for "current screen" and "all screens")
411 if (global.anim_status != anim_status_last)
413 // stop animations for last screen
414 HandleGlobalAnim(ANIM_STOP, anim_status_last);
416 // start animations for current screen
417 HandleGlobalAnim(ANIM_START, global.anim_status);
419 // start animations for all screens after loading new artwork set
420 if (anim_status_last == GAME_MODE_LOADING)
421 HandleGlobalAnim(ANIM_START, GAME_MODE_DEFAULT);
423 anim_status_last = global.anim_status;
426 // start or stop global animations by change of animation class
427 // (generic handling of animations for "class of screens")
428 if (anim_classes != anim_classes_last)
430 for (i = 0; i < NUM_ANIM_CLASSES; i++)
432 int anim_class_check = (1 << i);
433 int anim_class_game_mode = anim_class_game_modes[i];
434 int anim_class_last = anim_classes_last & anim_class_check;
435 int anim_class = anim_classes & anim_class_check;
437 if (anim_class_last && !anim_class)
438 HandleGlobalAnim(ANIM_STOP, anim_class_game_mode);
439 else if (!anim_class_last && anim_class)
440 HandleGlobalAnim(ANIM_START, anim_class_game_mode);
443 anim_classes_last = anim_classes;
446 if (!setup.toons || global.anim_status == GAME_MODE_LOADING)
449 if (drawing_stage == DRAW_GLOBAL_ANIM_STAGE_1)
452 for (mode_nr = 0; mode_nr < NUM_GAME_MODES; mode_nr++)
454 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
458 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
459 mode_nr != game_status)
463 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
465 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
466 struct GraphicInfo *c = &anim->control_info;
467 int part_first, part_last;
470 if (!(anim->state & ANIM_STATE_RUNNING))
473 part_first = part_last = anim->active_part_nr;
475 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
477 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
480 part_last = num_parts - 1;
483 for (part_nr = part_first; part_nr <= part_last; part_nr++)
485 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
486 struct GraphicInfo *g = &part->graphic_info;
489 int width = g->width;
490 int height = g->height;
498 if (!(part->state & ANIM_STATE_RUNNING))
501 if (part->drawing_stage != drawing_stage)
510 else if (part->x > part->viewport_width - g->width)
511 width -= (part->x - (part->viewport_width - g->width));
519 else if (part->y > part->viewport_height - g->height)
520 height -= (part->y - (part->viewport_height - g->height));
522 dst_x += part->viewport_x;
523 dst_y += part->viewport_y;
525 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
526 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
527 g->anim_mode, g->anim_start_frame,
530 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
536 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
543 void DrawGlobalAnim(int drawing_stage)
545 DrawGlobalAnimExt(drawing_stage);
548 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
554 boolean changed = FALSE;
556 if (part->last_anim_status == global.anim_status)
559 part->last_anim_status = global.anim_status;
561 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_1;
563 if (part->control_info.class == get_hash_from_key("window") ||
564 part->control_info.class == get_hash_from_key("border"))
568 viewport_width = WIN_XSIZE;
569 viewport_height = WIN_YSIZE;
571 part->drawing_stage = DRAW_GLOBAL_ANIM_STAGE_2;
573 else if (part->control_info.class == get_hash_from_key("door_1"))
577 viewport_width = DXSIZE;
578 viewport_height = DYSIZE;
580 else if (part->control_info.class == get_hash_from_key("door_2"))
584 viewport_width = VXSIZE;
585 viewport_height = VYSIZE;
587 else // default: "playfield"
589 viewport_x = REAL_SX;
590 viewport_y = REAL_SY;
591 viewport_width = FULL_SXSIZE;
592 viewport_height = FULL_SYSIZE;
595 if (viewport_x != part->viewport_x ||
596 viewport_y != part->viewport_y ||
597 viewport_width != part->viewport_width ||
598 viewport_height != part->viewport_height)
600 part->viewport_x = viewport_x;
601 part->viewport_y = viewport_y;
602 part->viewport_width = viewport_width;
603 part->viewport_height = viewport_height;
611 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
613 struct GraphicInfo *g = &part->graphic_info;
614 struct GraphicInfo *c = &part->control_info;
615 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
617 if (viewport_changed)
618 state |= ANIM_STATE_RESTART;
620 if (state & ANIM_STATE_RESTART)
622 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
624 part->init_delay_counter =
625 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
627 part->anim_delay_counter =
628 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
630 part->initial_anim_sync_frame =
631 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
633 if (c->direction & MV_HORIZONTAL)
635 int pos_bottom = part->viewport_height - g->height;
637 if (c->position == POS_TOP)
639 else if (c->position == POS_UPPER)
640 part->y = GetSimpleRandom(pos_bottom / 2);
641 else if (c->position == POS_MIDDLE)
642 part->y = pos_bottom / 2;
643 else if (c->position == POS_LOWER)
644 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
645 else if (c->position == POS_BOTTOM)
646 part->y = pos_bottom;
648 part->y = GetSimpleRandom(pos_bottom);
650 if (c->direction == MV_RIGHT)
652 part->step_xoffset = c->step_offset;
653 part->x = -g->width + part->step_xoffset;
657 part->step_xoffset = -c->step_offset;
658 part->x = part->viewport_width + part->step_xoffset;
661 part->step_yoffset = 0;
663 else if (c->direction & MV_VERTICAL)
665 int pos_right = part->viewport_width - g->width;
667 if (c->position == POS_LEFT)
669 else if (c->position == POS_RIGHT)
672 part->x = GetSimpleRandom(pos_right);
674 if (c->direction == MV_DOWN)
676 part->step_yoffset = c->step_offset;
677 part->y = -g->height + part->step_yoffset;
681 part->step_yoffset = -c->step_offset;
682 part->y = part->viewport_height + part->step_yoffset;
685 part->step_xoffset = 0;
692 part->step_xoffset = 0;
693 part->step_yoffset = 0;
696 if (c->x != ARG_UNDEFINED_VALUE)
698 if (c->y != ARG_UNDEFINED_VALUE)
701 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
702 part->step_xoffset = c->step_xoffset;
703 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
704 part->step_yoffset = c->step_yoffset;
707 if (part->init_delay_counter > 0)
709 part->init_delay_counter--;
711 return ANIM_STATE_WAITING;
714 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
715 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
716 (part->y <= -g->height && part->step_yoffset <= 0) ||
717 (part->y >= part->viewport_height && part->step_yoffset >= 0))
718 return ANIM_STATE_RESTART;
720 if (part->anim_delay_counter > 0)
722 part->anim_delay_counter--;
724 if (part->anim_delay_counter == 0)
726 part->post_delay_counter =
727 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
729 if (part->post_delay_counter > 0)
730 return ANIM_STATE_RUNNING;
732 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
736 if (part->post_delay_counter > 0)
738 part->post_delay_counter--;
740 if (part->post_delay_counter == 0)
741 return ANIM_STATE_RESTART;
743 return ANIM_STATE_WAITING;
746 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
748 return ANIM_STATE_RUNNING;
752 static unsigned int last_counter = -1;
753 unsigned int counter = Counter();
755 printf("::: NEXT ANIM PART [%d, %d]\n",
756 anim_sync_frame, counter - last_counter);
758 last_counter = counter;
762 part->x += part->step_xoffset;
763 part->y += part->step_yoffset;
765 return ANIM_STATE_RUNNING;
768 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
770 struct GlobalAnimPartControlInfo *part;
771 struct GraphicInfo *c = &anim->control_info;
772 boolean skip = FALSE;
775 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
776 anim->mode_nr, anim->nr, anim->num_parts);
777 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
781 printf("::: %s(%d): %d, %d, %d [%d]\n",
782 (action == ANIM_START ? "ANIM_START" :
783 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
784 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
786 anim->state & ANIM_STATE_RESTART,
787 anim->state & ANIM_STATE_WAITING,
788 anim->state & ANIM_STATE_RUNNING,
795 anim->state = ANIM_STATE_RESTART;
796 anim->part_counter = 0;
797 anim->active_part_nr = 0;
803 if (anim->state == ANIM_STATE_INACTIVE)
809 anim->state = ANIM_STATE_INACTIVE;
818 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
820 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
824 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
825 anim->mode_nr, anim->nr, num_parts);
828 for (i = 0; i < num_parts; i++)
830 part = &anim->part[i];
835 anim->state = ANIM_STATE_RUNNING;
836 part->state = ANIM_STATE_RESTART;
842 if (part->state == ANIM_STATE_INACTIVE)
848 part->state = ANIM_STATE_INACTIVE;
860 part->state = HandleGlobalAnim_Part(part, part->state);
869 if (anim->state & ANIM_STATE_RESTART) // directly after restart
870 anim->active_part_nr = getGlobalAnimationPart(anim);
872 part = &anim->part[anim->active_part_nr];
874 part->state = ANIM_STATE_RUNNING;
876 anim->state = HandleGlobalAnim_Part(part, anim->state);
878 if (anim->state & ANIM_STATE_RESTART)
879 anim->part_counter++;
882 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
887 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
888 ctrl->nr, ctrl->num_anims);
891 for (i = 0; i < ctrl->num_anims; i++)
892 HandleGlobalAnim_Main(&ctrl->anim[i], action);
895 static void HandleGlobalAnim(int action, int game_mode)
898 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
901 HandleGlobalAnim_Mode(&global_anim_ctrl[game_mode], action);
912 static void DoAnimationExt()
917 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
921 WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
924 if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
928 for (i = 0; i < NUM_GAME_MODES; i++)
929 HandleGlobalAnim(ANIM_CONTINUE, i);
932 // force screen redraw in next frame to continue drawing global animations
933 redraw_mask = REDRAW_ALL;
939 // HandleAnimation(ANIM_CONTINUE);
942 // force screen redraw in next frame to continue drawing global animations
943 redraw_mask = REDRAW_ALL;