1 /***********************************************************
2 * Artsoft Retro-Game Library *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment *
6 * Detmolder Strasse 189 *
9 * e-mail: info@artsoft.org *
10 *----------------------------------------------------------*
12 ***********************************************************/
18 /* values for toon animation */
20 #define ANIM_CONTINUE 1
24 static struct ToonScreenInfo screen_info;
27 /* ========================================================================= */
28 /* generic animation frame calculation */
29 /* ========================================================================= */
31 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
36 sync_frame += start_frame * delay;
38 if (mode & ANIM_LOOP) /* looping animation */
40 frame = (sync_frame % (delay * num_frames)) / delay;
42 else if (mode & ANIM_LINEAR) /* linear (non-looping) animation */
44 frame = sync_frame / delay;
46 if (frame > num_frames - 1)
47 frame = num_frames - 1;
49 else if (mode & ANIM_PINGPONG) /* oscillate (border frames once) */
51 int max_anim_frames = 2 * num_frames - 2;
53 frame = (sync_frame % (delay * max_anim_frames)) / delay;
54 frame = (frame < num_frames ? frame : max_anim_frames - frame);
56 else if (mode & ANIM_PINGPONG2) /* oscillate (border frames twice) */
58 int max_anim_frames = 2 * num_frames;
60 frame = (sync_frame % (delay * max_anim_frames)) / delay;
61 frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
63 else if (mode & ANIM_RANDOM) /* play frames in random order */
65 /* note: expect different frames for the same delay cycle! */
67 if (gfx.anim_random_frame < 0)
68 frame = SimpleRND(num_frames);
70 frame = gfx.anim_random_frame % num_frames;
73 if (mode & ANIM_REVERSE) /* use reverse animation direction */
74 frame = num_frames - frame - 1;
80 /* ========================================================================= */
81 /* toon animation functions */
82 /* ========================================================================= */
84 static int get_toon_direction(char *direction_string_raw)
86 char *direction_string = getStringToLower(direction_string_raw);
87 int direction = (strcmp(direction_string, "left") == 0 ? MV_LEFT :
88 strcmp(direction_string, "right") == 0 ? MV_RIGHT :
89 strcmp(direction_string, "up") == 0 ? MV_UP :
90 strcmp(direction_string, "down") == 0 ? MV_DOWN :
93 free(direction_string);
98 void InitToonScreen(Bitmap *save_buffer,
99 void (*update_function)(void),
100 void (*prepare_backbuffer_function)(void),
101 boolean (*redraw_needed_function)(void),
102 struct ToonInfo *toons, int num_toons,
103 int startx, int starty,
104 int width, int height,
105 int frame_delay_value)
107 screen_info.save_buffer = save_buffer;
108 screen_info.update_function = update_function;
109 screen_info.prepare_backbuffer_function = prepare_backbuffer_function;
110 screen_info.redraw_needed_function = redraw_needed_function;
111 screen_info.toons = toons;
112 screen_info.num_toons = num_toons;
113 screen_info.startx = startx;
114 screen_info.starty = starty;
115 screen_info.width = width;
116 screen_info.height = height;
117 screen_info.frame_delay_value = frame_delay_value;
120 void DrawAnim(Bitmap *toon_bitmap, GC toon_clip_gc,
121 int src_x, int src_y, int width, int height,
122 int dest_x, int dest_y, int pad_x, int pad_y)
124 int buf_x = DOOR_GFX_PAGEX3, buf_y = DOOR_GFX_PAGEY1;
127 /* special method to avoid flickering interference with BackToFront() */
128 BlitBitmap(backbuffer, screen_info.save_buffer, dest_x-pad_x, dest_y-pad_y,
129 width+2*pad_x, height+2*pad_y, buf_x, buf_y);
130 SetClipOrigin(toon_bitmap, toon_clip_gc, dest_x-src_x, dest_y-src_y);
131 BlitBitmapMasked(toon_bitmap, backbuffer,
132 src_x, src_y, width, height, dest_x, dest_y);
133 BlitBitmap(backbuffer, window, dest_x-pad_x, dest_y-pad_y,
134 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
136 screen_info.update_function();
138 BlitBitmap(screen_info.save_buffer, backbuffer, buf_x, buf_y,
139 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
141 /* normal method, causing flickering interference with BackToFront() */
142 BlitBitmap(backbuffer, screen_info.save_buffer, dest_x-pad_x, dest_y-pad_y,
143 width+2*pad_x, height+2*pad_y, buf_x, buf_y);
144 SetClipOrigin(toon_bitmap,toon_clip_gc, buf_x-src_x+pad_x,buf_y-src_y+pad_y);
145 BlitBitmapMasked(toon_bitmap, screen_info.save_buffer,
146 src_x, src_y, width, height, buf_x+pad_x, buf_y+pad_y);
147 BlitBitmap(screen_info.save_buffer, window, buf_x, buf_y,
148 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
154 boolean AnimateToon(int toon_nr, boolean restart)
156 static unsigned long animation_frame_counter = 0;
157 static int pos_x = 0, pos_y = 0;
158 static int delta_x = 0, delta_y = 0;
159 static int frame = 0;
160 static boolean horiz_move, vert_move;
161 static unsigned long anim_delay = 0;
162 static unsigned long anim_delay_value = 0;
163 static int width,height;
164 static int pad_x,pad_y;
165 static int cut_x,cut_y;
166 static int src_x, src_y;
167 static int dest_x, dest_y;
168 struct ToonInfo *anim = &screen_info.toons[toon_nr];
169 Bitmap *anim_bitmap = screen_info.toons[toon_nr].bitmap;
170 GC anim_clip_gc = anim_bitmap->stored_clip_gc;
171 int direction = get_toon_direction(anim->direction);
175 horiz_move = (direction & (MV_LEFT | MV_RIGHT));
176 vert_move = (direction & (MV_UP | MV_DOWN));
177 anim_delay_value = anim->step_delay * screen_info.frame_delay_value;
179 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
180 anim->anim_mode, anim->anim_start_frame,
181 animation_frame_counter++);
185 int pos_bottom = screen_info.height - anim->height;
187 if (strcmp(anim->position, "top") == 0)
189 else if (strcmp(anim->position, "bottom") == 0)
191 else if (strcmp(anim->position, "upper") == 0)
192 pos_y = SimpleRND(pos_bottom / 2);
193 else if (strcmp(anim->position, "lower") == 0)
194 pos_y = pos_bottom / 2 + SimpleRND(pos_bottom / 2);
196 pos_y = SimpleRND(pos_bottom);
198 if (direction == MV_RIGHT)
200 delta_x = anim->step_offset;
201 pos_x = -anim->width + delta_x;
205 delta_x = -anim->step_offset;
206 pos_x = screen_info.width + delta_x;
213 int pos_right = screen_info.width - anim->width;
215 if (strcmp(anim->position, "left") == 0)
217 else if (strcmp(anim->position, "right") == 0)
220 pos_x = SimpleRND(pos_right);
222 if (direction == MV_DOWN)
224 delta_y = anim->step_offset;
225 pos_y = -anim->height + delta_y;
229 delta_y = -anim->step_offset;
230 pos_y = screen_info.width + delta_y;
237 if (pos_x <= -anim->width - anim->step_offset ||
238 pos_x >= screen_info.width + anim->step_offset ||
239 pos_y <= -anim->height - anim->step_offset ||
240 pos_y >= screen_info.height + anim->step_offset)
243 if (!DelayReached(&anim_delay, anim_delay_value))
245 if (screen_info.redraw_needed_function() && !restart)
246 DrawAnim(anim_bitmap, anim_clip_gc,
247 src_x + cut_x, src_y + cut_y,
249 screen_info.startx + dest_x,
250 screen_info.starty + dest_y,
256 if (pos_x < -anim->width)
257 pos_x = -anim->width;
258 else if (pos_x > screen_info.width)
259 pos_x = screen_info.width;
260 if (pos_y < -anim->height)
261 pos_y = -anim->height;
262 else if (pos_y > screen_info.height)
263 pos_y = screen_info.height;
265 pad_x = (horiz_move ? anim->step_offset : 0);
266 pad_y = (vert_move ? anim->step_offset : 0);
267 src_x = anim->src_x + frame * anim->width;
273 height = anim->height;
281 else if (pos_x > screen_info.width - anim->width)
282 width -= (pos_x - (screen_info.width - anim->width));
290 else if (pos_y > screen_info.height - anim->height)
291 height -= (pos_y - (screen_info.height - anim->height));
293 DrawAnim(anim_bitmap, anim_clip_gc,
294 src_x + cut_x, src_y + cut_y,
296 screen_info.startx + dest_x,
297 screen_info.starty + dest_y,
303 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
304 anim->anim_mode, anim->anim_start_frame,
305 animation_frame_counter++);
310 void HandleAnimation(int mode)
312 static unsigned long animstart_delay = -1;
313 static unsigned long animstart_delay_value = 0;
314 static boolean anim_running = FALSE;
315 static boolean anim_restart = TRUE;
316 static boolean reset_delay = TRUE;
317 static int toon_nr = 0;
323 /* this may happen after reloading graphics and redefining "num_toons" */
324 if (toon_nr >= screen_info.num_toons)
330 screen_info.prepare_backbuffer_function();
345 redraw_mask |= (REDRAW_FIELD | REDRAW_FROM_BACKBUFFER);
347 /* Redraw background even when in direct drawing mode */
348 draw_mode = setup.direct_draw;
349 setup.direct_draw = FALSE;
350 screen_info.update_function();
351 setup.direct_draw = draw_mode;
353 anim_running = FALSE;
363 animstart_delay = Counter();
364 animstart_delay_value = SimpleRND(3000);
370 if (!DelayReached(&animstart_delay, animstart_delay_value))
373 toon_nr = SimpleRND(screen_info.num_toons);
376 anim_restart = reset_delay = AnimateToon(toon_nr, anim_restart);
381 HandleAnimation(ANIM_START);
386 HandleAnimation(ANIM_STOP);
391 HandleAnimation(ANIM_CONTINUE);