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;
35 unsigned int initial_anim_sync_frame;
36 unsigned int step_frames, step_frames_value;
37 unsigned int step_delay, step_delay_value;
39 unsigned int init_delay, init_delay_value;
40 unsigned int anim_delay, anim_delay_value;
41 unsigned int post_delay, post_delay_value;
46 struct GlobalAnimMainControlInfo
48 struct GlobalAnimPartControlInfo base;
49 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
54 struct GraphicInfo control_info;
62 unsigned int init_delay, init_delay_value;
67 struct GlobalAnimControlInfo
69 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
76 /* forward declaration for internal use */
77 static void DoAnimationExt(void);
79 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_SPECIAL_GFX_ARGS];
80 static struct ToonInfo toons[MAX_NUM_TOONS];
82 static unsigned int anim_sync_frame = 0;
83 static unsigned int anim_sync_frame_delay = 0;
84 static unsigned int anim_sync_frame_delay_value = GAME_FRAME_DELAY;
86 static boolean do_animations = FALSE;
89 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
91 struct GraphicInfo *c = &anim->control_info;
92 int last_anim_random_frame = gfx.anim_random_frame;
95 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
97 part_nr = getAnimationFrame(anim->num_parts, 1,
98 c->anim_mode, c->anim_start_frame,
101 gfx.anim_random_frame = last_anim_random_frame;
106 static void PrepareBackbuffer()
108 if (game_status != GAME_MODE_PLAYING)
111 BlitScreenToBitmap(backbuffer);
114 boolean ToonNeedsRedraw()
121 int num_toons = MAX_NUM_TOONS;
124 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
125 num_toons = global.num_toons;
127 for (i = 0; i < num_toons; i++)
129 int graphic = IMG_TOON_1 + i;
130 struct FileInfo *image = getImageListEntryFromImageID(graphic);
132 toons[i].bitmap = graphic_info[graphic].bitmap;
134 toons[i].src_x = graphic_info[graphic].src_x;
135 toons[i].src_y = graphic_info[graphic].src_y;
137 toons[i].width = graphic_info[graphic].width;
138 toons[i].height = graphic_info[graphic].height;
140 toons[i].anim_frames = graphic_info[graphic].anim_frames;
141 toons[i].anim_delay = graphic_info[graphic].anim_delay;
142 toons[i].anim_mode = graphic_info[graphic].anim_mode;
143 toons[i].anim_start_frame = graphic_info[graphic].anim_start_frame;
145 toons[i].step_offset = graphic_info[graphic].step_offset;
146 toons[i].step_delay = graphic_info[graphic].step_delay;
148 toons[i].direction = image->parameter[GFX_ARG_DIRECTION];
149 toons[i].position = image->parameter[GFX_ARG_POSITION];
152 InitToonScreen(bitmap_db_toons,
153 BackToFront, PrepareBackbuffer, ToonNeedsRedraw,
155 REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE,
159 static void InitToonControls()
161 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_DEFAULT];
162 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
163 int mode_nr, anim_nr, part_nr;
164 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
165 int num_toons = MAX_NUM_TOONS;
168 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
169 num_toons = global.num_toons;
171 mode_nr = GAME_MODE_DEFAULT;
172 anim_nr = ctrl->num_anims;
175 anim->mode_nr = mode_nr;
176 anim->control_info = graphic_info[control];
179 anim->part_counter = 0;
180 anim->active_part_nr = 0;
182 anim->has_base = FALSE;
184 anim->init_delay = 0;
185 anim->init_delay_value = 0;
187 anim->state = ANIM_STATE_INACTIVE;
191 for (i = 0; i < num_toons; i++)
193 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
194 int graphic = IMG_TOON_1 + i;
195 int control = graphic;
198 part->anim_nr = anim_nr;
199 part->mode_nr = mode_nr;
200 part->graphic = graphic;
201 part->graphic_info = graphic_info[graphic];
202 part->control_info = graphic_info[control];
204 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
206 part->control_info.init_delay_fixed = 0;
207 part->control_info.init_delay_random = 150;
209 part->control_info.x = ARG_UNDEFINED_VALUE;
210 part->control_info.y = ARG_UNDEFINED_VALUE;
212 part->step_frames = 0;
213 part->step_frames_value = graphic_info[control].step_frames;
215 part->step_delay = 0;
216 part->step_delay_value = graphic_info[control].step_delay;
218 part->state = ANIM_STATE_INACTIVE;
227 void InitGlobalAnimControls()
230 int mode_nr, anim_nr, part_nr;
231 int graphic, control;
235 ResetDelayCounter(&anim_sync_frame_delay);
237 for (m = 0; m < NUM_SPECIAL_GFX_ARGS; m++)
241 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
248 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
250 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
251 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
253 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
255 // if no base animation parameters defined, use default values
256 if (control == IMG_UNDEFINED)
257 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
260 anim->mode_nr = mode_nr;
261 anim->control_info = graphic_info[control];
264 anim->part_counter = 0;
265 anim->active_part_nr = 0;
267 anim->has_base = FALSE;
269 anim->init_delay = 0;
270 anim->init_delay_value = 0;
272 anim->state = ANIM_STATE_INACTIVE;
276 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
278 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
280 graphic = global_anim_info[a].graphic[p][m];
281 control = global_anim_info[ctrl_id].graphic[p][m];
283 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
284 control == IMG_UNDEFINED)
288 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
289 m, a, p, mode_nr, anim_nr, part_nr, control);
293 part->anim_nr = anim_nr;
294 part->mode_nr = mode_nr;
295 part->graphic = graphic;
296 part->graphic_info = graphic_info[graphic];
297 part->control_info = graphic_info[control];
299 part->step_frames = 0;
300 part->step_frames_value = graphic_info[control].step_frames;
302 part->step_delay = 0;
303 part->step_delay_value = graphic_info[control].step_delay;
305 part->state = ANIM_STATE_INACTIVE;
307 if (p < GLOBAL_ANIM_ID_PART_BASE)
315 anim->has_base = TRUE;
319 if (anim->num_parts > 0 || anim->has_base)
330 void DrawGlobalAnim()
334 if (game_status == GAME_MODE_LOADING)
335 do_animations = FALSE;
337 if (!do_animations || !setup.toons)
342 for (mode_nr = 0; mode_nr < NUM_SPECIAL_GFX_ARGS; mode_nr++)
344 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
347 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
348 mode_nr != game_status)
351 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
353 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
354 struct GraphicInfo *c = &anim->control_info;
355 int part_first, part_last;
358 if (anim->state != ANIM_STATE_RUNNING)
361 part_first = part_last = anim->active_part_nr;
363 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
365 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
368 part_last = num_parts - 1;
371 for (part_nr = part_first; part_nr <= part_last; part_nr++)
373 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
374 struct GraphicInfo *g = &part->graphic_info;
377 int width = g->width;
378 int height = g->height;
386 if (part->state != ANIM_STATE_RUNNING)
395 else if (part->x > FULL_SXSIZE - g->width)
396 width -= (part->x - (FULL_SXSIZE - g->width));
404 else if (part->y > FULL_SYSIZE - g->height)
405 height -= (part->y - (FULL_SYSIZE - g->height));
410 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
411 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
412 g->anim_mode, g->anim_start_frame,
415 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
421 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
428 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
430 struct GraphicInfo *g = &part->graphic_info;
431 struct GraphicInfo *c = &part->control_info;
433 if (state & ANIM_STATE_RESTART)
435 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
437 part->initial_anim_sync_frame = (g->anim_global_sync ? 0 : anim_sync_frame);
438 part->step_frames = 0;
440 if (c->direction & MV_HORIZONTAL)
442 int pos_bottom = FULL_SYSIZE - g->height;
444 if (c->position == POS_TOP)
446 else if (c->position == POS_UPPER)
447 part->y = GetSimpleRandom(pos_bottom / 2);
448 else if (c->position == POS_MIDDLE)
449 part->y = pos_bottom / 2;
450 else if (c->position == POS_LOWER)
451 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
452 else if (c->position == POS_BOTTOM)
453 part->y = pos_bottom;
455 part->y = GetSimpleRandom(pos_bottom);
457 if (c->direction == MV_RIGHT)
459 part->step_xoffset = c->step_offset;
460 part->x = -g->width + part->step_xoffset;
464 part->step_xoffset = -c->step_offset;
465 part->x = FULL_SXSIZE + part->step_xoffset;
468 part->step_yoffset = 0;
470 else if (c->direction & MV_VERTICAL)
472 int pos_right = FULL_SXSIZE - g->width;
474 if (c->position == POS_LEFT)
476 else if (c->position == POS_RIGHT)
479 part->x = GetSimpleRandom(pos_right);
481 if (c->direction == MV_DOWN)
483 part->step_yoffset = c->step_offset;
484 part->y = -g->height + part->step_yoffset;
488 part->step_yoffset = -c->step_offset;
489 part->y = FULL_SYSIZE + part->step_yoffset;
492 part->step_xoffset = 0;
499 part->step_xoffset = 0;
500 part->step_yoffset = 0;
503 if (c->x != ARG_UNDEFINED_VALUE)
505 if (c->y != ARG_UNDEFINED_VALUE)
508 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
509 part->step_xoffset = c->step_xoffset;
510 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
511 part->step_yoffset = c->step_yoffset;
514 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
515 (part->x >= FULL_SXSIZE && part->step_xoffset >= 0) ||
516 (part->y <= -g->height && part->step_yoffset <= 0) ||
517 (part->y >= FULL_SYSIZE && part->step_yoffset >= 0))
518 return ANIM_STATE_RESTART;
520 if (part->step_frames_value != ARG_UNDEFINED_VALUE &&
521 part->step_frames >= part->step_frames_value)
522 return ANIM_STATE_RESTART;
524 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
526 return ANIM_STATE_RUNNING;
530 static unsigned int last_counter = -1;
531 unsigned int counter = Counter();
533 printf("::: NEXT ANIM PART [%d, %d]\n",
534 anim_sync_frame, counter - last_counter);
536 last_counter = counter;
540 part->x += part->step_xoffset;
541 part->y += part->step_yoffset;
545 return ANIM_STATE_RUNNING;
548 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
550 struct GlobalAnimPartControlInfo *part;
551 struct GraphicInfo *c = &anim->control_info;
552 struct GraphicInfo *cp;
553 boolean skip = FALSE;
556 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
557 anim->mode_nr, anim->nr, anim->num_parts);
558 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
562 printf("::: %s(%d): %d, %d, %d [%d]\n",
563 (action == ANIM_START ? "ANIM_START" :
564 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
565 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
567 anim->state & ANIM_STATE_RESTART,
568 anim->state & ANIM_STATE_WAITING,
569 anim->state & ANIM_STATE_RUNNING,
576 anim->state = ANIM_STATE_RESTART;
577 anim->part_counter = 0;
578 anim->active_part_nr = 0;
584 if (anim->state == ANIM_STATE_INACTIVE)
590 anim->state = ANIM_STATE_INACTIVE;
599 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
601 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
605 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
606 anim->mode_nr, anim->nr, num_parts);
609 for (i = 0; i < num_parts; i++)
611 part = &anim->part[i];
612 cp = &part->control_info;
617 anim->state = ANIM_STATE_RUNNING;
618 part->state = ANIM_STATE_RESTART;
624 if (part->state == ANIM_STATE_INACTIVE)
630 part->state = ANIM_STATE_INACTIVE;
642 if (part->state & ANIM_STATE_RESTART)
645 printf("::: RESTART %d.%d\n", part->anim_nr, part->nr);
648 if (!(part->state & ANIM_STATE_WAITING))
651 printf("::: WAITING %d.%d\n", part->anim_nr, part->nr);
654 ResetDelayCounterExt(&part->init_delay, anim_sync_frame);
656 part->init_delay_value =
657 (cp->init_delay_fixed + GetSimpleRandom(cp->init_delay_random));
659 part->state |= ANIM_STATE_WAITING;
662 if (!DelayReachedExt(&part->init_delay, part->init_delay_value,
667 printf("::: RUNNING %d.%d\n", part->anim_nr, part->nr);
670 part->state = ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
674 printf("::: DO PART %d.%d [%d, %d, %d] [%d]\n", part->anim_nr, part->nr,
675 part->restart, part->waiting, part->running,
679 part->state = HandleGlobalAnim_Part(part, part->state);
688 if (anim->state == ANIM_STATE_RESTART) // directly after restart
689 anim->active_part_nr = getGlobalAnimationPart(anim);
691 part = &anim->part[anim->active_part_nr];
693 if (anim->state & ANIM_STATE_RESTART)
695 if (!(anim->state & ANIM_STATE_WAITING))
697 cp = &part->control_info;
699 ResetDelayCounterExt(&part->init_delay, anim_sync_frame);
701 part->init_delay_value =
702 (cp->init_delay_fixed + GetSimpleRandom(cp->init_delay_random));
704 anim->state |= ANIM_STATE_WAITING;
707 if (!DelayReachedExt(&part->init_delay, part->init_delay_value,
711 anim->state = ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
714 part->state = ANIM_STATE_RUNNING;
716 anim->state = HandleGlobalAnim_Part(part, anim->state);
718 if (anim->state == ANIM_STATE_RESTART)
719 anim->part_counter++;
722 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
727 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
728 ctrl->nr, ctrl->num_anims);
731 for (i = 0; i < ctrl->num_anims; i++)
732 HandleGlobalAnim_Main(&ctrl->anim[i], action);
735 void HandleGlobalAnim(int action)
738 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
741 HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
742 HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
747 // HandleAnimation(ANIM_START);
750 printf("::: InitAnimation\n");
755 InitGlobalAnimControls();
757 HandleGlobalAnim(ANIM_START);
759 do_animations = TRUE;
764 // HandleAnimation(ANIM_STOP);
767 printf("::: StopAnimation\n");
770 HandleGlobalAnim(ANIM_STOP);
772 do_animations = FALSE;
775 static void DoAnimationExt()
778 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
782 WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
785 if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
789 HandleGlobalAnim(ANIM_CONTINUE);
792 // force screen redraw in next frame to continue drawing global animations
793 redraw_mask = REDRAW_ALL;
799 // HandleAnimation(ANIM_CONTINUE);
802 // force screen redraw in next frame to continue drawing global animations
803 redraw_mask = REDRAW_ALL;