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