diff options
| author | realtradam <[email protected]> | 2023-01-11 22:09:48 -0500 |
|---|---|---|
| committer | realtradam <[email protected]> | 2023-01-11 22:09:48 -0500 |
| commit | b172c2a6b22796dc16c059979d2ec6108b0402e4 (patch) | |
| tree | 9eda4db6d584b4c0415d30c9b5295aaf541f938b | |
| parent | b4bc89485ab18ccdc2e381e3e3f2c3bb5e346e1e (diff) | |
| download | RodeoKit-b172c2a6b22796dc16c059979d2ec6108b0402e4.tar.gz RodeoKit-b172c2a6b22796dc16c059979d2ec6108b0402e4.zip | |
isolated dependencies into seperate compilation unit, added basic logging
| -rw-r--r-- | CMakeLists.txt | 6 | ||||
| -rw-r--r-- | include/rodeo.h | 41 | ||||
| -rw-r--r-- | include/rodeo_config.h | 2 | ||||
| -rw-r--r-- | include/rodeo_math.h | 8 | ||||
| -rw-r--r-- | include/rodeo_types.h | 47 | ||||
| -rw-r--r-- | src/private/rodeo_error.h | 20 | ||||
| -rw-r--r-- | src/private/rodeo_internal.h | 7 | ||||
| -rw-r--r-- | src/private/rodeo_internal_types.h | 35 | ||||
| -rw-r--r-- | src/rodeo.c | 83 | ||||
| -rw-r--r-- | src/rodeo_error.c | 48 | ||||
| -rw-r--r-- | src/rodeo_math.c | 4 | ||||
| -rw-r--r-- | src/rodeo_types.c | 34 |
12 files changed, 262 insertions, 73 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index ed2944b..c51caf3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,6 +6,8 @@ project(RodeoEngine) file(GLOB SOURCES "src/rodeo.c" "src/rodeo_math.c" + "src/rodeo_types.c" + "src/rodeo_error.c" ) #add_library(lib src/lib.c) @@ -49,6 +51,7 @@ target_include_directories(${PROJECT_NAME} PUBLIC external/bx/include PUBLIC external/cglm/include PUBLIC include + PRIVATE src ) target_link_directories(${PROJECT_NAME} @@ -62,5 +65,6 @@ ExternalProject_Get_Property(project_bgfx BINARY_DIR) target_link_libraries(${PROJECT_NAME} PRIVATE SDL2::SDL2 # dynamic lib cglm_headers - ${BINARY_DIR}/.build/linux64_gcc/bin/libbgfx-shared-libDebug.so + ${BINARY_DIR}/.build/linux64_gcc/bin/libbgfx-shared-libRelease.so + #${BINARY_DIR}/.build/linux64_gcc/bin/libbgfx-shared-libDebug.so ) diff --git a/include/rodeo.h b/include/rodeo.h index 4686cb5..7b29d17 100644 --- a/include/rodeo.h +++ b/include/rodeo.h @@ -1,19 +1,19 @@ + +// public internal +#include "rodeo_types.h" + +// system #include <stdbool.h> #include <stdio.h> #include <string.h> #include <limits.h> -#include "SDL2/SDL.h" -#include "SDL2/SDL_syswm.h" -#include "bgfx/c99/bgfx.h" - -#include "rodeo_types.h" void Rodeo__\ init_window( - Rodeo__data_t* state, + Rodeo__data_p *state, int screen_height, int screen_width, char* title @@ -21,7 +21,7 @@ init_window( void Rodeo__\ -deinit_window(Rodeo__data_t* state); +deinit_window(Rodeo__data_p state); void Rodeo__\ @@ -29,15 +29,19 @@ quit(); void Rodeo__\ -begin(Rodeo__data_t* state); +begin(Rodeo__data_p state); void Rodeo__\ -end(Rodeo__data_t* state); +end(Rodeo__data_p state); + +bool +Rodeo__\ +should_quit(Rodeo__data_p state); void Rodeo__\ -draw_debug_text(u_int16_t x, u_int16_t y, const char *format, ...); +draw_debug_text(uint16_t x, uint16_t y, const char *format, ...); const char * Rodeo__\ @@ -45,19 +49,16 @@ get_renderer_name_as_string(); void Rodeo__\ -flush_batch(Rodeo__data_t *state); +flush_batch(Rodeo__data_p state); void Rodeo__\ draw_rectangle( - Rodeo__data_t *state, - u_int16_t x, - u_int16_t y, - u_int16_t width, - u_int16_t height, - struct Rodeo__color_rgba_t color + Rodeo__data_p state, + uint16_t x, + uint16_t y, + uint16_t width, + uint16_t height, + Rodeo__color_rgba_t color ); -bgfx_shader_handle_t -Rodeo__\ -load_shader(const char* path); diff --git a/include/rodeo_config.h b/include/rodeo_config.h new file mode 100644 index 0000000..55bf057 --- /dev/null +++ b/include/rodeo_config.h @@ -0,0 +1,2 @@ + +#define RODEO__MAX_VERTEX_SIZE 8192 diff --git a/include/rodeo_math.h b/include/rodeo_math.h index 13db9b3..f7df53f 100644 --- a/include/rodeo_math.h +++ b/include/rodeo_math.h @@ -1,7 +1,11 @@ -#include "stdint.h" + +// public internal #include "rodeo_types.h" +// system +#include "stdint.h" + uint32_t Rodeo__\ Math__\ -color_rgba_to_uint32(const struct Rodeo__color_rgba_t color); +color_rgba_to_uint32(const Rodeo__color_rgba_t color); diff --git a/include/rodeo_types.h b/include/rodeo_types.h index 08748e5..fd4ed82 100644 --- a/include/rodeo_types.h +++ b/include/rodeo_types.h @@ -1,57 +1,36 @@ #pragma once + +// system #include <stdbool.h> #include <stdint.h> -#include "SDL2/SDL.h" -#include "SDL2/SDL_syswm.h" -#include "bgfx/c99/bgfx.h" - -#define RODEO__MAX_VERTEX_SIZE 8192 - typedef struct Rodeo__\ -color_rgba_t +color_rgba { float red; float green; float blue; float alpha; -} Rodeo__color_t; +} +Rodeo__\ +color_rgba_t; typedef struct -Rodeo__\ -position_color_vertex_t +Rodeo__position_color_vertex { float x; float y; float z; uint32_t abgr; -} Rodeo__position_color_vertex_t; +} +Rodeo__\ +position_color_vertex_t; typedef struct -Rodeo__\ -data_t -{ - SDL_Window* window; - SDL_Surface* screen_surface; - SDL_SysWMinfo wmi; - int screen_width; - int screen_height; - SDL_Event sdl_event; - bool quit; - - bgfx_vertex_layout_t vertex_layout; - bgfx_dynamic_vertex_buffer_handle_t vertex_buffer_handle; - bgfx_dynamic_index_buffer_handle_t index_buffer_handle; - uint16_t vertex_size; - Rodeo__position_color_vertex_t batched_vertices[RODEO__MAX_VERTEX_SIZE]; - uint16_t index_count; - uint16_t index_size; - uint16_t batched_indices[(RODEO__MAX_VERTEX_SIZE / 4) * 6]; - bgfx_shader_handle_t vertex_shader; - bgfx_shader_handle_t fragment_shader; - bgfx_program_handle_t program_shader; -} Rodeo__data_t; +Rodeo__data +*Rodeo__\ +data_p; diff --git a/src/private/rodeo_error.h b/src/private/rodeo_error.h new file mode 100644 index 0000000..d1b9eb5 --- /dev/null +++ b/src/private/rodeo_error.h @@ -0,0 +1,20 @@ + +#define RODEO__EMPTY_ERROR_MESSAGE NULL + +typedef +enum +{ + RODEO__ERROR__UNREACHABLE_REACHED, + RODEO__ERROR__UNINITIALIZED_STATE +} +Rodeo__\ +error; + +void +Rodeo__\ +error_exit( + Rodeo__error error, + const char *function, + int line_number, + const char *extra_info +); diff --git a/src/private/rodeo_internal.h b/src/private/rodeo_internal.h new file mode 100644 index 0000000..f01ac87 --- /dev/null +++ b/src/private/rodeo_internal.h @@ -0,0 +1,7 @@ + +// private internal +#include "private/rodeo_internal_types.h" + +bgfx_shader_handle_t +_Rodeo__\ +load_shader(const char* path); diff --git a/src/private/rodeo_internal_types.h b/src/private/rodeo_internal_types.h new file mode 100644 index 0000000..1dfa485 --- /dev/null +++ b/src/private/rodeo_internal_types.h @@ -0,0 +1,35 @@ +#pragma once + +// public internal +#include "rodeo_config.h" +#include "rodeo_types.h" + +// system +#include "SDL2/SDL.h" +#include "SDL2/SDL_syswm.h" +#include "bgfx/c99/bgfx.h" + +struct +Rodeo__\ +data +{ + SDL_Window* window; + SDL_Surface* screen_surface; + SDL_SysWMinfo wmi; + int screen_width; + int screen_height; + SDL_Event sdl_event; + bool quit; + + bgfx_vertex_layout_t vertex_layout; + bgfx_dynamic_vertex_buffer_handle_t vertex_buffer_handle; + bgfx_dynamic_index_buffer_handle_t index_buffer_handle; + uint16_t vertex_size; + Rodeo__position_color_vertex_t batched_vertices[RODEO__MAX_VERTEX_SIZE]; + uint16_t index_count; + uint16_t index_size; + uint16_t batched_indices[(RODEO__MAX_VERTEX_SIZE / 4) * 6]; + bgfx_shader_handle_t vertex_shader; + bgfx_shader_handle_t fragment_shader; + bgfx_program_handle_t program_shader; +} Rodeo__data_t; diff --git a/src/rodeo.c b/src/rodeo.c index 3f946ea..e50debb 100644 --- a/src/rodeo.c +++ b/src/rodeo.c @@ -1,23 +1,42 @@ + +// public internal +#include "rodeo.h" +#include "rodeo_math.h" +#include "rodeo_types.h" +// private internal +#include "private/rodeo_internal.h" +#include "private/rodeo_internal_types.h" +#include "private/rodeo_error.h" + +// external #include "SDL2/SDL.h" #include "SDL2/SDL_syswm.h" #include "bgfx/c99/bgfx.h" //#define CGLM_FORCE_LEFT_HANDED -//#define CGLM_FORCE_DEPTH_ZERO_TO_ONE -#define CGLM_CLIPSPACE_INCLUDE_ALL +#define CGLM_FORCE_DEPTH_ZERO_TO_ONE +//#define CGLM_CLIPSPACE_INCLUDE_ALL #include "cglm/cglm.h" -#include "rodeo.h" -#include "rodeo_math.h" - void Rodeo__\ init_window( - Rodeo__data_t* state, + Rodeo__data_p *state_p, int screen_height, int screen_width, char* title ) { + *state_p = (Rodeo__data_p)malloc(sizeof(Rodeo__data_t)); + Rodeo__data_p state = *state_p; + if(!state) + { + Rodeo__error_exit( + RODEO__ERROR__UNINITIALIZED_STATE, + __FUNCTION__, + __LINE__, + "Malloc failed to initialize state." + ); + } state->window = NULL; state->screen_surface = NULL; state->screen_height = screen_height; @@ -97,8 +116,8 @@ init_window( state->index_buffer_handle = bgfx_create_dynamic_index_buffer((RODEO__MAX_VERTEX_SIZE / 4) * 6, BGFX_BUFFER_NONE); // load shaders - state->vertex_shader = Rodeo__load_shader("./external/RodeoEngine/build_dir/simple.vertex.bin"); - state->fragment_shader = Rodeo__load_shader("./external/RodeoEngine/build_dir/simple.fragment.bin"); + state->vertex_shader = _Rodeo__load_shader("./external/RodeoEngine/build_dir/simple.vertex.bin"); + state->fragment_shader = _Rodeo__load_shader("./external/RodeoEngine/build_dir/simple.fragment.bin"); state->program_shader = bgfx_create_program( state->vertex_shader, state->fragment_shader, @@ -108,8 +127,17 @@ init_window( void Rodeo__\ -deinit_window(Rodeo__data_t* state) +deinit_window(Rodeo__data_p state) { + if(!state) + { + Rodeo__error_exit( + RODEO__ERROR__UNINITIALIZED_STATE, + __FUNCTION__, + __LINE__, + RODEO__EMPTY_ERROR_MESSAGE + ); + } bgfx_destroy_dynamic_index_buffer(state->index_buffer_handle); bgfx_destroy_dynamic_vertex_buffer(state->vertex_buffer_handle); bgfx_destroy_program(state->program_shader); @@ -126,8 +154,17 @@ quit() void Rodeo__\ -begin(Rodeo__data_t* state) +begin(Rodeo__data_p state) { + if(!state) + { + Rodeo__error_exit( + RODEO__ERROR__UNINITIALIZED_STATE, + __FUNCTION__, + __LINE__, + RODEO__EMPTY_ERROR_MESSAGE + ); + } //vec3 eye = {0.0f, 0.0f, -35.0f}; //vec3 center = {0.0f, 0.0f, 0.0f}; //vec3 up = {0, 1, 0}; @@ -158,8 +195,17 @@ begin(Rodeo__data_t* state) void Rodeo__\ -end(Rodeo__data_t* state) +end(Rodeo__data_p state) { + if(!state) + { + Rodeo__error_exit( + RODEO__ERROR__UNINITIALIZED_STATE, + __FUNCTION__, + __LINE__, + RODEO__EMPTY_ERROR_MESSAGE + ); + } Rodeo__flush_batch(state); bgfx_frame(false); @@ -173,6 +219,13 @@ end(Rodeo__data_t* state) } } +bool +Rodeo__\ +should_quit(Rodeo__data_p state) +{ + return state->quit; +} + void Rodeo__\ draw_debug_text(u_int16_t x, u_int16_t y, const char *format, ...) @@ -192,7 +245,7 @@ get_renderer_name_as_string() void Rodeo__\ -flush_batch(Rodeo__data_t *state) +flush_batch(Rodeo__data_p state) { if(state->vertex_size > 0) { @@ -229,12 +282,12 @@ flush_batch(Rodeo__data_t *state) void Rodeo__\ draw_rectangle( - Rodeo__data_t *state, + Rodeo__data_p state, u_int16_t x, u_int16_t y, u_int16_t width, u_int16_t height, - struct Rodeo__color_rgba_t color + Rodeo__color_rgba_t color ) { const uint32_t abgr = Rodeo__Math__color_rgba_to_uint32(color); @@ -294,7 +347,7 @@ draw_rectangle( } bgfx_shader_handle_t -Rodeo__\ +_Rodeo__\ load_shader(const char* path) { bgfx_shader_handle_t invalid = BGFX_INVALID_HANDLE; diff --git a/src/rodeo_error.c b/src/rodeo_error.c new file mode 100644 index 0000000..1b5b03d --- /dev/null +++ b/src/rodeo_error.c @@ -0,0 +1,48 @@ + +// private internal +#include "private/rodeo_error.h" + +// system +#include <stdlib.h> +#include <stdio.h> + +void +Rodeo__\ +error_exit( + Rodeo__error error, + const char *function, + int line_number, + const char *extra_info +) +{ + switch(error) + { + case RODEO__ERROR__UNREACHABLE_REACHED: + printf("[ERROR] RODEO: \n\t(func: %s, line: %d)\n", function, line_number); + printf("\tUnreachable code reached.\n"); + if(extra_info) + { + printf("\t%s\n", extra_info); + } + fflush(stdout); + break; + case RODEO__ERROR__UNINITIALIZED_STATE: + printf("RODEO ERROR: \n\t(func: %s, line: %d)\n", function, line_number); + printf("\tUninitialized State.\n"); + if(extra_info) + { + printf("\t%s\n", extra_info); + } + fflush(stdout); + break; + default: + Rodeo__error_exit( + RODEO__ERROR__UNREACHABLE_REACHED, + __FUNCTION__, + __LINE__, + "Unhandled error code." + ); + } + exit(EXIT_FAILURE); +} + diff --git a/src/rodeo_math.c b/src/rodeo_math.c index 99f61af..248e600 100644 --- a/src/rodeo_math.c +++ b/src/rodeo_math.c @@ -1,10 +1,12 @@ + +// public internal #include "rodeo.h" #include "rodeo_math.h" uint32_t Rodeo__\ Math__\ -color_rgba_to_uint32(const struct Rodeo__color_rgba_t color) +color_rgba_to_uint32(const Rodeo__color_rgba_t color) { return ((uint32_t)(uint8_t)(color.red * 255)) diff --git a/src/rodeo_types.c b/src/rodeo_types.c new file mode 100644 index 0000000..d2c56aa --- /dev/null +++ b/src/rodeo_types.c @@ -0,0 +1,34 @@ + +// public internal +#include "rodeo_types.h" +#include "rodeo_config.h" + +// external +#include "SDL2/SDL.h" +#include "SDL2/SDL_syswm.h" +#include "bgfx/c99/bgfx.h" + +struct +Rodeo__\ +data_t +{ + SDL_Window* window; + SDL_Surface* screen_surface; + SDL_SysWMinfo wmi; + int screen_width; + int screen_height; + SDL_Event sdl_event; + bool quit; + + bgfx_vertex_layout_t vertex_layout; + bgfx_dynamic_vertex_buffer_handle_t vertex_buffer_handle; + bgfx_dynamic_index_buffer_handle_t index_buffer_handle; + uint16_t vertex_size; + Rodeo__position_color_vertex_t batched_vertices[RODEO__MAX_VERTEX_SIZE]; + uint16_t index_count; + uint16_t index_size; + uint16_t batched_indices[(RODEO__MAX_VERTEX_SIZE / 4) * 6]; + bgfx_shader_handle_t vertex_shader; + bgfx_shader_handle_t fragment_shader; + bgfx_program_handle_t program_shader; +}; |
