rnd-20030823-2-src
[rocksndiamonds.git] / src / libgame / toons.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1995-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * toons.c                                                  *
12 ***********************************************************/
13
14 #include "toons.h"
15 #include "misc.h"
16
17
18 /* values for toon animation */
19 #define ANIM_START      0
20 #define ANIM_CONTINUE   1
21 #define ANIM_STOP       2
22
23
24 static struct ToonScreenInfo screen_info;
25
26
27 /* ========================================================================= */
28 /* generic animation frame calculation                                       */
29 /* ========================================================================= */
30
31 int getAnimationFrame(int num_frames, int delay, int mode, int start_frame,
32                       int sync_frame)
33 {
34   int frame = 0;
35
36   sync_frame += start_frame * delay;
37
38   if (mode & ANIM_LOOP)                 /* looping animation */
39   {
40     frame = (sync_frame % (delay * num_frames)) / delay;
41   }
42   else if (mode & ANIM_LINEAR)          /* linear (non-looping) animation */
43   {
44     frame = sync_frame / delay;
45
46     if (frame > num_frames - 1)
47       frame = num_frames - 1;
48   }
49   else if (mode & ANIM_PINGPONG)        /* oscillate (border frames once) */
50   {
51     int max_anim_frames = 2 * num_frames - 2;
52
53     frame = (sync_frame % (delay * max_anim_frames)) / delay;
54     frame = (frame < num_frames ? frame : max_anim_frames - frame);
55   }
56   else if (mode & ANIM_PINGPONG2)       /* oscillate (border frames twice) */
57   {
58     int max_anim_frames = 2 * num_frames;
59
60     frame = (sync_frame % (delay * max_anim_frames)) / delay;
61     frame = (frame < num_frames ? frame : max_anim_frames - frame - 1);
62   }
63   else if (mode & ANIM_RANDOM)          /* play frames in random order */
64   {
65     /* note: expect different frames for the same delay cycle! */
66
67     if (gfx.anim_random_frame < 0)
68       frame = SimpleRND(num_frames);
69     else
70       frame = gfx.anim_random_frame % num_frames;
71   }
72
73   if (mode & ANIM_REVERSE)              /* use reverse animation direction */
74     frame = num_frames - frame - 1;
75
76   return frame;
77 }
78
79
80 /* ========================================================================= */
81 /* toon animation functions                                                  */
82 /* ========================================================================= */
83
84 static int get_toon_direction(char *direction_raw)
85 {
86   static char *direction = NULL;
87
88   /* !!! MEMORY LEAK HERE! FIX IT! !!! */
89   setString(&direction, getStringToLower(direction_raw));
90
91   return (strcmp(direction, "left")  == 0 ? MV_LEFT :
92           strcmp(direction, "right") == 0 ? MV_RIGHT :
93           strcmp(direction, "up")    == 0 ? MV_UP :
94           strcmp(direction, "down")  == 0 ? MV_DOWN : MV_NO_MOVING);
95 }
96
97 void InitToonScreen(Bitmap *save_buffer,
98                     void (*update_function)(void),
99                     void (*prepare_backbuffer_function)(void),
100                     boolean (*redraw_needed_function)(void),
101                     struct ToonInfo *toons, int num_toons,
102                     int startx, int starty,
103                     int width, int height,
104                     int frame_delay_value)
105 {
106   screen_info.save_buffer = save_buffer;
107   screen_info.update_function = update_function;
108   screen_info.prepare_backbuffer_function = prepare_backbuffer_function;
109   screen_info.redraw_needed_function = redraw_needed_function;
110   screen_info.toons = toons;
111   screen_info.num_toons = num_toons;
112   screen_info.startx = startx;
113   screen_info.starty = starty;
114   screen_info.width = width;
115   screen_info.height = height;
116   screen_info.frame_delay_value = frame_delay_value;
117 }
118
119 void DrawAnim(Bitmap *toon_bitmap, GC toon_clip_gc,
120               int src_x, int src_y, int width, int height,
121               int dest_x, int dest_y, int pad_x, int pad_y)
122 {
123   int buf_x = DOOR_GFX_PAGEX3, buf_y = DOOR_GFX_PAGEY1;
124
125 #if 1
126   /* special method to avoid flickering interference with BackToFront() */
127   BlitBitmap(backbuffer, screen_info.save_buffer, dest_x-pad_x, dest_y-pad_y,
128              width+2*pad_x, height+2*pad_y, buf_x, buf_y);
129   SetClipOrigin(toon_bitmap, toon_clip_gc, dest_x-src_x, dest_y-src_y);
130   BlitBitmapMasked(toon_bitmap, backbuffer,
131                    src_x, src_y, width, height, dest_x, dest_y);
132   BlitBitmap(backbuffer, window, dest_x-pad_x, dest_y-pad_y,
133              width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
134
135   screen_info.update_function();
136
137   BlitBitmap(screen_info.save_buffer, backbuffer, buf_x, buf_y,
138             width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
139 #else
140   /* normal method, causing flickering interference with BackToFront() */
141   BlitBitmap(backbuffer, screen_info.save_buffer, dest_x-pad_x, dest_y-pad_y,
142              width+2*pad_x, height+2*pad_y, buf_x, buf_y);
143   SetClipOrigin(toon_bitmap,toon_clip_gc, buf_x-src_x+pad_x,buf_y-src_y+pad_y);
144   BlitBitmapMasked(toon_bitmap, screen_info.save_buffer,
145                    src_x, src_y, width, height, buf_x+pad_x, buf_y+pad_y);
146   BlitBitmap(screen_info.save_buffer, window, buf_x, buf_y,
147              width+2*pad_x, height+2*pad_y, dest_x-pad_x, dest_y-pad_y);
148 #endif
149
150   FlushDisplay();
151 }
152
153 boolean AnimateToon(int toon_nr, boolean restart)
154 {
155   static unsigned long animation_frame_counter = 0;
156   static int pos_x = 0, pos_y = 0;
157   static int delta_x = 0, delta_y = 0;
158   static int frame = 0;
159   static boolean horiz_move, vert_move;
160   static unsigned long anim_delay = 0;
161   static unsigned long anim_delay_value = 0;
162   static int width,height;
163   static int pad_x,pad_y;
164   static int cut_x,cut_y;
165   static int src_x, src_y;
166   static int dest_x, dest_y;
167   struct ToonInfo *anim = &screen_info.toons[toon_nr];
168   Bitmap *anim_bitmap = screen_info.toons[toon_nr].bitmap;
169   GC anim_clip_gc = anim_bitmap->stored_clip_gc;
170   int direction = get_toon_direction(anim->direction);
171
172   if (restart)
173   {
174     horiz_move = (direction & (MV_LEFT | MV_RIGHT));
175     vert_move = (direction & (MV_UP | MV_DOWN));
176     anim_delay_value = anim->step_delay * screen_info.frame_delay_value;
177
178     frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
179                               anim->anim_mode, anim->anim_start_frame,
180                               animation_frame_counter++);
181
182     if (horiz_move)
183     {
184       int pos_bottom = screen_info.height - anim->height;
185
186       if (strcmp(anim->position, "top") == 0)
187         pos_y = 0;
188       else if (strcmp(anim->position, "bottom") == 0)
189         pos_y = pos_bottom;
190       else if (strcmp(anim->position, "upper")  == 0)
191         pos_y = SimpleRND(pos_bottom / 2);
192       else if (strcmp(anim->position, "lower")  == 0)
193         pos_y = pos_bottom / 2 + SimpleRND(pos_bottom / 2);
194       else
195         pos_y = SimpleRND(pos_bottom);
196
197       if (direction == MV_RIGHT)
198       {
199         delta_x = anim->step_offset;
200         pos_x = -anim->width + delta_x;
201       }
202       else
203       {
204         delta_x = -anim->step_offset;
205         pos_x = screen_info.width + delta_x;
206       }
207
208       delta_y = 0;
209     }
210     else
211     {
212       int pos_right = screen_info.width - anim->width;
213
214       if (strcmp(anim->position, "left") == 0)
215         pos_x = 0;
216       else if (strcmp(anim->position, "right")  == 0)
217         pos_x = pos_right;
218       else
219         pos_x = SimpleRND(pos_right);
220
221       if (direction == MV_DOWN)
222       {
223         delta_y = anim->step_offset;
224         pos_y = -anim->height + delta_y;
225       }
226       else
227       {
228         delta_y = -anim->step_offset;
229         pos_y = screen_info.width + delta_y;
230       }
231
232       delta_x = 0;
233     }
234   }
235
236   if (pos_x <= -anim->width        - anim->step_offset ||
237       pos_x >=  screen_info.width  + anim->step_offset ||
238       pos_y <= -anim->height       - anim->step_offset ||
239       pos_y >=  screen_info.height + anim->step_offset)
240     return TRUE;
241
242   if (!DelayReached(&anim_delay, anim_delay_value))
243   {
244     if (screen_info.redraw_needed_function() && !restart)
245       DrawAnim(anim_bitmap, anim_clip_gc,
246                src_x + cut_x, src_y + cut_y,
247                width, height,
248                screen_info.startx + dest_x,
249                screen_info.starty + dest_y,
250                pad_x, pad_y);
251
252     return FALSE;
253   }
254
255   if (pos_x < -anim->width)
256     pos_x = -anim->width;
257   else if (pos_x > screen_info.width)
258     pos_x = screen_info.width;
259   if (pos_y < -anim->height)
260     pos_y = -anim->height;
261   else if (pos_y > screen_info.height)
262     pos_y = screen_info.height;
263
264   pad_x = (horiz_move ? anim->step_offset : 0);
265   pad_y = (vert_move  ? anim->step_offset : 0);
266   src_x = anim->src_x + frame * anim->width;
267   src_y = anim->src_y;
268   dest_x = pos_x;
269   dest_y = pos_y;
270   cut_x = cut_y = 0;
271   width  = anim->width;
272   height = anim->height;
273
274   if (pos_x < 0)
275   {
276     dest_x = 0;
277     width += pos_x;
278     cut_x = -pos_x;
279   }
280   else if (pos_x > screen_info.width - anim->width)
281     width -= (pos_x - (screen_info.width - anim->width));
282
283   if (pos_y < 0)
284   {
285     dest_y = 0;
286     height += pos_y;
287     cut_y = -pos_y;
288   }
289   else if (pos_y > screen_info.height - anim->height)
290     height -= (pos_y - (screen_info.height - anim->height));
291
292   DrawAnim(anim_bitmap, anim_clip_gc,
293            src_x + cut_x, src_y + cut_y,
294            width, height,
295            screen_info.startx + dest_x,
296            screen_info.starty + dest_y,
297            pad_x, pad_y);
298
299   pos_x += delta_x;
300   pos_y += delta_y;
301
302   frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
303                             anim->anim_mode, anim->anim_start_frame,
304                             animation_frame_counter++);
305
306   return FALSE;
307 }
308
309 void HandleAnimation(int mode)
310 {
311   static unsigned long animstart_delay = -1;
312   static unsigned long animstart_delay_value = 0;
313   static boolean anim_running = FALSE;
314   static boolean anim_restart = TRUE;
315   static boolean reset_delay = TRUE;
316   static int toon_nr = 0;
317   int draw_mode;
318
319   if (!setup.toons)
320     return;
321
322   /* this may happen after reloading graphics and redefining "num_toons" */
323   if (toon_nr >= screen_info.num_toons)
324     anim_restart = TRUE;
325
326   switch(mode)
327   {
328     case ANIM_START:
329       screen_info.prepare_backbuffer_function();
330
331       anim_running = TRUE;
332       anim_restart = TRUE;
333       reset_delay = TRUE;
334
335       return;
336
337     case ANIM_CONTINUE:
338       if (!anim_running)
339         return;
340
341       break;
342
343     case ANIM_STOP:
344       redraw_mask |= (REDRAW_FIELD | REDRAW_FROM_BACKBUFFER);
345
346       /* Redraw background even when in direct drawing mode */
347       draw_mode = setup.direct_draw;
348       setup.direct_draw = FALSE;
349       screen_info.update_function();
350       setup.direct_draw = draw_mode;
351
352       anim_running = FALSE;
353
354       return;
355
356     default:
357       break;
358   }
359
360   if (reset_delay)
361   {
362     animstart_delay = Counter();
363     animstart_delay_value = SimpleRND(3000);
364     reset_delay = FALSE;
365   }
366
367   if (anim_restart)
368   {
369     if (!DelayReached(&animstart_delay, animstart_delay_value))
370       return;
371
372     toon_nr = SimpleRND(screen_info.num_toons);
373   }
374
375   anim_restart = reset_delay = AnimateToon(toon_nr, anim_restart);
376 }
377
378 void InitAnimation()
379 {
380   HandleAnimation(ANIM_START);
381 }
382
383 void StopAnimation()
384 {
385   HandleAnimation(ANIM_STOP);
386 }
387
388 void DoAnimation()
389 {
390   HandleAnimation(ANIM_CONTINUE);
391 }