Fix accidental switch/teleporter behavior changes
[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 int           state_drawn;
24 static struct state *state;
25
26 struct state *curr_state(void)
27 {
28     return state;
29 }
30
31 float time_state(void)
32 {
33     return state_time;
34 }
35
36 void init_state(struct state *st)
37 {
38     state = st;
39 }
40
41 int goto_state(struct state *st)
42 {
43     struct state *prev = state;
44
45     if (state && state->leave)
46         state->leave(state, st, state->gui_id);
47
48     state       = st;
49     state_time  = 0;
50     state_drawn = 0;
51
52     if (state && state->enter)
53         state->gui_id = state->enter(state, prev);
54
55     return 1;
56 }
57
58 /*---------------------------------------------------------------------------*/
59
60 void st_paint(float t)
61 {
62     int stereo = config_get_d(CONFIG_STEREO);
63
64     state_drawn = 1;
65
66     if (state && state->paint)
67     {
68         if (stereo)
69         {
70             glDrawBuffer(GL_BACK_LEFT);
71             video_clear();
72             state->paint(state->gui_id, t);
73
74             glDrawBuffer(GL_BACK_RIGHT);
75             video_clear();
76             state->paint(state->gui_id, t);
77         }
78         else
79         {
80             video_clear();
81             state->paint(state->gui_id, t);
82         }
83     }
84 }
85
86 void st_timer(float dt)
87 {
88     if (!state_drawn)
89         return;
90
91     state_time += dt;
92
93     if (state && state->timer)
94         state->timer(state->gui_id, dt);
95 }
96
97 void st_point(int x, int y, int dx, int dy)
98 {
99     if (state && state->point)
100         state->point(state->gui_id, x, y, dx, dy);
101 }
102
103 void st_stick(int a, int k)
104 {
105     if ((config_tst_d(CONFIG_JOYSTICK_AXIS_X, a) &&
106          config_get_d(CONFIG_JOYSTICK_AXIS_X_INVERT)) ||
107         (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a) &&
108          config_get_d(CONFIG_JOYSTICK_AXIS_Y_INVERT)) ||
109         (config_tst_d(CONFIG_JOYSTICK_AXIS_U, a) &&
110          config_get_d(CONFIG_JOYSTICK_AXIS_U_INVERT)))
111         k = -k;
112
113     if (state && state->stick)
114         state->stick(state->gui_id, a, k);
115 }
116
117 void st_angle(int x, int z)
118 {
119     if (state && state->angle)
120         state->angle(state->gui_id, x, z);
121 }
122
123 /*---------------------------------------------------------------------------*/
124
125 int st_click(int b, int d)
126 {
127     return (state && state->click) ? state->click(b, d) : 1;
128 }
129
130 int st_keybd(int c, int d)
131 {
132     return (state && state->keybd) ? state->keybd(c, d) : 1;
133 }
134
135 int st_buttn(int b, int d)
136 {
137     return (state && state->buttn) ? state->buttn(b, d) : 1;
138 }
139
140 /*---------------------------------------------------------------------------*/