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 unsigned int init_delay, init_delay_value;
39 unsigned int anim_delay, anim_delay_value;
40 unsigned int post_delay, post_delay_value;
45 struct GlobalAnimMainControlInfo
47 struct GlobalAnimPartControlInfo base;
48 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
53 struct GraphicInfo control_info;
61 unsigned int init_delay, init_delay_value;
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 = 0;
184 anim->init_delay_value = 0;
186 anim->state = ANIM_STATE_INACTIVE;
190 for (i = 0; i < num_toons; i++)
192 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
193 int graphic = IMG_TOON_1 + i;
194 int control = graphic;
197 part->anim_nr = anim_nr;
198 part->mode_nr = mode_nr;
199 part->graphic = graphic;
200 part->graphic_info = graphic_info[graphic];
201 part->control_info = graphic_info[control];
203 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
205 part->control_info.init_delay_fixed = 0;
206 part->control_info.init_delay_random = 150;
208 part->control_info.x = ARG_UNDEFINED_VALUE;
209 part->control_info.y = ARG_UNDEFINED_VALUE;
211 part->step_delay = 0;
212 part->step_delay_value = graphic_info[control].step_delay;
214 part->state = ANIM_STATE_INACTIVE;
223 void InitGlobalAnimControls()
226 int mode_nr, anim_nr, part_nr;
227 int graphic, control;
231 ResetDelayCounter(&anim_sync_frame_delay);
233 for (m = 0; m < NUM_SPECIAL_GFX_ARGS; m++)
237 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
244 for (a = 0; a < NUM_GLOBAL_ANIMS; a++)
246 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
247 int ctrl_id = GLOBAL_ANIM_ID_CONTROL_FIRST + a;
249 control = global_anim_info[ctrl_id].graphic[GLOBAL_ANIM_ID_PART_BASE][m];
251 // if no base animation parameters defined, use default values
252 if (control == IMG_UNDEFINED)
253 control = IMG_INTERNAL_GLOBAL_ANIM_DEFAULT;
256 anim->mode_nr = mode_nr;
257 anim->control_info = graphic_info[control];
260 anim->part_counter = 0;
261 anim->active_part_nr = 0;
263 anim->has_base = FALSE;
265 anim->init_delay = 0;
266 anim->init_delay_value = 0;
268 anim->state = ANIM_STATE_INACTIVE;
272 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
274 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
276 graphic = global_anim_info[a].graphic[p][m];
277 control = global_anim_info[ctrl_id].graphic[p][m];
279 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
280 control == IMG_UNDEFINED)
284 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
285 m, a, p, mode_nr, anim_nr, part_nr, control);
289 part->anim_nr = anim_nr;
290 part->mode_nr = mode_nr;
291 part->graphic = graphic;
292 part->graphic_info = graphic_info[graphic];
293 part->control_info = graphic_info[control];
295 part->step_delay = 0;
296 part->step_delay_value = graphic_info[control].step_delay;
298 part->state = ANIM_STATE_INACTIVE;
300 if (p < GLOBAL_ANIM_ID_PART_BASE)
308 anim->has_base = TRUE;
312 if (anim->num_parts > 0 || anim->has_base)
323 void DrawGlobalAnim()
327 if (game_status == GAME_MODE_LOADING)
328 do_animations = FALSE;
330 if (!do_animations || !setup.toons)
335 for (mode_nr = 0; mode_nr < NUM_SPECIAL_GFX_ARGS; mode_nr++)
337 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
340 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
341 mode_nr != game_status)
344 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
346 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
347 struct GraphicInfo *c = &anim->control_info;
348 int part_first, part_last;
351 if (!(anim->state & ANIM_STATE_RUNNING))
354 part_first = part_last = anim->active_part_nr;
356 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
358 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
361 part_last = num_parts - 1;
364 for (part_nr = part_first; part_nr <= part_last; part_nr++)
366 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
367 struct GraphicInfo *g = &part->graphic_info;
370 int width = g->width;
371 int height = g->height;
379 if (!(part->state & ANIM_STATE_RUNNING))
388 else if (part->x > FULL_SXSIZE - g->width)
389 width -= (part->x - (FULL_SXSIZE - g->width));
397 else if (part->y > FULL_SYSIZE - g->height)
398 height -= (part->y - (FULL_SYSIZE - g->height));
403 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
404 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
405 g->anim_mode, g->anim_start_frame,
408 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
414 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
421 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
423 struct GraphicInfo *g = &part->graphic_info;
424 struct GraphicInfo *c = &part->control_info;
426 if (state & ANIM_STATE_RESTART)
428 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
430 part->init_delay_value =
431 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
433 part->anim_delay_value =
434 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
436 part->initial_anim_sync_frame =
437 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_value);
439 if (c->direction & MV_HORIZONTAL)
441 int pos_bottom = FULL_SYSIZE - g->height;
443 if (c->position == POS_TOP)
445 else if (c->position == POS_UPPER)
446 part->y = GetSimpleRandom(pos_bottom / 2);
447 else if (c->position == POS_MIDDLE)
448 part->y = pos_bottom / 2;
449 else if (c->position == POS_LOWER)
450 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
451 else if (c->position == POS_BOTTOM)
452 part->y = pos_bottom;
454 part->y = GetSimpleRandom(pos_bottom);
456 if (c->direction == MV_RIGHT)
458 part->step_xoffset = c->step_offset;
459 part->x = -g->width + part->step_xoffset;
463 part->step_xoffset = -c->step_offset;
464 part->x = FULL_SXSIZE + part->step_xoffset;
467 part->step_yoffset = 0;
469 else if (c->direction & MV_VERTICAL)
471 int pos_right = FULL_SXSIZE - g->width;
473 if (c->position == POS_LEFT)
475 else if (c->position == POS_RIGHT)
478 part->x = GetSimpleRandom(pos_right);
480 if (c->direction == MV_DOWN)
482 part->step_yoffset = c->step_offset;
483 part->y = -g->height + part->step_yoffset;
487 part->step_yoffset = -c->step_offset;
488 part->y = FULL_SYSIZE + part->step_yoffset;
491 part->step_xoffset = 0;
498 part->step_xoffset = 0;
499 part->step_yoffset = 0;
502 if (c->x != ARG_UNDEFINED_VALUE)
504 if (c->y != ARG_UNDEFINED_VALUE)
507 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
508 part->step_xoffset = c->step_xoffset;
509 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
510 part->step_yoffset = c->step_yoffset;
513 if (part->init_delay_value > 0)
515 part->init_delay_value--;
517 return ANIM_STATE_WAITING;
520 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
521 (part->x >= FULL_SXSIZE && part->step_xoffset >= 0) ||
522 (part->y <= -g->height && part->step_yoffset <= 0) ||
523 (part->y >= FULL_SYSIZE && part->step_yoffset >= 0))
524 return ANIM_STATE_RESTART;
526 if (part->anim_delay_value > 0)
528 part->anim_delay_value--;
530 if (part->anim_delay_value == 0)
531 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
534 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
536 return ANIM_STATE_RUNNING;
540 static unsigned int last_counter = -1;
541 unsigned int counter = Counter();
543 printf("::: NEXT ANIM PART [%d, %d]\n",
544 anim_sync_frame, counter - last_counter);
546 last_counter = counter;
550 part->x += part->step_xoffset;
551 part->y += part->step_yoffset;
553 return ANIM_STATE_RUNNING;
556 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
558 struct GlobalAnimPartControlInfo *part;
559 struct GraphicInfo *c = &anim->control_info;
560 boolean skip = FALSE;
563 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
564 anim->mode_nr, anim->nr, anim->num_parts);
565 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
569 printf("::: %s(%d): %d, %d, %d [%d]\n",
570 (action == ANIM_START ? "ANIM_START" :
571 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
572 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
574 anim->state & ANIM_STATE_RESTART,
575 anim->state & ANIM_STATE_WAITING,
576 anim->state & ANIM_STATE_RUNNING,
583 anim->state = ANIM_STATE_RESTART;
584 anim->part_counter = 0;
585 anim->active_part_nr = 0;
591 if (anim->state == ANIM_STATE_INACTIVE)
597 anim->state = ANIM_STATE_INACTIVE;
606 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
608 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
612 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
613 anim->mode_nr, anim->nr, num_parts);
616 for (i = 0; i < num_parts; i++)
618 part = &anim->part[i];
623 anim->state = ANIM_STATE_RUNNING;
624 part->state = ANIM_STATE_RESTART;
630 if (part->state == ANIM_STATE_INACTIVE)
636 part->state = ANIM_STATE_INACTIVE;
648 part->state = HandleGlobalAnim_Part(part, part->state);
657 if (anim->state & ANIM_STATE_RESTART) // directly after restart
658 anim->active_part_nr = getGlobalAnimationPart(anim);
660 part = &anim->part[anim->active_part_nr];
662 part->state = ANIM_STATE_RUNNING;
664 anim->state = HandleGlobalAnim_Part(part, anim->state);
666 if (anim->state & ANIM_STATE_RESTART)
667 anim->part_counter++;
670 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
675 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
676 ctrl->nr, ctrl->num_anims);
679 for (i = 0; i < ctrl->num_anims; i++)
680 HandleGlobalAnim_Main(&ctrl->anim[i], action);
683 void HandleGlobalAnim(int action)
686 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
689 HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
690 HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
695 // HandleAnimation(ANIM_START);
698 printf("::: InitAnimation\n");
703 InitGlobalAnimControls();
705 HandleGlobalAnim(ANIM_START);
707 do_animations = TRUE;
712 // HandleAnimation(ANIM_STOP);
715 printf("::: StopAnimation\n");
718 HandleGlobalAnim(ANIM_STOP);
720 do_animations = FALSE;
723 static void DoAnimationExt()
726 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
730 WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
733 if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
737 HandleGlobalAnim(ANIM_CONTINUE);
740 // force screen redraw in next frame to continue drawing global animations
741 redraw_mask = REDRAW_ALL;
747 // HandleAnimation(ANIM_CONTINUE);
750 // force screen redraw in next frame to continue drawing global animations
751 redraw_mask = REDRAW_ALL;