rnd-20051126-1-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 = (num_frames > 1 ? 2 * num_frames - 2 : 1);
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_string_raw)
85 {
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 :
91                    MV_NO_MOVING);
92
93   free(direction_string);
94
95   return direction;
96 }
97
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)
106 {
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;
118 }
119
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)
123 {
124   int buf_x = DOOR_GFX_PAGEX3, buf_y = DOOR_GFX_PAGEY1;
125
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
140   FlushDisplay();
141 }
142
143 boolean AnimateToon(int toon_nr, boolean restart)
144 {
145   static unsigned long animation_frame_counter = 0;
146   static int pos_x = 0, pos_y = 0;
147   static int delta_x = 0, delta_y = 0;
148   static int frame = 0;
149   static boolean horiz_move, vert_move;
150   static unsigned long anim_delay = 0;
151   static unsigned long anim_delay_value = 0;
152   static int width,height;
153   static int pad_x,pad_y;
154   static int cut_x,cut_y;
155   static int src_x, src_y;
156   static int dest_x, dest_y;
157   struct ToonInfo *anim = &screen_info.toons[toon_nr];
158   Bitmap *anim_bitmap = screen_info.toons[toon_nr].bitmap;
159   GC anim_clip_gc = anim_bitmap->stored_clip_gc;
160   int direction = get_toon_direction(anim->direction);
161
162   if (restart)
163   {
164     horiz_move = (direction & (MV_LEFT | MV_RIGHT));
165     vert_move = (direction & (MV_UP | MV_DOWN));
166     anim_delay_value = anim->step_delay * screen_info.frame_delay_value;
167
168     frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
169                               anim->anim_mode, anim->anim_start_frame,
170                               animation_frame_counter++);
171
172     if (horiz_move)
173     {
174       int pos_bottom = screen_info.height - anim->height;
175
176       if (strcmp(anim->position, "top") == 0)
177         pos_y = 0;
178       else if (strcmp(anim->position, "bottom") == 0)
179         pos_y = pos_bottom;
180       else if (strcmp(anim->position, "upper")  == 0)
181         pos_y = SimpleRND(pos_bottom / 2);
182       else if (strcmp(anim->position, "lower")  == 0)
183         pos_y = pos_bottom / 2 + SimpleRND(pos_bottom / 2);
184       else
185         pos_y = SimpleRND(pos_bottom);
186
187       if (direction == MV_RIGHT)
188       {
189         delta_x = anim->step_offset;
190         pos_x = -anim->width + delta_x;
191       }
192       else
193       {
194         delta_x = -anim->step_offset;
195         pos_x = screen_info.width + delta_x;
196       }
197
198       delta_y = 0;
199     }
200     else
201     {
202       int pos_right = screen_info.width - anim->width;
203
204       if (strcmp(anim->position, "left") == 0)
205         pos_x = 0;
206       else if (strcmp(anim->position, "right")  == 0)
207         pos_x = pos_right;
208       else
209         pos_x = SimpleRND(pos_right);
210
211       if (direction == MV_DOWN)
212       {
213         delta_y = anim->step_offset;
214         pos_y = -anim->height + delta_y;
215       }
216       else
217       {
218         delta_y = -anim->step_offset;
219         pos_y = screen_info.width + delta_y;
220       }
221
222       delta_x = 0;
223     }
224   }
225
226   if (pos_x <= -anim->width        - anim->step_offset ||
227       pos_x >=  screen_info.width  + anim->step_offset ||
228       pos_y <= -anim->height       - anim->step_offset ||
229       pos_y >=  screen_info.height + anim->step_offset)
230     return TRUE;
231
232   if (!DelayReached(&anim_delay, anim_delay_value))
233   {
234     if (screen_info.redraw_needed_function() && !restart)
235       DrawAnim(anim_bitmap, anim_clip_gc,
236                src_x + cut_x, src_y + cut_y,
237                width, height,
238                screen_info.startx + dest_x,
239                screen_info.starty + dest_y,
240                pad_x, pad_y);
241
242     return FALSE;
243   }
244
245   if (pos_x < -anim->width)
246     pos_x = -anim->width;
247   else if (pos_x > screen_info.width)
248     pos_x = screen_info.width;
249   if (pos_y < -anim->height)
250     pos_y = -anim->height;
251   else if (pos_y > screen_info.height)
252     pos_y = screen_info.height;
253
254   pad_x = (horiz_move ? anim->step_offset : 0);
255   pad_y = (vert_move  ? anim->step_offset : 0);
256   src_x = anim->src_x + frame * anim->width;
257   src_y = anim->src_y;
258   dest_x = pos_x;
259   dest_y = pos_y;
260   cut_x = cut_y = 0;
261   width  = anim->width;
262   height = anim->height;
263
264   if (pos_x < 0)
265   {
266     dest_x = 0;
267     width += pos_x;
268     cut_x = -pos_x;
269   }
270   else if (pos_x > screen_info.width - anim->width)
271     width -= (pos_x - (screen_info.width - anim->width));
272
273   if (pos_y < 0)
274   {
275     dest_y = 0;
276     height += pos_y;
277     cut_y = -pos_y;
278   }
279   else if (pos_y > screen_info.height - anim->height)
280     height -= (pos_y - (screen_info.height - anim->height));
281
282   DrawAnim(anim_bitmap, anim_clip_gc,
283            src_x + cut_x, src_y + cut_y,
284            width, height,
285            screen_info.startx + dest_x,
286            screen_info.starty + dest_y,
287            pad_x, pad_y);
288
289   pos_x += delta_x;
290   pos_y += delta_y;
291
292   frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
293                             anim->anim_mode, anim->anim_start_frame,
294                             animation_frame_counter++);
295
296   return FALSE;
297 }
298
299 void HandleAnimation(int mode)
300 {
301   static unsigned long animstart_delay = -1;
302   static unsigned long animstart_delay_value = 0;
303   static boolean anim_running = FALSE;
304   static boolean anim_restart = TRUE;
305   static boolean reset_delay = TRUE;
306   static int toon_nr = 0;
307   int draw_mode;
308
309   if (!setup.toons)
310     return;
311
312   /* this may happen after reloading graphics and redefining "num_toons" */
313   if (toon_nr >= screen_info.num_toons)
314     anim_restart = TRUE;
315
316   switch(mode)
317   {
318     case ANIM_START:
319       screen_info.prepare_backbuffer_function();
320
321       anim_running = TRUE;
322       anim_restart = TRUE;
323       reset_delay = TRUE;
324
325       return;
326
327     case ANIM_CONTINUE:
328       if (!anim_running)
329         return;
330
331       break;
332
333     case ANIM_STOP:
334       redraw_mask |= (REDRAW_FIELD | REDRAW_FROM_BACKBUFFER);
335
336       /* Redraw background even when in direct drawing mode */
337       draw_mode = setup.direct_draw;
338       setup.direct_draw = FALSE;
339       screen_info.update_function();
340       setup.direct_draw = draw_mode;
341
342       anim_running = FALSE;
343
344       return;
345
346     default:
347       break;
348   }
349
350   if (reset_delay)
351   {
352     animstart_delay = Counter();
353     animstart_delay_value = SimpleRND(3000);
354     reset_delay = FALSE;
355   }
356
357   if (anim_restart)
358   {
359     if (!DelayReached(&animstart_delay, animstart_delay_value))
360       return;
361
362     toon_nr = SimpleRND(screen_info.num_toons);
363   }
364
365   anim_restart = reset_delay = AnimateToon(toon_nr, anim_restart);
366 }
367
368 void InitAnimation()
369 {
370   HandleAnimation(ANIM_START);
371 }
372
373 void StopAnimation()
374 {
375   HandleAnimation(ANIM_STOP);
376 }
377
378 void DoAnimation()
379 {
380   HandleAnimation(ANIM_CONTINUE);
381 }