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 struct GlobalAnimPartControlInfo
34 struct GraphicInfo graphic_info;
35 struct GraphicInfo control_info;
43 int step_xoffset, step_yoffset;
45 unsigned int initial_anim_sync_frame;
46 unsigned int step_delay, step_delay_value;
48 int init_delay_counter;
49 int anim_delay_counter;
50 int post_delay_counter;
56 struct GlobalAnimMainControlInfo
58 struct GlobalAnimPartControlInfo base;
59 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
64 struct GraphicInfo control_info;
72 int init_delay_counter;
77 struct GlobalAnimControlInfo
79 struct GlobalAnimMainControlInfo anim[NUM_GLOBAL_ANIMS_AND_TOONS];
86 /* forward declaration for internal use */
87 static void DoAnimationExt(void);
89 static struct GlobalAnimControlInfo global_anim_ctrl[NUM_SPECIAL_GFX_ARGS];
90 static struct ToonInfo toons[MAX_NUM_TOONS];
92 static unsigned int anim_sync_frame = 0;
93 static unsigned int anim_sync_frame_delay = 0;
94 static unsigned int anim_sync_frame_delay_value = GAME_FRAME_DELAY;
96 static boolean do_animations = FALSE;
99 static int getGlobalAnimationPart(struct GlobalAnimMainControlInfo *anim)
101 struct GraphicInfo *c = &anim->control_info;
102 int last_anim_random_frame = gfx.anim_random_frame;
105 gfx.anim_random_frame = -1; // (use simple, ad-hoc random numbers)
107 part_nr = getAnimationFrame(anim->num_parts, 1,
108 c->anim_mode, c->anim_start_frame,
111 gfx.anim_random_frame = last_anim_random_frame;
116 static void PrepareBackbuffer()
118 if (game_status != GAME_MODE_PLAYING)
121 BlitScreenToBitmap(backbuffer);
124 boolean ToonNeedsRedraw()
131 int num_toons = MAX_NUM_TOONS;
134 if (global.num_toons >= 0 && global.num_toons < MAX_NUM_TOONS)
135 num_toons = global.num_toons;
137 for (i = 0; i < num_toons; i++)
139 int graphic = IMG_TOON_1 + i;
140 struct FileInfo *image = getImageListEntryFromImageID(graphic);
142 toons[i].bitmap = graphic_info[graphic].bitmap;
144 toons[i].src_x = graphic_info[graphic].src_x;
145 toons[i].src_y = graphic_info[graphic].src_y;
147 toons[i].width = graphic_info[graphic].width;
148 toons[i].height = graphic_info[graphic].height;
150 toons[i].anim_frames = graphic_info[graphic].anim_frames;
151 toons[i].anim_delay = graphic_info[graphic].anim_delay;
152 toons[i].anim_mode = graphic_info[graphic].anim_mode;
153 toons[i].anim_start_frame = graphic_info[graphic].anim_start_frame;
155 toons[i].step_offset = graphic_info[graphic].step_offset;
156 toons[i].step_delay = graphic_info[graphic].step_delay;
158 toons[i].direction = image->parameter[GFX_ARG_DIRECTION];
159 toons[i].position = image->parameter[GFX_ARG_POSITION];
162 InitToonScreen(bitmap_db_toons,
163 BackToFront, PrepareBackbuffer, ToonNeedsRedraw,
165 REAL_SX, REAL_SY, FULL_SXSIZE, FULL_SYSIZE,
169 static void InitToonControls()
171 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[GAME_MODE_DEFAULT];
172 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[ctrl->num_anims];
173 int mode_nr, anim_nr, part_nr;
174 int control = IMG_INTERNAL_GLOBAL_TOON_DEFAULT;
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 mode_nr = GAME_MODE_DEFAULT;
182 anim_nr = ctrl->num_anims;
185 anim->mode_nr = mode_nr;
186 anim->control_info = graphic_info[control];
189 anim->part_counter = 0;
190 anim->active_part_nr = 0;
192 anim->has_base = FALSE;
194 anim->init_delay_counter = 0;
196 anim->state = ANIM_STATE_INACTIVE;
200 for (i = 0; i < num_toons; i++)
202 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
203 int graphic = IMG_TOON_1 + i;
204 int control = graphic;
207 part->anim_nr = anim_nr;
208 part->mode_nr = mode_nr;
209 part->graphic = graphic;
210 part->graphic_info = graphic_info[graphic];
211 part->control_info = graphic_info[control];
213 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
215 part->control_info.init_delay_fixed = 0;
216 part->control_info.init_delay_random = 150;
218 part->control_info.x = ARG_UNDEFINED_VALUE;
219 part->control_info.y = ARG_UNDEFINED_VALUE;
221 part->initial_anim_sync_frame = 0;
223 part->step_delay = 0;
224 part->step_delay_value = graphic_info[control].step_delay;
226 part->state = ANIM_STATE_INACTIVE;
227 part->last_game_status = -1;
236 void InitGlobalAnimControls()
239 int mode_nr, anim_nr, part_nr;
240 int graphic, control;
244 ResetDelayCounter(&anim_sync_frame_delay);
246 for (m = 0; m < NUM_SPECIAL_GFX_ARGS; m++)
250 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
257 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
259 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
260 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
262 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
264 // if no base animation parameters defined, use default values
265 if (control == IMG_UNDEFINED)
266 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
269 anim->mode_nr = mode_nr;
270 anim->control_info = graphic_info[control];
273 anim->part_counter = 0;
274 anim->active_part_nr = 0;
276 anim->has_base = FALSE;
278 anim->init_delay_counter = 0;
280 anim->state = ANIM_STATE_INACTIVE;
284 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
286 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
288 graphic = global_anim_info[a].graphic[p][m];
289 control = global_anim_info[ctrl_id].graphic[p][m];
291 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
292 control == IMG_UNDEFINED)
296 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
297 m, a, p, mode_nr, anim_nr, part_nr, control);
301 part->anim_nr = anim_nr;
302 part->mode_nr = mode_nr;
303 part->graphic = graphic;
304 part->graphic_info = graphic_info[graphic];
305 part->control_info = graphic_info[control];
307 part->initial_anim_sync_frame = 0;
309 part->step_delay = 0;
310 part->step_delay_value = graphic_info[control].step_delay;
312 part->state = ANIM_STATE_INACTIVE;
313 part->last_game_status = -1;
315 if (p < GLOBAL_ANIM_ID_PART_BASE)
323 anim->has_base = TRUE;
327 if (anim->num_parts > 0 || anim->has_base)
338 void DrawGlobalAnim()
342 if (game_status == GAME_MODE_LOADING)
343 do_animations = FALSE;
345 if (!do_animations || !setup.toons)
350 for (mode_nr = 0; mode_nr < NUM_SPECIAL_GFX_ARGS; mode_nr++)
352 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
355 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
356 mode_nr != game_status)
359 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
361 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
362 struct GraphicInfo *c = &anim->control_info;
363 int part_first, part_last;
366 if (!(anim->state & ANIM_STATE_RUNNING))
369 part_first = part_last = anim->active_part_nr;
371 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
373 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
376 part_last = num_parts - 1;
379 for (part_nr = part_first; part_nr <= part_last; part_nr++)
381 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
382 struct GraphicInfo *g = &part->graphic_info;
385 int width = g->width;
386 int height = g->height;
394 if (!(part->state & ANIM_STATE_RUNNING))
403 else if (part->x > part->viewport_width - g->width)
404 width -= (part->x - (part->viewport_width - g->width));
412 else if (part->y > part->viewport_height - g->height)
413 height -= (part->y - (part->viewport_height - g->height));
415 dst_x += part->viewport_x;
416 dst_y += part->viewport_y;
418 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
419 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
420 g->anim_mode, g->anim_start_frame,
423 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
429 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
436 boolean SetGlobalAnimPart_Viewport(struct GlobalAnimPartControlInfo *part)
442 boolean changed = FALSE;
444 if (part->last_game_status == game_status)
447 part->last_game_status = game_status;
449 if (part->control_info.class == get_hash_from_key("window") ||
450 part->control_info.class == get_hash_from_key("border"))
454 viewport_width = WIN_XSIZE;
455 viewport_height = WIN_YSIZE;
457 else if (part->control_info.class == get_hash_from_key("door_1"))
461 viewport_width = DXSIZE;
462 viewport_height = DYSIZE;
464 else if (part->control_info.class == get_hash_from_key("door_2"))
468 viewport_width = VXSIZE;
469 viewport_height = VYSIZE;
471 else // default: "playfield"
473 viewport_x = REAL_SX;
474 viewport_y = REAL_SY;
475 viewport_width = FULL_SXSIZE;
476 viewport_height = FULL_SYSIZE;
479 if (viewport_x != part->viewport_x ||
480 viewport_y != part->viewport_y ||
481 viewport_width != part->viewport_width ||
482 viewport_height != part->viewport_height)
484 part->viewport_x = viewport_x;
485 part->viewport_y = viewport_y;
486 part->viewport_width = viewport_width;
487 part->viewport_height = viewport_height;
495 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
497 struct GraphicInfo *g = &part->graphic_info;
498 struct GraphicInfo *c = &part->control_info;
499 boolean viewport_changed = SetGlobalAnimPart_Viewport(part);
501 if (viewport_changed)
502 state |= ANIM_STATE_RESTART;
504 if (state & ANIM_STATE_RESTART)
506 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
508 part->init_delay_counter =
509 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
511 part->anim_delay_counter =
512 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
514 part->initial_anim_sync_frame =
515 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
517 if (c->direction & MV_HORIZONTAL)
519 int pos_bottom = part->viewport_height - g->height;
521 if (c->position == POS_TOP)
523 else if (c->position == POS_UPPER)
524 part->y = GetSimpleRandom(pos_bottom / 2);
525 else if (c->position == POS_MIDDLE)
526 part->y = pos_bottom / 2;
527 else if (c->position == POS_LOWER)
528 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
529 else if (c->position == POS_BOTTOM)
530 part->y = pos_bottom;
532 part->y = GetSimpleRandom(pos_bottom);
534 if (c->direction == MV_RIGHT)
536 part->step_xoffset = c->step_offset;
537 part->x = -g->width + part->step_xoffset;
541 part->step_xoffset = -c->step_offset;
542 part->x = part->viewport_width + part->step_xoffset;
545 part->step_yoffset = 0;
547 else if (c->direction & MV_VERTICAL)
549 int pos_right = part->viewport_width - g->width;
551 if (c->position == POS_LEFT)
553 else if (c->position == POS_RIGHT)
556 part->x = GetSimpleRandom(pos_right);
558 if (c->direction == MV_DOWN)
560 part->step_yoffset = c->step_offset;
561 part->y = -g->height + part->step_yoffset;
565 part->step_yoffset = -c->step_offset;
566 part->y = part->viewport_height + part->step_yoffset;
569 part->step_xoffset = 0;
576 part->step_xoffset = 0;
577 part->step_yoffset = 0;
580 if (c->x != ARG_UNDEFINED_VALUE)
582 if (c->y != ARG_UNDEFINED_VALUE)
585 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
586 part->step_xoffset = c->step_xoffset;
587 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
588 part->step_yoffset = c->step_yoffset;
591 if (part->init_delay_counter > 0)
593 part->init_delay_counter--;
595 return ANIM_STATE_WAITING;
598 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
599 (part->x >= part->viewport_width && part->step_xoffset >= 0) ||
600 (part->y <= -g->height && part->step_yoffset <= 0) ||
601 (part->y >= part->viewport_height && part->step_yoffset >= 0))
602 return ANIM_STATE_RESTART;
604 if (part->anim_delay_counter > 0)
606 part->anim_delay_counter--;
608 if (part->anim_delay_counter == 0)
610 part->post_delay_counter =
611 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
613 if (part->post_delay_counter > 0)
614 return ANIM_STATE_RUNNING;
616 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
620 if (part->post_delay_counter > 0)
622 part->post_delay_counter--;
624 if (part->post_delay_counter == 0)
625 return ANIM_STATE_RESTART;
627 return ANIM_STATE_WAITING;
630 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
632 return ANIM_STATE_RUNNING;
636 static unsigned int last_counter = -1;
637 unsigned int counter = Counter();
639 printf("::: NEXT ANIM PART [%d, %d]\n",
640 anim_sync_frame, counter - last_counter);
642 last_counter = counter;
646 part->x += part->step_xoffset;
647 part->y += part->step_yoffset;
649 return ANIM_STATE_RUNNING;
652 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
654 struct GlobalAnimPartControlInfo *part;
655 struct GraphicInfo *c = &anim->control_info;
656 boolean skip = FALSE;
659 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
660 anim->mode_nr, anim->nr, anim->num_parts);
661 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
665 printf("::: %s(%d): %d, %d, %d [%d]\n",
666 (action == ANIM_START ? "ANIM_START" :
667 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
668 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
670 anim->state & ANIM_STATE_RESTART,
671 anim->state & ANIM_STATE_WAITING,
672 anim->state & ANIM_STATE_RUNNING,
679 anim->state = ANIM_STATE_RESTART;
680 anim->part_counter = 0;
681 anim->active_part_nr = 0;
687 if (anim->state == ANIM_STATE_INACTIVE)
693 anim->state = ANIM_STATE_INACTIVE;
702 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
704 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
708 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
709 anim->mode_nr, anim->nr, num_parts);
712 for (i = 0; i < num_parts; i++)
714 part = &anim->part[i];
719 anim->state = ANIM_STATE_RUNNING;
720 part->state = ANIM_STATE_RESTART;
726 if (part->state == ANIM_STATE_INACTIVE)
732 part->state = ANIM_STATE_INACTIVE;
744 part->state = HandleGlobalAnim_Part(part, part->state);
753 if (anim->state & ANIM_STATE_RESTART) // directly after restart
754 anim->active_part_nr = getGlobalAnimationPart(anim);
756 part = &anim->part[anim->active_part_nr];
758 part->state = ANIM_STATE_RUNNING;
760 anim->state = HandleGlobalAnim_Part(part, anim->state);
762 if (anim->state & ANIM_STATE_RESTART)
763 anim->part_counter++;
766 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
771 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
772 ctrl->nr, ctrl->num_anims);
775 for (i = 0; i < ctrl->num_anims; i++)
776 HandleGlobalAnim_Main(&ctrl->anim[i], action);
779 void HandleGlobalAnim(int action)
782 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
785 HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
786 HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
791 // HandleAnimation(ANIM_START);
794 printf("::: InitAnimation\n");
799 InitGlobalAnimControls();
801 HandleGlobalAnim(ANIM_START);
803 do_animations = TRUE;
808 // HandleAnimation(ANIM_STOP);
811 printf("::: StopAnimation\n");
814 HandleGlobalAnim(ANIM_STOP);
816 do_animations = FALSE;
819 static void DoAnimationExt()
822 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
826 WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
829 if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
833 HandleGlobalAnim(ANIM_CONTINUE);
836 // force screen redraw in next frame to continue drawing global animations
837 redraw_mask = REDRAW_ALL;
843 // HandleAnimation(ANIM_CONTINUE);
846 // force screen redraw in next frame to continue drawing global animations
847 redraw_mask = REDRAW_ALL;