From 43e60465a0b5686f3db71c87a1d73717d93e9964 Mon Sep 17 00:00:00 2001 From: Danny Holman Date: Tue, 17 Sep 2024 02:22:44 -0500 Subject: ui: add new subsystem Add the UI subsystem. This subsystem will control the window display, events and input. Signed-off-by: Danny Holman --- CMakeLists.txt | 6 ++ include/rune/ui/input.h | 40 +++++++++++++ include/rune/ui/renderer.h | 40 +++++++++++++ include/rune/ui/scancode.h | 143 +++++++++++++++++++++++++++++++++++++++++++++ include/rune/ui/window.h | 38 ++++++++++++ ui/input.c | 40 +++++++++++++ ui/renderer.c | 32 ++++++++++ ui/window.c | 33 +++++++++++ 8 files changed, 372 insertions(+) create mode 100644 include/rune/ui/input.h create mode 100644 include/rune/ui/renderer.h create mode 100644 include/rune/ui/scancode.h create mode 100644 include/rune/ui/window.h create mode 100644 ui/input.c create mode 100644 ui/renderer.c create mode 100644 ui/window.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 38db705..cc8828e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -15,6 +15,12 @@ list(APPEND SOURCE_FILES core/network.c ) +list(APPEND SOURCE_FILES + ui/window.c + ui/input.c + ui/renderer.c +) + set(HEADER_DIR include) add_compile_definitions(VERSION="${PROJECT_VERSION}") diff --git a/include/rune/ui/input.h b/include/rune/ui/input.h new file mode 100644 index 0000000..dd86b17 --- /dev/null +++ b/include/rune/ui/input.h @@ -0,0 +1,40 @@ +/* + * Rune Game Engine + * Copyright 2024 Danny Holman + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +#ifndef RUNE_UI_INPUT_H +#define RUNE_UI_INPUT_H + +#include +#include + +#define KB_MODE_RAW 0 +#define KB_MODE_TEXT 1 + +RAPI int rune_input_init(struct rune_window *window); +RAPI void rune_input_quit(void); + +RAPI void set_keyboard_mode(int mode); +RAPI int get_keyboard_mode(void); + +RAPI void register_key_hook(int key, void (*func)(void)); +RAPI void rune_input_tick(void); + +#endif diff --git a/include/rune/ui/renderer.h b/include/rune/ui/renderer.h new file mode 100644 index 0000000..f1849e3 --- /dev/null +++ b/include/rune/ui/renderer.h @@ -0,0 +1,40 @@ +/* + * Rune Game Engine + * Copyright 2024 Danny Holman + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +#ifndef RUNE_UI_RENDERER_H +#define RUNE_UI_RENDERER_H + +#include +#include + +#define RUNE_RENDERER_VULKAN 0 +#define RUNE_RENDERER_DIRECTX 1 + +struct rune_renderer { + struct rune_window *window; + int (*init)(struct rune_renderer *renderer); + void (*close)(struct rune_renderer *renderer); +}; + +RAPI struct rune_renderer* rune_create_renderer(struct rune_window *window, int type); +RAPI void rune_destroy_renderer(struct rune_renderer *renderer); + +#endif diff --git a/include/rune/ui/scancode.h b/include/rune/ui/scancode.h new file mode 100644 index 0000000..34a4f6a --- /dev/null +++ b/include/rune/ui/scancode.h @@ -0,0 +1,143 @@ +/* + * Rune Game Engine + * Copyright 2024 Danny Holman + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +#ifndef RUNE_UI_SCANCODE_H +#define RUNE_UI_SCANCODE_H + +#define RUNE_SCANCODE_UNKNOWN 0x00 + +#define RUNE_SCANCODE_ESCAPE 0x01 + +#define RUNE_SCANCODE_F1 0x3B +#define RUNE_SCANCODE_F2 0x3C +#define RUNE_SCANCODE_F3 0x3D +#define RUNE_SCANCODE_F4 0x3E +#define RUNE_SCANCODE_F5 0x3F +#define RUNE_SCANCODE_F6 0x40 +#define RUNE_SCANCODE_F7 0x41 +#define RUNE_SCANCODE_F8 0x42 +#define RUNE_SCANCODE_F9 0x43 +#define RUNE_SCANCODE_F10 0x44 +#define RUNE_SCANCODE_F11 0x57 +#define RUNE_SCANCODE_F12 0x58 + +#define RUNE_SCANCODE_SYSRQ 0x63 +#define RUNE_SCANCODE_SCLCK 0x46 +#define RUNE_SCANCODE_PAUSE 0x77 + +#define RUNE_SCANCODE_TILDE 0x29 +#define RUNE_SCANCODE_1 0x02 +#define RUNE_SCANCODE_2 0x03 +#define RUNE_SCANCODE_3 0x04 +#define RUNE_SCANCODE_4 0x05 +#define RUNE_SCANCODE_5 0x06 +#define RUNE_SCANCODE_6 0x07 +#define RUNE_SCANCODE_7 0x08 +#define RUNE_SCANCODE_8 0x09 +#define RUNE_SCANCODE_9 0x0A +#define RUNE_SCANCODE_0 0x0B +#define RUNE_SCANCODE_MINUS 0x0C +#define RUNE_SCANCODE_EQUAL 0x0D + +#define RUNE_SCANCODE_Q 0x10 +#define RUNE_SCANCODE_W 0x11 +#define RUNE_SCANCODE_E 0x12 +#define RUNE_SCANCODE_R 0x13 +#define RUNE_SCANCODE_T 0x14 +#define RUNE_SCANCODE_Y 0x15 +#define RUNE_SCANCODE_U 0x16 +#define RUNE_SCANCODE_I 0x17 +#define RUNE_SCANCODE_O 0x18 +#define RUNE_SCANCODE_P 0x19 +#define RUNE_SCANCODE_A 0x1E +#define RUNE_SCANCODE_S 0x1F +#define RUNE_SCANCODE_D 0x20 +#define RUNE_SCANCODE_F 0x21 +#define RUNE_SCANCODE_G 0x22 +#define RUNE_SCANCODE_H 0x23 +#define RUNE_SCANCODE_J 0x24 +#define RUNE_SCANCODE_K 0x25 +#define RUNE_SCANCODE_L 0x26 +#define RUNE_SCANCODE_Z 0x2C +#define RUNE_SCANCODE_X 0x2D +#define RUNE_SCANCODE_C 0x2E +#define RUNE_SCANCODE_V 0x2F +#define RUNE_SCANCODE_B 0x30 +#define RUNE_SCANCODE_N 0x31 +#define RUNE_SCANCODE_M 0x32 + +#define RUNE_SCANCODE_BACKSPACE 0x0E +#define RUNE_SCANCODE_TAB 0x0F +#define RUNE_SCANCODE_BACKSLASH 0x2B +#define RUNE_SCANCODE_RETURN 0x1C + +#define RUNE_SCANCODE_LCTRL 0x1D +#define RUNE_SCANCODE_LSHIFT 0x2A +#define RUNE_SCANCODE_LALT 0x38 +#define RUNE_SCANCODE_LWIN 0x7D + +#define RUNE_SCANCODE_SPACE 0x39 + +#define RUNE_SCANCODE_RCTRL 0x61 +#define RUNE_SCANCODE_RSHIFT 0x36 +#define RUNE_SCANCODE_RALT 0x64 +#define RUNE_SCANCODE_RWIN 0x7E +#define RUNE_SCANCODE_MENU 0x7F + +#define RUNE_SCANCODE_LBRKT 0x1A +#define RUNE_SCANCODE_RBRKT 0x1B +#define RUNE_SCANCODE_SEMICOL 0x27 +#define RUNE_SCANCODE_QUOTE 0x28 +#define RUNE_SCANCODE_COMMA 0x33 +#define RUNE_SCANCODE_PERIOD 0x34 +#define RUNE_SCANCODE_SLASH 0x35 + +#define RUNE_SCANCODE_INSERT 0x6E +#define RUNE_SCANCODE_HOME 0x66 +#define RUNE_SCANCODE_PGUP 0x68 +#define RUNE_SCANCODE_DELETE 0x6F +#define RUNE_SCANCODE_END 0x6B +#define RUNE_SCANCODE_PGDN 0x6D + +#define RUNE_SCANCODE_UP 0x67 +#define RUNE_SCANCODE_DOWN 0x6C +#define RUNE_SCANCODE_LEFT 0x69 +#define RUNE_SCANCODE_RIGHT 0x6A + +#define RUNE_SCANCODE_NMLCK 0x45 +#define RUNE_SCANCODE_NMDIV 0x62 +#define RUNE_SCANCODE_NMMUL 0x37 +#define RUNE_SCANCODE_NMMNS 0x4A +#define RUNE_SCANCODE_NMPLS 0x4E +#define RUNE_SCANCODE_NMENT 0x60 +#define RUNE_SCANCODE_NMDEL 0x53 +#define RUNE_SCANCODE_NM1 0x4F +#define RUNE_SCANCODE_NM2 0x50 +#define RUNE_SCANCODE_NM3 0x51 +#define RUNE_SCANCODE_NM4 0x4B +#define RUNE_SCANCODE_NM5 0x4C +#define RUNE_SCANCODE_NM6 0x4D +#define RUNE_SCANCODE_NM7 0x47 +#define RUNE_SCANCODE_NM8 0x48 +#define RUNE_SCANCODE_NM9 0x49 +#define RUNE_SCANCODE_NM0 0x52 + +#endif diff --git a/include/rune/ui/window.h b/include/rune/ui/window.h new file mode 100644 index 0000000..42bb8b9 --- /dev/null +++ b/include/rune/ui/window.h @@ -0,0 +1,38 @@ +/* + * Rune Game Engine + * Copyright 2024 Danny Holman + * + * This software is provided 'as-is', without any express or implied + * warranty. In no event will the authors be held liable for any damages + * arising from the use of this software. + * + * Permission is granted to anyone to use this software for any purpose, + * including commercial applications, and to alter it and redistribute it + * freely, subject to the following restrictions: + * + * 1. The origin of this software must not be misrepresented; you must not + * claim that you wrote the original software. If you use this software + * in a product, an acknowledgment in the product documentation would be + * appreciated but is not required. + * 2. Altered source versions must be plainly marked as such, and must not be + * misrepresented as being the original software. + * 3. This notice may not be removed or altered from any source distribution. + */ + +#ifndef RUNE_UI_WINDOW_H +#define RUNE_UI_WINDOW_H + +#include +#include + +struct rune_window { + uint32_t winw; + uint32_t winh; + const char *wintitle; + GLFWwindow *window; +}; + +RAPI struct rune_window* rune_create_window(uint32_t width, uint32_t height, const char *title); +RAPI void rune_destroy_window(struct rune_window *window); + +#endif diff --git a/ui/input.c b/ui/input.c new file mode 100644 index 0000000..728e540 --- /dev/null +++ b/ui/input.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +static int keyboard_mode; +static void (*key_hooks[256])(void); + +void _key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { + if (action == GLFW_PRESS && key_hooks[scancode] != NULL) + (*key_hooks[scancode])(); +} + +int rune_input_init(struct rune_window *window) { + keyboard_mode = KB_MODE_RAW; + glfwSetKeyCallback(window->window, _key_callback); + for (int i = 0; i < 256; i++) + key_hooks[i] = NULL; +} + +void set_keyboard_mode(int mode) { + if (mode != KB_MODE_RAW || mode != KB_MODE_TEXT) { + log_output(LOG_ERROR, "Attempted to set keyboard to invalid mode"); + return; + } + keyboard_mode = mode; +} + +int get_keyboard_mode(void) { + return keyboard_mode; +} + +void register_key_hook(int key, void (*func)(void)) { + key_hooks[key] = func; +} + +void rune_input_tick(void) { + glfwPollEvents(); +} diff --git a/ui/renderer.c b/ui/renderer.c new file mode 100644 index 0000000..a4220b1 --- /dev/null +++ b/ui/renderer.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include +#include + +struct rune_renderer* rune_create_renderer(struct rune_window *window, int type) { + struct rune_renderer *ret = rune_alloc(sizeof(struct rune_renderer)); + ret->window = window; + + switch (type) { + case RUNE_RENDERER_VULKAN: + ret->init = rune_init_vulkan; + ret->close = rune_close_vulkan; + break; + case RUNE_RENDERER_DIRECTX: + ret->init = rune_init_directx; + ret->close = rune_close_directx; + break; + default: + log_output(LOG_FATAL, "Invalid rendering API selected"); + rune_abort(); + } + + (*ret->init)(ret); +} + +void rune_destroy_renderer(struct rune_renderer *renderer) { + (*renderer->close)(renderer); + rune_free(renderer); +} diff --git a/ui/window.c b/ui/window.c new file mode 100644 index 0000000..a15a45b --- /dev/null +++ b/ui/window.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include +#include +#include +#include + +struct rune_window* rune_create_window(uint32_t width, uint32_t height, const char *title) { + glfwInit(); + glfwSetErrorCallback(error_callback); + glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); + + struct rune_window *ret = rune_alloc(sizeof(struct rune_window)); + ret->winw = width; + ret->winh = height; + ret->wintitle = title; + ret->window = glfwCreateWindow(ret->winw, ret->winh, + ret->wintitle, + NULL, NULL); + if (ret->window == NULL) { + log_output(LOG_FATAL, "Cannot create window"); + rune_abort(); + } + + return ret; +} + +void rune_destroy_window(struct rune_window *window) { + glfwDestroyWindow(window->window); + glfwTerminate(); +} -- cgit v1.2.3