fix 'free mode' score handling
[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 "level.h"
24 #include "config.h"
25
26 /*---------------------------------------------------------------------------*/
27
28 static int Lhud_id;
29 static int Rhud_id;
30 static int Rhud2_id;
31 static int time_id;
32
33 static int coin_id;
34 static int coin2_id;
35 static int ball_id;
36 static int scor_id;
37 static int goal_id;
38 static int view_id;
39 static int fps_id;
40
41 static float view_timer;
42
43 static void hud_fps(void)
44 {
45     static int fps   = 0;
46     static int then  = 0;
47     static int count = 0;
48
49     int now = SDL_GetTicks();
50
51     if (now - then > 250)
52     {
53         fps   = count * 1000 / (now - then);
54         then  = now;
55         count = 0;
56
57         gui_set_count(fps_id, fps);
58     }
59     else count++;
60 }
61
62 void hud_init(void)
63 {
64     int id;
65     char * str_view;
66
67     if ((Rhud_id = gui_hstack(0)))
68     {
69         if ((id = gui_vstack(Rhud_id)))
70         {
71             gui_label(id, _("Coins"), GUI_SML, 0, gui_wht, gui_wht);
72             gui_label(id, _("Goal"),  GUI_SML, 0, gui_wht, gui_wht);
73         }
74         if ((id = gui_vstack(Rhud_id)))
75         {
76             coin_id = gui_count(id, 100, GUI_SML, GUI_NW);
77             goal_id = gui_count(id, 10,  GUI_SML, 0);
78         }
79         gui_layout(Rhud_id, +1, -1);
80     }
81     
82     if ((Rhud2_id = gui_hstack(0)))
83     {
84         gui_label(Rhud2_id, _("Coins"), GUI_SML, 0, gui_wht, gui_wht);
85         coin2_id = gui_count(Rhud2_id, 100,   GUI_SML, GUI_NW);
86         gui_layout(Rhud2_id, +1, -1);
87     }
88
89     if ((Lhud_id = gui_hstack(0)))
90     {
91         if ((id = gui_vstack(Lhud_id)))
92         {
93             ball_id = gui_count(id, 10,   GUI_SML, GUI_NE);
94             scor_id = gui_count(id, 1000, GUI_SML, 0);
95         }
96         if ((id = gui_vstack(Lhud_id)))
97         {
98             gui_label(id, _("Balls"), GUI_SML, 0, gui_wht, gui_wht);
99             gui_label(id, _("Score"), GUI_SML, 0, gui_wht, gui_wht);
100         }
101         gui_layout(Lhud_id, -1, -1);
102     }
103
104     if ((time_id = gui_clock(0, 59999, GUI_MED, GUI_TOP)))
105         gui_layout(time_id, 0, -1);
106
107     str_view = strlen(STR_VIEW0) > strlen(STR_VIEW1) ? STR_VIEW0 : STR_VIEW1;
108     if (strlen(str_view) < strlen(STR_VIEW2))
109         str_view = STR_VIEW2;
110     if ((view_id = gui_label(0, str_view, GUI_SML, GUI_SW, gui_wht, gui_wht)))
111         gui_layout(view_id, 1, 1);
112
113     if ((fps_id = gui_count(0, 1000, GUI_SML, GUI_SE)))
114         gui_layout(fps_id, -1, 1);
115 }
116
117 void hud_free(void)
118 {
119     gui_delete(Rhud_id);
120     gui_delete(Lhud_id);
121     gui_delete(time_id);
122     gui_delete(view_id);
123     gui_delete(fps_id);
124 }
125
126 void hud_paint(void)
127 {
128     int mode = level_mode();
129     if (mode == MODE_CHALLENGE)
130         gui_paint(Lhud_id);
131     if (mode == MODE_FREE)
132         gui_paint(Rhud2_id);
133     else
134         gui_paint(Rhud_id);
135     gui_paint(time_id);
136
137     if (config_get_d(CONFIG_FPS))
138         gui_paint(fps_id);
139
140     if (view_timer > 0.0f)
141         gui_paint(view_id);
142 }
143
144 void hud_timer(float dt)
145 {
146     const int clock = curr_clock();
147     const int balls = curr_balls();
148     const int coins = curr_coins();
149     const int score = curr_score();
150     const int goal  = curr_goal();
151     int mode = level_mode();
152     int c_id;
153
154     if (gui_value(time_id) != clock) gui_set_clock(time_id, clock);
155     if (mode == MODE_CHALLENGE)
156     {
157         if (gui_value(ball_id) != balls) gui_set_count(ball_id, balls);
158         if (gui_value(scor_id) != score) gui_set_count(scor_id, score);
159         c_id = coin_id;
160     } else if (mode == MODE_FREE)
161         c_id = coin2_id;
162     else
163         c_id = coin_id;
164     
165     
166     if (gui_value(c_id) != coins)
167         gui_set_count(c_id, coins);
168     if (gui_value(goal_id) != goal)  gui_set_count(goal_id, goal);
169
170     if (config_get_d(CONFIG_FPS))
171         hud_fps();
172
173     view_timer -= dt;
174
175     gui_timer(Rhud_id, dt);
176     gui_timer(Lhud_id, dt);
177     gui_timer(time_id, dt);
178     gui_timer(view_id, dt);
179 }
180
181 void hud_ball_pulse(float k) { gui_pulse(ball_id, k); }
182 void hud_time_pulse(float k) { gui_pulse(time_id, k); }
183 void hud_coin_pulse(float k) { gui_pulse(coin_id, k); }
184 void hud_goal_pulse(float k) { gui_pulse(goal_id, k); }
185
186 void hud_view_pulse(int c)
187 {
188     switch (c)
189     {   
190     case 0: gui_set_label(view_id, STR_VIEW0); break;
191     case 1: gui_set_label(view_id, STR_VIEW1); break;
192     case 2: gui_set_label(view_id, STR_VIEW2); break;
193     }
194
195     gui_pulse(view_id, 1.2f);
196     view_timer = 2.0f;
197 }
198
199 /*---------------------------------------------------------------------------*/