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); glfw_window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!glfw_window) if (!glfw_window)
throw ("Failed to create GLFW Window!"); throw ("Failed to create GLFW Window!");
setWidth(width); setSize(width, height);
setHeight(height);
setTitle(title); setTitle(title);
} }
@ -15,8 +14,7 @@ Window::~Window() {
} }
void Window::setWidth(int width) { void Window::setWidth(int width) {
this->width = width; setSize(width, height);
glfwSetWindowSize(glfw_window, width, height);
} }
int Window::getWidth() { int Window::getWidth() {
@ -24,14 +22,19 @@ int Window::getWidth() {
} }
void Window::setHeight(int height) { void Window::setHeight(int height) {
this->height = height; setSize(width, height);
glfwSetWindowSize(glfw_window, width, height);
} }
int Window::getHeight() { int Window::getHeight() {
return height; 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) { void Window::setTitle(const char* title) {
this->title = title; this->title = title;
glfwSetWindowTitle(glfw_window, title); glfwSetWindowTitle(glfw_window, title);
@ -41,12 +44,57 @@ const char* Window::getTitle() {
return title; return title;
} }
void Window::setFullscreen(bool fullscreen) { void Window::setFullscreen(int width, int height, int refreshRate) {
is_fullscreen = fullscreen; setFullscreen(width, height, refreshRate, glfwGetPrimaryMonitor());
if (fullscreen) }
glfwSetWindowMonitor(glfw_window, glfwGetPrimaryMonitor(), 0, 0, width, height, GLFW_DONT_CARE);
else void Window::setFullscreen(int width, int height, int refreshRate, GLFWmonitor* monitor) {
glfwSetWindowMonitor(glfw_window, NULL); 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() { bool Window::isFullscreen() {
@ -64,3 +112,15 @@ void Window::setResizable(bool resizable) {
bool Window::isResizable() { bool Window::isResizable() {
return is_resizable; 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 #pragma once
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <vector>
class Window { class Window {
private: private:
@ -8,6 +9,7 @@ private:
const char* title; const char* title;
bool is_fullscreen; bool is_fullscreen;
bool is_resizable; bool is_resizable;
int windowed_mode_x, windowed_mode_y;
public: public:
Window(int width, int height, const char* title); Window(int width, int height, const char* title);
@ -16,11 +18,22 @@ public:
int getHeight(); int getHeight();
void setWidth(int width); void setWidth(int width);
void setHeight(int height); void setHeight(int height);
void setSize(int width, int height);
const char* getTitle(); const char* getTitle();
void setTitle(const char* title); void setTitle(const char* title);
std::vector<GLFWvidmode> getVideoModes();
std::vector<GLFWvidmode> getVideoModes(GLFWmonitor* monitor);
std::vector<GLFWmonitor*> getMonitors();
bool isFullscreen(); bool isFullscreen();
bool isResizable(); 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 setResizable(bool resizable);
void beginRender();
void endRender();
bool isClosing();
}; };

View File

@ -1,28 +1,32 @@
#include <iostream> #include <iostream>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include "Window.h"
void init() {
if (!glfwInit())
throw ("Unable to init GLFW");
}
void deinit() {
glfwTerminate();
}
int main() { int main() {
GLFWwindow* window; init();
if (!glfwInit()) Window* window = new Window(640, 480, "Test Window");
return -1; window->setWindowedFullscreen(window->getMonitors()[1]);
window = glfwCreateWindow(649, 480, "Test", NULL, NULL); window->beginRender();
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window); while (!window->isClosing()) {
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window); window->endRender();
glfwPollEvents(); glfwPollEvents();
} }
glfwTerminate(); deinit();
return 0; return 0;
} }