Player controller

This commit is contained in:
2024-02-27 21:07:15 -08:00
parent ab5c442433
commit 5f522abcb5
11 changed files with 256 additions and 35 deletions

View File

@ -15,6 +15,10 @@ void sys_init() {
sys_dpal_reset();
}
void sys_update() {
sys_input_update();
}
bool sys_get_initialized() {
return sys_initialized;
}

View File

@ -10,6 +10,11 @@
*/
void sys_init();
/**
* Update sys -- usually for input state.
*/
void sys_update();
/**
* Return whether sys was initialized.
*/

36
sys/sys_input.c Normal file
View File

@ -0,0 +1,36 @@
#include "device/device.h"
#include "sys/sys.h"
#include "sys_input.h"
sys_i32 sys_input_down_frames[DEVICE_BUTTON_N];
void sys_input_update() {
for (DeviceButton db = 0; db < DEVICE_BUTTON_N; db++) {
if (device_buttons[db]) {
sys_input_down_frames[db] += 1;
} else {
sys_input_down_frames[db] = 0;
}
}
}
void sys_input_reset() {
for (DeviceButton db = 0; db < DEVICE_BUTTON_N; db++) {
sys_input_down_frames[db] = 0;
}
}
bool sys_btn(DeviceButton button) {
if (button >= DEVICE_BUTTON_N) { return false; }
return sys_input_down_frames[button] >= 1;
}
bool sys_btnp(DeviceButton button) {
if (button >= DEVICE_BUTTON_N) { return false; }
if (sys_input_down_frames[button] == 1) { return true; }
// 31 and every 8 frames after that
if (sys_input_down_frames[button] >= 31 && (sys_input_down_frames[button] - 31) % 8 == 0) {
return true;
}
return false;
}

33
sys/sys_input.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef SYS_INPUT_H
#define SYS_INPUT_H
/**
* Update the state of every button. Each button's btnp state depends
* on how long it's been down, and calling this function adds one frame.
*
* Usually this is called by sys_update(), at the start of frame.
*/
void sys_input_update();
/**
* Resets the input state -- all buttons have been held for 0 frames.
*
* If the buttons are held next frame, this will lead to a btnp().
*
* There's rarely any reason for user code to call this.
*/
void sys_input_reset();
/**
* Return whether a button is down.
*/
bool sys_btn(DeviceButton button);
/**
* Return whether a button was just pressed.
*
* Repeats after 30 frames, returning true every 8 frames after that.
*/
bool sys_btnp(DeviceButton button);
#endif // SYS_INPUT_H