From 3705b14a4b2ac0d6baa418f08991424cfad89891 Mon Sep 17 00:00:00 2001 From: 0scar Date: Tue, 6 Feb 2024 10:48:05 +0100 Subject: Works on my machine Albeit when compiled twice :) --- CMakeLists.txt | 30 +-- src/CMakeLists.txt | 10 + src/core/CMakeLists.txt | 5 +- src/core/include/engine/core/logging.h | 2 +- src/core/include/engine/core/memory.h | 3 +- src/core/include/engine/core/state.h | 2 +- src/core/include/engine/engine.h | 19 +- src/core/src/dltools.c | 5 +- src/core/src/logging.c | 4 +- src/core/src/loop.c | 10 +- src/core/src/memory.c | 5 +- src/core/src/state.c | 8 +- src/ctrl/CMakeLists.txt | 2 +- src/ctrl/include/engine/ctrl/input.h | 2 +- src/ctrl/src/input.c | 6 +- src/main.c | 1 + src/rendering/CMakeLists.txt | 18 +- src/rendering/include/engine/rendering/rendering.h | 10 +- src/rendering/src/rendering.c | 2 +- src/resources/CMakeLists.txt | 2 +- src/resources/include/engine/resources.h | 79 ++++++ src/resources/include/engine/resources/resources.h | 79 ------ src/ui/CMakeLists.txt | 2 +- src/ui/include/engine/ui.h | 264 +++++++++++++++++++++ src/ui/include/engine/ui/ui.h | 264 --------------------- src/ui/src/positioning.c | 2 +- src/ui/src/rendering.c | 2 +- src/utils/CMakeLists.txt | 2 +- src/utils/include/engine/utils.h | 36 +++ src/utils/include/engine/utils/fov.h | 4 +- src/utils/include/engine/utils/hashmap.h | 8 +- src/utils/include/engine/utils/stack.h | 2 +- src/utils/include/engine/utils/utils.h | 36 --- src/utils/include/engine/utils/vector.h | 2 +- src/utils/src/btree.c | 2 +- src/utils/src/fov.c | 2 +- src/utils/src/hashmap.c | 4 +- src/utils/src/misc.c | 2 +- src/utils/src/stack.c | 4 +- src/utils/src/vector.c | 2 +- 40 files changed, 486 insertions(+), 458 deletions(-) create mode 100644 src/main.c create mode 100644 src/resources/include/engine/resources.h delete mode 100644 src/resources/include/engine/resources/resources.h create mode 100644 src/ui/include/engine/ui.h delete mode 100644 src/ui/include/engine/ui/ui.h create mode 100644 src/utils/include/engine/utils.h delete mode 100644 src/utils/include/engine/utils/utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index ca9021a..39f8e76 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -91,17 +91,21 @@ FetchContent_Declare(cglm ) FetchContent_MakeAvailable(cglm) -## Compilation information -add_subdirectory(src) +## Glad +set(GLAD_HEADER ${CMAKE_BINARY_DIR}/include/glad/gl.h) +## Compilation information add_compile_options(${RELEASE_OPTS}) add_link_options(${RELEASE_OPTS}) +#set_property(SOURCE src/core/include/engine/engine.h APPEND PROPERTY OBJECT_DEPENDS ${GLAD_HEADER}) + add_library(${PROJECT_NAME} - ${CMAKE_BINARY_DIR}/include/glad/gl.h - #${ENGINE_SOURCES} + src/main.c + ${GLAD_HEADER} ) + set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C) target_include_directories(${PROJECT_NAME} PUBLIC @@ -109,6 +113,10 @@ target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_BINARY_DIR}/include ${ENGINE_INCLUDE}) + +# Add the modules +add_subdirectory(src) + target_link_libraries(${PROJECT_NAME} glfw OpenGL::GL @@ -119,7 +127,7 @@ target_link_libraries(${PROJECT_NAME} daw_resources daw_ui daw_utils - NOT:$>:m> + $<$>:m> $<$:dl> $<$>,$>:ubsan> ) @@ -166,18 +174,6 @@ if (NOT NOT_SUBPROJECT) endif() include(DawAddState) -add_custom_command( - OUTPUT ${CMAKE_BINARY_DIR}/include/glad/gl.h - COMMAND - glad - --api gl:core=3.3 - --out-path ${CMAKE_BINARY_DIR} - --reproducible - c - --header-only - --mx -) - message("Configured ${PROJECT_NAME} ${PROJECT_VERSION}") message("version: ${ENGINE_VERSION_MAJOR}.${ENGINE_VERSION_MINOR}.${ENGINE_VERSION_PATCH}-${ENGINE_VERSION_TWEAK} (${ENGINE_BUILD_TYPE} build)") message("enable debug: ${DAW_BUILD_DEBUG}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5f0b8b5..b474ff7 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,3 +1,13 @@ +set(DAW_INCLUDE_DIRS + ${CMAKE_CURRENT_LIST_DIR}/core/include + ${CMAKE_CURRENT_LIST_DIR}/ctrl/include + ${CMAKE_CURRENT_LIST_DIR}/rendering/include + ${CMAKE_CURRENT_LIST_DIR}/resources/include + ${CMAKE_CURRENT_LIST_DIR}/ui/include + ${CMAKE_CURRENT_LIST_DIR}/utils/include + ${CMAKE_BINARY_DIR}/include + ) + add_subdirectory(core) add_subdirectory(ctrl) add_subdirectory(rendering) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c6adabf..a9e2b6d 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -9,4 +9,7 @@ add_library(daw_core src/state.c ) -target_include_directories(daw_core PRIVATE include) +set_property(SOURCE src/loop.c APPEND PROPERTY OBJECT_DEPENDS ${GLAD_HEADER}) + +target_include_directories(daw_core PRIVATE ${DAW_INCLUDE_DIRS}) +target_link_libraries(daw_core PRIVATE cglm) diff --git a/src/core/include/engine/core/logging.h b/src/core/include/engine/core/logging.h index d1c5890..52bb446 100644 --- a/src/core/include/engine/core/logging.h +++ b/src/core/include/engine/core/logging.h @@ -7,7 +7,7 @@ #include #include -#include "types.h" +#include #if defined __linux__ || defined __APPLE__ #define TERM_COLOR_RESET "\033[0m" diff --git a/src/core/include/engine/core/memory.h b/src/core/include/engine/core/memory.h index 04c24d5..dbdd065 100644 --- a/src/core/include/engine/core/memory.h +++ b/src/core/include/engine/core/memory.h @@ -1,8 +1,7 @@ #ifndef MEMORY_H #define MEMORY_H -#include "types.h" -// #include +#include typedef struct memory { void* data; diff --git a/src/core/include/engine/core/state.h b/src/core/include/engine/core/state.h index 13593ed..af94560 100644 --- a/src/core/include/engine/core/state.h +++ b/src/core/include/engine/core/state.h @@ -1,7 +1,7 @@ #ifndef STATE_H #define STATE_H -#include +#include typedef enum StateType { STATE_null, diff --git a/src/core/include/engine/engine.h b/src/core/include/engine/engine.h index e8a5ace..ba4e227 100644 --- a/src/core/include/engine/engine.h +++ b/src/core/include/engine/engine.h @@ -3,14 +3,17 @@ #include -#include -#include -#include +// TODO: Cleanup the includes, ideally this header file should only include all +// "public-facing" headers. + +#include +#include +#include +#include +#include #include -#include -#include -#include -#include +#include +#include typedef struct { u32 texture_id; @@ -83,7 +86,7 @@ void engine_input_ctx_push(i_ctx* ctx); void engine_input_ctx_pop(void); void engine_input_ctx_reset(void); -#include "rendering.h" +#include #ifdef ENGINE_INTERNALS diff --git a/src/core/src/dltools.c b/src/core/src/dltools.c index 1e43b79..d27c4ff 100644 --- a/src/core/src/dltools.c +++ b/src/core/src/dltools.c @@ -1,4 +1,5 @@ #include +#include #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) /* include winapi */ @@ -6,8 +7,8 @@ #include #endif -#include -#include +#include +#include bool dynamic_library_close(void* shared_library) { #if defined(_WIN32) || defined(__WIN32__) || defined(WIN32) diff --git a/src/core/src/logging.c b/src/core/src/logging.c index 7870258..94782ac 100644 --- a/src/core/src/logging.c +++ b/src/core/src/logging.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include char* itoa(i32 x) { diff --git a/src/core/src/loop.c b/src/core/src/loop.c index 79e0c0c..837d193 100644 --- a/src/core/src/loop.c +++ b/src/core/src/loop.c @@ -25,14 +25,12 @@ #endif #define ENGINE_INTERNALS -#include #include -#include -#include +#include +#include +#include -#include -// #include -// #include +#include #define DEFAULT_NUM_PROCS 8 diff --git a/src/core/src/memory.c b/src/core/src/memory.c index f19803e..7a51a0d 100644 --- a/src/core/src/memory.c +++ b/src/core/src/memory.c @@ -2,9 +2,8 @@ #include #include -#include - -#include +#include +#include memory* memory_new(usize max_size) { memory* m = malloc(sizeof(memory)); diff --git a/src/core/src/state.c b/src/core/src/state.c index 55f2a12..a2af5e1 100644 --- a/src/core/src/state.c +++ b/src/core/src/state.c @@ -1,9 +1,9 @@ #include -#include -#include -#include -#include +#include +#include +#include +#include typedef StateType state_update_t(void*); diff --git a/src/ctrl/CMakeLists.txt b/src/ctrl/CMakeLists.txt index 7a449d6..3f76069 100644 --- a/src/ctrl/CMakeLists.txt +++ b/src/ctrl/CMakeLists.txt @@ -6,4 +6,4 @@ add_library(daw_ctrl src/mouse.c ) -target_include_directories(daw_ctrl PRIVATE include) +target_include_directories(daw_ctrl PRIVATE ${DAW_INCLUDE_DIRS}) diff --git a/src/ctrl/include/engine/ctrl/input.h b/src/ctrl/include/engine/ctrl/input.h index 49b3a96..26a5f5b 100644 --- a/src/ctrl/include/engine/ctrl/input.h +++ b/src/ctrl/include/engine/ctrl/input.h @@ -1,7 +1,7 @@ #ifndef INPUT_H #define INPUT_H -#include +#include typedef void input_callback_t(void*); typedef i32 scancode_t; diff --git a/src/ctrl/src/input.c b/src/ctrl/src/input.c index 34b1718..2fd025e 100644 --- a/src/ctrl/src/input.c +++ b/src/ctrl/src/input.c @@ -1,6 +1,6 @@ -#include -#include -#include +#include +#include +#include #include /* Lazy binds, used internally. They are similar to BindAction and friends. diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..adef7b9 --- /dev/null +++ b/src/main.c @@ -0,0 +1 @@ +/* Intentionally left blank for now */ diff --git a/src/rendering/CMakeLists.txt b/src/rendering/CMakeLists.txt index e04d858..f19cce0 100644 --- a/src/rendering/CMakeLists.txt +++ b/src/rendering/CMakeLists.txt @@ -3,9 +3,25 @@ add_library(daw_rendering src/rendering.c src/text.c src/window.c + ${GLAD_HEADER} ) -target_include_directories(daw_rendering PRIVATE include) +add_custom_command( + OUTPUT ${GLAD_HEADER} + COMMAND + glad + --api gl:core=4.6 + --out-path ${CMAKE_BINARY_DIR} + --reproducible + c + --header-only + --mx + ) + +set_property(SOURCE src/window.c APPEND PROPERTY OBJECT_DEPENDS ${GLAD_HEADER}) +set_property(SOURCE src/rendering.c APPEND PROPERTY OBJECT_DEPENDS ${GLAD_HEADER}) + +target_include_directories(daw_rendering PRIVATE ${DAW_INCLUDE_DIRS}) target_link_libraries(daw_rendering PRIVATE OpenGL::GL cglm diff --git a/src/rendering/include/engine/rendering/rendering.h b/src/rendering/include/engine/rendering/rendering.h index ff24412..6ae6535 100644 --- a/src/rendering/include/engine/rendering/rendering.h +++ b/src/rendering/include/engine/rendering/rendering.h @@ -1,8 +1,8 @@ #ifndef RENDERING_H #define RENDERING_H -#include "types.h" -#include "vector.h" +#include +#include #define GLFW_INCLUDE_NONE #include @@ -26,8 +26,8 @@ typedef struct { } Sprite; -#include "engine.h" -#include "ui.h" +#include +#include /* Rendering functions */ void render_begin(Window* w); @@ -52,7 +52,7 @@ void engine_draw_sprite_ex(Sprite* s, v2_i32* pos, f32 scale, Sprite sprite_new(u64 tid, u8 coord); #ifdef ENGINE_INTERNALS -#include "engine.h" +#include //#include //#define GLFW_INCLUDE_NONE diff --git a/src/rendering/src/rendering.c b/src/rendering/src/rendering.c index dff3432..808897c 100644 --- a/src/rendering/src/rendering.c +++ b/src/rendering/src/rendering.c @@ -9,7 +9,7 @@ #define ENGINE_INTERNALS #include -#include +#include /* Extern globals */ extern Platform* GLOBAL_PLATFORM; diff --git a/src/resources/CMakeLists.txt b/src/resources/CMakeLists.txt index 02ab1ca..6a99990 100644 --- a/src/resources/CMakeLists.txt +++ b/src/resources/CMakeLists.txt @@ -4,4 +4,4 @@ add_library(daw_resources src/textures.c ) -target_include_directories(daw_resources PRIVATE include) +target_include_directories(daw_resources PRIVATE ${DAW_INCLUDE_DIRS}) diff --git a/src/resources/include/engine/resources.h b/src/resources/include/engine/resources.h new file mode 100644 index 0000000..9de24ce --- /dev/null +++ b/src/resources/include/engine/resources.h @@ -0,0 +1,79 @@ +#ifndef RESOURCES_H +#define RESOURCES_H + +#include + +/* We need some "global resources", available to all states. + * These are resources such as common fonts, GUI frames, button background + * images. + * + * We need to define state-specific resources as well. + * - Can both be defined alike? + * If we lazy-load all resources we can get away with a lot. + * Maybe use fall-back resources? like the missing source texture, and an ugly + * font? + * - Then declare to the engine, in the main function for the game, that these + * resources are to be made available throughout? + * - Make all resource specifications A UNION?! 🤯 + * */ + +enum Asset { + Asset_error, + Asset_font, + Asset_texture, + Asset_audio, +}; + +typedef struct { + enum Asset type; + const char* font_path; + i32 ptsize; +} Asset_FontSpec; + +typedef struct { + enum Asset type; + const char* path; + i32 width; + i32 height; +} Asset_TextureSpec; + +typedef struct { + enum Asset type; + const char* path; +} Asset_AudioSpec; + +typedef union { + enum Asset type; + Asset_FontSpec font; + Asset_TextureSpec texture; + Asset_AudioSpec audio; +} asset_t; + +#define Resource_FontDefinition(_path, _fontsize) \ + (const Asset_FontSpec) { \ + .type = Asset_font, .font_path = _path, .ptsize = _fontsize \ + } + +#define Resource_TextureAtlasDefinition(_path, _subtexture_width, \ + _subtexture_height) \ + (const Asset_TextureSpec) { \ + .type = Asset_texture, .width = _subtexture_width, \ + .height = _subtexture_height, .path = _path \ + } + +#define TextureDefinition(_path, ...) unimplemented + +#define Resource_AudioDefinition(_path, ...) unimplemented + +/* Each of resource_load_font, resource_load_texture, and resource_load_audio + * loads a given resource into the engines memory and returns an identifier. + */ +isize resource_load_font(Asset_FontSpec font_def); +isize resource_load_texture(Asset_TextureSpec texture_def); +isize resource_load_audio(Asset_AudioSpec audio_def); + +/* Makes a resource globally available. This must be called **BEFORE** any call + * to `engine_run` */ +isize resource_make_global(isize resource_id); + +#endif diff --git a/src/resources/include/engine/resources/resources.h b/src/resources/include/engine/resources/resources.h deleted file mode 100644 index 1689544..0000000 --- a/src/resources/include/engine/resources/resources.h +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef RESOURCES_H -#define RESOURCES_H - -#include - -/* We need some "global resources", available to all states. - * These are resources such as common fonts, GUI frames, button background - * images. - * - * We need to define state-specific resources as well. - * - Can both be defined alike? - * If we lazy-load all resources we can get away with a lot. - * Maybe use fall-back resources? like the missing source texture, and an ugly - * font? - * - Then declare to the engine, in the main function for the game, that these - * resources are to be made available throughout? - * - Make all resource specifications A UNION?! 🤯 - * */ - -enum Asset { - Asset_error, - Asset_font, - Asset_texture, - Asset_audio, -}; - -typedef struct { - enum Asset type; - const char* font_path; - i32 ptsize; -} Asset_FontSpec; - -typedef struct { - enum Asset type; - const char* path; - i32 width; - i32 height; -} Asset_TextureSpec; - -typedef struct { - enum Asset type; - const char* path; -} Asset_AudioSpec; - -typedef union { - enum Asset type; - Asset_FontSpec font; - Asset_TextureSpec texture; - Asset_AudioSpec audio; -} asset_t; - -#define Resource_FontDefinition(_path, _fontsize) \ - (const Asset_FontSpec) { \ - .type = Asset_font, .font_path = _path, .ptsize = _fontsize \ - } - -#define Resource_TextureAtlasDefinition(_path, _subtexture_width, \ - _subtexture_height) \ - (const Asset_TextureSpec) { \ - .type = Asset_texture, .width = _subtexture_width, \ - .height = _subtexture_height, .path = _path \ - } - -#define TextureDefinition(_path, ...) unimplemented - -#define Resource_AudioDefinition(_path, ...) unimplemented - -/* Each of resource_load_font, resource_load_texture, and resource_load_audio - * loads a given resource into the engines memory and returns an identifier. - */ -isize resource_load_font(Asset_FontSpec font_def); -isize resource_load_texture(Asset_TextureSpec texture_def); -isize resource_load_audio(Asset_AudioSpec audio_def); - -/* Makes a resource globally available. This must be called **BEFORE** any call - * to `engine_run` */ -isize resource_make_global(isize resource_id); - -#endif diff --git a/src/ui/CMakeLists.txt b/src/ui/CMakeLists.txt index 23dcae6..fa91929 100644 --- a/src/ui/CMakeLists.txt +++ b/src/ui/CMakeLists.txt @@ -3,4 +3,4 @@ add_library(daw_ui src/rendering.c ) -target_include_directories(daw_ui PRIVATE include) +target_include_directories(daw_ui PRIVATE ${DAW_INCLUDE_DIRS}) diff --git a/src/ui/include/engine/ui.h b/src/ui/include/engine/ui.h new file mode 100644 index 0000000..eea81af --- /dev/null +++ b/src/ui/include/engine/ui.h @@ -0,0 +1,264 @@ +#ifndef ENGINE_UI_H +#define ENGINE_UI_H + +#include +#include +#include + +#define DIRECTION_HORIZONTAL true +#define DIRECTION_VERTICAL false + +/* Ui positioning constraints */ +typedef enum { + ui_size_pixel, + ui_size_percent, +} ui_size_t; + +typedef union { + ui_size_t type; + struct { + ui_size_t type; + i32 value; + } pixel; + struct { + ui_size_t type; + f32 value; + } percent; +} ui_size; + +typedef enum { + ui_constraint_width, + ui_constraint_height, + + ui_constraint_horizontal_align, + ui_constraint_vertical_align, + + ui_constraint_left, + ui_constraint_right, + ui_constraint_top, + ui_constraint_bottom, +} ui_constraint_t; + +typedef enum { + ui_constraint_vertical_align_top, + ui_constraint_vertical_align_center, + ui_constraint_vertical_align_bottom, +} ui_constraint_vertical_align_t; + +typedef enum { + ui_constraint_horizontal_align_left, + ui_constraint_horizontal_align_center, + ui_constraint_horizontal_align_right, +} ui_constraint_horizontal_align_t; + +typedef union { + struct { + ui_size size; + } width; + struct { + ui_size size; + } height; + + /* spacing is the padding between the aligned edge and the container itself */ + struct { + ui_constraint_horizontal_align_t align; + ui_size spacing; + } horizontal_align; + struct { + ui_constraint_vertical_align_t align; + ui_size spacing; + } vertical_align; + + struct { + ui_size pos; + } left; + struct { + ui_size pos; + } top; + struct { + ui_size pos; + } right; + struct { + ui_size pos; + } bottom; +} constraintval_t; + +#define CONSTRAINT_POSITION(axis, alignment_sub, margin, cnext) \ + { \ + .value = \ + { \ + .type = ui_constraint_##axis##_align, \ + .constraint.axis##_align = \ + { \ + .align = ui_constraint_##axis##_align_##alignment_sub, \ + .spacing = margin, \ + }, \ + }, \ + .next = cnext, \ + } + +#define CONSTRAINT_SIZE(c_type, csize, cnext) \ + { \ + .value = \ + { \ + .type = ui_constraint_##c_type, \ + .constraint.c_type = \ + { \ + .size = csize, \ + }, \ + }, \ + .next = cnext, \ + } + +typedef struct { + ui_constraint_t type; + constraintval_t constraint; +} ui_constraint; + +typedef DEFINE_LLIST(ui_constraint); + +enum uitype { + uitype_container, + uitype_button, + uitype_title, + uitype_text, + + uitype_MAX, +}; + +typedef struct UITree_container { + enum uitype type; + bool visible; + bool direction; + i32 x, y, w, h; + i32 padding; + i32 margin; + + Engine_color fg; + Engine_color bg; + Engine_color bordercolor; + + usize children_len; /* Number of children */ + usize children_size; /* Memory size */ + union UITree** children; + struct List_ui_constraint* constraints; +} UITree_container; + +typedef struct UITree_button { + enum uitype type; + + bool enabled; + u64 id; + + i32 x, y, w, h; + i32 padding; + i32 margin; + + Engine_color fg; + Engine_color bg; + + i32 font_id; + const char* text_original; + u64 text_texture_index; + v2_i32 texture_size; + struct List_ui_constraint* constraints; +} UITree_button; + +typedef struct UITree_title { + enum uitype type; + i32 x, y, w, h; + + i32 padding; + i32 margin; + + Engine_color fg; + + i32 font_id; + const char* text_original; + u64 text_texture_index; + v2_i32 texture_size; + struct List_ui_constraint* constraints; +} UITree_title; + +typedef struct UITree_text { + enum uitype type; + i32 x, y, w, h; + + i32 padding; + i32 margin; + + Engine_color fg; + + i32 font_id; + const char* text_original; + u64 text_texture_index; + v2_i32 texture_size; + struct List_ui_constraint* constraints; +} UITree_text; + +typedef union UITree { + enum uitype type; + + UITree_container container; + UITree_button button; + UITree_title title; + UITree_text text; + struct List_ui_constraint constraints; +} UITree; + +/* Sizes */ +f32 ui_size_pixel_to_percent_width(i32 pixels); +f32 ui_size_pixel_to_percent_height(i32 pixels); +f32 ui_size_pixel_to_percent(i32 pixels); +i32 ui_size_percent_width_to_pixel(f32 percent); +i32 ui_size_percent_height_to_pixel(f32 percent); +i32 ui_size_percent_to_pixel(f32 percent); +i32 ui_size_to_pixel(ui_size s); + +#define ui_size_percent_new(p) \ + { \ + .percent = {.type = ui_size_percent, .value = p } \ + } +#define ui_size_pixel_new(p) \ + { \ + .pixel = {.type = ui_size_pixel, .value = p } \ + } + +/* Constructors */ +UITree* ui_container(bool direction, struct List_ui_constraint* constraints); +UITree* ui_button(u64 id, i32 font_id, char* text, + struct List_ui_constraint* constraints); +UITree* ui_title(i32 font_id, const char* text, + struct List_ui_constraint* constraints); +UITree* ui_text(i32 font_id, const char* text, + struct List_ui_constraint* constraints); + +/* Extended Constructors */ +UITree* ui_container_new_ex(Engine_color fg, Engine_color bg, + Engine_color border, bool direction, + struct List_ui_constraint* constraints); + +/* Destructors */ +void clear_ui(void); +void uitree_free(UITree* t); + +/* Manipulation */ +void ui_container_attach(UITree* container, UITree* child); + +void ui_resolve_constraints(void); + +void ui_button_enable(UITree_button* b); +void ui_button_disable(UITree_button* b); + +/* Tweaking default colors */ +void ui_set_default_colors(Engine_color fg, Engine_color bg); +void ui_set_default_padding(i32 padding); +Engine_color ui_get_default_fg(void); +Engine_color ui_get_default_bg(void); + +#define ELEM_NOT_FOUND (-1) +#define NO_CLICK (-2) +/* Returns -1 if not found */ +u64 ui_check_click(UITree* root); + +#endif diff --git a/src/ui/include/engine/ui/ui.h b/src/ui/include/engine/ui/ui.h deleted file mode 100644 index 4a3cd3e..0000000 --- a/src/ui/include/engine/ui/ui.h +++ /dev/null @@ -1,264 +0,0 @@ -#ifndef ENGINE_UI_H -#define ENGINE_UI_H - -#include "list.h" -#include "types.h" -#include "vector.h" - -#define DIRECTION_HORIZONTAL true -#define DIRECTION_VERTICAL false - -/* Ui positioning constraints */ -typedef enum { - ui_size_pixel, - ui_size_percent, -} ui_size_t; - -typedef union { - ui_size_t type; - struct { - ui_size_t type; - i32 value; - } pixel; - struct { - ui_size_t type; - f32 value; - } percent; -} ui_size; - -typedef enum { - ui_constraint_width, - ui_constraint_height, - - ui_constraint_horizontal_align, - ui_constraint_vertical_align, - - ui_constraint_left, - ui_constraint_right, - ui_constraint_top, - ui_constraint_bottom, -} ui_constraint_t; - -typedef enum { - ui_constraint_vertical_align_top, - ui_constraint_vertical_align_center, - ui_constraint_vertical_align_bottom, -} ui_constraint_vertical_align_t; - -typedef enum { - ui_constraint_horizontal_align_left, - ui_constraint_horizontal_align_center, - ui_constraint_horizontal_align_right, -} ui_constraint_horizontal_align_t; - -typedef union { - struct { - ui_size size; - } width; - struct { - ui_size size; - } height; - - /* spacing is the padding between the aligned edge and the container itself */ - struct { - ui_constraint_horizontal_align_t align; - ui_size spacing; - } horizontal_align; - struct { - ui_constraint_vertical_align_t align; - ui_size spacing; - } vertical_align; - - struct { - ui_size pos; - } left; - struct { - ui_size pos; - } top; - struct { - ui_size pos; - } right; - struct { - ui_size pos; - } bottom; -} constraintval_t; - -#define CONSTRAINT_POSITION(axis, alignment_sub, margin, cnext) \ - { \ - .value = \ - { \ - .type = ui_constraint_##axis##_align, \ - .constraint.axis##_align = \ - { \ - .align = ui_constraint_##axis##_align_##alignment_sub, \ - .spacing = margin, \ - }, \ - }, \ - .next = cnext, \ - } - -#define CONSTRAINT_SIZE(c_type, csize, cnext) \ - { \ - .value = \ - { \ - .type = ui_constraint_##c_type, \ - .constraint.c_type = \ - { \ - .size = csize, \ - }, \ - }, \ - .next = cnext, \ - } - -typedef struct { - ui_constraint_t type; - constraintval_t constraint; -} ui_constraint; - -typedef DEFINE_LLIST(ui_constraint); - -enum uitype { - uitype_container, - uitype_button, - uitype_title, - uitype_text, - - uitype_MAX, -}; - -typedef struct UITree_container { - enum uitype type; - bool visible; - bool direction; - i32 x, y, w, h; - i32 padding; - i32 margin; - - Engine_color fg; - Engine_color bg; - Engine_color bordercolor; - - usize children_len; /* Number of children */ - usize children_size; /* Memory size */ - union UITree** children; - struct List_ui_constraint* constraints; -} UITree_container; - -typedef struct UITree_button { - enum uitype type; - - bool enabled; - u64 id; - - i32 x, y, w, h; - i32 padding; - i32 margin; - - Engine_color fg; - Engine_color bg; - - i32 font_id; - const char* text_original; - u64 text_texture_index; - v2_i32 texture_size; - struct List_ui_constraint* constraints; -} UITree_button; - -typedef struct UITree_title { - enum uitype type; - i32 x, y, w, h; - - i32 padding; - i32 margin; - - Engine_color fg; - - i32 font_id; - const char* text_original; - u64 text_texture_index; - v2_i32 texture_size; - struct List_ui_constraint* constraints; -} UITree_title; - -typedef struct UITree_text { - enum uitype type; - i32 x, y, w, h; - - i32 padding; - i32 margin; - - Engine_color fg; - - i32 font_id; - const char* text_original; - u64 text_texture_index; - v2_i32 texture_size; - struct List_ui_constraint* constraints; -} UITree_text; - -typedef union UITree { - enum uitype type; - - UITree_container container; - UITree_button button; - UITree_title title; - UITree_text text; - struct List_ui_constraint constraints; -} UITree; - -/* Sizes */ -f32 ui_size_pixel_to_percent_width(i32 pixels); -f32 ui_size_pixel_to_percent_height(i32 pixels); -f32 ui_size_pixel_to_percent(i32 pixels); -i32 ui_size_percent_width_to_pixel(f32 percent); -i32 ui_size_percent_height_to_pixel(f32 percent); -i32 ui_size_percent_to_pixel(f32 percent); -i32 ui_size_to_pixel(ui_size s); - -#define ui_size_percent_new(p) \ - { \ - .percent = {.type = ui_size_percent, .value = p } \ - } -#define ui_size_pixel_new(p) \ - { \ - .pixel = {.type = ui_size_pixel, .value = p } \ - } - -/* Constructors */ -UITree* ui_container(bool direction, struct List_ui_constraint* constraints); -UITree* ui_button(u64 id, i32 font_id, char* text, - struct List_ui_constraint* constraints); -UITree* ui_title(i32 font_id, const char* text, - struct List_ui_constraint* constraints); -UITree* ui_text(i32 font_id, const char* text, - struct List_ui_constraint* constraints); - -/* Extended Constructors */ -UITree* ui_container_new_ex(Engine_color fg, Engine_color bg, - Engine_color border, bool direction, - struct List_ui_constraint* constraints); - -/* Destructors */ -void clear_ui(void); -void uitree_free(UITree* t); - -/* Manipulation */ -void ui_container_attach(UITree* container, UITree* child); - -void ui_resolve_constraints(void); - -void ui_button_enable(UITree_button* b); -void ui_button_disable(UITree_button* b); - -/* Tweaking default colors */ -void ui_set_default_colors(Engine_color fg, Engine_color bg); -void ui_set_default_padding(i32 padding); -Engine_color ui_get_default_fg(void); -Engine_color ui_get_default_bg(void); - -#define ELEM_NOT_FOUND (-1) -#define NO_CLICK (-2) -/* Returns -1 if not found */ -u64 ui_check_click(UITree* root); - -#endif diff --git a/src/ui/src/positioning.c b/src/ui/src/positioning.c index 3db73fe..6bb32fb 100644 --- a/src/ui/src/positioning.c +++ b/src/ui/src/positioning.c @@ -1,8 +1,8 @@ #include #define ENGINE_INTERNALS -#include #include +#include #include static Engine_color DEFAULT_FG = {0xFF, 0xFF, 0xFF, 0xFF}; diff --git a/src/ui/src/rendering.c b/src/ui/src/rendering.c index 1516736..093f373 100644 --- a/src/ui/src/rendering.c +++ b/src/ui/src/rendering.c @@ -5,7 +5,7 @@ #define ENGINE_INTERNALS #include -#include +#include extern Platform* GLOBAL_PLATFORM; diff --git a/src/utils/CMakeLists.txt b/src/utils/CMakeLists.txt index 7508d26..64f5586 100644 --- a/src/utils/CMakeLists.txt +++ b/src/utils/CMakeLists.txt @@ -7,4 +7,4 @@ add_library(daw_utils src/vector.c ) -target_include_directories(daw_utils PRIVATE include) +target_include_directories(daw_utils PRIVATE ${DAW_INCLUDE_DIRS}) diff --git a/src/utils/include/engine/utils.h b/src/utils/include/engine/utils.h new file mode 100644 index 0000000..d10a03a --- /dev/null +++ b/src/utils/include/engine/utils.h @@ -0,0 +1,36 @@ +#ifndef ENGINE_UTILS_H +#define ENGINE_UTILS_H + +#include +#include + +#define MIN(a, b) ((a < b) ? (a) : (b)) +#define MAX(a, b) ((a > b) ? (a) : (b)) + +#define MASK_TL (1 << 0) +#define MASK_T (1 << 1) +#define MASK_TR (1 << 2) +#define MASK_L (1 << 3) +#define MASK_C (1 << 4) +#define MASK_R (1 << 5) +#define MASK_BL (1 << 6) +#define MASK_B (1 << 7) +#define MASK_BR (1 << 8) + +/* Linear interpolate */ +f32 lerp(f32 dt, f32 a, f32 b); +i32 int_lerp(f32 dt, i32 a, i32 b); + +/* Hashes */ +u32 hash(char* str); + +/* Masks surrounding tiles of a kernel size of 3x3 */ +/* In reality we only need 9 bits for this, but I think I had a reason for using + * i32 */ +i32* kernmap(const void* map, i32* dstmap, const v2_i32 mapsize, + predicate_t* predicate); + +/* Returns an index from the given weights. */ +i32 pick_from_sample(const i32* weights, i32 len); + +#endif diff --git a/src/utils/include/engine/utils/fov.h b/src/utils/include/engine/utils/fov.h index 15ef38b..b083d2b 100644 --- a/src/utils/include/engine/utils/fov.h +++ b/src/utils/include/engine/utils/fov.h @@ -1,8 +1,8 @@ #ifndef ENGINE_FOV_H #define ENGINE_FOV_H -#include "types.h" -#include "vector.h" +#include +#include #include /* `fov_shadowcast`: */ diff --git a/src/utils/include/engine/utils/hashmap.h b/src/utils/include/engine/utils/hashmap.h index bf1d87c..046c810 100644 --- a/src/utils/include/engine/utils/hashmap.h +++ b/src/utils/include/engine/utils/hashmap.h @@ -1,12 +1,12 @@ #ifndef ENGINE_HASHMAP_H #define ENGINE_HASHMAP_H -#include "types.h" - -#include "list.h" -#include "memory.h" #include +#include +#include +#include + i32 lolhash(const usize s, i32 v); /* Define a linked list before using this */ diff --git a/src/utils/include/engine/utils/stack.h b/src/utils/include/engine/utils/stack.h index 69975df..9fc53aa 100644 --- a/src/utils/include/engine/utils/stack.h +++ b/src/utils/include/engine/utils/stack.h @@ -25,7 +25,7 @@ #ifndef STACK_H #define STACK_H -#include "types.h" +#include typedef struct { isize head; /* current number of elements */ diff --git a/src/utils/include/engine/utils/utils.h b/src/utils/include/engine/utils/utils.h deleted file mode 100644 index e537f52..0000000 --- a/src/utils/include/engine/utils/utils.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef ENGINE_UTILS_H -#define ENGINE_UTILS_H - -#include "types.h" -#include "vector.h" - -#define MIN(a, b) ((a < b) ? (a) : (b)) -#define MAX(a, b) ((a > b) ? (a) : (b)) - -#define MASK_TL (1 << 0) -#define MASK_T (1 << 1) -#define MASK_TR (1 << 2) -#define MASK_L (1 << 3) -#define MASK_C (1 << 4) -#define MASK_R (1 << 5) -#define MASK_BL (1 << 6) -#define MASK_B (1 << 7) -#define MASK_BR (1 << 8) - -/* Linear interpolate */ -f32 lerp(f32 dt, f32 a, f32 b); -i32 int_lerp(f32 dt, i32 a, i32 b); - -/* Hashes */ -u32 hash(char* str); - -/* Masks surrounding tiles of a kernel size of 3x3 */ -/* In reality we only need 9 bits for this, but I think I had a reason for using - * i32 */ -i32* kernmap(const void* map, i32* dstmap, const v2_i32 mapsize, - predicate_t* predicate); - -/* Returns an index from the given weights. */ -i32 pick_from_sample(const i32* weights, i32 len); - -#endif diff --git a/src/utils/include/engine/utils/vector.h b/src/utils/include/engine/utils/vector.h index 58bb0a2..11517dc 100644 --- a/src/utils/include/engine/utils/vector.h +++ b/src/utils/include/engine/utils/vector.h @@ -1,7 +1,7 @@ #ifndef VECTOR_H #define VECTOR_H -#include "types.h" +#include #include #include diff --git a/src/utils/src/btree.c b/src/utils/src/btree.c index c125564..1e85e6c 100644 --- a/src/utils/src/btree.c +++ b/src/utils/src/btree.c @@ -1,4 +1,4 @@ -#include +#include #include #include diff --git a/src/utils/src/fov.c b/src/utils/src/fov.c index 3d5ae16..7bd27e2 100644 --- a/src/utils/src/fov.c +++ b/src/utils/src/fov.c @@ -1,4 +1,4 @@ -#include +#include #include #include #include diff --git a/src/utils/src/hashmap.c b/src/utils/src/hashmap.c index 1652bb6..61c5e43 100644 --- a/src/utils/src/hashmap.c +++ b/src/utils/src/hashmap.c @@ -1,3 +1,5 @@ -#include +#include +/* Currently, this is a "works, but very poorly" placeholder implementation. + * Should be avoided in practice */ i32 lolhash(const usize s, i32 v) { return v % s; } diff --git a/src/utils/src/misc.c b/src/utils/src/misc.c index 0f3d218..290c417 100644 --- a/src/utils/src/misc.c +++ b/src/utils/src/misc.c @@ -3,7 +3,7 @@ #include -#include +#include #include /* These should all be in some external facing module "tools" */ diff --git a/src/utils/src/stack.c b/src/utils/src/stack.c index ff195ba..a5fc419 100644 --- a/src/utils/src/stack.c +++ b/src/utils/src/stack.c @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include Stack stack_new_ex(const usize element_size, const usize size) { diff --git a/src/utils/src/vector.c b/src/utils/src/vector.c index 3465df7..5fedb1f 100644 --- a/src/utils/src/vector.c +++ b/src/utils/src/vector.c @@ -1,5 +1,5 @@ #include -#include +#include bool v2_i32_eq(const v2_i32 a, const v2_i32 b) { return (a.x == b.x) && (a.y == b.y); -- cgit v1.3