joeylib2/src/core/input.c

88 lines
2.2 KiB
C

// Public input API. Maintains current/previous state buffers for both
// the keyboard (gKeyState/gKeyPrev) and the mouse buttons
// (gMouseButtonState/gMouseButtonPrev). joeyInputPoll snapshots the
// previous state, then asks the port HAL to refresh the current state;
// the predicates derive held/edge values from those two buffers.
//
// Mouse position (gMouseX/gMouseY) is plain current state -- there is
// no "previous position" exposed; games that want delta movement can
// remember the previous joeyMouseX/Y themselves.
#include <string.h>
#include "joey/input.h"
#include "hal.h"
#include "inputInternal.h"
bool gKeyState [KEY_COUNT];
bool gKeyPrev [KEY_COUNT];
int16_t gMouseX = 0;
int16_t gMouseY = 0;
bool gMouseButtonState[MOUSE_BUTTON_COUNT];
bool gMouseButtonPrev [MOUSE_BUTTON_COUNT];
void joeyInputPoll(void) {
memcpy(gKeyPrev, gKeyState, sizeof(gKeyState));
memcpy(gMouseButtonPrev, gMouseButtonState, sizeof(gMouseButtonState));
halInputPoll();
}
bool joeyKeyDown(JoeyKeyE key) {
if (key <= KEY_NONE || key >= KEY_COUNT) {
return false;
}
return gKeyState[key];
}
bool joeyKeyPressed(JoeyKeyE key) {
if (key <= KEY_NONE || key >= KEY_COUNT) {
return false;
}
return gKeyState[key] && !gKeyPrev[key];
}
bool joeyKeyReleased(JoeyKeyE key) {
if (key <= KEY_NONE || key >= KEY_COUNT) {
return false;
}
return !gKeyState[key] && gKeyPrev[key];
}
bool joeyMouseDown(JoeyMouseButtonE button) {
if (button <= MOUSE_BUTTON_NONE || button >= MOUSE_BUTTON_COUNT) {
return false;
}
return gMouseButtonState[button];
}
bool joeyMousePressed(JoeyMouseButtonE button) {
if (button <= MOUSE_BUTTON_NONE || button >= MOUSE_BUTTON_COUNT) {
return false;
}
return gMouseButtonState[button] && !gMouseButtonPrev[button];
}
bool joeyMouseReleased(JoeyMouseButtonE button) {
if (button <= MOUSE_BUTTON_NONE || button >= MOUSE_BUTTON_COUNT) {
return false;
}
return !gMouseButtonState[button] && gMouseButtonPrev[button];
}
int16_t joeyMouseX(void) {
return gMouseX;
}
int16_t joeyMouseY(void) {
return gMouseY;
}