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;
38 int step_xoffset, step_yoffset;
40 unsigned int initial_anim_sync_frame;
41 unsigned int step_delay, step_delay_value;
43 int init_delay_counter;
44 int anim_delay_counter;
45 int post_delay_counter;
50 struct GlobalAnimMainControlInfo
52 struct GlobalAnimPartControlInfo base;
53 struct GlobalAnimPartControlInfo part[NUM_GLOBAL_ANIM_PARTS_AND_TOONS];
58 struct GraphicInfo control_info;
66 int init_delay_counter;
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->init_delay_counter = 0;
190 anim->state = ANIM_STATE_INACTIVE;
194 for (i = 0; i < num_toons; i++)
196 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
197 int graphic = IMG_TOON_1 + i;
198 int control = graphic;
201 part->anim_nr = anim_nr;
202 part->mode_nr = mode_nr;
203 part->graphic = graphic;
204 part->graphic_info = graphic_info[graphic];
205 part->control_info = graphic_info[control];
207 part->graphic_info.anim_delay *= part->graphic_info.step_delay;
209 part->control_info.init_delay_fixed = 0;
210 part->control_info.init_delay_random = 150;
212 part->control_info.x = ARG_UNDEFINED_VALUE;
213 part->control_info.y = ARG_UNDEFINED_VALUE;
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_counter = 0;
271 anim->state = ANIM_STATE_INACTIVE;
275 for (p = 0; p < NUM_GLOBAL_ANIM_PARTS_ALL; p++)
277 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
279 graphic = global_anim_info[a].graphic[p][m];
280 control = global_anim_info[ctrl_id].graphic[p][m];
282 if (graphic == IMG_UNDEFINED || graphic_info[graphic].bitmap == NULL ||
283 control == IMG_UNDEFINED)
287 printf("::: mode == %d, anim = %d, part = %d [%d, %d, %d] [%d]\n",
288 m, a, p, mode_nr, anim_nr, part_nr, control);
292 part->anim_nr = anim_nr;
293 part->mode_nr = mode_nr;
294 part->graphic = graphic;
295 part->graphic_info = graphic_info[graphic];
296 part->control_info = graphic_info[control];
298 part->step_delay = 0;
299 part->step_delay_value = graphic_info[control].step_delay;
301 part->state = ANIM_STATE_INACTIVE;
303 if (p < GLOBAL_ANIM_ID_PART_BASE)
311 anim->has_base = TRUE;
315 if (anim->num_parts > 0 || anim->has_base)
326 void DrawGlobalAnim()
330 if (game_status == GAME_MODE_LOADING)
331 do_animations = FALSE;
333 if (!do_animations || !setup.toons)
338 for (mode_nr = 0; mode_nr < NUM_SPECIAL_GFX_ARGS; mode_nr++)
340 struct GlobalAnimControlInfo *ctrl = &global_anim_ctrl[mode_nr];
343 if (mode_nr != GFX_SPECIAL_ARG_DEFAULT &&
344 mode_nr != game_status)
347 for (anim_nr = 0; anim_nr < ctrl->num_anims; anim_nr++)
349 struct GlobalAnimMainControlInfo *anim = &ctrl->anim[anim_nr];
350 struct GraphicInfo *c = &anim->control_info;
351 int part_first, part_last;
354 if (!(anim->state & ANIM_STATE_RUNNING))
357 part_first = part_last = anim->active_part_nr;
359 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
361 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
364 part_last = num_parts - 1;
367 for (part_nr = part_first; part_nr <= part_last; part_nr++)
369 struct GlobalAnimPartControlInfo *part = &anim->part[part_nr];
370 struct GraphicInfo *g = &part->graphic_info;
373 int width = g->width;
374 int height = g->height;
382 if (!(part->state & ANIM_STATE_RUNNING))
391 else if (part->x > FULL_SXSIZE - g->width)
392 width -= (part->x - (FULL_SXSIZE - g->width));
400 else if (part->y > FULL_SYSIZE - g->height)
401 height -= (part->y - (FULL_SYSIZE - g->height));
406 sync_frame = anim_sync_frame - part->initial_anim_sync_frame;
407 frame = getAnimationFrame(g->anim_frames, g->anim_delay,
408 g->anim_mode, g->anim_start_frame,
411 getFixedGraphicSource(part->graphic, frame, &src_bitmap,
417 BlitToScreenMasked(src_bitmap, src_x, src_y, width, height,
424 int HandleGlobalAnim_Part(struct GlobalAnimPartControlInfo *part, int state)
426 struct GraphicInfo *g = &part->graphic_info;
427 struct GraphicInfo *c = &part->control_info;
429 if (state & ANIM_STATE_RESTART)
431 ResetDelayCounterExt(&part->step_delay, anim_sync_frame);
433 part->init_delay_counter =
434 (c->init_delay_fixed + GetSimpleRandom(c->init_delay_random));
436 part->anim_delay_counter =
437 (c->anim_delay_fixed + GetSimpleRandom(c->anim_delay_random));
439 part->initial_anim_sync_frame =
440 (g->anim_global_sync ? 0 : anim_sync_frame + part->init_delay_counter);
442 if (c->direction & MV_HORIZONTAL)
444 int pos_bottom = FULL_SYSIZE - g->height;
446 if (c->position == POS_TOP)
448 else if (c->position == POS_UPPER)
449 part->y = GetSimpleRandom(pos_bottom / 2);
450 else if (c->position == POS_MIDDLE)
451 part->y = pos_bottom / 2;
452 else if (c->position == POS_LOWER)
453 part->y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
454 else if (c->position == POS_BOTTOM)
455 part->y = pos_bottom;
457 part->y = GetSimpleRandom(pos_bottom);
459 if (c->direction == MV_RIGHT)
461 part->step_xoffset = c->step_offset;
462 part->x = -g->width + part->step_xoffset;
466 part->step_xoffset = -c->step_offset;
467 part->x = FULL_SXSIZE + part->step_xoffset;
470 part->step_yoffset = 0;
472 else if (c->direction & MV_VERTICAL)
474 int pos_right = FULL_SXSIZE - g->width;
476 if (c->position == POS_LEFT)
478 else if (c->position == POS_RIGHT)
481 part->x = GetSimpleRandom(pos_right);
483 if (c->direction == MV_DOWN)
485 part->step_yoffset = c->step_offset;
486 part->y = -g->height + part->step_yoffset;
490 part->step_yoffset = -c->step_offset;
491 part->y = FULL_SYSIZE + part->step_yoffset;
494 part->step_xoffset = 0;
501 part->step_xoffset = 0;
502 part->step_yoffset = 0;
505 if (c->x != ARG_UNDEFINED_VALUE)
507 if (c->y != ARG_UNDEFINED_VALUE)
510 if (c->step_xoffset != ARG_UNDEFINED_VALUE)
511 part->step_xoffset = c->step_xoffset;
512 if (c->step_yoffset != ARG_UNDEFINED_VALUE)
513 part->step_yoffset = c->step_yoffset;
516 if (part->init_delay_counter > 0)
518 part->init_delay_counter--;
520 return ANIM_STATE_WAITING;
523 if ((part->x <= -g->width && part->step_xoffset <= 0) ||
524 (part->x >= FULL_SXSIZE && part->step_xoffset >= 0) ||
525 (part->y <= -g->height && part->step_yoffset <= 0) ||
526 (part->y >= FULL_SYSIZE && part->step_yoffset >= 0))
527 return ANIM_STATE_RESTART;
529 if (part->anim_delay_counter > 0)
531 part->anim_delay_counter--;
533 if (part->anim_delay_counter == 0)
535 part->post_delay_counter =
536 (c->post_delay_fixed + GetSimpleRandom(c->post_delay_random));
538 if (part->post_delay_counter > 0)
539 return ANIM_STATE_RUNNING;
541 return ANIM_STATE_RESTART | ANIM_STATE_RUNNING;
545 if (part->post_delay_counter > 0)
547 part->post_delay_counter--;
549 if (part->post_delay_counter == 0)
550 return ANIM_STATE_RESTART;
552 return ANIM_STATE_WAITING;
555 if (!DelayReachedExt(&part->step_delay, part->step_delay_value,
557 return ANIM_STATE_RUNNING;
561 static unsigned int last_counter = -1;
562 unsigned int counter = Counter();
564 printf("::: NEXT ANIM PART [%d, %d]\n",
565 anim_sync_frame, counter - last_counter);
567 last_counter = counter;
571 part->x += part->step_xoffset;
572 part->y += part->step_yoffset;
574 return ANIM_STATE_RUNNING;
577 void HandleGlobalAnim_Main(struct GlobalAnimMainControlInfo *anim, int action)
579 struct GlobalAnimPartControlInfo *part;
580 struct GraphicInfo *c = &anim->control_info;
581 boolean skip = FALSE;
584 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
585 anim->mode_nr, anim->nr, anim->num_parts);
586 printf("::: %d, %d, %d\n", global.num_toons, setup.toons, num_toons);
590 printf("::: %s(%d): %d, %d, %d [%d]\n",
591 (action == ANIM_START ? "ANIM_START" :
592 action == ANIM_CONTINUE ? "ANIM_CONTINUE" :
593 action == ANIM_STOP ? "ANIM_STOP" : "(should not happen)"),
595 anim->state & ANIM_STATE_RESTART,
596 anim->state & ANIM_STATE_WAITING,
597 anim->state & ANIM_STATE_RUNNING,
604 anim->state = ANIM_STATE_RESTART;
605 anim->part_counter = 0;
606 anim->active_part_nr = 0;
612 if (anim->state == ANIM_STATE_INACTIVE)
618 anim->state = ANIM_STATE_INACTIVE;
627 if (c->anim_mode & ANIM_ALL || anim->num_parts == 0)
629 int num_parts = anim->num_parts + (anim->has_base ? 1 : 0);
633 printf("::: HandleGlobalAnim_Main: %d, %d => %d\n",
634 anim->mode_nr, anim->nr, num_parts);
637 for (i = 0; i < num_parts; i++)
639 part = &anim->part[i];
644 anim->state = ANIM_STATE_RUNNING;
645 part->state = ANIM_STATE_RESTART;
651 if (part->state == ANIM_STATE_INACTIVE)
657 part->state = ANIM_STATE_INACTIVE;
669 part->state = HandleGlobalAnim_Part(part, part->state);
678 if (anim->state & ANIM_STATE_RESTART) // directly after restart
679 anim->active_part_nr = getGlobalAnimationPart(anim);
681 part = &anim->part[anim->active_part_nr];
683 part->state = ANIM_STATE_RUNNING;
685 anim->state = HandleGlobalAnim_Part(part, anim->state);
687 if (anim->state & ANIM_STATE_RESTART)
688 anim->part_counter++;
691 void HandleGlobalAnim_Mode(struct GlobalAnimControlInfo *ctrl, int action)
696 printf("::: HandleGlobalAnim_Mode: %d => %d\n",
697 ctrl->nr, ctrl->num_anims);
700 for (i = 0; i < ctrl->num_anims; i++)
701 HandleGlobalAnim_Main(&ctrl->anim[i], action);
704 void HandleGlobalAnim(int action)
707 printf("::: HandleGlobalAnim [mode == %d]\n", game_status);
710 HandleGlobalAnim_Mode(&global_anim_ctrl[GAME_MODE_DEFAULT], action);
711 HandleGlobalAnim_Mode(&global_anim_ctrl[game_status], action);
716 // HandleAnimation(ANIM_START);
719 printf("::: InitAnimation\n");
724 InitGlobalAnimControls();
726 HandleGlobalAnim(ANIM_START);
728 do_animations = TRUE;
733 // HandleAnimation(ANIM_STOP);
736 printf("::: StopAnimation\n");
739 HandleGlobalAnim(ANIM_STOP);
741 do_animations = FALSE;
744 static void DoAnimationExt()
747 printf("::: DoAnimation [%d, %d]\n", anim_sync_frame, Counter());
751 WaitUntilDelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value);
754 if (DelayReached(&anim_sync_frame_delay, anim_sync_frame_delay_value))
758 HandleGlobalAnim(ANIM_CONTINUE);
761 // force screen redraw in next frame to continue drawing global animations
762 redraw_mask = REDRAW_ALL;
768 // HandleAnimation(ANIM_CONTINUE);
771 // force screen redraw in next frame to continue drawing global animations
772 redraw_mask = REDRAW_ALL;