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 animation definition */
18 #define NUM_GLOBAL_ANIMS_AND_TOONS (NUM_GLOBAL_ANIMS + 1)
19 #define NUM_GLOBAL_ANIM_PARTS_AND_TOONS MAX(NUM_GLOBAL_ANIM_PARTS_ALL, \
22 struct GlobalAnimPartControlInfo
29 struct GraphicInfo graphic_info;
30 struct GraphicInfo control_info;
33 int step_xoffset, step_yoffset;
39 unsigned int initial_anim_sync_frame;
40 unsigned int step_frames, step_frames_value;
41 unsigned int step_delay, step_delay_value;
43 unsigned int init_delay, init_delay_value;
44 unsigned int anim_delay, anim_delay_value;
45 unsigned int post_delay, post_delay_value;
48 struct GlobalAnimMainControlInfo
50 struct GlobalAnimPartControlInfo base;
51 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
56 struct GraphicInfo control_info;
68 unsigned int init_delay, init_delay_value;
71 struct GlobalAnimControlInfo
73 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
80 /* forward declaration for internal use */
81 static void DoAnimationExt(void);
83 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_SPECIAL_GFX_ARGS];
84 static struct ToonInfo toons[MAX_NUM_TOONS];
86 static unsigned int anim_sync_frame = 0;
87 static unsigned int anim_sync_frame_delay = 0;
88 static unsigned int anim_sync_frame_delay_value = GAME_FRAME_DELAY;
90 static boolean do_animations = FALSE;
93 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
95 struct GraphicInfo *c = &anim->control_info;
96 int last_anim_random_frame = gfx.anim_random_frame;
99 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
101 part_nr = getAnimationFrame(anim->num_parts, 1,
102 c->anim_mode, c->anim_start_frame,
105 gfx.anim_random_frame = last_anim_random_frame;
110 static void PrepareBackbuffer()
112 if (game_status != GAME_MODE_PLAYING)
115 BlitScreenToBitmap(backbuffer);
118 boolean ToonNeedsRedraw()
125 int num_toons = MAX_NUM_TOONS;
128 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
129 num_toons = global.num_toons;
131 for (i = 0; i < num_toons; i++)
133 int graphic = IMG_TOON_1 + i;
134 struct FileInfo *image = getImageListEntryFromImageID(graphic);
136 toons[i].bitmap = graphic_info[graphic].bitmap;
138 toons[i].src_x = graphic_info[graphic].src_x;
139 toons[i].src_y = graphic_info[graphic].src_y;
141 toons[i].width = graphic_info[graphic].width;
142 toons[i].height = graphic_info[graphic].height;
144 toons[i].anim_frames = graphic_info[graphic].anim_frames;
145 toons[i].anim_delay = graphic_info[graphic].anim_delay;
146 toons[i].anim_mode = graphic_info[graphic].anim_mode;
147 toons[i].anim_start_frame = graphic_info[graphic].anim_start_frame;
149 toons[i].step_offset = graphic_info[graphic].step_offset;
150 toons[i].step_delay = graphic_info[graphic].step_delay;
152 toons[i].direction = image->parameter[GFX_ARG_DIRECTION];
153 toons[i].position = image->parameter[GFX_ARG_POSITION];
156 InitToonScreen(bitmap_db_toons,
157 BackToFront, PrepareBackbuffer, ToonNeedsRedraw,
159 REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE,
163 static void InitToonControls()
165 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_DEFAULT];
166 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
167 int mode_nr, anim_nr, part_nr;
168 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
169 int num_toons = MAX_NUM_TOONS;
172 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
173 num_toons = global.num_toons;
175 mode_nr = GAME_MODE_DEFAULT;
176 anim_nr = ctrl->num_anims;
179 anim->mode_nr = mode_nr;
180 anim->control_info = graphic_info[control];
183 anim->part_counter = 0;
184 anim->active_part_nr = 0;
186 anim->has_base = FALSE;
188 anim->restart = FALSE;
189 anim->waiting = FALSE;
190 anim->running = FALSE;
192 anim->init_delay = 0;
193 anim->init_delay_value = 0;
197 for (i = 0; i < num_toons; i++)
199 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
200 int graphic = IMG_TOON_1 + i;
201 int control = graphic;
204 part->anim_nr = anim_nr;
205 part->mode_nr = mode_nr;
206 part->graphic = graphic;
207 part->graphic_info = graphic_info[graphic];
208 part->control_info = graphic_info[control];
210 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
212 part->control_info.init_delay_fixed = 0;
213 part->control_info.init_delay_random = 150;
215 part->control_info.x = ARG_UNDEFINED_VALUE;
216 part->control_info.y = ARG_UNDEFINED_VALUE;
218 part->restart = FALSE;
219 part->waiting = FALSE;
220 part->running = FALSE;
222 part->step_frames = 0;
223 part->step_frames_value = graphic_info[control].step_frames;
225 part->step_delay = 0;
226 part->step_delay_value = graphic_info[control].step_delay;
235 void InitGlobalAnimControls()
238 int mode_nr, anim_nr, part_nr;
239 int graphic, control;
243 ResetDelayCounter(&anim_sync_frame_delay);
245 for (m = 0; m < NUM_SPECIAL_GFX_ARGS; m++)
249 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
256 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
258 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
259 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
261 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
263 // if no base animation parameters defined, use default values
264 if (control == IMG_UNDEFINED)
265 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
268 anim->mode_nr = mode_nr;
269 anim->control_info = graphic_info[control];
272 anim->part_counter = 0;
273 anim->active_part_nr = 0;
275 anim->has_base = FALSE;
277 anim->restart = FALSE;
278 anim->waiting = FALSE;
279 anim->running = FALSE;
281 anim->init_delay = 0;
282 anim->init_delay_value = 0;
286 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
288 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
290 graphic = global_anim_info[a].graphic[p][m];
291 control = global_anim_info[ctrl_id].graphic[p][m];
293 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
294 control == IMG_UNDEFINED)
298 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
299 m, a, p, mode_nr, anim_nr, part_nr, control);
303 part->anim_nr = anim_nr;
304 part->mode_nr = mode_nr;
305 part->graphic = graphic;
306 part->graphic_info = graphic_info[graphic];
307 part->control_info = graphic_info[control];
309 part->restart = FALSE;
310 part->waiting = FALSE;
311 part->running = FALSE;
313 part->step_frames = 0;
314 part->step_frames_value = graphic_info[control].step_frames;
316 part->step_delay = 0;
317 part->step_delay_value = graphic_info[control].step_delay;
319 if (p < GLOBAL_ANIM_ID_PART_BASE)
327 anim->has_base = TRUE;
331 if (anim->num_parts > 0 || anim->has_base)
342 void DrawGlobalAnim()
346 if (game_status == GAME_MODE_LOADING)
347 do_animations = FALSE;
349 if (!do_animations || !setup.toons)
354 for (mode_nr = 0; mode_nr < NUM_SPECIAL_GFX_ARGS; mode_nr++)
356 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
359 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
360 mode_nr != game_status)
363 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
365 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
366 struct GraphicInfo *c = &anim->control_info;
367 int part_first, part_last;
373 part_first = part_last = anim->active_part_nr;
375 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
377 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
380 part_last = num_parts - 1;
383 for (part_nr = part_first; part_nr <= part_last; part_nr++)
385 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
386 struct GraphicInfo *g = &part->graphic_info;
389 int width = g->width;
390 int height = g->height;
407 else if (part->x > FULL_SXSIZE - g->width)
408 width -= (part->x - (FULL_SXSIZE - g->width));
416 else if (part->y > FULL_SYSIZE - g->height)
417 height -= (part->y - (FULL_SYSIZE - g->height));
422 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
423 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
424 g->anim_mode, g->anim_start_frame,
427 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
433 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
440 boolean HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part,
443 struct GraphicInfo *g = &part->graphic_info;
444 struct GraphicInfo *c = &part->control_info;
448 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
450 part->initial_anim_sync_frame = (g->anim_global_sync ? 0 : anim_sync_frame);
451 part->step_frames = 0;
453 if (c->direction & MV_HORIZONTAL)
455 int pos_bottom = FULL_SYSIZE - g->height;
457 if (c->position == POS_TOP)
459 else if (c->position == POS_UPPER)
460 part->y = GetSimpleRandom(pos_bottom / 2);
461 else if (c->position == POS_MIDDLE)
462 part->y = pos_bottom / 2;
463 else if (c->position == POS_LOWER)
464 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
465 else if (c->position == POS_BOTTOM)
466 part->y = pos_bottom;
468 part->y = GetSimpleRandom(pos_bottom);
470 if (c->direction == MV_RIGHT)
472 part->step_xoffset = c->step_offset;
473 part->x = -g->width + part->step_xoffset;
477 part->step_xoffset = -c->step_offset;
478 part->x = FULL_SXSIZE + part->step_xoffset;
481 part->step_yoffset = 0;
483 else if (c->direction & MV_VERTICAL)
485 int pos_right = FULL_SXSIZE - g->width;
487 if (c->position == POS_LEFT)
489 else if (c->position == POS_RIGHT)
492 part->x = GetSimpleRandom(pos_right);
494 if (c->direction == MV_DOWN)
496 part->step_yoffset = c->step_offset;
497 part->y = -g->height + part->step_yoffset;
501 part->step_yoffset = -c->step_offset;
502 part->y = FULL_SYSIZE + part->step_yoffset;
505 part->step_xoffset = 0;
512 part->step_xoffset = 0;
513 part->step_yoffset = 0;
516 if (c->x != ARG_UNDEFINED_VALUE)
518 if (c->y != ARG_UNDEFINED_VALUE)
521 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
522 part->step_xoffset = c->step_xoffset;
523 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
524 part->step_yoffset = c->step_yoffset;
527 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
528 (part->x >= FULL_SXSIZE && part->step_xoffset >= 0) ||
529 (part->y <= -g->height && part->step_yoffset <= 0) ||
530 (part->y >= FULL_SYSIZE && part->step_yoffset >= 0))
533 if (part->step_frames_value != ARG_UNDEFINED_VALUE &&
534 part->step_frames >= part->step_frames_value)
537 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
543 static unsigned int last_counter = -1;
544 unsigned int counter = Counter();
546 printf("::: NEXT ANIM PART [%d, %d]\n",
547 anim_sync_frame, counter - last_counter);
549 last_counter = counter;
553 part->x += part->step_xoffset;
554 part->y += part->step_yoffset;
561 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
563 struct GlobalAnimPartControlInfo *part;
564 struct GraphicInfo *c = &anim->control_info;
565 struct GraphicInfo *cp;
566 boolean skip = FALSE;
569 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
570 anim->mode_nr, anim->nr, anim->num_parts);
571 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
575 printf("::: %s(%d): %d, %d, %d [%d]\n",
576 (action == ANIM_START ? "ANIM_START" :
577 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
578 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
580 anim->restart, anim->waiting, anim->running,
587 anim->restart = TRUE;
588 anim->waiting = FALSE;
589 anim->running = FALSE;
590 anim->part_counter = 0;
591 anim->active_part_nr = 0;
597 if (!anim->restart &&
605 anim->restart = FALSE;
606 anim->waiting = FALSE;
607 anim->running = FALSE;
616 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
618 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
622 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
623 anim->mode_nr, anim->nr, num_parts);
626 for (i = 0; i < num_parts; i++)
628 part = &anim->part[i];
629 cp = &part->control_info;
634 anim->running = TRUE;
636 part->restart = TRUE;
637 part->waiting = FALSE;
638 part->running = FALSE;
644 if (!part->restart &&
652 part->restart = FALSE;
653 part->waiting = FALSE;
654 part->running = FALSE;
669 printf("::: RESTART %d.%d\n", part->anim_nr, part->nr);
675 printf("::: WAITING %d.%d\n", part->anim_nr, part->nr);
678 ResetDelayCounterExt(&part->init_delay, anim_sync_frame);
680 part->init_delay_value =
681 (cp->init_delay_fixed + GetSimpleRandom(cp->init_delay_random));
683 part->waiting = TRUE;
686 if (!DelayReachedExt(&part->init_delay, part->init_delay_value,
691 printf("::: RUNNING %d.%d\n", part->anim_nr, part->nr);
694 part->waiting = FALSE;
695 part->running = TRUE;
699 printf("::: DO PART %d.%d [%d, %d, %d] [%d]\n", part->anim_nr, part->nr,
700 part->restart, part->waiting, part->running,
704 part->restart = HandleGlobalAnim_Part(part, part->restart);
707 part->running = FALSE;
716 if (anim->restart && !anim->waiting) // directly after restart
717 anim->active_part_nr = getGlobalAnimationPart(anim);
719 part = &anim->part[anim->active_part_nr];
725 cp = &part->control_info;
727 ResetDelayCounterExt(&part->init_delay, anim_sync_frame);
729 part->init_delay_value =
730 (cp->init_delay_fixed + GetSimpleRandom(cp->init_delay_random));
732 anim->waiting = TRUE;
735 if (!DelayReachedExt(&part->init_delay, part->init_delay_value,
739 anim->waiting = FALSE;
740 anim->running = TRUE;
743 part->running = TRUE;
745 anim->restart = HandleGlobalAnim_Part(part, anim->restart);
749 anim->running = FALSE;
751 anim->part_counter++;
755 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
760 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
761 ctrl->nr, ctrl->num_anims);
764 for (i = 0; i < ctrl->num_anims; i++)
765 HandleGlobalAnim_Main(&ctrl->anim[i], action);
768 void HandleGlobalAnim(int action)
771 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
774 HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
775 HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
780 // HandleAnimation(ANIM_START);
783 printf("::: InitAnimation\n");
788 InitGlobalAnimControls();
790 HandleGlobalAnim(ANIM_START);
792 do_animations = TRUE;
797 // HandleAnimation(ANIM_STOP);
800 printf("::: StopAnimation\n");
803 HandleGlobalAnim(ANIM_STOP);
805 do_animations = FALSE;
808 static void DoAnimationExt()
811 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
815 WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
818 if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
822 HandleGlobalAnim(ANIM_CONTINUE);
825 // force screen redraw in next frame to continue drawing global animations
826 redraw_mask = REDRAW_ALL;
832 // HandleAnimation(ANIM_CONTINUE);
835 // force screen redraw in next frame to continue drawing global animations
836 redraw_mask = REDRAW_ALL;