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) /* normal, looping animation */
40 frame = (sync_frame % (delay * num_frames)) / delay;
42 else if (mode & ANIM_LINEAR) /* normal, 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) /* use 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) /* use 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);
64 if (mode & ANIM_REVERSE) /* use reverse animation direction */
65 frame = num_frames - frame - 1;
71 /* ========================================================================= */
72 /* toon animation functions */
73 /* ========================================================================= */
75 void InitToonScreen(Bitmap *save_buffer,
76 void (*update_function)(void),
77 void (*prepare_backbuffer_function)(void),
78 boolean (*redraw_needed_function)(void),
79 struct ToonInfo *toons, int num_toons,
80 int startx, int starty,
81 int width, int height)
83 screen_info.save_buffer = save_buffer;
84 screen_info.update_function = update_function;
85 screen_info.prepare_backbuffer_function = prepare_backbuffer_function;
86 screen_info.redraw_needed_function = redraw_needed_function;
87 screen_info.toons = toons;
88 screen_info.num_toons = num_toons;
89 screen_info.startx = startx;
90 screen_info.starty = starty;
91 screen_info.width = width;
92 screen_info.height = height;
95 void DrawAnim(Bitmap *toon_bitmap, GC toon_clip_gc,
96 int src_x, int src_y, int width, int height,
97 int dest_x, int dest_y, int pad_x, int pad_y)
99 int buf_x = DOOR_GFX_PAGEX3, buf_y = DOOR_GFX_PAGEY1;
102 /* special method to avoid flickering interference with BackToFront() */
103 BlitBitmap(backbuffer, screen_info.save_buffer, dest_x-pad_x, dest_y-pad_y,
104 width+2*pad_x, height+2*pad_y, buf_x, buf_y);
105 SetClipOrigin(toon_bitmap, toon_clip_gc, dest_x-src_x, dest_y-src_y);
106 BlitBitmapMasked(toon_bitmap, backbuffer,
107 src_x, src_y, width, height, dest_x, dest_y);
108 BlitBitmap(backbuffer, window, dest_x-pad_x, dest_y-pad_y,
109 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
111 screen_info.update_function();
113 BlitBitmap(screen_info.save_buffer, backbuffer, buf_x, buf_y,
114 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
116 /* normal method, causing flickering interference with BackToFront() */
117 BlitBitmap(backbuffer, screen_info.save_buffer, dest_x-pad_x, dest_y-pad_y,
118 width+2*pad_x, height+2*pad_y, buf_x, buf_y);
119 SetClipOrigin(toon_bitmap,toon_clip_gc, buf_x-src_x+pad_x,buf_y-src_y+pad_y);
120 BlitBitmapMasked(toon_bitmap, screen_info.save_buffer,
121 src_x, src_y, width, height, buf_x+pad_x, buf_y+pad_y);
122 BlitBitmap(screen_info.save_buffer, window, buf_x, buf_y,
123 width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
129 boolean AnimateToon(int toon_nr, boolean restart)
131 static unsigned long animation_frame_counter = 0;
132 static int pos_x = 0, pos_y = 0;
133 static int delta_x = 0, delta_y = 0;
134 static int frame = 0;
135 static boolean horiz_move, vert_move;
136 static unsigned long anim_delay = 0;
137 static unsigned long anim_delay_value = 0;
138 static int width,height;
139 static int pad_x,pad_y;
140 static int cut_x,cut_y;
141 static int src_x, src_y;
142 static int dest_x, dest_y;
143 struct ToonInfo *anim = &screen_info.toons[toon_nr];
144 Bitmap *anim_bitmap = screen_info.toons[toon_nr].bitmap;
145 GC anim_clip_gc = anim_bitmap->stored_clip_gc;
149 horiz_move = (anim->direction & (ANIMDIR_LEFT | ANIMDIR_RIGHT));
150 vert_move = (anim->direction & (ANIMDIR_UP | ANIMDIR_DOWN));
151 anim_delay_value = anim->move_delay;
153 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
154 anim->anim_mode, anim->start_frame,
155 animation_frame_counter++);
159 if (anim->position == ANIMPOS_UP)
161 else if (anim->position == ANIMPOS_DOWN)
162 pos_y = screen_info.height - anim->height;
163 else if (anim->position == ANIMPOS_UPPER)
164 pos_y = SimpleRND((screen_info.height - anim->height) / 2);
166 pos_y = SimpleRND(screen_info.height - anim->height);
168 if (anim->direction == ANIMDIR_RIGHT)
170 delta_x = anim->stepsize;
171 pos_x = -anim->width + delta_x;
175 delta_x = -anim->stepsize;
176 pos_x = screen_info.width + delta_x;
182 if (anim->position == ANIMPOS_LEFT)
184 else if (anim->position == ANIMPOS_RIGHT)
185 pos_x = screen_info.width - anim->width;
187 pos_x = SimpleRND(screen_info.width - anim->width);
189 if (anim->direction == ANIMDIR_DOWN)
191 delta_y = anim->stepsize;
192 pos_y = -anim->height + delta_y;
196 delta_y = -anim->stepsize;
197 pos_y = screen_info.width + delta_y;
203 if (pos_x <= -anim->width - anim->stepsize ||
204 pos_x >= screen_info.width + anim->stepsize ||
205 pos_y <= -anim->height - anim->stepsize ||
206 pos_y >= screen_info.height + anim->stepsize)
209 if (!DelayReached(&anim_delay, anim_delay_value))
211 if (screen_info.redraw_needed_function() && !restart)
212 DrawAnim(anim_bitmap, anim_clip_gc,
213 src_x + cut_x, src_y + cut_y,
215 screen_info.startx + dest_x,
216 screen_info.starty + dest_y,
222 if (pos_x < -anim->width)
223 pos_x = -anim->width;
224 else if (pos_x > screen_info.width)
225 pos_x = screen_info.width;
226 if (pos_y < -anim->height)
227 pos_y = -anim->height;
228 else if (pos_y > screen_info.height)
229 pos_y = screen_info.height;
231 pad_x = (horiz_move ? anim->stepsize : 0);
232 pad_y = (vert_move ? anim->stepsize : 0);
233 src_x = anim->src_x + frame * anim->width;
239 height = anim->height;
247 else if (pos_x > screen_info.width - anim->width)
248 width -= (pos_x - (screen_info.width - anim->width));
256 else if (pos_y > screen_info.height - anim->height)
257 height -= (pos_y - (screen_info.height - anim->height));
259 DrawAnim(anim_bitmap, anim_clip_gc,
260 src_x + cut_x, src_y + cut_y,
262 screen_info.startx + dest_x,
263 screen_info.starty + dest_y,
269 frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
270 anim->anim_mode, anim->start_frame,
271 animation_frame_counter++);
276 void HandleAnimation(int mode)
278 static unsigned long animstart_delay = -1;
279 static unsigned long animstart_delay_value = 0;
280 static boolean anim_restart = TRUE;
281 static boolean reset_delay = TRUE;
282 static int toon_nr = 0;
291 screen_info.prepare_backbuffer_function();
301 redraw_mask |= (REDRAW_FIELD | REDRAW_FROM_BACKBUFFER);
303 /* Redraw background even when in direct drawing mode */
304 draw_mode = setup.direct_draw;
305 setup.direct_draw = FALSE;
306 screen_info.update_function();
307 setup.direct_draw = draw_mode;
317 animstart_delay = Counter();
318 animstart_delay_value = SimpleRND(3000);
324 if (!DelayReached(&animstart_delay, animstart_delay_value))
327 toon_nr = SimpleRND(screen_info.num_toons);
330 anim_restart = reset_delay = AnimateToon(toon_nr,anim_restart);
335 HandleAnimation(ANIM_START);
340 HandleAnimation(ANIM_STOP);
345 HandleAnimation(ANIM_CONTINUE);