diff options
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | CMakeLists.txt | 4 | ||||
| -rwxr-xr-x | build_shaders | 5 | ||||
| m--------- | external/STC | 0 | ||||
| -rw-r--r-- | include/rodeo.h | 106 | ||||
| -rw-r--r-- | include/rodeo_config.h | 4 | ||||
| -rw-r--r-- | include/rodeo_math.h | 11 | ||||
| -rw-r--r-- | include/rodeo_types.h | 33 | ||||
| -rw-r--r-- | src/compile_flags.txt | 1 | ||||
| -rw-r--r-- | src/private/rodeo_error.h | 20 | ||||
| -rw-r--r-- | src/private/rodeo_internal.h | 7 | ||||
| -rw-r--r-- | src/rodeo.c | 315 | ||||
| -rw-r--r-- | src/rodeo_error.c | 48 | ||||
| -rw-r--r-- | src/rodeo_internal.h | 7 | ||||
| -rw-r--r-- | src/rodeo_internal_types.h (renamed from src/private/rodeo_internal_types.h) | 10 | ||||
| -rw-r--r-- | src/rodeo_math.c | 8 | ||||
| -rw-r--r-- | src/rodeo_string.c | 91 | ||||
| -rw-r--r-- | src/rodeo_types.c | 37 |
18 files changed, 325 insertions, 385 deletions
diff --git a/.gitmodules b/.gitmodules index 78c2c7e..aeadb63 100644 --- a/.gitmodules +++ b/.gitmodules @@ -13,3 +13,6 @@ [submodule "external/cglm"] path = external/cglm url = https://github.com/recp/cglm +[submodule "external/STC"] + path = external/STC + url = https://github.com/realtradam/STC-modified diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c9fdec..b83057d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ file(GLOB SOURCES "src/rodeo.c" "src/rodeo_math.c" "src/rodeo_types.c" - "src/rodeo_error.c" + "src/rodeo_string.c" ) #add_library(lib src/lib.c) @@ -94,6 +94,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Emscripten") PUBLIC external/bgfx/include PUBLIC external/bx/include PUBLIC external/cglm/include + PUBLIC external/STC/include PUBLIC include PRIVATE src ) @@ -107,6 +108,7 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") PUBLIC external/bgfx/include PUBLIC external/bx/include PUBLIC external/cglm/include + PUBLIC external/STC/include PUBLIC include PRIVATE src ) diff --git a/build_shaders b/build_shaders deleted file mode 100755 index 5b00403..0000000 --- a/build_shaders +++ /dev/null @@ -1,5 +0,0 @@ -#! /bin/sh -./external/bgfx/.build/linux64_gcc/bin/shadercRelease -f ./src/shaders/simple.vertex.sc -o ./build_dir/simple.vertex.bin --platform linux --type vertex --verbose -i ./external/bgfx/src/ -p spirv -./external/bgfx/.build/linux64_gcc/bin/shadercRelease -f ./src/shaders/simple.fragment.sc -o ./build_dir/simple.fragment.bin --platform linux --type fragment --verbose -i ./external/bgfx/src/ -p spirv -#./external/bgfx/.build/linux64_gcc/bin/shadercRelease -f ./src/shaders/simple.vertex.sc -o ./build_dir/simple.vertex.bin --platform asm.js --type vertex --verbose -i ./external/bgfx/src/ -p 100_es -#./external/bgfx/.build/linux64_gcc/bin/shadercRelease -f ./src/shaders/simple.fragment.sc -o ./build_dir/simple.fragment.bin --platform asm.js --type fragment --verbose -i ./external/bgfx/src/ -p 100_es diff --git a/external/STC b/external/STC new file mode 160000 +Subproject bdbfc5dcbddc52c8bea5dc1a99464b2de724157 diff --git a/include/rodeo.h b/include/rodeo.h index 713e260..bd156cb 100644 --- a/include/rodeo.h +++ b/include/rodeo.h @@ -5,6 +5,7 @@ // system #include <stdbool.h> #include <stdio.h> +#include <stdint.h> #include <string.h> #include <limits.h> @@ -16,95 +17,114 @@ !mrodeo_macrovar(_macrovar_); \ (mrodeo_macrovar(_macrovar_) += 1), end) \ +/// --- Math --- + +uint32_t +rodeo_rgba_to_uint32(const rodeo_rgba_t color); + +/// --- Core --- + #define \ mrodeo_window_do( \ - state, \ screen_height, \ screen_width, \ title \ ) \ mrodeo_defer_do( \ rodeo_window_init( \ - &state, \ screen_height, \ screen_width, \ title \ ), \ - rodeo_window_deinit(state) \ + rodeo_window_deinit() \ ) void -rodeo_\ -window_init( - rodeo_data_p *state, +rodeo_window_init( int screen_height, int screen_width, char* title ); void -rodeo_\ -window_deinit(rodeo_data_p state); - -void -rodeo_\ -deinit(void); +rodeo_window_deinit(void); #define \ -mrodeo_do( \ +mrodeo_frame_do( \ state \ ) \ mrodeo_defer_do( \ - rodeo_begin(state), \ - rodeo_end(state) \ + rodeo_frame_begin(state), \ + rodeo_frame_end(state) \ ) void -rodeo_\ -begin(rodeo_data_p state); +rodeo_frame_begin(void); void -rodeo_\ -end(rodeo_data_p state); +rodeo_frame_end(void); void -rodeo_\ -mainloop_set( - rodeo_data_p state, +rodeo_mainloop_run( rodeo_mainloop_func main_loop_function ); bool -rodeo_\ -window_check_quit(rodeo_data_p state); +rodeo_window_check_quit(void); void -rodeo_\ -set_quit(rodeo_data_p state, bool quit); +rodeo_set_quit(bool quit); void -rodeo_\ -debug_text_draw(uint16_t x, uint16_t y, const char *format, ...); +rodeo_debug_text_draw(uint16_t x, uint16_t y, const char *format, ...); -// rodeo_renderer_name_get -const char * -rodeo_\ -renderer_name_get(void); +rodeo_string_p +rodeo_renderer_name_get(void); -// rodeo_renderer_flush void -rodeo_\ -renderer_flush(rodeo_data_p state); +rodeo_renderer_flush(void); -// rodeo_rectangle_draw void -rodeo_\ -rectangle_draw( - rodeo_data_p state, - uint16_t x, - uint16_t y, - uint16_t width, - uint16_t height, +rodeo_rectangle_draw( + rodeo_rectangle_t rectangle, rodeo_rgba_t color ); +/// --- String --- + +rodeo_string_p +rodeo_string_create(const char *c_string); + +void +rodeo_string_destroy(rodeo_string_p self); + +char* +rodeo_string_to_cstr(rodeo_string_p self); + +const char* +rodeo_string_to_constcstr(rodeo_string_p self); + +void +rodeo_string_insert( + rodeo_string_p self, + const rodeo_string_p insert, + intptr_t position +); + +void +rodeo_string_append( + rodeo_string_p self, + const rodeo_string_p append +); + +void +rodeo_string_prepend( + rodeo_string_p self, + const rodeo_string_p prepend +); + +void +rodeo_string_clear(rodeo_string_p self); + +void +rodeo_string_set(rodeo_string_p self, char* value); diff --git a/include/rodeo_config.h b/include/rodeo_config.h index 55bf057..247a0b2 100644 --- a/include/rodeo_config.h +++ b/include/rodeo_config.h @@ -1,2 +1,4 @@ -#define RODEO__MAX_VERTEX_SIZE 8192 +#ifndef mrodeo_vertex_size_max + #define mrodeo_vertex_size_max 8192 +#endif diff --git a/include/rodeo_math.h b/include/rodeo_math.h deleted file mode 100644 index 4dee9ae..0000000 --- a/include/rodeo_math.h +++ /dev/null @@ -1,11 +0,0 @@ - -// public internal -#include "rodeo_types.h" - -// system -#include "stdint.h" - -uint32_t -Rodeo__\ -Math__\ -color_rgba_to_uint32(const rodeo_rgba_t color); diff --git a/include/rodeo_types.h b/include/rodeo_types.h index e35c482..bdd7ab7 100644 --- a/include/rodeo_types.h +++ b/include/rodeo_types.h @@ -4,36 +4,35 @@ #include <stdbool.h> #include <stdint.h> -typedef -struct +typedef struct { float red; float green; float blue; float alpha; } -rodeo_\ -rgba_t; +rodeo_rgba_t; -typedef -struct +typedef struct { float x; float y; float z; uint32_t abgr; } -rodeo_\ -position_color_vertex_t; -//rodeo_poscolvert_t - -typedef -struct -rodeo_data_t -*rodeo_data_p; +rodeo_vertex_t; typedef void -(*rodeo_\ -mainloop_func) -(void); +(*rodeo_mainloop_func)(void); + +typedef struct +{ + float x; + float y; + float width; + float height; +} +rodeo_rectangle_t; + +typedef union rodeo_string_t *rodeo_string_p; diff --git a/src/compile_flags.txt b/src/compile_flags.txt index 2cccc56..e59f678 100644 --- a/src/compile_flags.txt +++ b/src/compile_flags.txt @@ -4,6 +4,7 @@ -I../external/bgfx/include -I../external/bx/include -I../external/cglm/include +-I../external/STC/include -Wall -Wextra -Wpedantic diff --git a/src/private/rodeo_error.h b/src/private/rodeo_error.h deleted file mode 100644 index d1b9eb5..0000000 --- a/src/private/rodeo_error.h +++ /dev/null @@ -1,20 +0,0 @@ - -#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 deleted file mode 100644 index 0773f12..0000000 --- a/src/private/rodeo_internal.h +++ /dev/null @@ -1,7 +0,0 @@ - -// private internal -#include "private/rodeo_internal_types.h" - -bgfx_shader_handle_t -irodeo_\ -shader_load(const char* path); diff --git a/src/rodeo.c b/src/rodeo.c index 10654ac..b513bdf 100644 --- a/src/rodeo.c +++ b/src/rodeo.c @@ -1,16 +1,14 @@ // -- internal -- // 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" +#include "rodeo_internal.h" +#include "rodeo_internal_types.h" // -- external -- #if __EMSCRIPTEN__ -#include <emscripten/emscripten.h> + #include <emscripten/emscripten.h> #endif #include "SDL2/SDL.h" #include "SDL2/SDL_syswm.h" @@ -20,30 +18,19 @@ //#define CGLM_CLIPSPACE_INCLUDE_ALL #include "cglm/cglm.h" +static irodeo_state_t state = {0}; + void -rodeo_\ -window_init( - rodeo_data_p *state_p, +rodeo_window_init( 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, - __func__, - __LINE__, - "Malloc failed to initialize state." - ); - } - state->window = NULL; - state->screen_surface = NULL; - state->screen_height = screen_height; - state->screen_width = screen_width; + state.window = NULL; + state.screen_surface = NULL; + state.screen_height = screen_height; + state.screen_width = screen_width; printf("SDL_Init...\n"); if(SDL_Init(SDL_INIT_VIDEO) < 0) @@ -54,7 +41,7 @@ window_init( printf("done\n"); printf("SDL_CreateWindow...\n"); - state->window = SDL_CreateWindow( + state.window = SDL_CreateWindow( title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, @@ -64,18 +51,18 @@ window_init( ); printf("done\n"); - if(state->window == NULL) + if(state.window == NULL) { printf("Window could not be created! SDL_Error %s\n", SDL_GetError()); exit(EXIT_FAILURE); } #if !__EMSCRIPTEN__ printf("SDL_VERSION...\n"); - SDL_VERSION(&state->wmi.version); + SDL_VERSION(&state.wmi.version); if( !SDL_GetWindowWMInfo( - state->window, - &state->wmi + state.window, + &state.wmi ) ) { @@ -92,8 +79,8 @@ window_init( #if !__EMSCRIPTEN__ // x11 - pd.ndt = state->wmi.info.x11.display; - pd.nwh = (void*)(uintptr_t)state->wmi.info.x11.window; + pd.ndt = state.wmi.info.x11.display; + pd.nwh = (void*)(uintptr_t)state.wmi.info.x11.window; #else // web pd.nwh = (void*)"#canvas"; @@ -103,8 +90,8 @@ window_init( bgfx_init_ctor(&init); init.type = BGFX_RENDERER_TYPE_COUNT; // auto determine renderer //init.type = BGFX_RENDERER_TYPE_OPENGL; // force opengl renderer - init.resolution.width = state->screen_width; - init.resolution.height = state->screen_height; + init.resolution.width = state.screen_width; + init.resolution.height = state.screen_height; init.resolution.reset = BGFX_RESET_VSYNC; init.platformData = pd; bgfx_init(&init); @@ -120,96 +107,74 @@ window_init( 1.0f, 0 ); - bgfx_set_view_rect(0, 0, 0, state->screen_width, state->screen_height); - bgfx_vertex_layout_begin(&state->vertex_layout, bgfx_get_renderer_type()); - bgfx_vertex_layout_add(&state->vertex_layout, BGFX_ATTRIB_POSITION, 3, BGFX_ATTRIB_TYPE_FLOAT, false, false); - bgfx_vertex_layout_add(&state->vertex_layout, BGFX_ATTRIB_COLOR0, 4, BGFX_ATTRIB_TYPE_UINT8, true, false); - bgfx_vertex_layout_end(&state->vertex_layout); + bgfx_set_view_rect(0, 0, 0, state.screen_width, state.screen_height); + bgfx_vertex_layout_begin(&state.vertex_layout, bgfx_get_renderer_type()); + bgfx_vertex_layout_add(&state.vertex_layout, BGFX_ATTRIB_POSITION, 3, BGFX_ATTRIB_TYPE_FLOAT, false, false); + bgfx_vertex_layout_add(&state.vertex_layout, BGFX_ATTRIB_COLOR0, 4, BGFX_ATTRIB_TYPE_UINT8, true, false); + bgfx_vertex_layout_end(&state.vertex_layout); - state->vertex_buffer_handle = bgfx_create_dynamic_vertex_buffer(RODEO__MAX_VERTEX_SIZE, &state->vertex_layout, BGFX_BUFFER_NONE); + state.vertex_buffer_handle = bgfx_create_dynamic_vertex_buffer(mrodeo_vertex_size_max, &state.vertex_layout, BGFX_BUFFER_NONE); - state->index_buffer_handle = bgfx_create_dynamic_index_buffer((RODEO__MAX_VERTEX_SIZE / 4) * 6, BGFX_BUFFER_NONE); + state.index_buffer_handle = bgfx_create_dynamic_index_buffer((mrodeo_vertex_size_max / 4) * 6, BGFX_BUFFER_NONE); // load shaders - const char* shader_path = "???"; + //const char* shader_path = "???"; + rodeo_string_p shader_path = rodeo_string_create("???"); switch(bgfx_get_renderer_type()) { case BGFX_RENDERER_TYPE_NOOP: printf("Noop renderer error"); exit(EXIT_FAILURE); case BGFX_RENDERER_TYPE_OPENGLES: - shader_path = "shaders/100_es/"; + rodeo_string_set( + shader_path, + "shaders/100_es/" + ); break; case BGFX_RENDERER_TYPE_VULKAN: - shader_path = "shaders/spirv/"; + rodeo_string_set( + shader_path, + "shaders/spirv/" + ); break; default: printf("No shaders for selected renderer. Exiting..."); exit(EXIT_FAILURE); } - const char* vertex_shader_filename = "simple.vertex.bin"; - const char* fragment_shader_filename = "simple.fragment.bin"; - size_t shader_length = strlen(shader_path); - size_t fragment_length = strlen(fragment_shader_filename); - size_t vertex_length = strlen(vertex_shader_filename); - char *fragment_path = (char *)malloc(shader_length + fragment_length); - char *vertex_path = (char *)malloc(shader_length + vertex_length); - memcpy(fragment_path, shader_path, shader_length); - memcpy(&fragment_path[shader_length], fragment_shader_filename, fragment_length); - memcpy(vertex_path, shader_path, shader_length); - memcpy(&vertex_path[shader_length], vertex_shader_filename, vertex_length); - - fragment_path[shader_length + fragment_length] = 0; - vertex_path[shader_length + vertex_length] = 0; - - state->vertex_shader = irodeo_shader_load(vertex_path); - state->fragment_shader = irodeo_shader_load(fragment_path); - state->program_shader = bgfx_create_program( - state->vertex_shader, - state->fragment_shader, + rodeo_string_p vertex_shader_path = rodeo_string_create("simple.vertex.bin"); + rodeo_string_p fragment_shader_path = rodeo_string_create("simple.fragment.bin"); + + rodeo_string_prepend( + vertex_shader_path, + shader_path + ); + rodeo_string_prepend( + fragment_shader_path, + shader_path + ); + + state.vertex_shader = irodeo_shader_load(vertex_shader_path); + state.fragment_shader = irodeo_shader_load(fragment_shader_path); + state.program_shader = bgfx_create_program( + state.vertex_shader, + state.fragment_shader, true ); } void -rodeo_\ -window_deinit(rodeo_data_p state) +rodeo_window_deinit(void) { - if(!state) - { - Rodeo__error_exit( - RODEO__ERROR__UNINITIALIZED_STATE, - __func__, - __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); + bgfx_destroy_dynamic_index_buffer(state.index_buffer_handle); + bgfx_destroy_dynamic_vertex_buffer(state.vertex_buffer_handle); + bgfx_destroy_program(state.program_shader); bgfx_shutdown(); - SDL_DestroyWindow(state->window); -} - -void -rodeo_\ -deinit(void) -{ + SDL_DestroyWindow(state.window); SDL_Quit(); } void -rodeo_\ -begin(rodeo_data_p state) +rodeo_frame_begin(void) { - if(!state) - { - Rodeo__error_exit( - RODEO__ERROR__UNINITIALIZED_STATE, - __func__, - __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}; @@ -224,8 +189,8 @@ begin(rodeo_data_p state) // but 'no' is incorrect glm_ortho_rh_zo( 0, - state->screen_width, - state->screen_height, + state.screen_width, + state.screen_height, 0, // near -0.1f, @@ -234,47 +199,35 @@ begin(rodeo_data_p state) proj ); bgfx_set_view_transform(0, view, proj); - bgfx_set_view_rect(0, 0, 0, state->screen_width, state->screen_height); + bgfx_set_view_rect(0, 0, 0, state.screen_width, state.screen_height); bgfx_touch(0); } void -rodeo_\ -end(rodeo_data_p state) +rodeo_frame_end(void) { - if(!state) - { - Rodeo__error_exit( - RODEO__ERROR__UNINITIALIZED_STATE, - __func__, - __LINE__, - RODEO__EMPTY_ERROR_MESSAGE - ); - } - rodeo_renderer_flush(state); + rodeo_renderer_flush(); bgfx_frame(false); - while(SDL_PollEvent(&state->sdl_event)) + while(SDL_PollEvent(&state.sdl_event)) { - if(state->sdl_event.type == SDL_QUIT) + if(state.sdl_event.type == SDL_QUIT) { - state->quit = true; + state.quit = true; } } } void -rodeo_\ -mainloop_set( - rodeo_data_p state, +rodeo_mainloop_run( rodeo_mainloop_func main_loop_function ) { #if __EMSCRIPTEN__ emscripten_set_main_loop(main_loop_function, 0, 1); #else - while(!rodeo_window_check_quit(state)) + while(!rodeo_window_check_quit()) { main_loop_function(); } @@ -282,22 +235,19 @@ mainloop_set( } bool -rodeo_\ -window_check_quit(rodeo_data_p state) +rodeo_window_check_quit(void) { - return state->quit; + return state.quit; } void -rodeo_\ -set_quit(rodeo_data_p state, bool quit) +rodeo_set_quit(bool quit) { - state->quit = quit; + state.quit = quit; } void -rodeo_\ -debug_text_draw(u_int16_t x, u_int16_t y, const char *format, ...) +rodeo_debug_text_draw(u_int16_t x, u_int16_t y, const char *format, ...) { va_list argList; va_start(argList, format); @@ -305,28 +255,26 @@ debug_text_draw(u_int16_t x, u_int16_t y, const char *format, ...) va_end(argList); } -const char * -rodeo_\ -renderer_name_get(void) +rodeo_string_p +rodeo_renderer_name_get(void) { - return bgfx_get_renderer_name(bgfx_get_renderer_type()); + return rodeo_string_create(bgfx_get_renderer_name(bgfx_get_renderer_type())); } void -rodeo_\ -renderer_flush(rodeo_data_p state) +rodeo_renderer_flush(void) { - if(state->vertex_size > 0) + if(state.vertex_size > 0) { // upload remaining batched vertices - bgfx_set_dynamic_vertex_buffer(0, state->vertex_buffer_handle, 0, state->vertex_size); - const bgfx_memory_t* vbm = bgfx_copy(state->batched_vertices, sizeof(rodeo_position_color_vertex_t) * state->vertex_size); - bgfx_update_dynamic_vertex_buffer(state->vertex_buffer_handle, 0, vbm); + bgfx_set_dynamic_vertex_buffer(0, state.vertex_buffer_handle, 0, state.vertex_size); + const bgfx_memory_t* vbm = bgfx_copy(state.batched_vertices, sizeof(rodeo_vertex_t) * state.vertex_size); + bgfx_update_dynamic_vertex_buffer(state.vertex_buffer_handle, 0, vbm); // upload remaining batched indices - bgfx_set_dynamic_index_buffer(state->index_buffer_handle, 0, state->index_size); - const bgfx_memory_t* ibm = bgfx_copy(state->batched_indices, sizeof(uint16_t) * state->index_size); - bgfx_update_dynamic_index_buffer(state->index_buffer_handle, 0, ibm); + bgfx_set_dynamic_index_buffer(state.index_buffer_handle, 0, state.index_size); + const bgfx_memory_t* ibm = bgfx_copy(state.batched_indices, sizeof(uint16_t) * state.index_size); + bgfx_update_dynamic_index_buffer(state.index_buffer_handle, 0, ibm); bgfx_set_state( BGFX_STATE_CULL_CW | @@ -339,53 +287,48 @@ renderer_flush(rodeo_data_p state) ); // submit vertices & batches - bgfx_submit(0, state->program_shader, 0, BGFX_DISCARD_ALL); + bgfx_submit(0, state.program_shader, 0, BGFX_DISCARD_ALL); // reset arrays - state->vertex_size = 0; - state->index_size = 0; - state->index_count = 0; + state.vertex_size = 0; + state.index_size = 0; + state.index_count = 0; } } void -rodeo_\ -rectangle_draw( - rodeo_data_p state, - u_int16_t x, - u_int16_t y, - u_int16_t width, - u_int16_t height, +rodeo_rectangle_draw( + rodeo_rectangle_t rectangle, rodeo_rgba_t color ) { - const uint32_t abgr = Rodeo__Math__color_rgba_to_uint32(color); - if(state->vertex_size < RODEO__MAX_VERTEX_SIZE) + const uint32_t abgr = rodeo_rgba_to_uint32(color); + if(state.vertex_size < mrodeo_vertex_size_max) { - state->batched_vertices[state->vertex_size] = - (rodeo_position_color_vertex_t) + state.batched_vertices[state.vertex_size] = + (rodeo_vertex_t) { - (float)width + (float)x, (float)height + (float)y, 0.0f, abgr + rectangle.width + rectangle.x, rectangle.height + rectangle.y, 0.0f, abgr }; - state->vertex_size += 1; - state->batched_vertices[state->vertex_size] = - (rodeo_position_color_vertex_t) + state.vertex_size += 1; + state.batched_vertices[state.vertex_size] = + (rodeo_vertex_t) { - (float)width + (float)x, (float)y, 0.0f, abgr + rectangle.width + rectangle.x, rectangle.y, 0.0f, abgr }; - state->vertex_size += 1; - state->batched_vertices[state->vertex_size] = - (rodeo_position_color_vertex_t) + state.vertex_size += 1; + state.batched_vertices[state.vertex_size] = + (rodeo_vertex_t) { - (float)x, (float)y, 0.0f, abgr + rectangle.x, rectangle.y, 0.0f, abgr }; - state->vertex_size += 1; - state->batched_vertices[state->vertex_size] = - (rodeo_position_color_vertex_t) + state.vertex_size += 1; + state.batched_vertices[state.vertex_size] = + (rodeo_vertex_t) { - (float)x, (float)height + (float)y, 0.0f, abgr + rectangle.x, rectangle.height + rectangle.y, 0.0f, abgr }; - state->vertex_size += 1; + state.vertex_size += 1; int indices[] = { @@ -394,43 +337,43 @@ rectangle_draw( //2, 1, 0, //2, 3, 1 }; - state->batched_indices[state->index_size] = state->index_count + indices[0]; - state->index_size += 1; - state->batched_indices[state->index_size] = state->index_count + indices[1]; - state->index_size += 1; - state->batched_indices[state->index_size] = state->index_count + indices[2]; - state->index_size += 1; - state->batched_indices[state->index_size] = state->index_count + indices[3]; - state->index_size += 1; - state->batched_indices[state->index_size] = state->index_count + indices[4]; - state->index_size += 1; - state->batched_indices[state->index_size] = state->index_count + indices[5]; - state->index_size += 1; - state->index_count += 4; + state.batched_indices[state.index_size] = state.index_count + indices[0]; + state.index_size += 1; + state.batched_indices[state.index_size] = state.index_count + indices[1]; + state.index_size += 1; + state.batched_indices[state.index_size] = state.index_count + indices[2]; + state.index_size += 1; + state.batched_indices[state.index_size] = state.index_count + indices[3]; + state.index_size += 1; + state.batched_indices[state.index_size] = state.index_count + indices[4]; + state.index_size += 1; + state.batched_indices[state.index_size] = state.index_count + indices[5]; + state.index_size += 1; + state.index_count += 4; } - if(state->vertex_size >= RODEO__MAX_VERTEX_SIZE) + if(state.vertex_size >= mrodeo_vertex_size_max) { - rodeo_renderer_flush(state); + rodeo_renderer_flush(); } } bgfx_shader_handle_t -irodeo_\ -shader_load(const char* path) +irodeo_shader_load(const rodeo_string_p path) { + const char* path_cstr = rodeo_string_to_constcstr(path); bgfx_shader_handle_t invalid = BGFX_INVALID_HANDLE; - FILE *file = fopen(path, "rb"); + FILE *file = fopen(path_cstr, "rb"); if(!file) { - printf("Error: shader file \"%s\" not found\n", path); + printf("Error: shader file \"%s\" not found\n", path_cstr); return invalid; } fseek(file, 0, SEEK_END); - long file_size = ftell(file); + int64_t file_size = ftell(file); fseek(file, 0, SEEK_SET); const bgfx_memory_t *mem = bgfx_alloc(file_size + 1); diff --git a/src/rodeo_error.c b/src/rodeo_error.c deleted file mode 100644 index 4190883..0000000 --- a/src/rodeo_error.c +++ /dev/null @@ -1,48 +0,0 @@ - -// 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, - __func__, - __LINE__, - "Unhandled error code." - ); - } - exit(EXIT_FAILURE); -} - diff --git a/src/rodeo_internal.h b/src/rodeo_internal.h new file mode 100644 index 0000000..56b188c --- /dev/null +++ b/src/rodeo_internal.h @@ -0,0 +1,7 @@ + +// private internal +#include "rodeo_internal_types.h" + +bgfx_shader_handle_t +irodeo_\ +shader_load(const rodeo_string_p path); diff --git a/src/private/rodeo_internal_types.h b/src/rodeo_internal_types.h index 20b8c43..9816e86 100644 --- a/src/private/rodeo_internal_types.h +++ b/src/rodeo_internal_types.h @@ -12,8 +12,7 @@ #include "SDL2/SDL_syswm.h" #include "bgfx/c99/bgfx.h" -struct -rodeo_data_t +typedef struct { SDL_Window* window; SDL_Surface* screen_surface; @@ -27,11 +26,12 @@ rodeo_data_t 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]; + rodeo_vertex_t batched_vertices[mrodeo_vertex_size_max]; uint16_t index_count; uint16_t index_size; - uint16_t batched_indices[(RODEO__MAX_VERTEX_SIZE / 4) * 6]; + uint16_t batched_indices[(mrodeo_vertex_size_max / 4) * 6]; bgfx_shader_handle_t vertex_shader; bgfx_shader_handle_t fragment_shader; bgfx_program_handle_t program_shader; -} rodeo_data_t; +} +irodeo_state_t; diff --git a/src/rodeo_math.c b/src/rodeo_math.c index f45c6e6..4985cd1 100644 --- a/src/rodeo_math.c +++ b/src/rodeo_math.c @@ -1,12 +1,12 @@ // public internal #include "rodeo.h" -#include "rodeo_math.h" + +// system +#include <stdint.h> uint32_t -Rodeo__\ -Math__\ -color_rgba_to_uint32(const rodeo_rgba_t color) +rodeo_rgba_to_uint32(const rodeo_rgba_t color) { return ((uint32_t)(uint8_t)(color.red * 255)) diff --git a/src/rodeo_string.c b/src/rodeo_string.c new file mode 100644 index 0000000..72baae7 --- /dev/null +++ b/src/rodeo_string.c @@ -0,0 +1,91 @@ + +// internal +#include "rodeo.h" +#include "rodeo_types.h" + +// external +#define i_implement +#include <stc/cstr.h> + +typedef union cstr rodeo_string_t; + +rodeo_string_p +rodeo_string_create(const char *c_string) +{ + cstr* result = calloc(1, sizeof(cstr)); + if(c_string != NULL) + { + *result = cstr_from(c_string); + } + return (rodeo_string_p)result; +} + +void +rodeo_string_destroy(rodeo_string_p self) +{ + cstr_drop((cstr*)self); + //free(string); // the above already calls free on the entire pointer +} + +char* +rodeo_string_to_cstr(rodeo_string_p self) +{ + return cstr_data((cstr*)self); +} + +const char* +rodeo_string_to_constcstr(rodeo_string_p self) +{ + return cstr_str((cstr*)self); +} + +void +rodeo_string_insert( + rodeo_string_p self, + const rodeo_string_p insert, + intptr_t position +) +{ + cstr_insert_s((cstr*)self, position, *(cstr*)insert); +} + +void +rodeo_string_append( + rodeo_string_p self, + const rodeo_string_p append +) +{ + rodeo_string_insert( + self, + append, + cstr_size((cstr*)self) - 1 + ); +} + +void +rodeo_string_prepend( + rodeo_string_p self, + const rodeo_string_p prepend +) +{ + rodeo_string_insert( + self, + prepend, + 0 + ); +} + +void +rodeo_string_clear(rodeo_string_p self) +{ + cstr_clear((cstr*)self); +} + +void +rodeo_string_set(rodeo_string_p self, char* value) +{ + cstr_clear((cstr*)self); + cstr *temp = (cstr*)self; + *temp = cstr_from(value); +} + diff --git a/src/rodeo_types.c b/src/rodeo_types.c deleted file mode 100644 index f7e5285..0000000 --- a/src/rodeo_types.c +++ /dev/null @@ -1,37 +0,0 @@ - -// public internal -#include "rodeo_types.h" -#include "rodeo_config.h" - -// external -#if __EMSCRIPTEN__ -#include <emscripten/emscripten.h> -#endif -#include "SDL2/SDL.h" -#include "SDL2/SDL_syswm.h" -#include "bgfx/c99/bgfx.h" - -struct -{ - 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; |
