diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-24 22:47:46 +1100 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-24 22:47:46 +1100 |
commit | 7b3afcaf77f96e7d62f6cd1623ead7f17512d79f (patch) | |
tree | b5f82c64e9c06a84e4d095ab4ac48712e860b673 /src/systems | |
parent | b047be5252aeb981faea077409c1768fda0301d9 (diff) |
repo init. partial port of existing code
Diffstat (limited to 'src/systems')
-rw-r--r-- | src/systems/input.c | 77 | ||||
-rw-r--r-- | src/systems/input.h | 41 | ||||
-rw-r--r-- | src/systems/keys.h | 6 | ||||
-rw-r--r-- | src/systems/screenspace.h | 53 | ||||
-rw-r--r-- | src/systems/text.c | 1 | ||||
-rw-r--r-- | src/systems/text.h | 58 |
6 files changed, 236 insertions, 0 deletions
diff --git a/src/systems/input.c b/src/systems/input.c new file mode 100644 index 0000000..3b7ab9e --- /dev/null +++ b/src/systems/input.c @@ -0,0 +1,77 @@ +#include "input.h" + +#include <GLFW/glfw3.h> + +#include "log.h" + +bool input_system_init(input_state *input, GLFWwindow *window) { + INFO("Input init"); + input->window = window; + // Set everything to false. Could just set memory to zero but where's the fun in that + for (int i = 0; i < KEYCODE_MAX; i++) { + input->depressed_keys[i] = false; + input->just_pressed_keys[i] = false; + input->just_released_keys[i] = false; + } + + return true; +} + +void input_update(input_state *input) { + // --- update keyboard input + + // if we go from un-pressed -> pressed, set as "just pressed" + // if we go from pressed -> un-pressed, set as "just released" + for (int i = 0; i < KEYCODE_MAX; i++) { + bool new_state = false; + if (glfwGetKey(input->window, i) == GLFW_PRESS) { + new_state = true; + } else { + new_state = false; + } + if (!input->depressed_keys[i] == false && new_state) { + input->just_pressed_keys[i] = true; + } else { + input->just_pressed_keys[i] = false; + } + + if (input->depressed_keys[i] && !new_state) { + input->just_released_keys[i] = true; + } else { + input->just_released_keys[i] = false; + } + + input->depressed_keys[i] = new_state; + } + + // --- update mouse input + + // cursor position + f64 current_x, current_y; + glfwGetCursorPos(input->window, ¤t_x, ¤t_y); + i32 quantised_cur_x = (i32)current_x; + i32 quantised_cur_y = (i32)current_y; + + mouse_state new_mouse_state = { 0 }; + new_mouse_state.x = quantised_cur_x; + new_mouse_state.y = quantised_cur_y; + new_mouse_state.x_delta = quantised_cur_x - input->mouse.x; + new_mouse_state.y_delta = quantised_cur_y - input->mouse.y; + + // buttons + int left_state = glfwGetMouseButton(input->window, GLFW_MOUSE_BUTTON_LEFT); + // int right_state = glfwGetMouseButton(input->window, GLFW_MOUSE_BUTTON_RIGHT); + + new_mouse_state.prev_left_btn_pressed = input->mouse.left_btn_pressed; + if (left_state == GLFW_PRESS) { + new_mouse_state.left_btn_pressed = true; + } else { + new_mouse_state.left_btn_pressed = false; + } + + // this was dumb! need to also check button state changes lol + // if (new_mouse_state.x != input->mouse.x || new_mouse_state.y != input->mouse.y) + // TRACE("Mouse (x,y) = (%d,%d)", input->mouse.x, input->mouse.y); + + input->mouse = new_mouse_state; +} diff --git a/src/systems/input.h b/src/systems/input.h new file mode 100644 index 0000000..c119016 --- /dev/null +++ b/src/systems/input.h @@ -0,0 +1,41 @@ +/** + * @brief + */ +#pragma once + +#include "defines.h" +#include "keys.h" + +struct core; +struct GLFWWindow; + +typedef struct mouse_state { + i32 x; + i32 y; + i32 x_delta; + i32 y_delta; + bool prev_left_btn_pressed; + bool left_btn_pressed; +} mouse_state; + +typedef struct input_state { + struct GLFWwindow *window; + mouse_state mouse; + bool depressed_keys[KEYCODE_MAX]; + bool just_pressed_keys[KEYCODE_MAX]; + bool just_released_keys[KEYCODE_MAX]; +} input_state; + +/** @brief `key` is currently being held down */ +bool key_is_pressed(keycode key); + +/** @brief `key` was just pressed */ +bool key_just_pressed(keycode key); + +/** @brief `key` was just released */ +bool key_just_released(keycode key); + +// --- Lifecycle +bool input_system_init(input_state *input, struct GLFWwindow *window); +void input_system_shutdown(input_state *input); +void input_update(input_state *state);
\ No newline at end of file diff --git a/src/systems/keys.h b/src/systems/keys.h new file mode 100644 index 0000000..090bb49 --- /dev/null +++ b/src/systems/keys.h @@ -0,0 +1,6 @@ +#pragma once + +typedef enum keycode { + // TODO: add all keycodes + KEYCODE_MAX +} keycode;
\ No newline at end of file diff --git a/src/systems/screenspace.h b/src/systems/screenspace.h new file mode 100644 index 0000000..d36404a --- /dev/null +++ b/src/systems/screenspace.h @@ -0,0 +1,53 @@ +/** + * @brief Drawing shapes for UI or other reasons in screenspace + */ +#pragma once + +#include "colours.h" +#include "darray.h" +#include "defines.h" +#include "render_types.h" + +/** A draw_cmd packet for rendering a rectangle */ +struct draw_rect { + i32 x, y; // signed ints so we can draw things offscreen (e.g. a window half inside the viewport) + u32 width, height; + rgba colour; + // TODO: border colour, gradients +}; + +/** A draw_cmd packet for rendering a circle */ +struct draw_circle { + i32 x, y; + f32 radius; + rgba colour; +}; + +/** @brief Tagged union that represents a UI shape to be drawn. */ +typedef struct draw_cmd { + enum { RECT, CIRCLE } draw_cmd_type; + union { + struct draw_rect rect; + struct draw_circle circle; + }; +} draw_cmd; + +KITC_DECL_TYPED_ARRAY(draw_cmd) + +typedef struct screenspace_state { + u32 rect_vbo; + u32 rect_vao; + // shader rect_shader; + draw_cmd_darray* draw_cmd_buf; +} screenspace_state; + +// --- Lifecycle +bool screenspace_2d_init(screenspace_state* state); +void screenspace_2d_shutdown(screenspace_state* state); +/** Drains the draw_cmd buffer and emits draw calls to render each one */ +void screenspace_2d_render(screenspace_state* state); + +struct core; + +/** @brief Draw a rectangle to the screen. (0,0) is the bottom-left */ +void draw_rectangle(struct core* core, rgba colour, i32 x, i32 y, u32 width, u32 height);
\ No newline at end of file diff --git a/src/systems/text.c b/src/systems/text.c new file mode 100644 index 0000000..2bb5399 --- /dev/null +++ b/src/systems/text.c @@ -0,0 +1 @@ +// TODO: Port from previous repo
\ No newline at end of file diff --git a/src/systems/text.h b/src/systems/text.h new file mode 100644 index 0000000..3c580df --- /dev/null +++ b/src/systems/text.h @@ -0,0 +1,58 @@ +/** + * @brief + */ +#pragma once + +#include <stb_truetype.h> + +#include "darray.h" +#include "defines.h" +#include "render_types.h" + +struct core; +typedef struct texture_handle { + u32 raw +} texture_handle; +typedef struct shader { + u32 raw +} shader; + +/** @brief internal font struct */ +typedef struct font { + const char *name; + stbtt_fontinfo stbtt_font; + stbtt_bakedchar c_data[96]; + texture_handle bitmap_tex; +} font; + +typedef struct draw_text_packet { + char *contents; + f32 x; + f32 y; +} draw_text_packet; + +KITC_DECL_TYPED_ARRAY(draw_text_packet) + +typedef struct text_system_state { + font default_font; + shader glyph_shader; + u32 glyph_vbo; + u32 glyph_vao; + draw_text_packet_darray *draw_cmd_buf; + // TODO: fonts array or hashtable +} text_system_state; + +void text_system_render(text_system_state *text); + +// --- Lifecycle functions +bool text_system_init(text_system_state *text); +void text_system_shutdown(text_system_state *text); + +// --- Drawing + +/** + * @brief immediate mode draw text. + * @note immediately emits draw calls causing a shader program switch if you weren't previously + drawing text in the current frame. +*/ +void draw_text(struct core *core, f32 x, f32 y, char *contents);
\ No newline at end of file |