8d0a8ba53a57d515fcecac5b469f5a621c40ec4d
[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, GC toon_clip_gc,
123               int src_x, int src_y, int width, int height,
124               int dest_x, int dest_y, int pad_x, int pad_y)
125 {
126   int pad_dest_x = dest_x - pad_x;
127   int pad_dest_y = dest_y - pad_y;
128   int pad_width  = width  + 2 * pad_x;
129   int pad_height = height + 2 * pad_y;
130   int buffer_x = 0;
131   int buffer_y = 0;
132
133   /* correct values to avoid off-screen blitting (start position) */
134   if (pad_dest_x < screen_info.startx)
135   {
136     pad_width -= (screen_info.startx - pad_dest_x);
137     pad_dest_x = screen_info.startx;
138   }
139   if (pad_dest_y < screen_info.starty)
140   {
141     pad_height -= (screen_info.starty - pad_dest_y);
142     pad_dest_y = screen_info.starty;
143   }
144
145   /* correct values to avoid off-screen blitting (blit size) */
146   if (pad_width > screen_info.width)
147     pad_width = screen_info.width;
148   if (pad_height > screen_info.height)
149     pad_height = screen_info.height;
150
151   /* special method to avoid flickering interference with BackToFront() */
152   BlitBitmap(backbuffer, screen_info.save_buffer, pad_dest_x, pad_dest_y,
153              pad_width, pad_height, buffer_x, buffer_y);
154   SetClipOrigin(toon_bitmap, toon_clip_gc, dest_x - src_x, dest_y - src_y);
155   BlitBitmapMasked(toon_bitmap, backbuffer, src_x, src_y, width, height,
156                    dest_x, dest_y);
157   BlitBitmap(backbuffer, window, pad_dest_x, pad_dest_y, pad_width, pad_height,
158              pad_dest_x, pad_dest_y);
159
160   screen_info.update_function();
161
162   BlitBitmap(screen_info.save_buffer, backbuffer, buffer_x, buffer_y,
163              pad_width, pad_height, pad_dest_x, pad_dest_y);
164
165   FlushDisplay();
166 }
167
168 boolean AnimateToon(int toon_nr, boolean restart)
169 {
170   static unsigned int animation_frame_counter = 0;
171   static int pos_x = 0, pos_y = 0;
172   static int delta_x = 0, delta_y = 0;
173   static int frame = 0;
174   static boolean horiz_move, vert_move;
175   static unsigned int anim_delay = 0;
176   static unsigned int anim_delay_value = 0;
177   static int width,height;
178   static int pad_x,pad_y;
179   static int cut_x,cut_y;
180   static int src_x, src_y;
181   static int dest_x, dest_y;
182   struct ToonInfo *anim = &screen_info.toons[toon_nr];
183   Bitmap *anim_bitmap = screen_info.toons[toon_nr].bitmap;
184   GC anim_clip_gc = anim_bitmap->stored_clip_gc;
185   int direction = get_toon_direction(anim->direction);
186
187   if (restart)
188   {
189     horiz_move = (direction & (MV_LEFT | MV_RIGHT));
190     vert_move = (direction & (MV_UP | MV_DOWN));
191     anim_delay_value = anim->step_delay * screen_info.frame_delay_value;
192
193     frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
194                               anim->anim_mode, anim->anim_start_frame,
195                               animation_frame_counter++);
196
197     if (horiz_move)
198     {
199       int pos_bottom = screen_info.height - anim->height;
200
201       if (strEqual(anim->position, "top"))
202         pos_y = 0;
203       else if (strEqual(anim->position, "bottom"))
204         pos_y = pos_bottom;
205       else if (strEqual(anim->position, "upper"))
206         pos_y = GetSimpleRandom(pos_bottom / 2);
207       else if (strEqual(anim->position, "lower"))
208         pos_y = pos_bottom / 2 + GetSimpleRandom(pos_bottom / 2);
209       else
210         pos_y = GetSimpleRandom(pos_bottom);
211
212       if (direction == MV_RIGHT)
213       {
214         delta_x = anim->step_offset;
215         pos_x = -anim->width + delta_x;
216       }
217       else
218       {
219         delta_x = -anim->step_offset;
220         pos_x = screen_info.width + delta_x;
221       }
222
223       delta_y = 0;
224     }
225     else
226     {
227       int pos_right = screen_info.width - anim->width;
228
229       if (strEqual(anim->position, "left"))
230         pos_x = 0;
231       else if (strEqual(anim->position, "right"))
232         pos_x = pos_right;
233       else
234         pos_x = GetSimpleRandom(pos_right);
235
236       if (direction == MV_DOWN)
237       {
238         delta_y = anim->step_offset;
239         pos_y = -anim->height + delta_y;
240       }
241       else
242       {
243         delta_y = -anim->step_offset;
244         pos_y = screen_info.height + delta_y;
245       }
246
247       delta_x = 0;
248     }
249   }
250
251   if (pos_x <= -anim->width        - anim->step_offset ||
252       pos_x >=  screen_info.width  + anim->step_offset ||
253       pos_y <= -anim->height       - anim->step_offset ||
254       pos_y >=  screen_info.height + anim->step_offset)
255     return TRUE;
256
257   if (!DelayReached(&anim_delay, anim_delay_value))
258   {
259     if (screen_info.redraw_needed_function() && !restart)
260       DrawAnim(anim_bitmap, anim_clip_gc,
261                src_x + cut_x, src_y + cut_y,
262                width, height,
263                screen_info.startx + dest_x,
264                screen_info.starty + dest_y,
265                pad_x, pad_y);
266
267     return FALSE;
268   }
269
270   if (pos_x < -anim->width)
271     pos_x = -anim->width;
272   else if (pos_x > screen_info.width)
273     pos_x = screen_info.width;
274   if (pos_y < -anim->height)
275     pos_y = -anim->height;
276   else if (pos_y > screen_info.height)
277     pos_y = screen_info.height;
278
279   pad_x = (horiz_move ? anim->step_offset : 0);
280   pad_y = (vert_move  ? anim->step_offset : 0);
281   src_x = anim->src_x + frame * anim->width;
282   src_y = anim->src_y;
283   dest_x = pos_x;
284   dest_y = pos_y;
285   cut_x = cut_y = 0;
286   width  = anim->width;
287   height = anim->height;
288
289   if (pos_x < 0)
290   {
291     dest_x = 0;
292     width += pos_x;
293     cut_x = -pos_x;
294   }
295   else if (pos_x > screen_info.width - anim->width)
296     width -= (pos_x - (screen_info.width - anim->width));
297
298   if (pos_y < 0)
299   {
300     dest_y = 0;
301     height += pos_y;
302     cut_y = -pos_y;
303   }
304   else if (pos_y > screen_info.height - anim->height)
305     height -= (pos_y - (screen_info.height - anim->height));
306
307   DrawAnim(anim_bitmap, anim_clip_gc,
308            src_x + cut_x, src_y + cut_y,
309            width, height,
310            screen_info.startx + dest_x,
311            screen_info.starty + dest_y,
312            pad_x, pad_y);
313
314   pos_x += delta_x;
315   pos_y += delta_y;
316
317   frame = getAnimationFrame(anim->anim_frames, anim->anim_delay,
318                             anim->anim_mode, anim->anim_start_frame,
319                             animation_frame_counter++);
320
321   return FALSE;
322 }
323
324 void HandleAnimation(int mode)
325 {
326   static unsigned int animstart_delay = -1;
327   static unsigned int animstart_delay_value = 0;
328   static boolean anim_running = FALSE;
329   static boolean anim_restart = TRUE;
330   static boolean reset_delay = TRUE;
331   static int toon_nr = 0;
332
333   if (!setup.toons || screen_info.num_toons == 0)
334     return;
335
336   /* this may happen after reloading graphics and redefining "num_toons" */
337   if (toon_nr >= screen_info.num_toons)
338     anim_restart = TRUE;
339
340   switch(mode)
341   {
342     case ANIM_START:
343       screen_info.prepare_backbuffer_function();
344
345       anim_running = TRUE;
346       anim_restart = TRUE;
347       reset_delay = TRUE;
348
349       return;
350
351     case ANIM_CONTINUE:
352       if (!anim_running)
353         return;
354
355       break;
356
357     case ANIM_STOP:
358       if (anim_running)
359       {
360         redraw_mask |= (REDRAW_FIELD | REDRAW_FROM_BACKBUFFER);
361
362         screen_info.update_function();
363
364         anim_running = FALSE;
365       }
366
367       return;
368
369     default:
370       break;
371   }
372
373   if (reset_delay)
374   {
375     animstart_delay = Counter();
376     animstart_delay_value = GetSimpleRandom(3000);
377     reset_delay = FALSE;
378   }
379
380   if (anim_restart)
381   {
382     if (!DelayReached(&animstart_delay, animstart_delay_value))
383       return;
384
385     toon_nr = GetSimpleRandom(screen_info.num_toons);
386   }
387
388   anim_restart = reset_delay = AnimateToon(toon_nr, anim_restart);
389 }
390
391 void InitAnimation()
392 {
393   HandleAnimation(ANIM_START);
394 }
395
396 void StopAnimation()
397 {
398   HandleAnimation(ANIM_STOP);
399 }
400
401 void DoAnimation()
402 {
403   HandleAnimation(ANIM_CONTINUE);
404 }