-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoystickModule.cpp
183 lines (159 loc) · 5.25 KB
/
JoystickModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "JoystickModule.hpp"
#include <Graphics/Graphics.hpp>
#include <Graphics/Font.hpp>
#include <Vector/VectorGL.hpp>
#include "shapes.hpp"
using std::cout;
namespace {
SDL_Joystick *joystick = NULL;
bool joystick_inited = false;
Module *create_joystickmodule(const std::string ¶ms) {
if (!joystick_inited) {
joystick_inited = true;
SDL_InitSubSystem(SDL_INIT_JOYSTICK);
joystick = NULL;
int joysticks = SDL_NumJoysticks();
if (joysticks > 0) {
cout << " Found " << joysticks << " joysticks." << endl;
int found = 0;
while (found < joysticks && SDL_JoystickOpened(found)) ++found;
if (found < joysticks) {
cout << " Using joystick #" << found << ":" << endl;
joystick = SDL_JoystickOpen(found);
if (joystick) {
cout << " Name: " << SDL_JoystickName(found) << endl;
cout << " Axes: " << SDL_JoystickNumAxes(joystick) << endl;
cout << " Buttons: " << SDL_JoystickNumButtons(joystick) << endl;
cout << " Balls: " << SDL_JoystickNumBalls(joystick) << endl;
} else {
cout << " Couldn't open!" << endl;
}
} else {
cout << " All already in use." << endl;
}
} else {
cout << " Found no joysticks." << endl;
}
}
return new JoystickModule();
}
class Fred {
public:
Fred() {
register_module("joystick", create_joystickmodule);
}
} fred;
}
Vector2f JoystickModule::size() {
return make_vector(1.0f, 1.0f);
}
void JoystickModule::draw(Box2f viewport, Box2f screen_viewport, float scale, unsigned int recurse) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glBoxToBox(viewport, screen_viewport);
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
const Vector3f highlight = make_vector(0.9f, 0.9f, 0.9f);
const Vector3f neutral = make_vector(0.6f, 0.6f, 0.6f);
const Vector3f shade = make_vector(0.3f, 0.3f, 0.3f);
float ang = -control().vel / 3.0f * float(M_PI) / 6.0f + float(M_PI) * 0.5f;
const Vector2f body = make_vector(0.6f, 0.2f);
const Vector2f stick = make_vector(0.1f, 0.65f);
{ //stick
Vector2f stick_start = make_vector(0.0f, -0.5f + body.y * 0.5f);
Vector2f stick_end = make_vector(cosf(ang) * stick.y, -0.5f + body.y * 0.5f + sinf(ang) * stick.y);
Vector2f perp = perpendicular(stick_end - stick_start) * (stick.x / stick.y) * 0.5f;
glBegin(GL_QUADS);
glColor(neutral);
glVertex(stick_start + perp);
glColor(shade);
glVertex(stick_start - perp);
glColor(neutral);
glVertex(stick_end - perp);
glColor(highlight);
glVertex(stick_end + perp);
glEnd();
glColor(highlight);
circle< 17 >(0.1f, stick_end);
}
{ //button
const Vector2f button = make_vector(0.1f, 0.1f);
float mod = 1.0f;
if (control().jump) mod = 0.1f;
glBegin(GL_QUADS);
glColor(neutral); glVertex2f(body.x * -0.5f + button.x * 0.1f, -0.5f + body.y);
glColor(highlight); glVertex2f(body.x * -0.5f + button.x * 0.1f, -0.5f + body.y + button.y * mod);
glColor(neutral); glVertex2f(body.x * -0.5f + button.x * 1.1f, -0.5f + body.y + button.y * mod);
glColor(shade); glVertex2f(body.x * -0.5f + button.x * 1.1f, -0.5f + body.y);
glEnd();
}
glBegin(GL_QUADS);
glColor(neutral); glVertex2f(body.x * -0.5f, -0.5f);
glColor(highlight); glVertex2f(body.x * -0.5f, -0.5f + body.y);
glColor(neutral); glVertex2f(body.x * 0.5f, -0.5f + body.y);
glColor(shade); glVertex2f(body.x * 0.5f, -0.5f);
glEnd();
glColor3f(1.0f, 1.0f, 1.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
Graphics::gl_errors("Joystick::draw");
}
void JoystickModule::update(float elapsed_time) {
if (joystick && !set) {
SDL_JoystickUpdate();
control().vel = 0.0f;
if (SDL_JoystickNumAxes(joystick) > 0) {
control().vel = (float)SDL_JoystickGetAxis(joystick, 0);
control().vel /= 32768.0f;
//Dead zone stuff:
if (fabs(control().vel) < 0.1f) control().vel = 0.0f;
else if (control().vel < 0.0f) control().vel += 0.1f;
else if (control().vel > 0.0f) control().vel -= 0.1f;
control().vel *= 1.0f / 0.9f;
}
control().vel *= 3.0f;
control().jump = false;
if (SDL_JoystickNumButtons(joystick) > 2) {
control().jump = SDL_JoystickGetButton(joystick, 2);
}
}
}
bool JoystickModule::handle_event(SDL_Event const &event, Vector2f local_mouse) {
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_LEFT) {
set = true;
control().vel = local_mouse.x * 6.0f;
if (control().vel < -3.0f) control().vel = -3.0f;
if (control().vel > 3.0f) control().vel = 3.0f;
return true;
}
if (event.type == SDL_MOUSEMOTION && (event.motion.state & SDL_BUTTON(SDL_BUTTON_LEFT))) {
set = true;
control().vel = local_mouse.x * 6.0f;
if (control().vel < -3.0f) control().vel = -3.0f;
if (control().vel > 3.0f) control().vel = 3.0f;
return true;
}
if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_SPACE) {
set = true;
control().jump = !control().jump;
return true;
}
if (event.type == SDL_MOUSEBUTTONDOWN && event.button.button == SDL_BUTTON_RIGHT) {
control().jump = false;
control().vel = 0.0f;
set = false;
return true;
}
return false;
}