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