]> git.bts.cx Git - benzene.git/blob - src_platform/playdate/bz/input/input.c
Sprites
[benzene.git] / src_platform / playdate / 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 <playdate/entrypoint.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 #define inputTableSize 8
23 static BZInputTableEntry inputTable[inputTableSize];
24
25 typedef struct {
26 bool down;
27 bool pressed;
28 } Button;
29
30 static Button buttons[8];
31
32 /*int buttonIdx(SDL_KeyCode keyCode) {
33 switch (keyCode) {
34 case SDLK_UP:
35 return 1;
36
37 case SDLK_DOWN:
38 return 2;
39
40 case SDLK_LEFT:
41 return 3;
42
43 case SDLK_RIGHT:
44 return 4;
45
46 case 'z':
47 return 5;
48
49 case 'x':
50 return 6;
51
52 default:
53 return 0;
54 }
55 }*/
56
57 void bzInputInit() {
58 for (size_t i = 0; i < 8; ++i) {
59 buttons[i].down = false;
60 }
61 }
62
63 void bzInputTeardown() {
64
65 }
66
67 void bzInputProcessFrame() {
68 for (size_t i = 0; i < inputTableSize; ++i) {
69 inputTable[i].integerValue = 0; // FIXME, max size and whatever.
70 }
71
72 // for (size_t i = 0; i < 8; ++i) {
73 // buttons[i].down = buttons[i].down || buttons[i].pressed;
74 // buttons[i].pressed = false;
75 // }
76
77 PDButtons current;
78 PDButtons pushed;
79 PDButtons released;
80 playdate->system->getButtonState(&current, &pushed, &released);
81
82 buttons[1].down = (current & kButtonUp) > 0;
83 buttons[1].pressed = (pushed & kButtonUp) > 0;
84
85 buttons[2].down = (current & kButtonDown) > 0;
86 buttons[2].pressed = (pushed & kButtonDown) > 0;
87
88 buttons[3].down = (current & kButtonLeft) > 0;
89 buttons[3].pressed = (pushed & kButtonLeft) > 0;
90
91 buttons[4].down = (current & kButtonRight) > 0;
92 buttons[4].pressed = (pushed & kButtonRight) > 0;
93
94 buttons[5].down = (current & kButtonB) > 0;
95 buttons[5].pressed = (pushed & kButtonB) > 0;
96
97 buttons[6].down = (current & kButtonA) > 0;
98 buttons[6].pressed = (pushed & kButtonA) > 0;
99
100 /*SDL_Event event;
101 while (SDL_PollEvent(&event)) {
102 switch (event.type) {
103 case SDL_QUIT:
104 inputTable[BZInputCodeQuit].booleanValue = true;
105 break;
106
107 case SDL_KEYDOWN:
108 buttons[buttonIdx(event.key.keysym.sym)].pressed = true;
109 break;
110
111 case SDL_KEYUP:
112 buttons[buttonIdx(event.key.keysym.sym)].pressed = false;
113 buttons[buttonIdx(event.key.keysym.sym)].down = false;
114 break;
115 }
116 }*/
117 }
118
119 bool bzInputGetBooleanValue(BZInputID inputID) {
120 return inputTable[inputID->id].booleanValue;
121 }
122
123 int bzInputGetIntegerValue(BZInputID inputID) {
124 return inputTable[inputID->id].integerValue;
125 }
126
127 float bzInputGetFloatValue(BZInputID inputID) {
128 return inputTable[inputID->id].floatValue;
129 }
130
131 bool bzInputBtn(int id) {
132 return buttons[id].down || buttons[id].pressed;
133 }
134
135 bool bzInputBtnP(int id) {
136 return buttons[id].pressed;
137 }
138