ball/util: clean up duplicate code
[neverball] / share / state.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 "glext.h"
16 #include "state.h"
17 #include "config.h"
18 #include "video.h"
19
20 /*---------------------------------------------------------------------------*/
21
22 static float  state_time;
23 static struct state *state;
24
25 struct state *curr_state(void)
26 {
27     return state;
28 }
29
30 float time_state(void)
31 {
32     return state_time;
33 }
34
35 void init_state(struct state *st)
36 {
37     state = st;
38 }
39
40 int goto_state(struct state *st)
41 {
42     if (state && state->leave)
43         state->leave(state->gui_id);
44
45     state      = st;
46     state_time =  0;
47
48     if (state && state->enter)
49         state->gui_id = state->enter();
50
51     return 1;
52 }
53
54 /*---------------------------------------------------------------------------*/
55
56 void st_paint(float t)
57 {
58     int stereo = config_get_d(CONFIG_STEREO);
59
60     if (state && state->paint)
61     {
62         if (stereo)
63         {
64             glDrawBuffer(GL_BACK_LEFT);
65             video_clear();
66             state->paint(state->gui_id, t);
67
68             glDrawBuffer(GL_BACK_RIGHT);
69             video_clear();
70             state->paint(state->gui_id, t);
71         }
72         else
73         {
74             video_clear();
75             state->paint(state->gui_id, t);
76         }
77     }
78 }
79
80 void st_timer(float dt)
81 {
82     state_time += dt;
83
84     if (state && state->timer)
85         state->timer(state->gui_id, dt);
86 }
87
88 void st_point(int x, int y, int dx, int dy)
89 {
90     if (state && state->point)
91         state->point(state->gui_id, x, y, dx, dy);
92 }
93
94 void st_stick(int a, int k)
95 {
96     if (state && state->stick)
97         state->stick(state->gui_id, a, k);
98 }
99
100 void st_angle(int x, int z)
101 {
102     if (state && state->angle)
103         state->angle(state->gui_id, x, z);
104 }
105
106 /*---------------------------------------------------------------------------*/
107
108 int st_click(int b, int d)
109 {
110     return (state && state->click) ? state->click(b, d) : 1;
111 }
112
113 int st_keybd(int c, int d)
114 {
115     return (state && state->keybd) ? state->keybd(c, d) : 1;
116 }
117
118 int st_buttn(int b, int d)
119 {
120     return (state && state->buttn) ? state->buttn(b, d) : 1;
121 }
122
123 /*---------------------------------------------------------------------------*/