Initial push
[shermanaquarium] / sherman-aquarium / shermans / over.c
1
2 /* This is a layer between main and different functions that can be called
3    when the mouse pointer is over */
4
5
6 #include <stdio.h>
7 #include "aquarium.h"
8 #include "over.h"
9 #include "draw.h"
10 #include "matrix.h"
11 #include "plasma.h"
12 #include "tetris.h"
13
14 static int fade, old_over = -1;
15 static unsigned char *screen;
16 static Over_settings over_settings;
17 static AquariumData *ad;
18
19
20 Over_settings *over_get_settings_ptr(void)
21 {
22     return &over_settings;
23 }
24
25 void over_keypress(int key)
26 {
27     if(over_settings.type == OVER_TETRIS)
28         tetris_keypress(key);
29
30 }
31 void over_joystick(GaiFlagsJoystick jflags)
32 {
33     if(over_settings.type == OVER_TETRIS)
34         tetris_joystick(jflags);
35 }
36
37 void over_init(void)
38 {
39     GaiFlagsType gf;
40
41     screen=NULL;
42     fade=0;
43
44     ad = aquarium_get_settings_ptr();
45
46     
47     if(over_settings.cursor_off)
48         gf = GAI_FLAGS_MOUSE_PTR_HIDE;
49     else
50         gf = GAI_FLAGS_MOUSE_PTR_SHOW;
51
52     gf |= GAI_FLAGS_ALLOW_ROTATE;
53
54     gai_flags_set(gf);
55
56     if(over_settings.type == OVER_OFF)
57         return;
58
59     old_over = over_settings.type;
60
61     if(over_settings.type == OVER_MATRIX)
62         matrix_init();
63     if(over_settings.type == OVER_PLASMA)
64         plasma_init();
65     if(over_settings.type == OVER_TETRIS)
66         tetris_init();
67 }
68
69 void over_start(void)
70 {
71     if(screen != NULL){
72         g_free(screen);
73         screen = NULL;
74     }
75
76     if(over_settings.type == OVER_OFF)
77         return;
78
79     screen = g_malloc0((ad->xmax+2)*4*(ad->ymax+2));
80
81     if(over_settings.type == OVER_MATRIX)
82         matrix_start();
83     if(over_settings.type == OVER_PLASMA)
84         plasma_start();
85     if(over_settings.type == OVER_TETRIS)
86         tetris_start();
87 }
88
89 void over_end(void)
90 {
91     if(screen != NULL)
92         g_free(screen);
93
94     screen = NULL;
95
96     if(over_settings.type == OVER_OFF)
97         return;
98
99     if(over_settings.type == OVER_MATRIX)
100         matrix_end();
101     if(over_settings.type == OVER_PLASMA)
102         plasma_end();
103     if(over_settings.type == OVER_TETRIS)
104         tetris_end();
105 }
106
107 void over_exit(void)
108 {
109
110
111     if(screen!=NULL)
112         g_free(screen);
113
114     screen=NULL;
115
116     if(old_over == OVER_MATRIX)
117         matrix_exit();
118     if(old_over == OVER_PLASMA)
119         plasma_exit();
120     if(old_over == OVER_TETRIS)
121         tetris_exit();
122 }
123
124
125
126 int over_update(int mouse_over)
127 {
128    
129     int full_over=0;
130   
131
132
133     if(over_settings.type == OVER_OFF)
134         return 0;
135
136     /* All is faded up and mouse is over, return */
137
138     if(fade==0 && !mouse_over) return 0;
139
140     if(mouse_over){
141
142         if(fade==0) 
143             over_start();
144
145         if(fade<255) 
146             fade+=5;
147
148         if(!over_settings.fade) 
149             fade=255;
150     }
151     else{
152         if(fade>0)
153             fade-=5;
154
155         if(!over_settings.fade) 
156             fade=0;
157
158         if(fade==0){ 
159             over_end();
160             return 0;
161         }
162
163     }
164
165     if(fade==0) full_over = 1;
166
167     if(over_settings.type == OVER_MATRIX)
168         matrix_update();
169     if(over_settings.type == OVER_PLASMA)
170         plasma_update();
171     if(over_settings.type == OVER_TETRIS)
172         tetris_update();
173
174     draw_pic_alpha(screen, ad->xmax, ad->ymax, 0, 0, 0, fade);
175
176     return full_over;
177 }
178
179
180
181 /* This one is very alike to draw_fish - Only small differences.*/
182
183 void over_draw(int x, int y, int idx, int width, int height, unsigned char *pic)
184 {
185     /* bounding box of the clipped sprite */
186     int dw, di, dh, ds;
187
188     int w, h, pos, fidx, ypos, pic_pos;
189
190     /* completely off the screen, don't bother drawing */
191     if ((y < (-height)) || (y > (ad->ymax)) || (x > (ad->xmax)) || (x < -(width)))
192         return;
193
194
195     /* do clipping for top side */
196     ds = 0;
197     if (y < 0)
198         ds = -(y);
199
200     /* do clipping for bottom side */
201     dh = height;
202     if ((y + height) > ad->ymax)
203         dh = ad->ymax - y;
204
205     /* do clipping for right side */
206     dw = width;
207     if ((x + width) > ad->xmax)
208         dw = width - ((x + width) - ad->xmax);
209
210     /* do clipping for left side */
211     di = 0;
212     if (x < 0)
213         di = -(x);
214
215     fidx = width * height * 4 * idx;
216
217     for (h = ds; h < dh; h++) {
218         /* offset to beginning of current row */
219         ypos = (h + y) * ad->xmax * 4;
220         for (w = di; w < dw; w++) {
221             pic_pos = h * width * 4 + w * 4 + fidx;
222             pos = ypos + w * 4 + x * 4;
223             if (pic[pic_pos + 3] != 0) {
224                 screen[pos] = pic[pic_pos];
225                 screen[pos + 1] = pic[pic_pos + 1];
226                 screen[pos + 2] = pic[pic_pos + 2];
227                 screen[pos + 3] = 1;
228             }
229         }
230
231     }
232
233     
234 }