-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleInput.cpp
More file actions
211 lines (179 loc) · 4.03 KB
/
ModuleInput.cpp
File metadata and controls
211 lines (179 loc) · 4.03 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "Globals.h"
#include "Application.h"
#include "ModuleInput.h"
#define MAX_KEYS 300
ModuleInput::ModuleInput(Application* app, bool start_enabled) : Module(app, start_enabled)
{
name = "input";
keyboard = new KEY_STATE[MAX_KEYS];
memset(keyboard, KEY_IDLE, sizeof(KEY_STATE) * MAX_KEYS);
memset(mouse_buttons, KEY_IDLE, sizeof(KEY_STATE) * MAX_MOUSE_BUTTONS);
memset(mouse_buttons, KEY_IDLE, sizeof(KEY_STATE) * 15);
}
// Destructor
ModuleInput::~ModuleInput()
{
delete[] keyboard;
}
// Called before render is available
bool ModuleInput::Init()
{
LOG("Init SDL input event system");
bool ret = true;
SDL_Init(0);
if(SDL_InitSubSystem(SDL_INIT_EVENTS) < 0)
{
LOG("SDL_EVENTS could not initialize! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK) < 0)
{
LOG("SDL_JOYSTICK could not initialize! SDL_Error: %s\n", SDL_GetError());
ret = false;
}
jaxis = accel = deaccel = 0;
return ret;
}
// Called every draw update
update_status ModuleInput::PreUpdate(float dt)
{
SDL_PumpEvents();
const Uint8* keys = SDL_GetKeyboardState(NULL);
SDL_Joystick* joystick;
SDL_JoystickEventState(SDL_ENABLE);
joystick = SDL_JoystickOpen(0);
for(int i = 0; i < MAX_KEYS; ++i)
{
if(keys[i] == 1)
{
if(keyboard[i] == KEY_IDLE)
keyboard[i] = KEY_DOWN;
else
keyboard[i] = KEY_REPEAT;
}
else
{
if(keyboard[i] == KEY_REPEAT || keyboard[i] == KEY_DOWN)
keyboard[i] = KEY_UP;
else
keyboard[i] = KEY_IDLE;
}
}
Uint32 buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
mouse_x /= SCREEN_SIZE;
mouse_y /= SCREEN_SIZE;
mouse_z = 0;
for(int i = 0; i < 5; ++i)
{
if(buttons & SDL_BUTTON(i))
{
if(mouse_buttons[i] == KEY_IDLE)
mouse_buttons[i] = KEY_DOWN;
else
mouse_buttons[i] = KEY_REPEAT;
}
else
{
if(mouse_buttons[i] == KEY_REPEAT || mouse_buttons[i] == KEY_DOWN)
mouse_buttons[i] = KEY_UP;
else
mouse_buttons[i] = KEY_IDLE;
}
}
mouse_x_motion = mouse_y_motion = 0;
bool quit = false;
SDL_Event e;
while(SDL_PollEvent(&e))
{
App->editor->CaptureInput(&e);
switch(e.type)
{
case SDL_MOUSEWHEEL:
mouse_z = e.wheel.y;
break;
case SDL_MOUSEMOTION:
mouse_x = e.motion.x / SCREEN_SIZE;
mouse_y = e.motion.y / SCREEN_SIZE;
mouse_x_motion = e.motion.xrel / SCREEN_SIZE;
mouse_y_motion = e.motion.yrel / SCREEN_SIZE;
break;
case SDL_JOYAXISMOTION:
if (e.jaxis.axis == 0)
{
if ((e.jaxis.value < -10000) || (e.jaxis.value > 10000))
{
if (e.jaxis.value < 0)
{
jaxis = (float)e.jaxis.value / 32768;
}
if (e.jaxis.value > 0)
{
jaxis = (float)e.jaxis.value / 32767;
}
}
else
jaxis = 0;
}
if (e.jaxis.axis == 1)
{
/* Up-Down movement code goes here */
}
if (e.jaxis.axis == 2)
{
//mouse_x_motion = (int)e.jaxis.value / 320;
}
if (e.jaxis.axis == 5)
{
if (e.jaxis.value > -32000)
accel = ((float)e.jaxis.value + 32768) / 32767 / 2;
else
accel = 0;
}
if (e.jaxis.axis == 4)
{
if (e.jaxis.value > -32000)
deaccel = ((float)e.jaxis.value + 32768) / 32767 / 2;
else
deaccel = 0;
}
break;
case SDL_JOYBUTTONDOWN:
for (int x = 0; x < 14; x++)
{
if (e.jbutton.button == x)
{
j_buttons[x] = KEY_DOWN;
}
}
break;
case SDL_JOYBUTTONUP:
for (int x = 0; x < 14; x++)
{
if (e.jbutton.button == x)
{
j_buttons[x] = KEY_UP;
}
}
break;
case SDL_QUIT:
quit = true;
break;
case SDL_WINDOWEVENT:
{
if(e.window.event == SDL_WINDOWEVENT_RESIZED)
App->renderer3D->OnResize(e.window.data1, e.window.data2);
}
}
}
if(quit == true || keyboard[SDL_SCANCODE_ESCAPE] == KEY_UP)
return UPDATE_STOP;
return UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleInput::CleanUp()
{
LOG("Quitting SDL input event subsystem");
SDL_QuitSubSystem(SDL_INIT_EVENTS);
SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
return true;
}