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