991248fc9848762775a793a6ca681e80289535e0
[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 #include "common.h"
20
21 /*---------------------------------------------------------------------------*/
22
23 static float         state_time;
24 static int           state_drawn;
25 static struct state *state;
26
27 struct state *curr_state(void)
28 {
29     return state;
30 }
31
32 float time_state(void)
33 {
34     return state_time;
35 }
36
37 void init_state(struct state *st)
38 {
39     state = st;
40 }
41
42 int goto_state(struct state *st)
43 {
44     struct state *prev = state;
45
46     if (state && state->leave)
47         state->leave(state, st, state->gui_id);
48
49     state       = st;
50     state_time  = 0;
51     state_drawn = 0;
52
53     if (state && state->enter)
54         state->gui_id = state->enter(state, prev);
55
56     return 1;
57 }
58
59 /*---------------------------------------------------------------------------*/
60
61 void st_paint(float t)
62 {
63     int stereo = config_get_d(CONFIG_STEREO);
64
65     state_drawn = 1;
66
67     if (state && state->paint)
68     {
69         if (stereo)
70         {
71             glDrawBuffer(GL_BACK_LEFT);
72             video_clear();
73             state->paint(state->gui_id, t);
74
75             glDrawBuffer(GL_BACK_RIGHT);
76             video_clear();
77             state->paint(state->gui_id, t);
78         }
79         else
80         {
81             video_clear();
82             state->paint(state->gui_id, t);
83         }
84     }
85 }
86
87 void st_timer(float dt)
88 {
89     if (!state_drawn)
90         return;
91
92     state_time += dt;
93
94     if (state && state->timer)
95         state->timer(state->gui_id, dt);
96 }
97
98 void st_point(int x, int y, int dx, int dy)
99 {
100     if (state && state->point)
101         state->point(state->gui_id, x, y, dx, dy);
102 }
103
104 void st_stick(int a, float v)
105 {
106     static struct
107     {
108         const int *num;
109         const int *inv;
110
111         float prev;
112     } axes[] = {
113         { &CONFIG_JOYSTICK_AXIS_X, &CONFIG_JOYSTICK_AXIS_X_INVERT },
114         { &CONFIG_JOYSTICK_AXIS_Y, &CONFIG_JOYSTICK_AXIS_Y_INVERT },
115         { &CONFIG_JOYSTICK_AXIS_U, &CONFIG_JOYSTICK_AXIS_U_INVERT }
116     };
117
118     int i, bump = 0;
119
120     for (i = 0; i < ARRAYSIZE(axes); i++)
121         if (config_tst_d(*axes[i].num, a))
122         {
123             float p = axes[i].prev;
124
125             /* Note the transition from centered to leaned position. */
126
127             bump = ((-0.5f <= p && p <= +0.5f) &&
128                     (v < -0.5f || +0.5f < v));
129
130             axes[i].prev = v;
131
132             if (config_get_d(*axes[i].inv))
133                 v = -v;
134
135             break;
136         }
137
138     if (state && state->stick)
139         state->stick(state->gui_id, a, v, bump);
140 }
141
142 void st_angle(int x, int z)
143 {
144     if (state && state->angle)
145         state->angle(state->gui_id, x, z);
146 }
147
148 /*---------------------------------------------------------------------------*/
149
150 int st_click(int b, int d)
151 {
152     return (state && state->click) ? state->click(b, d) : 1;
153 }
154
155 int st_keybd(int c, int d)
156 {
157     return (state && state->keybd) ? state->keybd(c, d) : 1;
158 }
159
160 int st_buttn(int b, int d)
161 {
162     return (state && state->buttn) ? state->buttn(b, d) : 1;
163 }
164
165 /*---------------------------------------------------------------------------*/