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