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