Allow camera switching during ready/set stage
[neverball] / ball / hud.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <SDL.h>
16 #include <math.h>
17 #include <string.h>
18
19 #include "glext.h"
20 #include "hud.h"
21 #include "gui.h"
22 #include "progress.h"
23 #include "config.h"
24 #include "video.h"
25 #include "audio.h"
26
27 #include "game_common.h"
28 #include "game_client.h"
29
30 /*---------------------------------------------------------------------------*/
31
32 static int Lhud_id;
33 static int Rhud_id;
34 static int time_id;
35
36 static int coin_id;
37 static int ball_id;
38 static int scor_id;
39 static int goal_id;
40 static int view_id;
41 static int fps_id;
42
43 static float view_timer;
44
45 static void hud_fps(void)
46 {
47     gui_set_count(fps_id, video_perf());
48 }
49
50 void hud_init(void)
51 {
52     int id;
53     const char *str_view;
54     int v;
55
56     if ((Rhud_id = gui_hstack(0)))
57     {
58         if ((id = gui_vstack(Rhud_id)))
59         {
60             gui_label(id, _("Coins"), GUI_SML, 0, gui_wht, gui_wht);
61             gui_label(id, _("Goal"),  GUI_SML, 0, gui_wht, gui_wht);
62         }
63         if ((id = gui_vstack(Rhud_id)))
64         {
65             coin_id = gui_count(id, 100, GUI_SML, GUI_NW);
66             goal_id = gui_count(id, 10,  GUI_SML, 0);
67         }
68         gui_layout(Rhud_id, +1, -1);
69     }
70
71     if ((Lhud_id = gui_hstack(0)))
72     {
73         if ((id = gui_vstack(Lhud_id)))
74         {
75             ball_id = gui_count(id, 10,   GUI_SML, GUI_NE);
76             scor_id = gui_count(id, 1000, GUI_SML, 0);
77         }
78         if ((id = gui_vstack(Lhud_id)))
79         {
80             gui_label(id, _("Balls"), GUI_SML, 0, gui_wht, gui_wht);
81             gui_label(id, _("Score"), GUI_SML, 0, gui_wht, gui_wht);
82         }
83         gui_layout(Lhud_id, -1, -1);
84     }
85
86     if ((time_id = gui_clock(0, 59999, GUI_MED, GUI_TOP)))
87         gui_layout(time_id, 0, -1);
88
89
90     /* Find the longest view name. */
91
92     for (str_view = "", v = VIEW_NONE + 1; v < VIEW_MAX; v++)
93         if (strlen(view_to_str(v)) > strlen(str_view))
94             str_view = view_to_str(v);
95
96     if ((view_id = gui_label(0, str_view, GUI_SML, GUI_SW, gui_wht, gui_wht)))
97         gui_layout(view_id, 1, 1);
98
99     if ((fps_id = gui_count(0, 1000, GUI_SML, GUI_SE)))
100         gui_layout(fps_id, -1, 1);
101 }
102
103 void hud_free(void)
104 {
105     gui_delete(Rhud_id);
106     gui_delete(Lhud_id);
107     gui_delete(time_id);
108     gui_delete(view_id);
109     gui_delete(fps_id);
110 }
111
112 void hud_paint(void)
113 {
114     if (curr_mode() == MODE_CHALLENGE)
115         gui_paint(Lhud_id);
116
117     gui_paint(Rhud_id);
118     gui_paint(time_id);
119
120     if (config_get_d(CONFIG_FPS))
121         gui_paint(fps_id);
122
123     hud_view_paint();
124 }
125
126 void hud_update(int pulse)
127 {
128     int clock = curr_clock();
129     int coins = curr_coins();
130     int goal  = curr_goal();
131     int balls = curr_balls();
132     int score = curr_score();
133
134     int c_id;
135     int last;
136
137     if (!pulse)
138     {
139         /* reset the hud */
140
141         gui_pulse(ball_id, 0.f);
142         gui_pulse(time_id, 0.f);
143         gui_pulse(coin_id, 0.f);
144     }
145
146     /* time and tick-tock */
147
148     if (clock != (last = gui_value(time_id)))
149     {
150         gui_set_clock(time_id, clock);
151
152         if (last > clock && pulse)
153         {
154             if (clock <= 1000 && (last / 100) > (clock / 100))
155             {
156                 audio_play(AUD_TICK, 1.f);
157                 gui_pulse(time_id, 1.50);
158             }
159             else if (clock < 500 && (last / 50) > (clock / 50))
160             {
161                 audio_play(AUD_TOCK, 1.f);
162                 gui_pulse(time_id, 1.25);
163             }
164         }
165     }
166
167     /* balls and score + select coin widget */
168
169     switch (curr_mode())
170     {
171     case MODE_CHALLENGE:
172         if (gui_value(ball_id) != balls) gui_set_count(ball_id, balls);
173         if (gui_value(scor_id) != score) gui_set_count(scor_id, score);
174
175         c_id = coin_id;
176         break;
177
178     default:
179         c_id = coin_id;
180         break;
181     }
182
183
184     /* coins and pulse */
185
186     if (coins != (last = gui_value(c_id)))
187     {
188         last = coins - last;
189
190         gui_set_count(c_id, coins);
191
192         if (pulse && last > 0)
193         {
194             if      (last >= 10) gui_pulse(coin_id, 2.00f);
195             else if (last >=  5) gui_pulse(coin_id, 1.50f);
196             else                 gui_pulse(coin_id, 1.25f);
197
198             if (goal > 0)
199             {
200                 if      (last >= 10) gui_pulse(goal_id, 2.00f);
201                 else if (last >=  5) gui_pulse(goal_id, 1.50f);
202                 else                 gui_pulse(goal_id, 1.25f);
203             }
204         }
205     }
206
207     /* goal and pulse */
208
209     if (goal != (last = gui_value(goal_id)))
210     {
211         gui_set_count(goal_id, goal);
212
213         if (pulse && goal == 0 && last > 0)
214             gui_pulse(goal_id, 2.00f);
215     }
216
217     if (config_get_d(CONFIG_FPS))
218         hud_fps();
219 }
220
221 void hud_timer(float dt)
222 {
223
224     hud_update(1);
225
226     gui_timer(Rhud_id, dt);
227     gui_timer(Lhud_id, dt);
228     gui_timer(time_id, dt);
229
230     hud_view_timer(dt);
231 }
232
233 /*---------------------------------------------------------------------------*/
234
235 void hud_view_pulse(int c)
236 {
237     gui_set_label(view_id, view_to_str(c));
238     gui_pulse(view_id, 1.2f);
239     view_timer = 2.0f;
240 }
241
242 void hud_view_timer(float dt)
243 {
244     view_timer -= dt;
245     gui_timer(view_id, dt);
246 }
247
248 void hud_view_paint(void)
249 {
250     if (view_timer > 0.0f)
251         gui_paint(view_id);
252 }
253
254 /*---------------------------------------------------------------------------*/