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