From 4d8be3bc36612ef4fe2716f20f754c31a9e90d84 Mon Sep 17 00:00:00 2001 From: Clownacy Date: Wed, 1 Apr 2020 15:21:40 +0100 Subject: [PATCH] More refactoring Get fullscreen mostly working in GLFW3 --- src/Backends/Rendering/OpenGL3.cpp | 33 +++++++++++++++++++++++++-- src/Backends/Window.h | 2 +- src/Backends/Window/GLFW3-OpenGL3.cpp | 29 +++++++++++++---------- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/src/Backends/Rendering/OpenGL3.cpp b/src/Backends/Rendering/OpenGL3.cpp index 225a5ea1..3ce68ad9 100644 --- a/src/Backends/Rendering/OpenGL3.cpp +++ b/src/Backends/Rendering/OpenGL3.cpp @@ -98,6 +98,9 @@ static Backend_Surface *glyph_destination_surface; static spritebatch_t glyph_batcher; +static int actual_screen_width; +static int actual_screen_height; + #ifdef USE_OPENGLES2 static const GLchar *vertex_shader_plain = " \ #version 100\n \ @@ -515,7 +518,10 @@ static void GlyphBatch_DestroyTexture(SPRITEBATCH_U64 texture_id, void *udata) Backend_Surface* Backend_Init(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) { - if (WindowBackend_OpenGL_CreateWindow(window_title, screen_width, screen_height, fullscreen)) + actual_screen_width = screen_width; + actual_screen_height = screen_height; + + if (WindowBackend_OpenGL_CreateWindow(window_title, &actual_screen_width, &actual_screen_height, fullscreen)) { printf("GL_VENDOR = %s\n", glGetString(GL_VENDOR)); printf("GL_RENDERER = %s\n", glGetString(GL_RENDERER)); @@ -656,7 +662,30 @@ void Backend_DrawScreen(void) // Target actual screen, and not our framebuffer glBindFramebuffer(GL_FRAMEBUFFER, 0); - glViewport(0, 0, framebuffer.width, framebuffer.height); + // Do some viewport trickery, to fit the framebuffer in the center of the screen + GLint x; + GLint y; + GLsizei width; + GLsizei height; + + if (actual_screen_width > actual_screen_height) + { + y = 0; + height = actual_screen_height; + + width = framebuffer.width * ((float)actual_screen_height / (float)framebuffer.height); + x = (actual_screen_width - width) / 2; + } + else + { + x = 0; + width = actual_screen_width; + + height = framebuffer.height * ((float)actual_screen_width / (float)framebuffer.width); + y = (actual_screen_height - height) / 2; + } + + glViewport(x, y, width, height); // Draw framebuffer to screen glBindTexture(GL_TEXTURE_2D, framebuffer.texture_id); diff --git a/src/Backends/Window.h b/src/Backends/Window.h index 0f10e899..ce929baf 100644 --- a/src/Backends/Window.h +++ b/src/Backends/Window.h @@ -2,6 +2,6 @@ #include "../WindowsWrapper.h" -BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int screen_width, int screen_height, BOOL fullscreen); +BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_width, int *screen_height, BOOL fullscreen); void WindowBackend_OpenGL_DestroyWindow(void); void WindowBackend_OpenGL_Display(void); diff --git a/src/Backends/Window/GLFW3-OpenGL3.cpp b/src/Backends/Window/GLFW3-OpenGL3.cpp index 112b94eb..5b869455 100644 --- a/src/Backends/Window/GLFW3-OpenGL3.cpp +++ b/src/Backends/Window/GLFW3-OpenGL3.cpp @@ -22,7 +22,7 @@ void KeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods void WindowFocusCallback(GLFWwindow *window, int focused); void WindowSizeCallback(GLFWwindow *window, int width, int height); -BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int screen_width, int screen_height, BOOL fullscreen) +BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int *screen_width, int *screen_height, BOOL fullscreen) { #ifdef USE_OPENGLES2 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); @@ -36,7 +36,22 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int screen_widt glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2); #endif - window = glfwCreateWindow(screen_width, screen_height, window_title, NULL, NULL); + GLFWmonitor *monitor = NULL; + + if (fullscreen) + { + monitor = glfwGetPrimaryMonitor(); + + if (monitor != NULL) + { + const GLFWvidmode *mode = glfwGetVideoMode(monitor); + + *screen_width = mode->width; + *screen_height = mode->height; + } + } + + window = glfwCreateWindow(*screen_width, *screen_height, window_title, monitor, NULL); if (window != NULL) {/* @@ -50,16 +65,6 @@ BOOL WindowBackend_OpenGL_CreateWindow(const char *window_title, int screen_widt #endif */ - if (fullscreen) - { - GLFWmonitor* monitor = glfwGetPrimaryMonitor(); - if (monitor) - { - const GLFWvidmode* mode = glfwGetVideoMode(monitor); - glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); - } - } - glfwMakeContextCurrent(window); #ifndef USE_OPENGLES2