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