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_delay, step_delay_value;
38 int init_delay_counter;
39 int anim_delay_counter;
40 int post_delay_counter;
45 struct GlobalAnimMainControlInfo
47 struct GlobalAnimPartControlInfo base;
48 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
53 struct GraphicInfo control_info;
61 int init_delay_counter;
66 struct GlobalAnimControlInfo
68 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
75 /* forward declaration for internal use */
76 static void DoAnimationExt(void);
78 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_SPECIAL_GFX_ARGS];
79 static struct ToonInfo toons[MAX_NUM_TOONS];
81 static unsigned int anim_sync_frame = 0;
82 static unsigned int anim_sync_frame_delay = 0;
83 static unsigned int anim_sync_frame_delay_value = GAME_FRAME_DELAY;
85 static boolean do_animations = FALSE;
88 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
90 struct GraphicInfo *c = &anim->control_info;
91 int last_anim_random_frame = gfx.anim_random_frame;
94 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
96 part_nr = getAnimationFrame(anim->num_parts, 1,
97 c->anim_mode, c->anim_start_frame,
100 gfx.anim_random_frame = last_anim_random_frame;
105 static void PrepareBackbuffer()
107 if (game_status != GAME_MODE_PLAYING)
110 BlitScreenToBitmap(backbuffer);
113 boolean ToonNeedsRedraw()
120 int num_toons = MAX_NUM_TOONS;
123 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
124 num_toons = global.num_toons;
126 for (i = 0; i < num_toons; i++)
128 int graphic = IMG_TOON_1 + i;
129 struct FileInfo *image = getImageListEntryFromImageID(graphic);
131 toons[i].bitmap = graphic_info[graphic].bitmap;
133 toons[i].src_x = graphic_info[graphic].src_x;
134 toons[i].src_y = graphic_info[graphic].src_y;
136 toons[i].width = graphic_info[graphic].width;
137 toons[i].height = graphic_info[graphic].height;
139 toons[i].anim_frames = graphic_info[graphic].anim_frames;
140 toons[i].anim_delay = graphic_info[graphic].anim_delay;
141 toons[i].anim_mode = graphic_info[graphic].anim_mode;
142 toons[i].anim_start_frame = graphic_info[graphic].anim_start_frame;
144 toons[i].step_offset = graphic_info[graphic].step_offset;
145 toons[i].step_delay = graphic_info[graphic].step_delay;
147 toons[i].direction = image->parameter[GFX_ARG_DIRECTION];
148 toons[i].position = image->parameter[GFX_ARG_POSITION];
151 InitToonScreen(bitmap_db_toons,
152 BackToFront, PrepareBackbuffer, ToonNeedsRedraw,
154 REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE,
158 static void InitToonControls()
160 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_DEFAULT];
161 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
162 int mode_nr, anim_nr, part_nr;
163 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
164 int num_toons = MAX_NUM_TOONS;
167 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
168 num_toons = global.num_toons;
170 mode_nr = GAME_MODE_DEFAULT;
171 anim_nr = ctrl->num_anims;
174 anim->mode_nr = mode_nr;
175 anim->control_info = graphic_info[control];
178 anim->part_counter = 0;
179 anim->active_part_nr = 0;
181 anim->has_base = FALSE;
183 anim->init_delay_counter = 0;
185 anim->state = ANIM_STATE_INACTIVE;
189 for (i = 0; i < num_toons; i++)
191 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
192 int graphic = IMG_TOON_1 + i;
193 int control = graphic;
196 part->anim_nr = anim_nr;
197 part->mode_nr = mode_nr;
198 part->graphic = graphic;
199 part->graphic_info = graphic_info[graphic];
200 part->control_info = graphic_info[control];
202 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
204 part->control_info.init_delay_fixed = 0;
205 part->control_info.init_delay_random = 150;
207 part->control_info.x = ARG_UNDEFINED_VALUE;
208 part->control_info.y = ARG_UNDEFINED_VALUE;
210 part->step_delay = 0;
211 part->step_delay_value = graphic_info[control].step_delay;
213 part->state = ANIM_STATE_INACTIVE;
222 void InitGlobalAnimControls()
225 int mode_nr, anim_nr, part_nr;
226 int graphic, control;
230 ResetDelayCounter(&anim_sync_frame_delay);
232 for (m = 0; m < NUM_SPECIAL_GFX_ARGS; m++)
236 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
243 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
245 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
246 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
248 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
250 // if no base animation parameters defined, use default values
251 if (control == IMG_UNDEFINED)
252 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
255 anim->mode_nr = mode_nr;
256 anim->control_info = graphic_info[control];
259 anim->part_counter = 0;
260 anim->active_part_nr = 0;
262 anim->has_base = FALSE;
264 anim->init_delay_counter = 0;
266 anim->state = ANIM_STATE_INACTIVE;
270 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
272 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
274 graphic = global_anim_info[a].graphic[p][m];
275 control = global_anim_info[ctrl_id].graphic[p][m];
277 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
278 control == IMG_UNDEFINED)
282 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
283 m, a, p, mode_nr, anim_nr, part_nr, control);
287 part->anim_nr = anim_nr;
288 part->mode_nr = mode_nr;
289 part->graphic = graphic;
290 part->graphic_info = graphic_info[graphic];
291 part->control_info = graphic_info[control];
293 part->step_delay = 0;
294 part->step_delay_value = graphic_info[control].step_delay;
296 part->state = ANIM_STATE_INACTIVE;
298 if (p < GLOBAL_ANIM_ID_PART_BASE)
306 anim->has_base = TRUE;
310 if (anim->num_parts > 0 || anim->has_base)
321 void DrawGlobalAnim()
325 if (game_status == GAME_MODE_LOADING)
326 do_animations = FALSE;
328 if (!do_animations || !setup.toons)
333 for (mode_nr = 0; mode_nr < NUM_SPECIAL_GFX_ARGS; mode_nr++)
335 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
338 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
339 mode_nr != game_status)
342 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
344 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
345 struct GraphicInfo *c = &anim->control_info;
346 int part_first, part_last;
349 if (!(anim->state & ANIM_STATE_RUNNING))
352 part_first = part_last = anim->active_part_nr;
354 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
356 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
359 part_last = num_parts - 1;
362 for (part_nr = part_first; part_nr <= part_last; part_nr++)
364 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
365 struct GraphicInfo *g = &part->graphic_info;
368 int width = g->width;
369 int height = g->height;
377 if (!(part->state & ANIM_STATE_RUNNING))
386 else if (part->x > FULL_SXSIZE - g->width)
387 width -= (part->x - (FULL_SXSIZE - g->width));
395 else if (part->y > FULL_SYSIZE - g->height)
396 height -= (part->y - (FULL_SYSIZE - g->height));
401 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
402 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
403 g->anim_mode, g->anim_start_frame,
406 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
412 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
419 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
421 struct GraphicInfo *g = &part->graphic_info;
422 struct GraphicInfo *c = &part->control_info;
424 if (state & ANIM_STATE_RESTART)
426 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
428 part->init_delay_counter =
429 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
431 part->anim_delay_counter =
432 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
434 part->initial_anim_sync_frame =
435 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
437 if (c->direction & MV_HORIZONTAL)
439 int pos_bottom = FULL_SYSIZE - g->height;
441 if (c->position == POS_TOP)
443 else if (c->position == POS_UPPER)
444 part->y = GetSimpleRandom(pos_bottom / 2);
445 else if (c->position == POS_MIDDLE)
446 part->y = pos_bottom / 2;
447 else if (c->position == POS_LOWER)
448 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
449 else if (c->position == POS_BOTTOM)
450 part->y = pos_bottom;
452 part->y = GetSimpleRandom(pos_bottom);
454 if (c->direction == MV_RIGHT)
456 part->step_xoffset = c->step_offset;
457 part->x = -g->width + part->step_xoffset;
461 part->step_xoffset = -c->step_offset;
462 part->x = FULL_SXSIZE + part->step_xoffset;
465 part->step_yoffset = 0;
467 else if (c->direction & MV_VERTICAL)
469 int pos_right = FULL_SXSIZE - g->width;
471 if (c->position == POS_LEFT)
473 else if (c->position == POS_RIGHT)
476 part->x = GetSimpleRandom(pos_right);
478 if (c->direction == MV_DOWN)
480 part->step_yoffset = c->step_offset;
481 part->y = -g->height + part->step_yoffset;
485 part->step_yoffset = -c->step_offset;
486 part->y = FULL_SYSIZE + part->step_yoffset;
489 part->step_xoffset = 0;
496 part->step_xoffset = 0;
497 part->step_yoffset = 0;
500 if (c->x != ARG_UNDEFINED_VALUE)
502 if (c->y != ARG_UNDEFINED_VALUE)
505 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
506 part->step_xoffset = c->step_xoffset;
507 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
508 part->step_yoffset = c->step_yoffset;
511 if (part->init_delay_counter > 0)
513 part->init_delay_counter--;
515 return ANIM_STATE_WAITING;
518 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
519 (part->x >= FULL_SXSIZE && part->step_xoffset >= 0) ||
520 (part->y <= -g->height && part->step_yoffset <= 0) ||
521 (part->y >= FULL_SYSIZE && part->step_yoffset >= 0))
522 return ANIM_STATE_RESTART;
524 if (part->anim_delay_counter > 0)
526 part->anim_delay_counter--;
528 if (part->anim_delay_counter == 0)
530 part->post_delay_counter =
531 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
533 if (part->post_delay_counter > 0)
534 return ANIM_STATE_RUNNING;
536 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
540 if (part->post_delay_counter > 0)
542 part->post_delay_counter--;
544 if (part->post_delay_counter == 0)
545 return ANIM_STATE_RESTART;
547 return ANIM_STATE_WAITING;
550 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
552 return ANIM_STATE_RUNNING;
556 static unsigned int last_counter = -1;
557 unsigned int counter = Counter();
559 printf("::: NEXT ANIM PART [%d, %d]\n",
560 anim_sync_frame, counter - last_counter);
562 last_counter = counter;
566 part->x += part->step_xoffset;
567 part->y += part->step_yoffset;
569 return ANIM_STATE_RUNNING;
572 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
574 struct GlobalAnimPartControlInfo *part;
575 struct GraphicInfo *c = &anim->control_info;
576 boolean skip = FALSE;
579 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
580 anim->mode_nr, anim->nr, anim->num_parts);
581 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
585 printf("::: %s(%d): %d, %d, %d [%d]\n",
586 (action == ANIM_START ? "ANIM_START" :
587 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
588 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
590 anim->state & ANIM_STATE_RESTART,
591 anim->state & ANIM_STATE_WAITING,
592 anim->state & ANIM_STATE_RUNNING,
599 anim->state = ANIM_STATE_RESTART;
600 anim->part_counter = 0;
601 anim->active_part_nr = 0;
607 if (anim->state == ANIM_STATE_INACTIVE)
613 anim->state = ANIM_STATE_INACTIVE;
622 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
624 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
628 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
629 anim->mode_nr, anim->nr, num_parts);
632 for (i = 0; i < num_parts; i++)
634 part = &anim->part[i];
639 anim->state = ANIM_STATE_RUNNING;
640 part->state = ANIM_STATE_RESTART;
646 if (part->state == ANIM_STATE_INACTIVE)
652 part->state = ANIM_STATE_INACTIVE;
664 part->state = HandleGlobalAnim_Part(part, part->state);
673 if (anim->state & ANIM_STATE_RESTART) // directly after restart
674 anim->active_part_nr = getGlobalAnimationPart(anim);
676 part = &anim->part[anim->active_part_nr];
678 part->state = ANIM_STATE_RUNNING;
680 anim->state = HandleGlobalAnim_Part(part, anim->state);
682 if (anim->state & ANIM_STATE_RESTART)
683 anim->part_counter++;
686 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
691 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
692 ctrl->nr, ctrl->num_anims);
695 for (i = 0; i < ctrl->num_anims; i++)
696 HandleGlobalAnim_Main(&ctrl->anim[i], action);
699 void HandleGlobalAnim(int action)
702 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
705 HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
706 HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
711 // HandleAnimation(ANIM_START);
714 printf("::: InitAnimation\n");
719 InitGlobalAnimControls();
721 HandleGlobalAnim(ANIM_START);
723 do_animations = TRUE;
728 // HandleAnimation(ANIM_STOP);
731 printf("::: StopAnimation\n");
734 HandleGlobalAnim(ANIM_STOP);
736 do_animations = FALSE;
739 static void DoAnimationExt()
742 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
746 WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
749 if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
753 HandleGlobalAnim(ANIM_CONTINUE);
756 // force screen redraw in next frame to continue drawing global animations
757 redraw_mask = REDRAW_ALL;
763 // HandleAnimation(ANIM_CONTINUE);
766 // force screen redraw in next frame to continue drawing global animations
767 redraw_mask = REDRAW_ALL;