aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-05-22 23:07:17 +0200
committerBad Diode <bd@badd10de.dev>2021-05-22 23:07:17 +0200
commitefbee96caa1452486a007eeeabb5073aa9025dae (patch)
treed048e38bd9749a6db3d1b47caa305d8d84b370ab /src
parent2e5e4955337dccc370ed72d0f4fc561abadf84f4 (diff)
downloaduxngba-efbee96caa1452486a007eeeabb5073aa9025dae.tar.gz
uxngba-efbee96caa1452486a007eeeabb5073aa9025dae.zip
Update mouse control code to use key taps instead of press
Diffstat (limited to 'src')
-rw-r--r--src/common.h16
-rw-r--r--src/main.c37
2 files changed, 32 insertions, 21 deletions
diff --git a/src/common.h b/src/common.h
index e293770..08736ea 100644
--- a/src/common.h
+++ b/src/common.h
@@ -301,14 +301,26 @@ poll_keys(void) {
301// function will return `true` only on the frame where the key initially 301// function will return `true` only on the frame where the key initially
302// activated. 302// activated.
303static inline u32 303static inline u32
304key_pressed(u32 key) { 304key_tap(u32 key) {
305 return (key_curr & key) & ~(key_prev & key); 305 return (key_curr & key) & ~(key_prev & key);
306} 306}
307 307
308// Check if a given key is currently pressed.
309static inline u32
310key_pressed(u32 key) {
311 return (key_curr & key);
312}
313
314// Check if a given key was just released.
315static inline u32
316key_released(u32 key) {
317 return ~(key_curr & key) & (key_prev & key);
318}
319
308// Check if the given key is pressed and has been since at least one frame. 320// Check if the given key is pressed and has been since at least one frame.
309static inline u32 321static inline u32
310key_hold(u32 key) { 322key_hold(u32 key) {
311 return (key_curr & key) & key_prev & key; 323 return key_curr & key_prev & key;
312} 324}
313 325
314// Check if the given key/button is currently pressed. 326// Check if the given key/button is currently pressed.
diff --git a/src/main.c b/src/main.c
index a414558..40be7b9 100644
--- a/src/main.c
+++ b/src/main.c
@@ -150,10 +150,11 @@ init_uxn(Uxn *u) {
150 mempoke16(devscreen->dat, 4, ppu.ver * 8); 150 mempoke16(devscreen->dat, 4, ppu.ver * 8);
151} 151}
152 152
153IWRAM_CODE
153void 154void
154handle_input(Uxn *u) { 155handle_input(Uxn *u) {
155 poll_keys(); 156 poll_keys();
156 if (key_pressed(KEY_SELECT)) { 157 if (key_tap(KEY_SELECT)) {
157 switch (control_method) { 158 switch (control_method) {
158 case CONTROL_CONTROLLER: { 159 case CONTROL_CONTROLLER: {
159 devctrl->dat[2] = 0; 160 devctrl->dat[2] = 0;
@@ -176,42 +177,42 @@ handle_input(Uxn *u) {
176 // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just 177 // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just
177 // swap some things if needed. 178 // swap some things if needed.
178 Uint8 *flag = &devctrl->dat[2]; 179 Uint8 *flag = &devctrl->dat[2];
179 if (key_pressed(KEY_A) | key_hold(KEY_A)) { 180 if (key_pressed(KEY_A)) {
180 *flag |= 0x01; 181 *flag |= 0x01;
181 } else { 182 } else {
182 *flag &= ~0x01; 183 *flag &= ~0x01;
183 } 184 }
184 if (key_pressed(KEY_B) | key_hold(KEY_B)) { 185 if (key_pressed(KEY_B)) {
185 *flag |= 0x02; 186 *flag |= 0x02;
186 } else { 187 } else {
187 *flag &= ~0x02; 188 *flag &= ~0x02;
188 } 189 }
189 if (key_pressed(KEY_L) | key_hold(KEY_L)) { 190 if (key_pressed(KEY_L)) {
190 *flag |= 0x04; 191 *flag |= 0x04;
191 } else { 192 } else {
192 *flag &= ~0x04; 193 *flag &= ~0x04;
193 } 194 }
194 if (key_pressed(KEY_R) | key_hold(KEY_R)) { 195 if (key_pressed(KEY_R)) {
195 *flag |= 0x08; 196 *flag |= 0x08;
196 } else { 197 } else {
197 *flag &= ~0x08; 198 *flag &= ~0x08;
198 } 199 }
199 if (key_pressed(KEY_UP) | key_hold(KEY_UP)) { 200 if (key_pressed(KEY_UP)) {
200 *flag |= 0x10; 201 *flag |= 0x10;
201 } else { 202 } else {
202 *flag &= ~0x10; 203 *flag &= ~0x10;
203 } 204 }
204 if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) { 205 if (key_pressed(KEY_DOWN)) {
205 *flag |= 0x20; 206 *flag |= 0x20;
206 } else { 207 } else {
207 *flag &= ~0x20; 208 *flag &= ~0x20;
208 } 209 }
209 if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) { 210 if (key_pressed(KEY_LEFT)) {
210 *flag |= 0x40; 211 *flag |= 0x40;
211 } else { 212 } else {
212 *flag &= ~0x40; 213 *flag &= ~0x40;
213 } 214 }
214 if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) { 215 if (key_pressed(KEY_RIGHT)) {
215 *flag |= 0x80; 216 *flag |= 0x80;
216 } else { 217 } else {
217 *flag &= ~0x80; 218 *flag &= ~0x80;
@@ -222,14 +223,14 @@ handle_input(Uxn *u) {
222 } else if (control_method == CONTROL_MOUSE) { 223 } else if (control_method == CONTROL_MOUSE) {
223 // Detect "mouse key press". 224 // Detect "mouse key press".
224 Uint8 flag = devmouse->dat[6]; 225 Uint8 flag = devmouse->dat[6];
225 if (key_pressed(KEY_B) | key_hold(KEY_B)) { 226 if (key_tap(KEY_B)) {
226 flag |= 0x01; 227 flag |= 0x01;
227 } else { 228 } else if (key_released(KEY_B)) {
228 flag &= ~0x01; 229 flag &= ~0x01;
229 } 230 }
230 if (key_pressed(KEY_A) | key_hold(KEY_A)) { 231 if (key_tap(KEY_A)) {
231 flag |= 0x10; 232 flag |= 0x10;
232 } else { 233 } else if (key_released(KEY_A)) {
233 flag &= ~0x10; 234 flag &= ~0x10;
234 } 235 }
235 236
@@ -243,16 +244,14 @@ handle_input(Uxn *u) {
243 } 244 }
244 245
245 // Detect mouse movement. 246 // Detect mouse movement.
246 if (key_pressed(KEY_UP) | key_hold(KEY_UP)) { 247 if (key_pressed(KEY_UP)) {
247 mouse.y = CLAMP(mouse.y - MOUSE_DELTA, 0, SCREEN_HEIGHT - 8); 248 mouse.y = CLAMP(mouse.y - MOUSE_DELTA, 0, SCREEN_HEIGHT - 8);
248 } 249 } else if (key_pressed(KEY_DOWN)) {
249 if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) {
250 mouse.y = CLAMP(mouse.y + MOUSE_DELTA, 0, SCREEN_HEIGHT - 8); 250 mouse.y = CLAMP(mouse.y + MOUSE_DELTA, 0, SCREEN_HEIGHT - 8);
251 } 251 }
252 if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) { 252 if (key_pressed(KEY_LEFT)) {
253 mouse.x = CLAMP(mouse.x - MOUSE_DELTA, 0, SCREEN_WIDTH - 8); 253 mouse.x = CLAMP(mouse.x - MOUSE_DELTA, 0, SCREEN_WIDTH - 8);
254 } 254 } else if (key_pressed(KEY_RIGHT)) {
255 if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) {
256 mouse.x = CLAMP(mouse.x + MOUSE_DELTA, 0, SCREEN_WIDTH - 8); 255 mouse.x = CLAMP(mouse.x + MOUSE_DELTA, 0, SCREEN_WIDTH - 8);
257 } 256 }
258 257