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 (anim.random_frame < 0)
68 frame = SimpleRND(num_frames);
70 frame = 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 void InitToonScreen(Bitmap *save_buffer,
85 void (*update_function)(void),
86 void (*prepare_backbuffer_function)(void),
87 boolean (*redraw_needed_function)(void),
88 struct ToonInfo *toons, int num_toons,
89 int startx, int starty,
90 int width, int height,
91 int frame_delay_value)
93 screen_info.save_buffer = save_buffer;
94 screen_info.update_function = update_function;
95 screen_info.prepare_backbuffer_function = prepare_backbuffer_function;
96 screen_info.redraw_needed_function = redraw_needed_function;
97 screen_info.toons = toons;
98 screen_info.num_toons = num_toons;
99 screen_info.startx = startx;
100 screen_info.starty = starty;
101 screen_info.width = width;
102 screen_info.height = height;
103 screen_info.frame_delay_value = frame_delay_value;
106 void DrawAnim(Bitmap *toon_bitmap, GC toon_clip_gc,
107 int src_x, int src_y, int width, int height,
108 int dest_x, int dest_y, int pad_x, int pad_y)
110 int buf_x = DOOR_GFX_PAGEX3, buf_y = DOOR_GFX_PAGEY1;
113 /* special method to avoid flickering interference with BackToFront() */
114 BlitBitmap(backbuffer, screen_info.save_buffer, dest_x-pad_x, dest_y-pad_y,
115 width+2*pad_x, height+2*pad_y, buf_x, buf_y);
116 SetClipOrigin(toon_bitmap, toon_clip_gc, dest_x-src_x, dest_y-src_y);
117 BlitBitmapMasked(toon_bitmap, backbuffer,
118 src_x, src_y, width, height, dest_x, dest_y);
119 BlitBitmap(backbuffer, window, dest_x-pad_x, dest_y-pad_y,
120 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
122 screen_info.update_function();
124 BlitBitmap(screen_info.save_buffer, backbuffer, buf_x, buf_y,
125 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
127 /* normal method, causing 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, buf_x-src_x+pad_x,buf_y-src_y+pad_y);
131 BlitBitmapMasked(toon_bitmap, screen_info.save_buffer,
132 src_x, src_y, width, height, buf_x+pad_x, buf_y+pad_y);
133 BlitBitmap(screen_info.save_buffer, window, buf_x, buf_y,
134 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
140 boolean AnimateToon(int toon_nr, boolean restart)
142 static unsigned long animation_frame_counter = 0;
143 static int pos_x = 0, pos_y = 0;
144 static int delta_x = 0, delta_y = 0;
145 static int frame = 0;
146 static boolean horiz_move, vert_move;
147 static unsigned long anim_delay = 0;
148 static unsigned long anim_delay_value = 0;
149 static int width,height;
150 static int pad_x,pad_y;
151 static int cut_x,cut_y;
152 static int src_x, src_y;
153 static int dest_x, dest_y;
154 struct ToonInfo *anim = &screen_info.toons[toon_nr];
155 Bitmap *anim_bitmap = screen_info.toons[toon_nr].bitmap;
156 GC anim_clip_gc = anim_bitmap->stored_clip_gc;
160 horiz_move = (anim->direction & (ANIMDIR_LEFT | ANIMDIR_RIGHT));
161 vert_move = (anim->direction & (ANIMDIR_UP | ANIMDIR_DOWN));
162 anim_delay_value = anim->move_delay * screen_info.frame_delay_value;
164 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
165 anim->anim_mode, anim->start_frame,
166 animation_frame_counter++);
170 if (anim->position == ANIMPOS_UP)
172 else if (anim->position == ANIMPOS_DOWN)
173 pos_y = screen_info.height - anim->height;
174 else if (anim->position == ANIMPOS_UPPER)
175 pos_y = SimpleRND((screen_info.height - anim->height) / 2);
177 pos_y = SimpleRND(screen_info.height - anim->height);
179 if (anim->direction == ANIMDIR_RIGHT)
181 delta_x = anim->stepsize;
182 pos_x = -anim->width + delta_x;
186 delta_x = -anim->stepsize;
187 pos_x = screen_info.width + delta_x;
193 if (anim->position == ANIMPOS_LEFT)
195 else if (anim->position == ANIMPOS_RIGHT)
196 pos_x = screen_info.width - anim->width;
198 pos_x = SimpleRND(screen_info.width - anim->width);
200 if (anim->direction == ANIMDIR_DOWN)
202 delta_y = anim->stepsize;
203 pos_y = -anim->height + delta_y;
207 delta_y = -anim->stepsize;
208 pos_y = screen_info.width + delta_y;
214 if (pos_x <= -anim->width - anim->stepsize ||
215 pos_x >= screen_info.width + anim->stepsize ||
216 pos_y <= -anim->height - anim->stepsize ||
217 pos_y >= screen_info.height + anim->stepsize)
220 if (!DelayReached(&anim_delay, anim_delay_value))
222 if (screen_info.redraw_needed_function() && !restart)
223 DrawAnim(anim_bitmap, anim_clip_gc,
224 src_x + cut_x, src_y + cut_y,
226 screen_info.startx + dest_x,
227 screen_info.starty + dest_y,
233 if (pos_x < -anim->width)
234 pos_x = -anim->width;
235 else if (pos_x > screen_info.width)
236 pos_x = screen_info.width;
237 if (pos_y < -anim->height)
238 pos_y = -anim->height;
239 else if (pos_y > screen_info.height)
240 pos_y = screen_info.height;
242 pad_x = (horiz_move ? anim->stepsize : 0);
243 pad_y = (vert_move ? anim->stepsize : 0);
244 src_x = anim->src_x + frame * anim->width;
250 height = anim->height;
258 else if (pos_x > screen_info.width - anim->width)
259 width -= (pos_x - (screen_info.width - anim->width));
267 else if (pos_y > screen_info.height - anim->height)
268 height -= (pos_y - (screen_info.height - anim->height));
270 DrawAnim(anim_bitmap, anim_clip_gc,
271 src_x + cut_x, src_y + cut_y,
273 screen_info.startx + dest_x,
274 screen_info.starty + dest_y,
280 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
281 anim->anim_mode, anim->start_frame,
282 animation_frame_counter++);
287 void HandleAnimation(int mode)
289 static unsigned long animstart_delay = -1;
290 static unsigned long animstart_delay_value = 0;
291 static boolean anim_restart = TRUE;
292 static boolean reset_delay = TRUE;
293 static int toon_nr = 0;
302 screen_info.prepare_backbuffer_function();
312 redraw_mask |= (REDRAW_FIELD | REDRAW_FROM_BACKBUFFER);
314 /* Redraw background even when in direct drawing mode */
315 draw_mode = setup.direct_draw;
316 setup.direct_draw = FALSE;
317 screen_info.update_function();
318 setup.direct_draw = draw_mode;
328 animstart_delay = Counter();
329 animstart_delay_value = SimpleRND(3000);
335 if (!DelayReached(&animstart_delay, animstart_delay_value))
338 toon_nr = SimpleRND(screen_info.num_toons);
341 anim_restart = reset_delay = AnimateToon(toon_nr,anim_restart);
346 HandleAnimation(ANIM_START);
351 HandleAnimation(ANIM_STOP);
356 HandleAnimation(ANIM_CONTINUE);