diff options
| -rw-r--r-- | include/rodeo/gfx.h | 24 | ||||
| -rw-r--r-- | include/rodeo/window.h | 4 | ||||
| -rw-r--r-- | src/gfx/irodeo_gfx_t.h | 2 | ||||
| -rw-r--r-- | src/gfx/rodeo_gfx.c | 49 | ||||
| -rw-r--r-- | src/window/irodeo_window_t.h | 4 | ||||
| -rw-r--r-- | src/window/rodeo_window.c | 21 |
6 files changed, 76 insertions, 28 deletions
diff --git a/include/rodeo/gfx.h b/include/rodeo/gfx.h index a10e901..151bdda 100644 --- a/include/rodeo/gfx.h +++ b/include/rodeo/gfx.h @@ -8,7 +8,7 @@ #include "stc/cstr.h" void -rodeo_gfx_init(void); +rodeo_gfx_init(float width, float height); void rodeo_gfx_deinit(void); @@ -79,19 +79,21 @@ rodeo_gfx_frame_limit_set(uint32_t limit); uint32_t rodeo_gfx_frame_limit_get(void); -#define \ -mrodeo_gfx_do( \ -) \ - mrodeo_defer_do( \ - rodeo_gfx_init(), \ - rodeo_gfx_deinit() \ - ) - - #define \ -mrodeo_gfx_frame_do( \ +mrodeo_gfx_do( \ + width, \ + height \ ) \ mrodeo_defer_do( \ + rodeo_gfx_init(width, height), \ + rodeo_gfx_deinit() \ + ) + + +#define \ +mrodeo_gfx_frame_do( \ +) \ + mrodeo_defer_do( \ rodeo_gfx_frame_begin(), \ rodeo_gfx_frame_end() \ ) diff --git a/include/rodeo/window.h b/include/rodeo/window.h index dd505f2..5674174 100644 --- a/include/rodeo/window.h +++ b/include/rodeo/window.h @@ -21,10 +21,10 @@ rodeo_window_init( void rodeo_window_deinit(void); -uint16_t +uint32_t rodeo_window_screen_width_get(void); -uint16_t +uint32_t rodeo_window_screen_height_get(void); bool diff --git a/src/gfx/irodeo_gfx_t.h b/src/gfx/irodeo_gfx_t.h index 7d9d1b7..b367e0f 100644 --- a/src/gfx/irodeo_gfx_t.h +++ b/src/gfx/irodeo_gfx_t.h @@ -31,6 +31,8 @@ struct bgfx_shader_handle_t fragment_shader; bgfx_program_handle_t program_shader; bgfx_uniform_handle_t texture_uniforms[2]; + float target_width; + float target_height; mat4 view_matrix; mat4 proj_matrix; uint64_t frame_count; diff --git a/src/gfx/rodeo_gfx.c b/src/gfx/rodeo_gfx.c index 04d923f..9004370 100644 --- a/src/gfx/rodeo_gfx.c +++ b/src/gfx/rodeo_gfx.c @@ -20,12 +20,14 @@ static irodeo_gfx_state_t irodeo_gfx_state = {0}; void -rodeo_gfx_init(void) +rodeo_gfx_init(float width, float height) { #ifdef __EMSCRIPTEN__ bgfx_render_frame(-1); #endif + irodeo_gfx_state.target_width = width; + irodeo_gfx_state.target_height = height; bgfx_platform_data_t pd = {0}; memset(&pd, 0, sizeof(bgfx_platform_data_t)); @@ -59,7 +61,8 @@ rodeo_gfx_init(void) bgfx_set_view_clear( 0, BGFX_CLEAR_COLOR | BGFX_CLEAR_DEPTH, - 0x443355FF, + //0x443355FF, + 0x000000FF, 1.0f, 0 ); @@ -205,23 +208,49 @@ rodeo_gfx_frame_begin(void) glm_mat4_identity(irodeo_gfx_state.view_matrix); //glm_perspective(glm_rad(60.f), 640.f / 480.f, 0.1f, 100.0f, proj); + + // scale the game screen to fit window size with letterbox + // what size we declare the game screen to be within + const float target_width = irodeo_gfx_state.target_width; + const float target_height = irodeo_gfx_state.target_height; + // what the calculated size is to fit in the window + float result_width = target_width; + float result_height = target_height; + const float game_aspect = target_width / target_height; + const float window_aspect = (float)rodeo_window_screen_width_get()/(float)rodeo_window_screen_height_get(); + if(window_aspect > game_aspect) + { + result_width /= (game_aspect) / window_aspect; + } + else + { + result_height *= (game_aspect) / window_aspect; + } + + // make the game screen centered in the window. + // "2" is 1 full screen width, therefore we multiply + // the target and result ratio by 1 for it to be a + // half of the screen size + vec3 offset = { + 1 - (1 * (target_width / result_width)), // x + -(1 - (1 * (target_height / result_height))), // y + 0 + }; - // TODO: figure out if why 'zo' is correct - // but 'no' is incorrect glm_ortho_rh_zo( 0, - (float)rodeo_window_screen_width_get(), - (float)rodeo_window_screen_height_get(), + result_width, + result_height, 0, - // near - -0.1f, - // far + -100.0f, 100.0f, irodeo_gfx_state.proj_matrix ); + glm_translated(irodeo_gfx_state.proj_matrix, offset); + bgfx_set_view_transform(0, irodeo_gfx_state.view_matrix, irodeo_gfx_state.proj_matrix); - bgfx_set_view_rect(0, 0, 0, rodeo_window_screen_width_get(), rodeo_window_screen_height_get()); + bgfx_set_view_rect(0, 0, 0, (uint16_t)rodeo_window_screen_width_get(), (uint16_t)rodeo_window_screen_height_get()); irodeo_gfx_render_buffer_transient_alloc(); bgfx_touch(0); diff --git a/src/window/irodeo_window_t.h b/src/window/irodeo_window_t.h index e2dd0a4..f7e0c26 100644 --- a/src/window/irodeo_window_t.h +++ b/src/window/irodeo_window_t.h @@ -14,8 +14,8 @@ struct SDL_Window* window; SDL_Surface* screen_surface; SDL_SysWMinfo wmi; - uint16_t screen_width; - uint16_t screen_height; + uint32_t screen_width; + uint32_t screen_height; bool quit; } irodeo_window_state_t; diff --git a/src/window/rodeo_window.c b/src/window/rodeo_window.c index c7c4ddf..e92382e 100644 --- a/src/window/rodeo_window.c +++ b/src/window/rodeo_window.c @@ -100,7 +100,6 @@ rodeo_window_init( "Success getting driver specific information" ); #endif - } void @@ -110,15 +109,31 @@ rodeo_window_deinit(void) SDL_Quit(); } -uint16_t +void +irodeo_window_update_screen_size(void) +{ + int32_t width = 0; + int32_t height = 0; + SDL_GetWindowSize( + irodeo_window_state.window, + &width, + &height + ); + irodeo_window_state.screen_width = (uint32_t)width; + irodeo_window_state.screen_height = (uint32_t)height; +} + +uint32_t rodeo_window_screen_width_get(void) { + irodeo_window_update_screen_size(); return irodeo_window_state.screen_width; } -uint16_t +uint32_t rodeo_window_screen_height_get(void) { + irodeo_window_update_screen_size(); return irodeo_window_state.screen_height; } |
