cd11f67c84cd19adb0ba83b66f8e45d4e22dec43
[neverball] / putt / hud.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERPUTT 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 "gui.h"
19 #include "hud.h"
20 #include "hole.h"
21 #include "config.h"
22
23 /*---------------------------------------------------------------------------*/
24
25 static int Lhud_id;
26 static int Rhud_id;
27 static int fps_id;
28
29 /*---------------------------------------------------------------------------*/
30
31 void hud_init(void)
32 {
33     static const float *color[5] = {
34         gui_wht,
35         gui_red,
36         gui_grn,
37         gui_blu,
38         gui_yel
39     };
40     int i = curr_player();
41
42     if ((Lhud_id = gui_hstack(0)))
43     {
44         gui_label(Lhud_id, curr_scr(), GUI_MED, GUI_NE, color[i], gui_wht);
45         gui_label(Lhud_id, _("Score"), GUI_SML, 0,      gui_wht,  gui_wht);
46         gui_layout(Lhud_id, -1, -1);
47     }
48     if ((Rhud_id = gui_hstack(0)))
49     {
50         gui_label(Rhud_id, curr_par(), GUI_MED, 0,      color[i], gui_wht);
51         gui_label(Rhud_id, _("Par"),   GUI_SML, GUI_NW, gui_wht,  gui_wht);
52         gui_layout(Rhud_id, +1, -1);
53     }
54     if ((fps_id = gui_count(0, 1000, GUI_SML, GUI_SE)))
55         gui_layout(fps_id, -1, +1);
56 }
57
58 void hud_free(void)
59 {
60     gui_delete(Lhud_id);
61     gui_delete(Rhud_id);
62     gui_delete(fps_id);
63 }
64
65 /*---------------------------------------------------------------------------*/
66
67 void hud_paint(void)
68 {
69     static int fps   = 0;
70     static int then  = 0;
71     static int count = 0;
72
73     int now = SDL_GetTicks();
74
75     if (now - then > 250)
76     {
77         fps   = count * 1000 / (now - then);
78         then  = now;
79         count = 0;
80
81         gui_set_count(fps_id, fps);
82     }
83     else count++;
84
85     if (config_get_d(CONFIG_FPS))
86         gui_paint(fps_id);
87
88     gui_paint(Rhud_id);
89     gui_paint(Lhud_id);
90 }
91
92 /*---------------------------------------------------------------------------*/