shithub: cstory

Download patch

ref: 4d8be3bc36612ef4fe2716f20f754c31a9e90d84
parent: c4aa8e28bb201ed52ae6203aa396b762c88c229b
author: Clownacy <Clownacy@users.noreply.github.com>
date: Wed Apr 1 11:21:40 EDT 2020

More refactoring

Get fullscreen mostly working in GLFW3

--- a/src/Backends/Rendering/OpenGL3.cpp
+++ b/src/Backends/Rendering/OpenGL3.cpp
@@ -98,6 +98,9 @@
 
 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 @@
 
 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 @@
 	// 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);
--- 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);
--- a/src/Backends/Window/GLFW3-OpenGL3.cpp
+++ b/src/Backends/Window/GLFW3-OpenGL3.cpp
@@ -22,7 +22,7 @@
 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,8 +36,23 @@
 	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)
 	{/*
 	#ifndef _WIN32	// On Windows, we use native icons instead (so we can give the taskbar and window separate icons, like the original EXE does)
@@ -49,16 +64,6 @@
 		SDL_FreeSurface(icon_surface);
 	#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);
 
--