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