#ifndef PW_INPUT_H #define PW_INPUT_H #include #include "fb.h" #include "player.h" #define PW_AXIS_THRESHOLD 9830 #define PW_TRIGGER_THRESHOLD 16384 #define PW_KEYBOARD_BUFFER_SIZE 256 #define PW_MOUSE_RELATIVE_SCALE 4.0 /** * PICO-8 window flags. */ enum pw_window_flags { PW_WINDOW_NONE = 0, PW_WINDOW_VISIBLE = 1 << 0, PW_WINDOW_FOCUS = 1 << 1, }; /** * PICO-8 controller buttons. */ enum pw_button { PW_BUTTON_NONE = 0, PW_BUTTON_LEFT = 1 << 0, PW_BUTTON_RIGHT = 1 << 1, PW_BUTTON_UP = 1 << 2, PW_BUTTON_DOWN = 1 << 3, PW_BUTTON_O = 1 << 4, PW_BUTTON_X = 1 << 5, PW_BUTTON_MENU = 1 << 6, PW_BUTTON_ALL = 0x7f, }; /** * A PICO-8 keyboard modifier. */ enum pw_keymod { PW_KEYMOD_NONE = 0, PW_KEYMOD_LSHIFT = 1 << 0, PW_KEYMOD_RSHIFT = 1 << 1, PW_KEYMOD_LCTRL = 1 << 6, PW_KEYMOD_RCTRL = 1 << 7, PW_KEYMOD_LALT = 1 << 8, PW_KEYMOD_RALT = 1 << 9, }; /** * A queue used for storing queued key events and text input. */ struct pw_keyboard_queue { uint32_t items[PW_KEYBOARD_BUFFER_SIZE]; size_t start; size_t end; }; /** * The PICO-8 keyboard. This is used both for collecting keyboard events that * are used by PICO-8 when no gamepads are connected, but also for buffering * text for the `devkit` mode. */ struct pw_keyboard { enum pw_keymod mod; struct pw_keyboard_queue scancodes; struct pw_keyboard_queue text; }; /** * A PICO-8 mouse button. */ enum pw_mouse_button { PW_MOUSE_BUTTON_NONE = 0, PW_MOUSE_BUTTON_LEFT = 1 << 0, PW_MOUSE_BUTTON_MIDDLE = 1 << 1, PW_MOUSE_BUTTON_RIGHT = 1 << 2, }; /** * PICO-8 mouse wheel motion. */ struct pw_mouse_wheel { int32_t dx; int32_t dy; }; /** * The PICO-8 mouse state. */ struct pw_mouse { enum pw_mouse_button button; struct pw_mouse_wheel wheel; struct pw_coordinates pos; struct pw_coordinates rel; }; /** * The PICO-8 cliboard buffer state. */ struct pw_clipboard { char *buffer; size_t size; }; /** * A PICO-8 input event. * * This is only a subset of SDL events that the PICO-8 engine uses. */ union pw_input_event { enum pw_input_event_type { PW_INPUT_EVENT_KEY, PW_INPUT_EVENT_MOUSE, } type; struct pw_input_event_key { enum pw_input_event_type type; bool pressed; bool repeat; uint32_t scancode; uint32_t keycode; enum pw_keymod mod; } key; struct pw_input_event_mouse { enum pw_input_event_type type; enum pw_mouse_button button; int32_t x; int32_t y; int32_t xrel; int32_t yrel; } mouse; }; /** * PICO-8 input. */ struct pw_input { SDL_Window *window; size_t nplayers; enum pw_button players[PW_PLAYERS_MAX]; struct pw_keyboard keyboard; struct pw_mouse mouse; struct pw_clipboard clipboard; }; /** * Allocates and initializes the PICO-8 input state. */ struct pw_input *pw_input_init(SDL_Window *window); /** * Cleanup and free the PICO-8 input state. */ void pw_input_free(struct pw_input *input); /** * Resets the player state. */ bool pw_input_reset(struct pw_input *input, struct pw_players *players); /** * Handles an SDL events for PICO-8 input. */ bool pw_input_event(struct pw_input *input, struct pw_players *players, SDL_Event *event); /** * Updates PICO-8 input state, once per frame. */ bool pw_input_iterate(struct pw_input *input, struct pw_framebuffer *fb); /** * Returns PICO-8 window flags. */ enum pw_window_flags pw_input_window(struct pw_input *input); /** * Gets the number of connected players. */ size_t pw_input_players(struct pw_input *input); /** * Gets the button state for a player. */ enum pw_button pw_input_btn(struct pw_input *input, size_t player); /** * Gets the current keyboard modifier state. */ enum pw_keymod pw_input_keymod(struct pw_input *input); /** * Gets the next buffered key. Passing `NULL` as `key` returns whether * or not there are any keys buffered. */ bool pw_input_key(struct pw_input *input, int32_t *key); /** * Gets the current mouse button state. */ enum pw_mouse_button pw_input_mouse(struct pw_input *input, struct pw_coordinates *pos); /** * Gets the accumulated mouse wheel change, resetting the accumulator. * * Can specify `NULL` to ignore coordinate. */ void pw_input_wheel(struct pw_input *input, int32_t *dx, int32_t *dy); /** * Gets a writable pointer to the clipboard buffer. */ char *pw_input_clipboard(struct pw_input *input, size_t size); /** * Commits the clipboard buffer to the system clipboard. */ bool pw_input_clipboard_commit(struct pw_input *input); /** * Fetches the system clipboard into our clipboard buffer. */ bool pw_input_clipboard_fetch(struct pw_input *input, size_t *len); /** * Polls for the next input event for the PICO-8 engine. */ bool pw_input_poll(struct pw_input *input, union pw_input_event *event); /** * Sets the mouse pointer lock. * * Returns `false` in case of an error. */ bool pw_input_set_pointer_lock(struct pw_input *input, bool lock); /** * Gets whether or not the pointer is currently locked. */ bool pw_input_get_pointer_lock(struct pw_input *input); #endif