aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-11 14:30:48 -0400
committeriamcheeseman <[email protected]>2026-05-11 14:30:48 -0400
commitf4017a52d90567c2df482c5d553076dc706ac59b (patch)
tree6cce9684cee03370188e6c46e745fabb64007a46
parentda2585ddc49abb80e7457ccc2805fff884eb2021 (diff)
Allow user input
-rw-r--r--dc/dc.c26
-rw-r--r--platform/gl/gl.c34
-rw-r--r--teensy/teensy.h36
-rw-r--r--teensy/teensy_context.c23
-rw-r--r--teensy/teensy_context.h8
-rw-r--r--teensy/teensy_platform.h7
6 files changed, 121 insertions, 13 deletions
diff --git a/dc/dc.c b/dc/dc.c
index adecb4e..b68988e 100644
--- a/dc/dc.c
+++ b/dc/dc.c
@@ -1,7 +1,7 @@
#include <stdio.h>
-#include <teensy.h>
#include <math.h>
+#include <teensy.h>
#include <time.h>
#define QOI_IMPLEMENTATION
@@ -17,6 +17,8 @@ struct ty_font font;
double last_frame = 0;
+bool toggled = false;
+
void tick(void)
{
ty_draw_clear(TY_COLOR_BLACK);
@@ -39,12 +41,21 @@ void tick(void)
ty_draw_text(&font, ty_vec2i(5, SCREEN_HEIGHT - 5 - 8), "hello, world!");
+ if (ty_button_pressed(TY_BTN_LEFT_UP)) {
+ toggled = !toggled;
+ }
+
+ if (toggled) {
+ ty_draw_image(img, ty_vec2i(200, 200));
+ }
+
double frame_time = ty_get_time() - last_frame;
last_frame = ty_get_time();
char ms_fmt[] = "%.2g ms";
int ms_width = ty_font_width(&font, ms_fmt, frame_time * 1000);
+ int y = 5;
ty_draw_rect(
(struct ty_recti){
5, 5,
@@ -58,6 +69,7 @@ void tick(void)
ms_fmt,
frame_time * 1000
);
+ y += 8;
char fps_fmt[] = "%2d fps";
int fps_width = ty_font_width(&font, fps_fmt, (int)(1.0 / frame_time));
@@ -75,8 +87,14 @@ void tick(void)
fps_fmt,
(int)(1.0 / frame_time)
);
+ y += 8;
+
+ ty_draw_text_fmt(&font, ty_vec2i(5, y),
+ "%.2g",
+ ty_get_time()
+ );
+ y += 8;
- int y = 5+24;
ty_draw_text(&font, ty_vec2i(5, y),
"abcdefghijklmnopqrstuvwxyz"
);
@@ -101,10 +119,6 @@ void tick(void)
"THe_value = MY_arr[15]"
);
y += 8;
- ty_draw_text_fmt(&font, ty_vec2i(5, y),
- "%.2g",
- ty_get_time()
- );
ty_draw_end();
diff --git a/platform/gl/gl.c b/platform/gl/gl.c
index 060a19d..acfa0c5 100644
--- a/platform/gl/gl.c
+++ b/platform/gl/gl.c
@@ -215,3 +215,37 @@ double ty_platform_get_time(void)
{
return glfwGetTime();
}
+
+struct ty_vec2i ty_platform_get_mouse(void)
+{
+ double x, y;
+ glfwGetCursorPos(p.win, &x, &y);
+ return ty_vec2i(x, y);
+}
+
+static
+int button_to_glfw_key(enum ty_button btn)
+{
+ switch (btn) {
+ case TY_BTN_LEFT_UP: return GLFW_KEY_W;
+ case TY_BTN_LEFT_DOWN: return GLFW_KEY_S;
+ case TY_BTN_LEFT_LEFT: return GLFW_KEY_A;
+ case TY_BTN_LEFT_RIGHT: return GLFW_KEY_D;
+ case TY_BTN_RIGHT_UP: return GLFW_KEY_I;
+ case TY_BTN_RIGHT_DOWN: return GLFW_KEY_K;
+ case TY_BTN_RIGHT_LEFT: return GLFW_KEY_J;
+ case TY_BTN_RIGHT_RIGHT: return GLFW_KEY_L;
+ case TY_BTN_ACTION_1: return GLFW_KEY_Q;
+ case TY_BTN_ACTION_2: return GLFW_KEY_E;
+ case TY_BTN_ACTION_3: return GLFW_KEY_U;
+ case TY_BTN_ACTION_4: return GLFW_KEY_O;
+ case TY_BTN_DB_CTRL: return GLFW_KEY_LEFT_CONTROL;
+ case TY_BTN_DB_SHIFT: return GLFW_KEY_LEFT_SHIFT;
+ }
+ return 0;
+}
+
+bool ty_platform_is_button_down(enum ty_button btn)
+{
+ return glfwGetKey(p.win, button_to_glfw_key(btn)) == GLFW_PRESS;
+}
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 <limits.h>
#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_