/
inputhandler.c
55 lines (44 loc) · 1.1 KB
/
inputhandler.c
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
#include "inputhandler.h"
#include "log.h"
struct InputHandler* inputhandler_ctor(struct InputHandler* self)
{
log_assert(self, "is NULL");
//TODO: Change 0 to a more appropriate value
self->events = vec_ctor(SDL_Event, 0);
self->keystate = SDL_GetKeyboardState(NULL);
self->mousestate = SDL_GetMouseState(&self->mousex, &self->mousey);
//TODO: Implement support for multiple controllers
if(SDL_NumJoysticks() && SDL_IsGameController(0)) //Uses the first one
{
self->controller = SDL_GameControllerOpen(0);
}
else
{
self->controller = NULL;
log_warning(
"No compatible game controllers found (%s)",
SDL_GetError()
);
}
return self;
}
void inputhandler_update(struct InputHandler* self)
{
log_assert(self, "is NULL");
vec_clear(self->events);
SDL_Event event;
while(SDL_PollEvent(&event))
{
vec_pushback(self->events, event);
}
self->mousestate = SDL_GetMouseState(&self->mousex, &self->mousey);
}
void inputhandler_dtor(struct InputHandler* self)
{
log_assert(self, "is NULL");
if(self->controller)
{
SDL_GameControllerClose(self->controller);
}
vec_dtor(self->events);
}