Implemented fullscreen modes

This commit is contained in:
2025-01-02 21:23:53 -07:00
parent 2bf9e8056c
commit 6697897d59
3 changed files with 103 additions and 26 deletions

View File

@ -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<GLFWvidmode> Window::getVideoModes() {
return getVideoModes(glfwGetPrimaryMonitor());
}
std::vector<GLFWvidmode> Window::getVideoModes(GLFWmonitor* monitor) {
int count;
const GLFWvidmode* modes = glfwGetVideoModes(monitor, &count);
std::vector<GLFWvidmode> modesArray;
for (int i = 0; i < count; i++) {
modesArray.push_back(modes[i]);
}
return modesArray;
}
std::vector<GLFWmonitor*> Window::getMonitors() {
int count;
GLFWmonitor** monitors = glfwGetMonitors(&count);
std::vector<GLFWmonitor*> 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);
}

View File

@ -1,5 +1,6 @@
#pragma once
#include <GLFW/glfw3.h>
#include <vector>
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<GLFWvidmode> getVideoModes();
std::vector<GLFWvidmode> getVideoModes(GLFWmonitor* monitor);
std::vector<GLFWmonitor*> 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();
};

View File

@ -1,28 +1,32 @@
#include <iostream>
#include <GLFW/glfw3.h>
#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;
}