From f4017a52d90567c2df482c5d553076dc706ac59b Mon Sep 17 00:00:00 2001 From: iamcheeseman Date: Mon, 11 May 2026 14:30:48 -0400 Subject: Allow user input --- teensy/teensy.h | 36 +++++++++++++++++++++++++++++------- teensy/teensy_context.c | 23 +++++++++++++++++++++++ teensy/teensy_context.h | 8 ++++++++ teensy/teensy_platform.h | 7 +++++++ 4 files changed, 67 insertions(+), 7 deletions(-) (limited to 'teensy') diff --git a/teensy/teensy.h b/teensy/teensy.h index cf0479f..568430c 100644 --- a/teensy/teensy.h +++ b/teensy/teensy.h @@ -4,7 +4,6 @@ #include #include "teensy_common.h" -#include "teensy_context.h" #include "teensy_list.h" #define ty_vec2(x, y) ((struct ty_vec2){x, y}) @@ -21,6 +20,30 @@ #define TY_COLOR_CYAN ty_color(0, 255, 255) #define TY_COLOR_WHITE ty_color(255, 255, 255) +// Virtual controller. Keyboard keys are also mapped to this. +enum ty_button { + TY_BTN_LEFT_UP, // D-pad #1 up + TY_BTN_LEFT_DOWN, // D-pad #1 down + TY_BTN_LEFT_LEFT, // D-pad #1 left + TY_BTN_LEFT_RIGHT, // D-pad #1 right + + TY_BTN_RIGHT_UP, // D-pad #2 up + TY_BTN_RIGHT_DOWN, // D-pad #2 down + TY_BTN_RIGHT_LEFT, // D-pad #2 left + TY_BTN_RIGHT_RIGHT, // D-pad #2 right + + TY_BTN_ACTION_1, // A + TY_BTN_ACTION_2, // B + TY_BTN_ACTION_3, // X + TY_BTN_ACTION_4, // Y + + // for use in the editor + TY_BTN_DB_CTRL, + TY_BTN_DB_SHIFT, + + TY_BTN_COUNT, +}; + struct ty_hints { int scr_width; int scr_height; @@ -28,12 +51,6 @@ struct ty_hints { int ticrate; }; -struct ty_ctx { - struct ty_hints hints; - double ticrate; - double prev_tic_ts; -}; - struct ty_color { uint8_t r; uint8_t g; @@ -81,6 +98,11 @@ struct ty_renderer { void ty_init(struct ty_hints hints); void ty_deinit(void); +// If the button is being held down +bool ty_button_down(enum ty_button btn); +// If the button was pressed just now +bool ty_button_pressed(enum ty_button btn); + // Whether or not the main loop should continue executing. bool ty_is_game_running(void); // Gets the real time that the game has been running. diff --git a/teensy/teensy_context.c b/teensy/teensy_context.c index 4a812dd..9dff405 100644 --- a/teensy/teensy_context.c +++ b/teensy/teensy_context.c @@ -35,6 +35,18 @@ void ty_deinit(void) ty_deinit_mem(); } +bool ty_button_down(enum ty_button btn) +{ + assert(btn >= 0 && btn < TY_BTN_COUNT); + return ctx.down[btn]; +} + +bool ty_button_pressed(enum ty_button btn) +{ + assert(btn >= 0 && btn < TY_BTN_COUNT); + return ctx.pressed[btn]; +} + bool ty_is_game_running(void) { assert(is_init()); @@ -47,6 +59,16 @@ double ty_get_time(void) return ty_platform_get_time(); } +static +void update_buttons(void) +{ + for (int btn = 0; btn < TY_BTN_COUNT; btn++) { + bool down = ty_platform_is_button_down(btn); + ctx.pressed[btn] = down && !ctx.down[btn]; + ctx.down[btn] = down; + } +} + int ty_tick(void) { assert(is_init()); @@ -56,6 +78,7 @@ int ty_tick(void) int tics = time_since_tic / ctx.ticrate; if (tics > 0) { + update_buttons(); ctx.prev_tic_ts = current_time; ty_platform_frame(r.screen); } diff --git a/teensy/teensy_context.h b/teensy/teensy_context.h index 056a0c2..7223d3d 100644 --- a/teensy/teensy_context.h +++ b/teensy/teensy_context.h @@ -5,6 +5,14 @@ #include "teensy_renderer.h" #include "teensy.h" +struct ty_ctx { + struct ty_hints hints; + double ticrate; + double prev_tic_ts; + bool down[TY_BTN_COUNT]; + bool pressed[TY_BTN_COUNT]; +}; + extern struct ty_ctx ctx; bool is_init(void); diff --git a/teensy/teensy_platform.h b/teensy/teensy_platform.h index 7733be1..2128c5e 100644 --- a/teensy/teensy_platform.h +++ b/teensy/teensy_platform.h @@ -1,6 +1,9 @@ #ifndef TEENSY_PLATFORM_H_ #define TEENSY_PLATFORM_H_ +#include "teensy.h" +#include "teensy_context.h" + // This platform functions are to be implemented by a platform library. void ty_platform_init(struct ty_ctx *ctx); @@ -8,5 +11,9 @@ void ty_platform_deinit(void); void ty_platform_frame(struct ty_image img); bool ty_platform_os_wants_quit(void); double ty_platform_get_time(void); +bool ty_platform_is_button_down(enum ty_button btn); + +// Optional, no editor if not implemented +struct ty_vec2i ty_platform_get_mouse(void); #endif // TEENSY_PLATFORM_H_ -- cgit v1.3-2-g0d8e