-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.c
More file actions
108 lines (93 loc) · 3.43 KB
/
input.c
File metadata and controls
108 lines (93 loc) · 3.43 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
/******************************************************************************
File: input.c
Created: 2019-06-21
Updated: 2019-08-03
Author: Aaron Oman
Notice: Creative Commons Attribution 4.0 International License (CC-BY 4.0)
******************************************************************************/
#include "SDL2/SDL.h"
#include "system.h"
//! \file input.c
//! The number of keys in the CHIP-8
#define NUM_KEYS 16
//! \brief Keypress state. Unexported.
struct input {
SDL_Keycode keycodeIndices[NUM_KEYS];
};
struct input *InputInit() {
struct input *i = (struct input *)malloc(sizeof(struct input));
memset(i, 0, sizeof(struct input));
// TODO: Support remapping of keys.
i->keycodeIndices[1] = SDLK_q;
i->keycodeIndices[2] = SDLK_w;
i->keycodeIndices[3] = SDLK_e;
i->keycodeIndices[0xC] = SDLK_r;
i->keycodeIndices[4] = SDLK_a;
i->keycodeIndices[5] = SDLK_s;
i->keycodeIndices[6] = SDLK_d;
i->keycodeIndices[0xD] = SDLK_f;
i->keycodeIndices[7] = SDLK_u;
i->keycodeIndices[8] = SDLK_i;
i->keycodeIndices[9] = SDLK_o;
i->keycodeIndices[0xE] = SDLK_p;
i->keycodeIndices[0xA] = SDLK_j;
i->keycodeIndices[0] = SDLK_k;
i->keycodeIndices[0xB] = SDLK_l;
i->keycodeIndices[0xF] = SDLK_SEMICOLON;
return i;
}
void InputDeinit(struct input *i) {
if (NULL == i)
return;
free(i);
}
//! \brief Key "down" event handler
//! \param[in,out] input Input state to be updated
//! \param[in,out] system CHIP-8 system state to be updated
//! \param[in] keycode Keycode mapping which key has been pressed
void HandleKeyDown(struct input *input, struct system *system, SDL_Keycode keycode) {
if (NULL == input)
return;
for (int i = 0; i < NUM_KEYS; i++) {
if (keycode == input->keycodeIndices[i]) {
SystemKeySetPressed(system, i, 1);
if (SystemWFKWaiting(system)) {
SystemWFKOccurred(system, i);
}
break;
}
}
}
//! \brief Key "up" event handler
//! \param[in,out] input Input state to be updated
//! \param[in,out] system CHIP-8 system state to be updated
//! \param[in] keycode Keycode maping which key has been released
void HandleKeyUp(struct input *input, struct system *system, SDL_Keycode keycode) {
if (NULL == input)
return;
for (int i = 0; i < NUM_KEYS; i++) {
if (keycode == input->keycodeIndices[i]) {
SystemKeySetPressed(system, i, 0);
break;
}
}
}
void InputCheck(struct input *i, struct system *s, SDL_Event *event) {
if (NULL == i)
return;
switch (event->type) {
case SDL_QUIT:
SystemSignalQuit(s);
break;
case SDL_KEYUP:
HandleKeyUp(i, s, event->key.keysym.sym);
break;
case SDL_KEYDOWN:
if (event->key.keysym.sym == SDLK_ESCAPE) {
SystemSignalQuit(s);
break;
}
HandleKeyDown(i, s, event->key.keysym.sym);
break;
}
}