Revert "Merged progression and putt-collisions"
[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 "game.h"
23 #include "levels.h"
24 #include "config.h"
25 #include "audio.h"
26
27 /*---------------------------------------------------------------------------*/
28
29 static int Lhud_id;
30 static int Rhud_id;
31 static int Rhud2_id;
32 static int time_id;
33
34 static int coin_id;
35 static int coin2_id;
36 static int ball_id;
37 static int scor_id;
38 static int goal_id;
39 static int view_id;
40 static int fps_id;
41
42 static float view_timer;
43
44 static void hud_fps(void)
45 {
46     gui_set_count(fps_id, config_perf());
47 }
48
49 void hud_init(void)
50 {
51     int id;
52     char *str_view;
53
54     if ((Rhud_id = gui_hstack(0)))
55     {
56         if ((id = gui_vstack(Rhud_id)))
57         {
58             gui_label(id, _("Coins"), GUI_SML, 0, gui_wht, gui_wht);
59             gui_label(id, _("Goal"),  GUI_SML, 0, gui_wht, gui_wht);
60         }
61         if ((id = gui_vstack(Rhud_id)))
62         {
63             coin_id = gui_count(id, 100, GUI_SML, GUI_NW);
64             goal_id = gui_count(id, 10,  GUI_SML, 0);
65         }
66         gui_layout(Rhud_id, +1, -1);
67     }
68
69     if ((Rhud2_id = gui_hstack(0)))
70     {
71         gui_label(Rhud2_id, _("Coins"), GUI_SML, 0, gui_wht, gui_wht);
72         coin2_id = gui_count(Rhud2_id, 100,   GUI_SML, GUI_NW);
73         gui_layout(Rhud2_id, +1, -1);
74     }
75
76     if ((Lhud_id = gui_hstack(0)))
77     {
78         if ((id = gui_vstack(Lhud_id)))
79         {
80             ball_id = gui_count(id, 10,   GUI_SML, GUI_NE);
81             scor_id = gui_count(id, 1000, GUI_SML, 0);
82         }
83         if ((id = gui_vstack(Lhud_id)))
84         {
85             gui_label(id, _("Balls"), GUI_SML, 0, gui_wht, gui_wht);
86             gui_label(id, _("Score"), GUI_SML, 0, gui_wht, gui_wht);
87         }
88         gui_layout(Lhud_id, -1, -1);
89     }
90
91     if ((time_id = gui_clock(0, 59999, GUI_MED, GUI_TOP)))
92         gui_layout(time_id, 0, -1);
93
94     str_view = strlen(STR_VIEW0) > strlen(STR_VIEW1) ? STR_VIEW0 : STR_VIEW1;
95     if (strlen(str_view) < strlen(STR_VIEW2))
96         str_view = STR_VIEW2;
97     if ((view_id = gui_label(0, str_view, GUI_SML, GUI_SW, gui_wht, gui_wht)))
98         gui_layout(view_id, 1, 1);
99
100     if ((fps_id = gui_count(0, 1000, GUI_SML, GUI_SE)))
101         gui_layout(fps_id, -1, 1);
102 }
103
104 void hud_free(void)
105 {
106     gui_delete(Rhud_id);
107     gui_delete(Lhud_id);
108     gui_delete(time_id);
109     gui_delete(view_id);
110     gui_delete(fps_id);
111 }
112
113 void hud_paint(void)
114 {
115     int mode = curr_lg()->mode;
116
117     if (mode == MODE_CHALLENGE)
118         gui_paint(Lhud_id);
119
120     if (mode == MODE_PRACTICE)
121         gui_paint(Rhud2_id);
122     else
123         gui_paint(Rhud_id);
124
125     gui_paint(time_id);
126
127     if (config_get_d(CONFIG_FPS))
128         gui_paint(fps_id);
129
130     if (view_timer > 0.0f)
131         gui_paint(view_id);
132 }
133
134 void hud_update(int pulse)
135 {
136     const struct level_game *lg = curr_lg();
137
138     int clock = curr_clock();
139     int coins = curr_coins();
140     int goal  = curr_goal();
141     int balls = lg->balls;
142     int score = lg->score;
143     int mode  = lg->mode;
144
145     int c_id;
146     int last;
147
148     if (!pulse)
149     {
150         /* reset the hud */
151
152         gui_pulse(ball_id, 0.f);
153         gui_pulse(time_id, 0.f);
154         gui_pulse(coin_id, 0.f);
155     }
156
157     /* time and tick-tock */
158
159     if (clock != (last = gui_value(time_id)))
160     {
161         gui_set_clock(time_id, clock);
162
163         if (last > clock && pulse && mode != MODE_PRACTICE)
164         {
165             if (clock <= 1000 && (last / 100) > (clock / 100))
166             {
167                 audio_play(AUD_TICK, 1.f);
168                 gui_pulse(time_id, 1.50);
169             }
170             else if (clock < 500 && (last / 50) > (clock / 50))
171             {
172                 audio_play(AUD_TOCK, 1.f);
173                 gui_pulse(time_id, 1.25);
174             }
175         }
176     }
177
178     /* balls and score + select coin widget */
179
180     switch (mode)
181     {
182     case MODE_CHALLENGE:
183         if (gui_value(ball_id) != balls) gui_set_count(ball_id, balls);
184         if (gui_value(scor_id) != score) gui_set_count(scor_id, score);
185
186         c_id = coin_id;
187         break;
188
189     case MODE_NORMAL:
190         c_id = coin_id;
191         break;
192
193     default:
194         c_id = coin2_id;
195         break;
196     }
197
198
199     /* coins and pulse */
200
201     if (coins != (last = gui_value(c_id)))
202     {
203         last = coins - last;
204
205         gui_set_count(c_id, coins);
206
207         if (pulse && last > 0)
208         {
209             if      (last >= 10) gui_pulse(coin_id, 2.00f);
210             else if (last >=  5) gui_pulse(coin_id, 1.50f);
211             else                 gui_pulse(coin_id, 1.25f);
212
213             if (goal > 0)
214             {
215                 if      (last >= 10) gui_pulse(goal_id, 2.00f);
216                 else if (last >=  5) gui_pulse(goal_id, 1.50f);
217                 else                 gui_pulse(goal_id, 1.25f);
218             }
219         }
220     }
221
222     /* goal and pulse */
223
224     if (goal != (last = gui_value(goal_id)))
225     {
226         gui_set_count(goal_id, goal);
227
228         if (pulse && goal == 0 && last > 0)
229             gui_pulse(goal_id, 2.00f);
230     }
231
232     if (config_get_d(CONFIG_FPS))
233         hud_fps();
234 }
235
236 void hud_timer(float dt)
237 {
238
239     hud_update(1);
240
241     view_timer -= dt;
242
243     gui_timer(Rhud_id, dt);
244     gui_timer(Lhud_id, dt);
245     gui_timer(time_id, dt);
246     gui_timer(view_id, dt);
247 }
248
249 void hud_view_pulse(int c)
250 {
251     switch (c)
252     {
253     case 0: gui_set_label(view_id, STR_VIEW0); break;
254     case 1: gui_set_label(view_id, STR_VIEW1); break;
255     case 2: gui_set_label(view_id, STR_VIEW2); break;
256     }
257
258     gui_pulse(view_id, 1.2f);
259     view_timer = 2.0f;
260 }
261
262 /*---------------------------------------------------------------------------*/