From efbee96caa1452486a007eeeabb5073aa9025dae Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sat, 22 May 2021 23:07:17 +0200 Subject: Update mouse control code to use key taps instead of press --- src/common.h | 16 ++++++++++++++-- src/main.c | 37 ++++++++++++++++++------------------- 2 files changed, 32 insertions(+), 21 deletions(-) (limited to 'src') 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) { // function will return `true` only on the frame where the key initially // activated. static inline u32 -key_pressed(u32 key) { +key_tap(u32 key) { return (key_curr & key) & ~(key_prev & key); } +// Check if a given key is currently pressed. +static inline u32 +key_pressed(u32 key) { + return (key_curr & key); +} + +// Check if a given key was just released. +static inline u32 +key_released(u32 key) { + return ~(key_curr & key) & (key_prev & key); +} + // Check if the given key is pressed and has been since at least one frame. static inline u32 key_hold(u32 key) { - return (key_curr & key) & key_prev & key; + return key_curr & key_prev & key; } // 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) { mempoke16(devscreen->dat, 4, ppu.ver * 8); } +IWRAM_CODE void handle_input(Uxn *u) { poll_keys(); - if (key_pressed(KEY_SELECT)) { + if (key_tap(KEY_SELECT)) { switch (control_method) { case CONTROL_CONTROLLER: { devctrl->dat[2] = 0; @@ -176,42 +177,42 @@ handle_input(Uxn *u) { // TODO: We don't need ifs if we use KEY_INPUTS directly and mayvbe just // swap some things if needed. Uint8 *flag = &devctrl->dat[2]; - if (key_pressed(KEY_A) | key_hold(KEY_A)) { + if (key_pressed(KEY_A)) { *flag |= 0x01; } else { *flag &= ~0x01; } - if (key_pressed(KEY_B) | key_hold(KEY_B)) { + if (key_pressed(KEY_B)) { *flag |= 0x02; } else { *flag &= ~0x02; } - if (key_pressed(KEY_L) | key_hold(KEY_L)) { + if (key_pressed(KEY_L)) { *flag |= 0x04; } else { *flag &= ~0x04; } - if (key_pressed(KEY_R) | key_hold(KEY_R)) { + if (key_pressed(KEY_R)) { *flag |= 0x08; } else { *flag &= ~0x08; } - if (key_pressed(KEY_UP) | key_hold(KEY_UP)) { + if (key_pressed(KEY_UP)) { *flag |= 0x10; } else { *flag &= ~0x10; } - if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) { + if (key_pressed(KEY_DOWN)) { *flag |= 0x20; } else { *flag &= ~0x20; } - if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) { + if (key_pressed(KEY_LEFT)) { *flag |= 0x40; } else { *flag &= ~0x40; } - if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) { + if (key_pressed(KEY_RIGHT)) { *flag |= 0x80; } else { *flag &= ~0x80; @@ -222,14 +223,14 @@ handle_input(Uxn *u) { } else if (control_method == CONTROL_MOUSE) { // Detect "mouse key press". Uint8 flag = devmouse->dat[6]; - if (key_pressed(KEY_B) | key_hold(KEY_B)) { + if (key_tap(KEY_B)) { flag |= 0x01; - } else { + } else if (key_released(KEY_B)) { flag &= ~0x01; } - if (key_pressed(KEY_A) | key_hold(KEY_A)) { + if (key_tap(KEY_A)) { flag |= 0x10; - } else { + } else if (key_released(KEY_A)) { flag &= ~0x10; } @@ -243,16 +244,14 @@ handle_input(Uxn *u) { } // Detect mouse movement. - if (key_pressed(KEY_UP) | key_hold(KEY_UP)) { + if (key_pressed(KEY_UP)) { mouse.y = CLAMP(mouse.y - MOUSE_DELTA, 0, SCREEN_HEIGHT - 8); - } - if (key_pressed(KEY_DOWN) | key_hold(KEY_DOWN)) { + } else if (key_pressed(KEY_DOWN)) { mouse.y = CLAMP(mouse.y + MOUSE_DELTA, 0, SCREEN_HEIGHT - 8); } - if (key_pressed(KEY_LEFT) | key_hold(KEY_LEFT)) { + if (key_pressed(KEY_LEFT)) { mouse.x = CLAMP(mouse.x - MOUSE_DELTA, 0, SCREEN_WIDTH - 8); - } - if (key_pressed(KEY_RIGHT) | key_hold(KEY_RIGHT)) { + } else if (key_pressed(KEY_RIGHT)) { mouse.x = CLAMP(mouse.x + MOUSE_DELTA, 0, SCREEN_WIDTH - 8); } -- cgit v1.2.1