]> git.bts.cx Git - benzene.git/blob - src_platform/sdl/bz/input/input.c
Initial version
[benzene.git] / src_platform / sdl / bz / input / input.c
1 #include <bz/input/input_internal.h>
2 #include <bz/input/platform.h>
3
4 #include <bz/input/input_id_internal.h>
5 #include <SDL.h>
6
7 typedef enum BZInputCode {
8 BZInputCodeQuit = 1,
9 } BZInputCode;
10 const BZInputCode BZInputCodeMax = BZInputCodeQuit;
11
12 typedef union BZInputTableEntry {
13 bool booleanValue;
14 int integerValue;
15 float floatValue;
16 } BZInputTableEntry;
17
18 static BZInput bzQuitInput = { .id = BZInputCodeQuit, };
19 BZInputID bzQuitInputID = &bzQuitInput;
20
21 const size_t inputTableSize = BZInputCodeMax + 1;
22 static BZInputTableEntry inputTable[inputTableSize];
23
24 typedef struct {
25 bool down;
26 bool pressed;
27 } Button;
28
29 #define kButtonCount 12
30
31 static Button buttons[kButtonCount];
32
33 static SDL_Joystick *joy;
34
35 int buttonIdx(SDL_KeyCode keyCode) {
36 switch (keyCode) {
37 case SDLK_UP:
38 return 1;
39
40 case SDLK_DOWN:
41 return 2;
42
43 case SDLK_LEFT:
44 return 3;
45
46 case SDLK_RIGHT:
47 return 4;
48
49 case 'z':
50 return 5;
51
52 case 'x':
53 return 6;
54
55 case 'w':
56 return 7;
57
58 case 's':
59 return 8;
60
61 case 'a':
62 return 9;
63
64 case 'd':
65 return 10;
66
67 default:
68 return 0;
69 }
70 }
71
72 void bzInputInit() {
73 for (size_t i = 0; i < kButtonCount; ++i) {
74 buttons[i].down = false;
75 }
76
77 for (size_t i = 0, c = SDL_NumJoysticks(); i < c; ++i) {
78 bzLog("Joystick: %s", SDL_JoystickName(i));
79
80 }
81
82 if (SDL_NumJoysticks() > 0) {
83 joy = SDL_JoystickOpen(0);
84 }
85 }
86
87 void bzInputTeardown() {
88
89 }
90
91 void bzInputProcessFrame() {
92 for (size_t i = 0; i < inputTableSize; ++i) {
93 inputTable[i].integerValue = 0; // FIXME, max size and whatever.
94 }
95
96 for (size_t i = 0; i < kButtonCount; ++i) {
97 buttons[i].down = buttons[i].down || buttons[i].pressed;
98 buttons[i].pressed = false;
99 }
100
101 SDL_Event event;
102 while (SDL_PollEvent(&event)) {
103 switch (event.type) {
104 case SDL_QUIT:
105 inputTable[BZInputCodeQuit].booleanValue = true;
106 break;
107
108 case SDL_KEYDOWN:
109 buttons[buttonIdx(event.key.keysym.sym)].pressed = true;
110 break;
111
112 case SDL_KEYUP:
113 buttons[buttonIdx(event.key.keysym.sym)].pressed = false;
114 buttons[buttonIdx(event.key.keysym.sym)].down = false;
115 break;
116 }
117 }
118 }
119
120 bool bzInputGetBooleanValue(BZInputID inputID) {
121 return inputTable[inputID->id].booleanValue;
122 }
123
124 int bzInputGetIntegerValue(BZInputID inputID) {
125 return inputTable[inputID->id].integerValue;
126 }
127
128 float bzInputGetFloatValue(BZInputID inputID) {
129 return inputTable[inputID->id].floatValue;
130 }
131
132 bool bzInputBtn(int id) {
133 return buttons[id].down || buttons[id].pressed;
134 }
135
136 bool bzInputBtnP(int id) {
137 return buttons[id].pressed;
138 }
139