diff --git a/game/src/event/evtman.c b/game/src/event/evtman.c index 87a8172..f0af41c 100644 --- a/game/src/event/evtman.c +++ b/game/src/event/evtman.c @@ -94,6 +94,7 @@ void evtman_init () { #define KEY(k) GINT_TO_POINTER(k) + evtman_add_handler (2, SDL_EVENT_WINDOW_RESIZED, NULL, NULL, graphics_cb_window_resize, NULL); /* The UI effectively eats most SDL events, as it needs it for it's own calculations. */ evtman_add_handler (1, SDL_EVENT_KEY_DOWN, evt_pred_flag, "ui_has_focus", uiman_cb_key_down, NULL); diff --git a/game/src/event/evtman.h b/game/src/event/evtman.h index d7720cc..180e5da 100644 --- a/game/src/event/evtman.h +++ b/game/src/event/evtman.h @@ -7,6 +7,7 @@ #include "handler.h" #include "../util.h" #include "../flags.h" +#include "../graphics.h" #include "../ui/uiman.h" typedef struct CompositePredicate diff --git a/game/src/graphics.c b/game/src/graphics.c index f555f51..8cc9cb8 100644 --- a/game/src/graphics.c +++ b/game/src/graphics.c @@ -8,13 +8,12 @@ int render_distance = 2; static SDL_GLContext gl_context; +static int window_w = 800, window_h = 600; + static uint64_t last_check_perf = 0; static int avg_counter = 0; static double avg_elapsed = 0; -#define WINDOW_WIDTH 800 -#define WINDOW_HEIGHT 600 - void graphics_init () { @@ -23,7 +22,7 @@ graphics_init () g_error ("Couldn't initialize SDL: %s", SDL_GetError ()); } - sdl_window = SDL_CreateWindow (GAME_NAME, WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL); + sdl_window = SDL_CreateWindow (GAME_NAME, window_w, window_h, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); if (!sdl_window) { @@ -53,7 +52,7 @@ graphics_init () g_info ("GLSL version: %s", glGetString (GL_SHADING_LANGUAGE_VERSION)); g_info ("Max vertex attributes: %d", GL_MAX_VERTEX_ATTRIBS); - glViewport (0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); + glViewport (0, 0, window_w, window_h); glClearColor (0, 0.3, 0.8, 1); glEnable (GL_CULL_FACE); @@ -71,7 +70,7 @@ graphics_render () uint64_t freq = SDL_GetPerformanceFrequency (); mat4 projection, view; - camera_get_projection (WINDOW_WIDTH / WINDOW_HEIGHT, projection); + camera_get_projection ((float) window_w / (float) window_h, projection); camera_get_view (view); resources_programs_set_mat4 ("projection", &projection); @@ -129,3 +128,12 @@ graphics_free () SDL_GL_DestroyContext (gl_context); SDL_DestroyWindow (sdl_window); } + +gboolean +graphics_cb_window_resize (SDL_Event *event, void *data) +{ + UNUSED (event, data); + SDL_GetWindowSize (sdl_window, &window_w, &window_h); + glViewport (0, 0, window_w, window_h); + return TRUE; +} diff --git a/game/src/graphics.h b/game/src/graphics.h index 28e0606..f8299b7 100644 --- a/game/src/graphics.h +++ b/game/src/graphics.h @@ -24,3 +24,4 @@ void graphics_init (); void graphics_render (); void graphics_free (); +gboolean graphics_cb_window_resize ();