diff options
| author | realtradam <[email protected]> | 2023-03-26 00:52:13 -0400 |
|---|---|---|
| committer | realtradam <[email protected]> | 2023-03-26 00:52:13 -0400 |
| commit | 4670ac42a773ea97157f71d78687f79d6ba3c1d9 (patch) | |
| tree | 3c70b7d4073d48ff3c5066c9cf7ab812031a3551 | |
| parent | 2577adf913e292a4a515e7dfc4023e37e8177f46 (diff) | |
| download | RodeoKit-4670ac42a773ea97157f71d78687f79d6ba3c1d9.tar.gz RodeoKit-4670ac42a773ea97157f71d78687f79d6ba3c1d9.zip | |
added loading images as well as loding textures
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | CMakeLists.txt | 5 | ||||
| m--------- | external/SDL_image | 0 | ||||
| -rw-r--r-- | include/rodeo.h | 49 | ||||
| -rw-r--r-- | include/rodeo_types.h | 31 | ||||
| -rw-r--r-- | src/compile_flags.txt | 1 | ||||
| -rw-r--r-- | src/rodeo.c | 388 | ||||
| -rw-r--r-- | src/rodeo_internal_types.h | 1 | ||||
| -rw-r--r-- | src/rodeo_log.c | 8 | ||||
| -rw-r--r-- | src/rodeo_math.c | 47 | ||||
| -rw-r--r-- | src/shaders/simple.fragment.sc | 18 | ||||
| -rw-r--r-- | src/shaders/simple.vertex.sc | 5 | ||||
| -rw-r--r-- | src/shaders/varying.def.sc | 2 |
13 files changed, 302 insertions, 256 deletions
diff --git a/.gitmodules b/.gitmodules index aeadb63..74a9ca9 100644 --- a/.gitmodules +++ b/.gitmodules @@ -16,3 +16,6 @@ [submodule "external/STC"] path = external/STC url = https://github.com/realtradam/STC-modified +[submodule "external/SDL_image"] + path = external/SDL_image + url = https://github.com/libsdl-org/SDL_image diff --git a/CMakeLists.txt b/CMakeLists.txt index 6590cf2..c779f26 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ target_compile_options(${PROJECT_NAME} PRIVATE -Wextra -Wpedantic #-Werror +-Wconversion ) endif() @@ -53,6 +54,7 @@ endif() if(NOT ${CMAKE_SYSTEM_NAME} MATCHES "Emscripten") add_subdirectory(external/SDL) + add_subdirectory(external/SDL_image) endif() add_subdirectory(external/cglm) @@ -107,6 +109,7 @@ if(${CMAKE_SYSTEM_NAME} MATCHES "Emscripten") elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") target_include_directories(${PROJECT_NAME} PUBLIC external/SDL/include + PUBLIC external/SDL_image/include PUBLIC external/bgfx/include PUBLIC external/bx/include PUBLIC external/cglm/include @@ -117,6 +120,7 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") target_link_directories(${PROJECT_NAME} PRIVATE external/SDL + PRIVATE external/SDL_image PRIVATE external/bgfx ) endif() @@ -135,6 +139,7 @@ elseif(${CMAKE_SYSTEM_NAME} MATCHES "Linux") #SDL2::SDL2-static # static lib #SDL2::SDL2main SDL2::SDL2 # dynamic lib + SDL2_image cglm ${BINARY_DIR}/.build/linux64_gcc/bin/libbgfx-shared-libRelease.so ) diff --git a/external/SDL_image b/external/SDL_image new file mode 160000 +Subproject 9811cb501b861cb3110c203bae0d6b89ada7c0f diff --git a/include/rodeo.h b/include/rodeo.h index f54792b..b67970c 100644 --- a/include/rodeo.h +++ b/include/rodeo.h @@ -32,14 +32,16 @@ mrodeo_vargs_do(final_arg) \ /// --- Math --- -rodeo_RGBA8_t -rodeo_RGBAFloat_to_RGBA8(const rodeo_RGBAFloat_t color); +rodeo_color_RGBA8_t +rodeo_color_RGBAFloat_to_RGBA8(const rodeo_color_RGBAFloat_t color); -rodeo_BGRA8_t -rodeo_RGBA8_to_BGRA8(const rodeo_RGBA8_t color); -/// --- Core --- +rodeo_color_RGBAFloat_t +rodeo_color_RGBA8_to_RGBAFloat(const rodeo_color_RGBA8_t color); + +int32_t +rodeo_random_simple_get(void); -extern rodeo_texture_2d_t rodeo_texture_2d_default; +/// --- Core --- #define \ mrodeo_window_do( \ @@ -82,15 +84,15 @@ void rodeo_frame_end(void); void -rodeo_mainloop_run( +rodeo_mainLoop_run( rodeo_mainLoop_function main_loop_func ); bool -rodeo_window_check_quit(void); +rodeo_window_quit_get(void); void -rodeo_set_quit(bool quit); +rodeo_window_quit_set(bool quit); void rodeo_debug_text_draw(uint16_t x, uint16_t y, const char *format, ...); @@ -101,11 +103,8 @@ rodeo_renderer_name_get(void); void rodeo_renderer_flush(void); -void -rodeo_rectangle_draw( - rodeo_rectangle_t rectangle, - rodeo_RGBAFloat_t color -); +const rodeo_texture_2d_t* +rodeo_texture_2d_default_get(void); rodeo_texture_2d_t rodeo_texture_2d_create_from_RGBA8( @@ -114,11 +113,23 @@ rodeo_texture_2d_create_from_RGBA8( const uint8_t memory[] ); +rodeo_texture_2d_t +rodeo_texture_2d_create_from_path(rodeo_string_t path); + +void +rodeo_texture_2d_destroy(rodeo_texture_2d_t *texture); + +void +rodeo_rectangle_draw( + const rodeo_rectangle_t *rectangle, + const rodeo_color_RGBAFloat_t *color +); + void rodeo_texture_2d_draw( - const rodeo_rectangle_t source, - const rodeo_rectangle_t destination, - const rodeo_RGBAFloat_t color, + const rodeo_rectangle_t *destination, + const rodeo_rectangle_t *source, + const rodeo_color_RGBAFloat_t *color, const rodeo_texture_2d_t *texture ); @@ -174,7 +185,7 @@ rodeo_string_vargs_format(const char *format, va_list vargs); void rodeo_log( - rodeo_loglevel_t loglevel, + rodeo_logLevel_t loglevel, const char *format, ... ); @@ -188,7 +199,7 @@ float rodeo_frame_time_get(void); float -rodeo_frame_persecond_get(void); +rodeo_frame_perSecond_get(void); void rodeo_frame_limit_set(uint32_t limit); diff --git a/include/rodeo_types.h b/include/rodeo_types.h index 5f9925e..18c3691 100644 --- a/include/rodeo_types.h +++ b/include/rodeo_types.h @@ -18,7 +18,7 @@ typedef union }; float array[4]; } -rodeo_RGBAFloat_t; +rodeo_color_RGBAFloat_t; typedef union { @@ -32,28 +32,19 @@ typedef union uint32_t rgba; uint8_t array[4]; } -rodeo_RGBA8_t; +rodeo_color_RGBA8_t; -typedef union -{ - struct - { - uint8_t blue; - uint8_t green; - uint8_t red; - uint8_t alpha; - }; - uint32_t bgra; - uint8_t array[4]; -} -rodeo_BGRA8_t; typedef struct { float x; float y; float z; - uint32_t bgra; + rodeo_color_RGBAFloat_t color; + //float red; + //float green; + //float blue; + //float alpha; float texture_x; float texture_y; float texture_id; @@ -106,11 +97,11 @@ typedef union { typedef enum { - rodeo_loglevel_info, - rodeo_loglevel_warning, - rodeo_loglevel_error + rodeo_logLevel_info, + rodeo_logLevel_warning, + rodeo_logLevel_error } -rodeo_loglevel_t; +rodeo_logLevel_t; typedef void diff --git a/src/compile_flags.txt b/src/compile_flags.txt index e59f678..2c81d8a 100644 --- a/src/compile_flags.txt +++ b/src/compile_flags.txt @@ -8,3 +8,4 @@ -Wall -Wextra -Wpedantic +-Wconversion diff --git a/src/rodeo.c b/src/rodeo.c index bf59ec4..fd344a9 100644 --- a/src/rodeo.c +++ b/src/rodeo.c @@ -11,6 +11,7 @@ #include <emscripten/emscripten.h> #endif #include "SDL2/SDL.h" +#include "SDL2/SDL_image.h" #include "SDL2/SDL_syswm.h" #include "bgfx/c99/bgfx.h" //#define CGLM_FORCE_LEFT_HANDED @@ -21,10 +22,6 @@ // -- system -- #include <time.h> - -rodeo_texture_2d_t rodeo_texture_2d_default; - - static irodeo_state_t state = {0}; void @@ -41,25 +38,25 @@ rodeo_window_init( state.frame_count = 0; rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "Initializing SDL..." ); if(SDL_Init(SDL_INIT_VIDEO) < 0) { rodeo_log( - rodeo_loglevel_error, + rodeo_logLevel_error, "Failed to initialize SDL. SDL_Error: %s", SDL_GetError() ); exit(EXIT_FAILURE); } rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "Success initializing SDL" ); rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "Initializing SDL window..." ); state.window = SDL_CreateWindow( @@ -73,20 +70,20 @@ rodeo_window_init( if(state.window == NULL) { rodeo_log( - rodeo_loglevel_error, + rodeo_logLevel_error, "Failed creating SDL window. SDL_Error: %s", SDL_GetError() ); exit(EXIT_FAILURE); } rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "Success initializing SDL window" ); #if !__EMSCRIPTEN__ rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "SDL setting up driver specific information..." ); SDL_VERSION(&state.wmi.version); @@ -98,13 +95,13 @@ rodeo_window_init( ) { rodeo_log( - rodeo_loglevel_error, + rodeo_logLevel_error, "SDL failed to get driver specific information. SDL Error: %s", SDL_GetError() ); exit(EXIT_FAILURE); } rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "Success getting driver specific information" ); bgfx_render_frame(-1); @@ -146,13 +143,14 @@ rodeo_window_init( ); bgfx_set_view_rect(0, 0, 0, state.screen_width, state.screen_height); - rodeo_log(rodeo_loglevel_info, "Setting up default render pipeline..."); + rodeo_log(rodeo_logLevel_info, "Setting up default render pipeline..."); // set up vertex layout mrodeo_bgfx_vertex_layout_do(&state.vertex_layout) { 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, true); - bgfx_vertex_layout_add(&state.vertex_layout, BGFX_ATTRIB_TEXCOORD0, 3, BGFX_ATTRIB_TYPE_FLOAT, false, false); + bgfx_vertex_layout_add(&state.vertex_layout, BGFX_ATTRIB_COLOR0, 4, BGFX_ATTRIB_TYPE_FLOAT, false, false); + bgfx_vertex_layout_add(&state.vertex_layout, BGFX_ATTRIB_TEXCOORD0, 2, BGFX_ATTRIB_TYPE_FLOAT, false, false); + bgfx_vertex_layout_add(&state.vertex_layout, BGFX_ATTRIB_TEXCOORD1, 1, BGFX_ATTRIB_TYPE_FLOAT, false, false); } state.vertex_buffer_handle = bgfx_create_dynamic_vertex_buffer(mrodeo_vertex_size_max, &state.vertex_layout, BGFX_BUFFER_NONE); @@ -164,7 +162,7 @@ rodeo_window_init( switch(bgfx_get_renderer_type()) { case BGFX_RENDERER_TYPE_NOOP: rodeo_log( - rodeo_loglevel_error, + rodeo_logLevel_error, "BGFX failed to get determine an appropriate renderer" ); exit(EXIT_FAILURE); @@ -182,7 +180,7 @@ rodeo_window_init( break; default: rodeo_log( - rodeo_loglevel_error, + rodeo_logLevel_error, "No shaders implemented for BGFX renderer chosen" ); exit(EXIT_FAILURE); @@ -207,13 +205,13 @@ rodeo_window_init( true ); rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "Default render pipeline finished setup" ); //bgfx_texture_handle_t default_bgfx_texture = rodeo_texture_2d_create_default(); - rodeo_texture_2d_default.internal_texture = malloc(sizeof(irodeo_texture_internal_t)); + state.default_texture.internal_texture = malloc(sizeof(irodeo_texture_internal_t)); // used for binding textures to shader uniforms state.texture_uniforms[0] = bgfx_create_uniform("default_texture", BGFX_UNIFORM_TYPE_SAMPLER, 1); @@ -221,31 +219,28 @@ rodeo_window_init( { // represents 1 pixel sized white texture - //const rodeo_RGBA8_t default_texture_data = - //{ - // .red = 0xff, - // .green = 0xff, - // .blue = 0xff, - // .alpha = 0xff, - //}; const uint8_t default_texture_data[] = { - 0xff, 0x00, 0xff, 0xff, - 0xff, 0xff, 0x00, 0xff, - 0x00, 0xff, 0xff, 0xff, + //red, blue, green, alpha 0xff, 0xff, 0xff, 0xff, - //0xff, 0xff, 0xff, 0xff, }; - rodeo_texture_2d_default.internal_texture->texture_bgfx = + state.default_texture.internal_texture->texture_bgfx = bgfx_create_texture_2d( - 2, - 2, + 1, + 1, false, 0, BGFX_TEXTURE_FORMAT_RGBA8, BGFX_SAMPLER_UVW_CLAMP | BGFX_SAMPLER_MAG_POINT, - bgfx_copy(&default_texture_data, sizeof(default_texture_data)) + bgfx_copy( + &default_texture_data, + sizeof(default_texture_data) + ) ); + state.default_texture.width = 1; + state.default_texture.height = 1; + + state.active_texture_p = &state.default_texture.internal_texture->texture_bgfx; } state.end_frame = SDL_GetPerformanceCounter(); @@ -254,11 +249,13 @@ rodeo_window_init( void rodeo_window_deinit(void) { - free(rodeo_texture_2d_default.internal_texture); + free(state.default_texture.internal_texture); + 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); SDL_Quit(); } @@ -318,28 +315,28 @@ rodeo_frame_end(void) } void -rodeo_mainloop_run( - rodeo_mainLoop_function mainloop_func +rodeo_mainLoop_run( + rodeo_mainLoop_function main_loop_func ) { #if __EMSCRIPTEN__ - emscripten_set_main_loop(mainloop_func, 0, 1); + emscripten_set_main_loop(main_loop_func, 0, 1); #else - while(!rodeo_window_check_quit()) + while(!rodeo_window_quit_get()) { - mainloop_func(); + main_loop_func(); } #endif } bool -rodeo_window_check_quit(void) +rodeo_window_quit_get(void) { return state.quit; } void -rodeo_set_quit(bool quit) +rodeo_window_quit_set(bool quit) { state.quit = quit; } @@ -366,7 +363,7 @@ rodeo_renderer_flush(void) bgfx_set_texture( 0, state.texture_uniforms[0], - rodeo_texture_2d_default.internal_texture->texture_bgfx, + rodeo_texture_2d_default_get()->internal_texture->texture_bgfx, UINT32_MAX ); if(state.active_texture_p != NULL) @@ -385,7 +382,7 @@ rodeo_renderer_flush(void) bgfx_set_texture( 1, state.texture_uniforms[1], - rodeo_texture_2d_default.internal_texture->texture_bgfx, + rodeo_texture_2d_default_get()->internal_texture->texture_bgfx, UINT32_MAX ); } @@ -423,93 +420,12 @@ rodeo_renderer_flush(void) state.active_texture_p = NULL; } -void -rodeo_rectangle_draw( - rodeo_rectangle_t rectangle, - rodeo_RGBAFloat_t color -) +const rodeo_texture_2d_t* +rodeo_texture_2d_default_get(void) { - rodeo_texture_2d_draw( - (rodeo_rectangle_t){ 0, 0, 1, 1 }, - rectangle, - color, - NULL - ); - /* - const rodeo_BGRA8_t bgra = rodeo_RGBA8_to_BGRA8( - rodeo_RGBAFloat_to_RGBA8(color) - ); - - - if(state.vertex_size < mrodeo_vertex_size_max) - { - state.batched_vertices[state.vertex_size] = - (rodeo_vertex_t) - { - .x = rectangle.width + rectangle.x, - .y = rectangle.height + rectangle.y, - //.z = 0.0f, - .bgra = bgra.bgra, - }; - state.vertex_size += 1; - state.batched_vertices[state.vertex_size] = - (rodeo_vertex_t) - { - .x = rectangle.width + rectangle.x, - .y = rectangle.y, - //.z = 0.0f, - .bgra = bgra.bgra, - }; - state.vertex_size += 1; - state.batched_vertices[state.vertex_size] = - (rodeo_vertex_t) - { - .x = rectangle.x, - .y = rectangle.y, - //.z = 0.0f, - .bgra = bgra.bgra, - }; - state.vertex_size += 1; - state.batched_vertices[state.vertex_size] = - (rodeo_vertex_t) - { - .x = rectangle.x, - .y = rectangle.height + rectangle.y, - //.z = 0.0f, - .bgra = bgra.bgra, - }; - state.vertex_size += 1; - - int indices[] = - { - 0, 1, 3, - 1, 2, 3 - //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; - } - - if(state.vertex_size >= mrodeo_vertex_size_max) - { - rodeo_renderer_flush(); - } - */ + return &state.default_texture; } - rodeo_texture_2d_t rodeo_texture_2d_create_from_RGBA8( const uint32_t width, @@ -519,34 +435,91 @@ rodeo_texture_2d_create_from_RGBA8( { rodeo_texture_2d_t texture; texture.internal_texture = malloc(sizeof(irodeo_texture_internal_t)); - texture.internal_texture->texture_bgfx = bgfx_create_texture_2d( - width, - height, - false, - 1, - BGFX_TEXTURE_FORMAT_RGBA8, - BGFX_TEXTURE_NONE, - bgfx_copy(&memory, width * height * 4) - ); + texture.internal_texture->texture_bgfx = + bgfx_create_texture_2d( + width, + height, + false, + 0, + BGFX_TEXTURE_FORMAT_RGBA8, + BGFX_SAMPLER_UVW_CLAMP | BGFX_SAMPLER_MAG_POINT, + bgfx_copy(memory, width * height * sizeof(uint8_t) * 4) + ); + + texture.width = width; + texture.height = height; return texture; } void +rodeo_texture_2d_destroy(rodeo_texture_2d_t *texture) +{ + bgfx_destroy_texture(texture->internal_texture->texture_bgfx); + free(texture->internal_texture); +} + +void +rodeo_rectangle_draw( + const rodeo_rectangle_t *rectangle, + const rodeo_color_RGBAFloat_t *color +) +{ + rodeo_texture_2d_draw( + rectangle, + NULL, + color, + NULL + ); +} + +void rodeo_texture_2d_draw( - const rodeo_rectangle_t source, - const rodeo_rectangle_t destination, - const rodeo_RGBAFloat_t color, + // cant be NULL + const rodeo_rectangle_t *destination, + // default: entire texture + const rodeo_rectangle_t *source, + // default: white + const rodeo_color_RGBAFloat_t *color, + // default: default texture const rodeo_texture_2d_t *texture ) { - // gonna need to change the shader pipeline - // to also accept textures - const rodeo_BGRA8_t bgra = rodeo_RGBA8_to_BGRA8( - rodeo_RGBAFloat_to_RGBA8(color) - ); + // whether to use default or custom texture float texture_uniform_slot = 0.0; + rodeo_rectangle_t source_applied; + if(source != NULL && texture != NULL) + { + source_applied = (rodeo_rectangle_t){ + .x = source->x / texture->width, + .y = source->y / texture->height, + .width = source->width / texture->width, + .height = source->height / texture->height, + }; + } + else + { + source_applied = (rodeo_rectangle_t){ + .x = 0.0f, + .y = 0.0f, + .width = 1.0f, + .height = 1.0f, + }; + } + + rodeo_color_RGBAFloat_t color_applied; + if(color != NULL) + { + color_applied = *color; + } + else + { + color_applied = (rodeo_color_RGBAFloat_t){ + { 1.0f, 1.0f, 1.0f, 1.0f } + }; + } + // if not using texture: use default instead // otherwise check what current texture is active // if none or the same: set it @@ -569,50 +542,50 @@ rodeo_texture_2d_draw( { state.batched_vertices[state.vertex_size] = (rodeo_vertex_t) - { - .x = destination.width + destination.x, - .y = destination.height + destination.y, - //.z = 0.0f, - .bgra = bgra.bgra, + { + .x = destination->width + destination->x, + .y = destination->height + destination->y, + //.z = 0.0f, + .color = color_applied, .texture_id = texture_uniform_slot, - .texture_x = 1,//source.width + source.x, - .texture_y = 1,//source.height + source.y, + .texture_x = source_applied.width + source_applied.x, + .texture_y = source_applied.height + source_applied.y, }; state.vertex_size += 1; state.batched_vertices[state.vertex_size] = (rodeo_vertex_t) - { - .x = destination.width + destination.x, - .y = destination.y, - //.z = 0.0f, - .bgra = bgra.bgra, + { + .x = destination->width + destination->x, + .y = destination->y, + //.z = 0.0f, + .color = color_applied, .texture_id = texture_uniform_slot, - .texture_x = 1,//source.width + source.x, - .texture_y = 0,//source.y, + .texture_x = source_applied.width + source_applied.x, + .texture_y = source_applied.y, }; state.vertex_size += 1; state.batched_vertices[state.vertex_size] = (rodeo_vertex_t) - { - .x = destination.x, - .y = destination.y, - //.z = 0.0f, - .bgra = bgra.bgra, + { + .x = destination->x, + .y = destination->y, + //.z = 0.0f, + .color = color_applied, .texture_id = texture_uniform_slot, - .texture_x = 0,//source.x, - .texture_y = 0,//source.y, + .texture_x = source_applied.x, + .texture_y = source_applied.y, }; state.vertex_size += 1; state.batched_vertices[state.vertex_size] = (rodeo_vertex_t) - { - .x = destination.x, - .y = destination.height + destination.y, - //.z = 0.0f, - .bgra = bgra.bgra, + { + .x = destination->x, + .y = destination->height + destination->y, + //.z = 0.0f, + .color = color_applied, .texture_id = texture_uniform_slot, - .texture_x = 0,//source.x, - .texture_y = 1,//source.height + source.y, + .texture_x = source_applied.x, + .texture_y = source_applied.height + source_applied.y, }; state.vertex_size += 1; @@ -644,21 +617,62 @@ rodeo_texture_2d_draw( } } - -/* -rodeo_texture_2d_p +rodeo_texture_2d_t rodeo_texture_2d_create_from_path(rodeo_string_t path) { - // call load file into data - // then call create_from_data variant -} + // load image to surface + SDL_Surface *surface = IMG_Load(rodeo_string_to_constcstr(&path)); + if(surface == NULL) + { + rodeo_log( + rodeo_logLevel_error, + "Loading texture from image failed: %s", + IMG_GetError() + ); + return (rodeo_texture_2d_t){0}; + } -void -rodeo_texture_2d_destroy(rodeo_texture_2d_t *texture) -{ + // check and handle a color key for transparency + // not sure if I actually need this... + //uint32_t color_key; + //if(SDL_GetColorKey(surface, &color_key) == 0) + //{ + // SDL_SetColorKey(surface, SDL_TRUE, color_key); + //} + + // convert pixel format of surface into RGBA8 + SDL_Surface *converted_surface = + SDL_ConvertSurfaceFormat( + surface, + // pixel format needs to be backwards + // of what bgfx uses for some reason + SDL_PIXELFORMAT_ABGR8888, + 0 + ); + if(converted_surface == NULL) + { + rodeo_log( + rodeo_logLevel_error, + "Converting image to RGBA8 while loading texture failed: %s", + SDL_GetError() + ); + SDL_FreeSurface(surface); + return (rodeo_texture_2d_t){0}; + } + + // load the pixel data into our own texture + rodeo_texture_2d_t texture = rodeo_texture_2d_create_from_RGBA8( + converted_surface->w, + converted_surface->h, + converted_surface->pixels + ); + + SDL_FreeSurface(surface); + SDL_FreeSurface(converted_surface); + return texture; } -*/ + bgfx_shader_handle_t irodeo_shader_load(const rodeo_string_t path) { @@ -670,7 +684,7 @@ irodeo_shader_load(const rodeo_string_t path) if(!file) { rodeo_log( - rodeo_loglevel_error, + rodeo_logLevel_error, "Shader file \"%s\" not found", path_cstr ); @@ -688,7 +702,7 @@ irodeo_shader_load(const rodeo_string_t path) bgfx_shader_handle_t shader = bgfx_create_shader(mem); rodeo_log( - rodeo_loglevel_info, + rodeo_logLevel_info, "Shader loaded with idx: %"PRIu8"", shader.idx ); @@ -709,7 +723,7 @@ rodeo_frame_time_get(void) } float -rodeo_frame_persecond_get(void) +rodeo_frame_perSecond_get(void) { return 1.0f / (rodeo_frame_time_get() / 1000.0f); } diff --git a/src/rodeo_internal_types.h b/src/rodeo_internal_types.h index bbe77ef..2c059e7 100644 --- a/src/rodeo_internal_types.h +++ b/src/rodeo_internal_types.h @@ -31,6 +31,7 @@ typedef struct uint16_t index_count; uint16_t index_size; uint16_t batched_indices[(mrodeo_vertex_size_max / 4) * 6]; + rodeo_texture_2d_t default_texture; bgfx_texture_handle_t *active_texture_p; bgfx_shader_handle_t vertex_shader; bgfx_shader_handle_t fragment_shader; diff --git a/src/rodeo_log.c b/src/rodeo_log.c index 1a37ffe..243e04b 100644 --- a/src/rodeo_log.c +++ b/src/rodeo_log.c @@ -10,7 +10,7 @@ static rodeo_log_function logging_function = NULL; void rodeo_log( - rodeo_loglevel_t loglevel, + rodeo_logLevel_t loglevel, const char *format, ... ) @@ -23,19 +23,19 @@ rodeo_log( switch(loglevel) { - case rodeo_loglevel_info: + case rodeo_logLevel_info: rodeo_string_prepend( &formatted, rodeo_string_create("[INFO]: ") ); break; - case rodeo_loglevel_warning: + case rodeo_logLevel_warning: rodeo_string_prepend( &formatted, rodeo_string_create("\033[33m[WARN]:\033[0m ") ); break; - case rodeo_loglevel_error: + case rodeo_logLevel_error: rodeo_string_prepend( &formatted, rodeo_string_create("\033[31;1m[ERROR]:\033[0m ") diff --git a/src/rodeo_math.c b/src/rodeo_math.c index 6406f6a..0e19a1e 100644 --- a/src/rodeo_math.c +++ b/src/rodeo_math.c @@ -1,17 +1,19 @@ -// public internal +// -- internal -- +// public #include "rodeo.h" -// system +// -- system -- #include <stdint.h> -// when casting from float to int(example): -// 20.50-21.00 rounds up to 21 -// 20.00-20.49 rounds down to 20 -rodeo_RGBA8_t -rodeo_RGBAFloat_to_RGBA8(const rodeo_RGBAFloat_t color) +// -- external -- +#include "SDL2/SDL.h" + +// rounds to nearest rather then truncation +rodeo_color_RGBA8_t +rodeo_color_RGBAFloat_to_RGBA8(const rodeo_color_RGBAFloat_t color) { - return (rodeo_RGBA8_t){ + return (rodeo_color_RGBA8_t){ .red = (uint8_t)((color.red * (float)UINT8_MAX) + 0.5), .green = (uint8_t)((color.green * (float)UINT8_MAX) + 0.5), .blue = (uint8_t)((color.blue * (float)UINT8_MAX) + 0.5), @@ -19,13 +21,28 @@ rodeo_RGBAFloat_to_RGBA8(const rodeo_RGBAFloat_t color) }; } -rodeo_BGRA8_t -rodeo_RGBA8_to_BGRA8(const rodeo_RGBA8_t color) +rodeo_color_RGBAFloat_t +rodeo_color_RGBA8_to_RGBAFloat(const rodeo_color_RGBA8_t color) { - return (rodeo_BGRA8_t){ - .alpha = color.alpha, - .blue = color.blue, - .green = color.green, - .red = color.red, + return (rodeo_color_RGBAFloat_t){ + .red = (float)color.red / 255.0f, + .green = (float)color.green / 255.0f, + .blue = (float)color.blue / 255.0f, + .alpha = (float)color.alpha / 255.0f, }; } + +double +rodeo_random_simple_float_get(void) +{ + return 0; +} + +uint64_t +rodeo_random_simple_uint64_get(void) +{ + return 0; +} + + + diff --git a/src/shaders/simple.fragment.sc b/src/shaders/simple.fragment.sc index 4ae2f48..ab72ae4 100644 --- a/src/shaders/simple.fragment.sc +++ b/src/shaders/simple.fragment.sc @@ -1,4 +1,4 @@ -$input v_color0, v_texcoord0 +$input v_color0, v_texcoord0, v_texcoord1 #include <bgfx_shader.sh> @@ -17,12 +17,12 @@ SAMPLER2D(texture_0, 1); void main() { - //if(v_texcoord0.y < 0.0) - //{ - gl_FragColor = v_color0 * texture2D(default_texture, v_texcoord0.xy); - //} - //else - //{ - //gl_FragColor = v_color0 * texture2D(texture_0, v_texcoord0.xy); - //} + if(v_texcoord1 == 1.0) + { + gl_FragColor = v_color0 * texture2D(texture_0, v_texcoord0.xy); + } + else + { + gl_FragColor = v_color0 * texture2D(default_texture, v_texcoord0.xy); + } } diff --git a/src/shaders/simple.vertex.sc b/src/shaders/simple.vertex.sc index b90a0c7..cda89a9 100644 --- a/src/shaders/simple.vertex.sc +++ b/src/shaders/simple.vertex.sc @@ -1,5 +1,5 @@ -$input a_position, a_color0, a_texcoord0 -$output v_color0, v_texcoord0 +$input a_position, a_color0, a_texcoord0, a_texcoord1 +$output v_color0, v_texcoord0, v_texcoord1 #include <bgfx_shader.sh> @@ -8,4 +8,5 @@ void main() gl_Position = mul(u_modelViewProj, vec4(a_position, 1.0)); v_color0 = a_color0; v_texcoord0 = a_texcoord0; + v_texcoord1 = a_texcoord1; } diff --git a/src/shaders/varying.def.sc b/src/shaders/varying.def.sc index 5f7be4e..17b1bab 100644 --- a/src/shaders/varying.def.sc +++ b/src/shaders/varying.def.sc @@ -1,8 +1,10 @@ // outputs; vec4 v_color0 : COLOR0; vec3 v_texcoord0 : TEXCOORD0; +float v_texcoord1 : TEXCOORD1; // inputs; vec3 a_position : POSITION; vec4 a_color0 : COLOR0; vec3 a_texcoord0 : TEXCOORD0; +float a_texcoord1 : TEXCOORD1; |
