From 6697897d59a62705351d4592ab26b8fd384d1ee3 Mon Sep 17 00:00:00 2001 From: Samuel Walker Date: Thu, 2 Jan 2025 21:23:53 -0700 Subject: [PATCH] Implemented fullscreen modes --- 2dGameProject/Window.cpp | 84 ++++++++++++++++++++++++++++++++++------ 2dGameProject/Window.h | 15 ++++++- 2dGameProject/main.cpp | 30 +++++++------- 3 files changed, 103 insertions(+), 26 deletions(-) diff --git a/2dGameProject/Window.cpp b/2dGameProject/Window.cpp index 0fd0857..44bcb40 100644 --- a/2dGameProject/Window.cpp +++ b/2dGameProject/Window.cpp @@ -4,8 +4,7 @@ Window::Window(int width, int height, const char* title) { glfw_window = glfwCreateWindow(width, height, title, NULL, NULL); if (!glfw_window) throw ("Failed to create GLFW Window!"); - setWidth(width); - setHeight(height); + setSize(width, height); setTitle(title); } @@ -15,8 +14,7 @@ Window::~Window() { } void Window::setWidth(int width) { - this->width = width; - glfwSetWindowSize(glfw_window, width, height); + setSize(width, height); } int Window::getWidth() { @@ -24,14 +22,19 @@ int Window::getWidth() { } void Window::setHeight(int height) { - this->height = height; - glfwSetWindowSize(glfw_window, width, height); + setSize(width, height); } int Window::getHeight() { return height; } +void Window::setSize(int width, int height) { + this->height = height; + this->width = width; + glfwSetWindowSize(glfw_window, width, height); +} + void Window::setTitle(const char* title) { this->title = title; glfwSetWindowTitle(glfw_window, title); @@ -41,12 +44,57 @@ const char* Window::getTitle() { return title; } -void Window::setFullscreen(bool fullscreen) { - is_fullscreen = fullscreen; - if (fullscreen) - glfwSetWindowMonitor(glfw_window, glfwGetPrimaryMonitor(), 0, 0, width, height, GLFW_DONT_CARE); - else - glfwSetWindowMonitor(glfw_window, NULL); +void Window::setFullscreen(int width, int height, int refreshRate) { + setFullscreen(width, height, refreshRate, glfwGetPrimaryMonitor()); +} + +void Window::setFullscreen(int width, int height, int refreshRate, GLFWmonitor* monitor) { + is_fullscreen = true; + glfwGetWindowPos(glfw_window, &windowed_mode_x, &windowed_mode_y); + glfwSetWindowMonitor(glfw_window, monitor, 0, 0, width, height, refreshRate); +} + +void Window::setWindowed() { + is_fullscreen = false; + glfwSetWindowMonitor(glfw_window, NULL, windowed_mode_x, windowed_mode_y, width, height, NULL); +} + +void Window::setWindowedFullscreen() { + setWindowedFullscreen(glfwGetPrimaryMonitor()); +} + +void Window::setWindowedFullscreen(GLFWmonitor* monitor) { + if (is_fullscreen) + setWindowed(); + glfwGetWindowPos(glfw_window, &windowed_mode_x, &windowed_mode_y); + is_fullscreen = true; + const GLFWvidmode* mode = glfwGetVideoMode(monitor); + glfwSetWindowMonitor(glfw_window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); + +} + +std::vector Window::getVideoModes() { + return getVideoModes(glfwGetPrimaryMonitor()); +} + +std::vector Window::getVideoModes(GLFWmonitor* monitor) { + int count; + const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count); + std::vector modesArray; + for (int i = 0; i < count; i++) { + modesArray.push_back(modes[i]); + } + return modesArray; +} + +std::vector Window::getMonitors() { + int count; + GLFWmonitor** monitors = glfwGetMonitors(&count); + std::vector monitorsArray; + for (int i = 0; i < count; i++) { + monitorsArray.push_back(monitors[i]); + } + return monitorsArray; } bool Window::isFullscreen() { @@ -63,4 +111,16 @@ void Window::setResizable(bool resizable) { bool Window::isResizable() { return is_resizable; +} + +void Window::beginRender() { + glfwMakeContextCurrent(glfw_window); +} + +void Window::endRender() { + glfwSwapBuffers(glfw_window); +} + +bool Window::isClosing() { + return glfwWindowShouldClose(glfw_window); } \ No newline at end of file diff --git a/2dGameProject/Window.h b/2dGameProject/Window.h index 5c875f6..84dc2cb 100644 --- a/2dGameProject/Window.h +++ b/2dGameProject/Window.h @@ -1,5 +1,6 @@ #pragma once #include +#include class Window { private: @@ -8,6 +9,7 @@ private: const char* title; bool is_fullscreen; bool is_resizable; + int windowed_mode_x, windowed_mode_y; public: Window(int width, int height, const char* title); @@ -16,11 +18,22 @@ public: int getHeight(); void setWidth(int width); void setHeight(int height); + void setSize(int width, int height); const char* getTitle(); void setTitle(const char* title); + std::vector getVideoModes(); + std::vector getVideoModes(GLFWmonitor* monitor); + std::vector getMonitors(); bool isFullscreen(); bool isResizable(); - void setFullscreen(bool fullscreen); + void setFullscreen(int width, int height, int refreshRate); + void setFullscreen(int width, int height, int refreshRate, GLFWmonitor* monitor); + void setWindowed(); + void setWindowedFullscreen(); + void setWindowedFullscreen(GLFWmonitor* monitor); void setResizable(bool resizable); + void beginRender(); + void endRender(); + bool isClosing(); }; diff --git a/2dGameProject/main.cpp b/2dGameProject/main.cpp index f489b0e..f62e527 100644 --- a/2dGameProject/main.cpp +++ b/2dGameProject/main.cpp @@ -1,28 +1,32 @@ #include #include +#include "Window.h" + +void init() { + if (!glfwInit()) + throw ("Unable to init GLFW"); +} + +void deinit() { + glfwTerminate(); +} int main() { - - GLFWwindow* window; - if (!glfwInit()) - return -1; + init(); - window = glfwCreateWindow(649, 480, "Test", NULL, NULL); - if (!window) { - glfwTerminate(); - return -1; - } + Window* window = new Window(640, 480, "Test Window"); + window->setWindowedFullscreen(window->getMonitors()[1]); - glfwMakeContextCurrent(window); + window->beginRender(); - while (!glfwWindowShouldClose(window)) { + while (!window->isClosing()) { glClear(GL_COLOR_BUFFER_BIT); - glfwSwapBuffers(window); + window->endRender(); glfwPollEvents(); } - glfwTerminate(); + deinit(); return 0; } \ No newline at end of file