fixed path to external icons
[shermanaquarium] / sherman-aquarium / shermans / bubble.c
1
2 #include <stdlib.h>
3 #include <string.h>
4
5
6 #include "aquarium.h"
7 #include "bubble.h"
8 #include "draw.h"
9 #include "soundeffects.h"
10
11 /* current bubble count */
12 static int nr_bubbles;
13
14 static Bubble *bubbles = NULL;
15 static SA_Image bubbles_image_data;
16
17
18 static int bubble_state_change;
19
20 static Bubble_settings bubble_settings;
21
22 Bubble_settings *bubble_get_settings_ptr(void)
23 {
24     return &bubble_settings;
25 }
26
27 void bubble_exit(void)
28 {
29     if(bubbles_image_data.pixbuf != NULL)
30         g_object_unref(bubbles_image_data.pixbuf);
31     memset(&bubbles_image_data, 0, sizeof(SA_Image));
32 }
33
34 void bubble_init(void)
35 {
36     AquariumData *ad;
37
38     ad = aquarium_get_settings_ptr();
39
40     nr_bubbles = 0;
41     bubble_state_change = ad->ymax / (BUBBLES_FRAMES - 1);
42
43
44     if(bubbles!=NULL)
45         g_free(bubbles);
46
47     bubbles = g_malloc0(sizeof(Bubble) * bubble_settings.max_bubbles);
48
49     /* Load bubbles */
50
51     load_image("bubbles.png", &bubbles_image_data, BUBBLES_FRAMES);
52
53 }
54
55 void bubble_update(void)
56 {
57     int i, x, y;
58     int seq;
59     AquariumData *ad;
60
61     ad = aquarium_get_settings_ptr();
62
63
64     for(i = 0; i < nr_bubbles; i++){
65         aquarium_clean_image(bubbles[i].x, bubbles[i].y,
66                              bubbles_image_data.width, 
67                              bubbles_image_data.height);
68     }
69
70
71     /* make a new bubble, if needed */
72     if(((nr_bubbles < bubble_settings.max_bubbles)) && (g_rand_int_range(ad->rnd, 0, 100) <= 32)) {
73         bubbles[nr_bubbles].x = ad->viewpoint_start_x + (g_rand_int_range(ad->rnd, 0, ad->xmax));
74         bubbles[nr_bubbles].y = ad->viewpoint_start_y + ad->ymax;
75
76         bubbles[nr_bubbles].speed = g_rand_double_range(ad->rnd, 1.0/13.0, 12.0/13.0);
77
78         nr_bubbles++;
79     }
80
81
82
83     /* Update and draw the bubbles */
84     for (i = 0; i < nr_bubbles; i++) {
85
86         /* Move the bubble vertically */
87         bubbles[i].y -= bubbles[i].speed;
88
89         /* Did we lose it? */
90         if (bubbles[i].y < ad->viewpoint_start_y) {
91             /* Yes; nuke it */
92             bubbles[i].x = bubbles[nr_bubbles - 1].x;
93             bubbles[i].y = bubbles[nr_bubbles - 1].y;
94             bubbles[i].speed = bubbles[nr_bubbles - 1].speed;
95             nr_bubbles--;
96
97             /* We must check the previously last bubble, which is
98              * now the current bubble, also. */
99             i--;
100             continue;
101         }
102
103         /* Draw the bubble */
104         x = bubbles[i].x - ad->viewpoint_start_x;
105         y = ((int) bubbles[i].y) - ad->viewpoint_start_y;
106
107         /* calculate bubble sequence - 0 to 4 (determine bubble sprite idx) */
108         seq = y / bubble_state_change;
109
110         /* draw the bubble, using offset-to-center calculated from current
111          * sequence, and make the bubble bigger as we go up. 120 - alpha */
112
113         if(seq < BUBBLES_FRAMES){
114             aquarium_draw_pic_alpha(&bubbles_image_data, bubbles_image_data.width, 
115                            bubbles_image_data.height, x, y,
116                            seq, 120);
117         }
118
119     }
120
121     /* Sometimes play bubble sound */
122     sound_bubbles();
123 }