medfall

A super great game engine
Log | Files | Refs

commit 1e3584c57fafb850c2cd9dc2219c0577394e9a64
parent 1d17dea9ef85e318275964f383fd8128f8d147d1
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat,  3 Aug 2019 15:10:02 +0300

Allow window resizing

Diffstat:
Mbsp.cc | 24++++++++++++++++++------
Mfullscreen.cc | 54+++++++++++++++++++++++++++++++-----------------------
Mgame.h | 4++--
Mgl.cc | 13+++++++++++++
Mmain.cc | 6++++++
Mrenderer.cc | 5++++-
6 files changed, 74 insertions(+), 32 deletions(-)

diff --git a/bsp.cc b/bsp.cc @@ -361,9 +361,21 @@ GAME_INIT( game_init ) { bspr_init( &game->bspr, &game->bsp ); - { - v2u32 window_size = get_window_size(); + tree_mesh = load_obj( "models/trees/PineTree.obj", &mem->persistent_arena ); + + world_depth_and_normals = { }; + model_depth = { }; + model_depth_outline = { }; +} + +static void recreate_framebuffers() { + renderer_delete_fb( world_depth_and_normals ); + renderer_delete_fb( model_depth ); + renderer_delete_fb( model_depth_outline ); + + v2u32 window_size = get_window_size(); + { TextureConfig texture_config; texture_config.width = window_size.x; texture_config.height = window_size.y; @@ -387,8 +399,6 @@ GAME_INIT( game_init ) { } { - v2u32 window_size = get_window_size(); - TextureConfig texture_config; texture_config.width = window_size.x; texture_config.height = window_size.y; @@ -403,11 +413,13 @@ GAME_INIT( game_init ) { model_depth = renderer_new_fb( fb_config ); model_depth_outline = renderer_new_fb( fb_config ); } - - tree_mesh = load_obj( "models/trees/PineTree.obj", &mem->persistent_arena ); } GAME_FRAME( game_frame ) { + if( input->resized ) { + recreate_framebuffers(); + } + const int fb = input->keys[ KEY_W ] - input->keys[ KEY_S ]; const int lr = input->keys[ KEY_D ] - input->keys[ KEY_A ]; const int dz = input->keys[ KEY_SPACE ] - input->keys[ KEY_LEFTSHIFT ]; diff --git a/fullscreen.cc b/fullscreen.cc @@ -61,29 +61,7 @@ GAME_INIT( game_init ) { shadow_fb = renderer_new_fb( config, FB_DEPTH ); } - { - v2u32 window_size = get_window_size(); - - TextureConfig texture_config; - texture_config.width = window_size.x; - texture_config.height = window_size.y; - - FramebufferConfig fb_config; - - texture_config.format = TEXFMT_RGB_U8; - fb_config.textures[ OUTPUT_ALBEDO ].config = texture_config; - fb_config.textures[ OUTPUT_ALBEDO ].attachment = FB_COLOUR; - - texture_config.format = TEXFMT_RGB_HALF; - fb_config.textures[ OUTPUT_NORMAL ].config = texture_config; - fb_config.textures[ OUTPUT_NORMAL ].attachment = FB_NORMAL; - - texture_config.format = TEXFMT_DEPTH; - fb_config.textures[ OUTPUT_DEPTH ].config = texture_config; - fb_config.textures[ OUTPUT_DEPTH ].attachment = FB_DEPTH; - - gbuffer = renderer_new_fb( fb_config ); - } + gbuffer = { }; game->pos = v3( -10, -10, 5 ); game->pitch = 0; @@ -106,7 +84,37 @@ GAME_INIT( game_init ) { tree_mesh = load_obj( "models/trees/PineTree.obj", &mem->persistent_arena ); } +static void recreate_framebuffers() { + renderer_delete_fb( gbuffer ); + + v2u32 window_size = get_window_size(); + + TextureConfig texture_config; + texture_config.width = window_size.x; + texture_config.height = window_size.y; + + FramebufferConfig fb_config; + + texture_config.format = TEXFMT_RGB_U8; + fb_config.textures[ OUTPUT_ALBEDO ].config = texture_config; + fb_config.textures[ OUTPUT_ALBEDO ].attachment = FB_COLOUR; + + texture_config.format = TEXFMT_RGB_HALF; + fb_config.textures[ OUTPUT_NORMAL ].config = texture_config; + fb_config.textures[ OUTPUT_NORMAL ].attachment = FB_NORMAL; + + texture_config.format = TEXFMT_DEPTH; + fb_config.textures[ OUTPUT_DEPTH ].config = texture_config; + fb_config.textures[ OUTPUT_DEPTH ].attachment = FB_DEPTH; + + gbuffer = renderer_new_fb( fb_config ); +} + GAME_FRAME( game_frame ) { + if( input->resized ) { + recreate_framebuffers(); + } + const float speed = 6.0f; const float angular_speed = 100.0f; diff --git a/game.h b/game.h @@ -49,8 +49,8 @@ struct GameMemory { struct GameInput { StaticArray< bool, KEY_COUNT > keys; StaticArray< bool, KEY_COUNT > key_edges; - double mouse_dx; - double mouse_dy; + double mouse_dx, mouse_dy; + bool resized; }; #define GAME_INIT( name ) void name( GameState * game, GameMemory * mem ) diff --git a/gl.cc b/gl.cc @@ -118,6 +118,11 @@ static void glfw_focus_callback( GLFWwindow * window, int focused ) { } } +static void glfw_resize_callback( GLFWwindow * window, int w, int h ) { + window_size.x = checked_cast< u32 >( w ); + window_size.y = checked_cast< u32 >( h ); +} + GLFWwindow * gl_init( WindowType window_type ) { glfwSetErrorCallback( glfw_error_callback ); @@ -125,7 +130,13 @@ GLFWwindow * gl_init( WindowType window_type ) { FATAL( "glfwInit" ); } +#if RELEASE_BUILD + if( window_type == WINDOW_LAUNCHER ) { + glfwWindowHint( GLFW_RESIZABLE, 0 ); + } +#else glfwWindowHint( GLFW_RESIZABLE, 0 ); +#endif int width = 800; int height = 265; @@ -185,6 +196,8 @@ GLFWwindow * gl_init( WindowType window_type ) { } #endif + glfwSetFramebufferSizeCallback( window, glfw_resize_callback ); + glfwMakeContextCurrent( window ); if( gladLoadGLLoader( ( GLADloadproc ) glfwGetProcAddress ) != 1 ) { diff --git a/main.cc b/main.cc @@ -64,6 +64,8 @@ int main( int argc, char ** argv ) { double last_xpos, last_ypos; glfwGetCursorPos( window, &last_xpos, &last_ypos ); + v2u32 last_window_size = v2u32( 0, 0 ); + double game_time = 0; while( !glfwWindowShouldClose( window ) ) { @@ -138,6 +140,10 @@ int main( int argc, char ** argv ) { input.mouse_dy = 0; #endif + v2u32 window_size = get_window_size(); + input.resized = window_size.x != last_window_size.x || window_size.y != last_window_size.y; + last_window_size = window_size; + if( total_frames % 60 == 0 ) { hotload_shaders(); } diff --git a/renderer.cc b/renderer.cc @@ -692,7 +692,10 @@ FB renderer_new_fb( TextureConfig texture_config, FramebufferAttachment attachme } void renderer_delete_fb( FB fb ) { - glDeleteBuffers( 1, &fb.fbo ); + if( fb.fbo == 0 ) + return; + + glDeleteFramebuffers( 1, &fb.fbo ); for( Texture texture : fb.textures ) { if( texture != 0 ) { glDeleteTextures( 1, &texture );