-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleInput.cpp
72 lines (61 loc) · 1.8 KB
/
ModuleInput.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
#include "Globals.h"
#include "Application.h"
#include "ModuleInput.h"
#include "ModuleRender.h"
#include "SDL/include/SDL.h"
ModuleInput::ModuleInput() : Module()
{}
// Destructor
ModuleInput::~ModuleInput()
{}
// 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;
return ret;
}
LOG("Events initialized succesfully!\n\n");
for (int i = 4; i < 285; i++) keyboardstate[i] = KEY_OUT;
for (int i = 0; i < 285; i++)keyboard2[i] = false;
space = false;
return ret;
}
// Called every draw update
update_status ModuleInput::PreUpdate()
{
SDL_Event quitevent;
SDL_PollEvent(&quitevent);
if(quitevent.type==SDL_QUIT)return update_status::UPDATE_STOP;
SDL_PumpEvents();
keyboard = SDL_GetKeyboardState(NULL);
for (int i = 4; i < 285; i++) {
if (keyboard[i] == 1||keyboard2[i]==1) {
if (keyboardstate[i] == KEY_PUSHED || keyboardstate[i]==KEY_REPEAT) keyboardstate[i] = KEY_REPEAT;
else keyboardstate[i] = KEY_PUSHED;
}
else {
if (keyboardstate[i] == KEY_PUSHED || keyboardstate[i] ==KEY_REPEAT) keyboardstate[i] = KEY_PULLED;
else keyboardstate[i] = KEY_OUT;
}
}
if (keyboardstate[SDL_SCANCODE_ESCAPE] == KEY_PUSHED) {
LOG("Escape pressed, exiting the game.\n");
return update_status::UPDATE_STOP;
}
if (keyboardstate[SDL_SCANCODE_SPACE] == KEY_PUSHED || keyboardstate[SDL_SCANCODE_SPACE] == KEY_REPEAT) space = true;
else if (keyboardstate[SDL_SCANCODE_SPACE] == KEY_PULLED) space = false;
return update_status::UPDATE_CONTINUE;
}
// Called before quitting
bool ModuleInput::CleanUp()
{
LOG("Quitting SDL input event subsystem");
SDL_QuitSubSystem(SDL_INIT_EVENTS);
return true;
}