rnd-20030125-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 (anim.random_frame < 0)
68       frame = SimpleRND(num_frames);
69     else
70       frame = 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 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)
92 {
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;
104 }
105
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)
109 {
110   int buf_x = DOOR_GFX_PAGEX3, buf_y = DOOR_GFX_PAGEY1;
111
112 #if 1
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);
121
122   screen_info.update_function();
123
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);
126 #else
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);
135 #endif
136
137   FlushDisplay();
138 }
139
140 boolean AnimateToon(int toon_nr, boolean restart)
141 {
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;
157
158   if (restart)
159   {
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;
163
164     frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
165                               anim->anim_mode, anim->start_frame,
166                               animation_frame_counter++);
167
168     if (horiz_move)
169     {
170       if (anim->position == ANIMPOS_UP)
171         pos_y = 0;
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);
176       else
177         pos_y = SimpleRND(screen_info.height - anim->height);
178
179       if (anim->direction == ANIMDIR_RIGHT)
180       {
181         delta_x = anim->stepsize;
182         pos_x = -anim->width + delta_x;
183       }
184       else
185       {
186         delta_x = -anim->stepsize;
187         pos_x = screen_info.width + delta_x;
188       }
189       delta_y = 0;
190     }
191     else
192     {
193       if (anim->position == ANIMPOS_LEFT)
194         pos_x = 0;
195       else if (anim->position == ANIMPOS_RIGHT)
196         pos_x = screen_info.width - anim->width;
197       else
198         pos_x = SimpleRND(screen_info.width - anim->width);
199
200       if (anim->direction == ANIMDIR_DOWN)
201       {
202         delta_y = anim->stepsize;
203         pos_y = -anim->height + delta_y;
204       }
205       else
206       {
207         delta_y = -anim->stepsize;
208         pos_y = screen_info.width + delta_y;
209       }
210       delta_x = 0;
211     }
212   }
213
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)
218     return TRUE;
219
220   if (!DelayReached(&anim_delay, anim_delay_value))
221   {
222     if (screen_info.redraw_needed_function() && !restart)
223       DrawAnim(anim_bitmap, anim_clip_gc,
224                src_x + cut_x, src_y + cut_y,
225                width, height,
226                screen_info.startx + dest_x,
227                screen_info.starty + dest_y,
228                pad_x, pad_y);
229
230     return FALSE;
231   }
232
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;
241
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;
245   src_y = anim->src_y;
246   dest_x = pos_x;
247   dest_y = pos_y;
248   cut_x = cut_y = 0;
249   width  = anim->width;
250   height = anim->height;
251
252   if (pos_x < 0)
253   {
254     dest_x = 0;
255     width += pos_x;
256     cut_x = -pos_x;
257   }
258   else if (pos_x > screen_info.width - anim->width)
259     width -= (pos_x - (screen_info.width - anim->width));
260
261   if (pos_y < 0)
262   {
263     dest_y = 0;
264     height += pos_y;
265     cut_y = -pos_y;
266   }
267   else if (pos_y > screen_info.height - anim->height)
268     height -= (pos_y - (screen_info.height - anim->height));
269
270   DrawAnim(anim_bitmap, anim_clip_gc,
271            src_x + cut_x, src_y + cut_y,
272            width, height,
273            screen_info.startx + dest_x,
274            screen_info.starty + dest_y,
275            pad_x, pad_y);
276
277   pos_x += delta_x;
278   pos_y += delta_y;
279
280   frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
281                             anim->anim_mode, anim->start_frame,
282                             animation_frame_counter++);
283
284   return FALSE;
285 }
286
287 void HandleAnimation(int mode)
288 {
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;
294   int draw_mode;
295
296   if (!setup.toons)
297     return;
298
299   switch(mode)
300   {
301     case ANIM_START:
302       screen_info.prepare_backbuffer_function();
303       anim_restart = TRUE;
304       reset_delay = TRUE;
305
306       return;
307
308     case ANIM_CONTINUE:
309       break;
310
311     case ANIM_STOP:
312       redraw_mask |= (REDRAW_FIELD | REDRAW_FROM_BACKBUFFER);
313
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;
319
320       return;
321
322     default:
323       break;
324   }
325
326   if (reset_delay)
327   {
328     animstart_delay = Counter();
329     animstart_delay_value = SimpleRND(3000);
330     reset_delay = FALSE;
331   }
332
333   if (anim_restart)
334   {
335     if (!DelayReached(&animstart_delay, animstart_delay_value))
336       return;
337
338     toon_nr = SimpleRND(screen_info.num_toons);
339   }
340
341   anim_restart = reset_delay = AnimateToon(toon_nr,anim_restart);
342 }
343
344 void InitAnimation()
345 {
346   HandleAnimation(ANIM_START);
347 }
348
349 void StopAnimation()
350 {
351   HandleAnimation(ANIM_STOP);
352 }
353
354 void DoAnimation()
355 {
356   HandleAnimation(ANIM_CONTINUE);
357 }