Merge pull request #25 from clasher113/main

Window fullscreen toggle & saving size
This commit is contained in:
MihailRis 2023-11-25 23:44:35 +03:00 committed by GitHub
commit 7a39ba6b2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 2 deletions

View File

@ -68,6 +68,9 @@ void Engine::updateHotkeys() {
png::write_image(filename.string(), image.get());
std::cout << "saved screenshot as " << filename << std::endl;
}
if (Events::jpressed(keycode::F11)) {
Window::toggleFullscreen();
}
}
void Engine::mainloop() {

View File

@ -12,6 +12,7 @@ using std::string;
toml::Wrapper create_wrapper(EngineSettings& settings) {
toml::Wrapper wrapper;
toml::Section& display = wrapper.add("display");
display.add("fullscreen", &settings.display.fullscreen);
display.add("width", &settings.display.width);
display.add("height", &settings.display.height);
display.add("samples", &settings.display.samples);

View File

@ -6,6 +6,8 @@
#include "typedefs.h"
struct DisplaySettings {
/* Is window in full screen mode */
bool fullscreen = false;
/* Window width (pixels) */
int width = 1280;
/* Window height (pixels) */

View File

@ -9,6 +9,7 @@
using glm::vec4;
GLFWwindow* Window::window = nullptr;
DisplaySettings* Window::settings = nullptr;
std::stack<vec4> Window::scissorStack;
vec4 Window::scissorArea;
uint Window::width = 0;
@ -61,6 +62,10 @@ void window_size_callback(GLFWwindow*, int width, int height) {
glViewport(0, 0, width, height);
Window::width = width;
Window::height = height;
if (!Window::isFullscreen()) {
Window::getSettings()->width = width;
Window::getSettings()->height = height;
}
Window::resetScissor();
}
@ -68,8 +73,8 @@ void character_callback(GLFWwindow* window, unsigned int codepoint){
Events::codepoints.push_back(codepoint);
}
int Window::initialize(DisplaySettings& settings){
Window::settings = &settings;
Window::width = settings.width;
Window::height = settings.height;
@ -111,6 +116,15 @@ int Window::initialize(DisplaySettings& settings){
glfwSetCharCallback(window, character_callback);
glfwSetScrollCallback(window, scroll_callback);
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
if (settings.fullscreen) {
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
}
else {
tryToMaximize(window, monitor);
}
glfwSwapInterval(settings.swapInterval);
return 0;
}
@ -123,7 +137,6 @@ void Window::setBgColor(glm::vec3 color) {
glClearColor(color.r, color.g, color.b, 1.0f);
}
void Window::viewport(int x, int y, int width, int height){
glViewport(x, y, width, height);
}
@ -196,6 +209,32 @@ void Window::swapInterval(int interval){
glfwSwapInterval(interval);
}
void Window::toggleFullscreen(){
settings->fullscreen = !settings->fullscreen;
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
if (Events::_cursor_locked) Events::toggleCursor();
if (settings->fullscreen) {
glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, GLFW_DONT_CARE);
}
else {
glfwSetWindowMonitor(window, nullptr, 0, 0, settings->width, settings->height, GLFW_DONT_CARE);
tryToMaximize(window, monitor);
}
double xPos, yPos;
glfwGetCursorPos(window, &xPos, &yPos);
Events::x = xPos;
Events::y = yPos;
}
bool Window::isFullscreen() {
return settings->fullscreen;
}
void Window::swapBuffers(){
glfwSwapBuffers(window);
Window::resetScissor();
@ -205,8 +244,30 @@ double Window::time() {
return glfwGetTime();
}
DisplaySettings* Window::getSettings() {
return settings;
}
ImageData* Window::takeScreenshot() {
ubyte* data = new ubyte[width * height * 3];
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
return new ImageData(ImageFormat::rgb888, width, height, data);
}
bool Window::tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor) {
glm::ivec4 windowFrame(0);
glm::ivec4 workArea(0);
glfwGetWindowFrameSize(window, &windowFrame.x, &windowFrame.y, &windowFrame.z, &windowFrame.w);
glfwGetMonitorWorkarea(monitor, &workArea.x, &workArea.y, &workArea.z, &workArea.w);
if (Window::width > workArea.z) Window::width = workArea.z;
if (Window::height > workArea.w) Window::height = workArea.w;
if (Window::width >= workArea.z - (windowFrame.x + windowFrame.z) &&
Window::height >= workArea.w - (windowFrame.y + windowFrame.w)) {
glfwMaximizeWindow(window);
return true;
}
glfwSetWindowSize(window, Window::width, Window::height);
glfwSetWindowPos(window, workArea.x + (workArea.z - Window::width) / 2,
workArea.y + (workArea.w - Window::height) / 2 + windowFrame.y / 2);
return false;
}

View File

@ -11,11 +11,16 @@
class GLFWwindow;
class ImageData;
struct DisplaySettings;
struct GLFWmonitor;
class Window {
static GLFWwindow* window;
static DisplaySettings* settings;
static std::stack<glm::vec4> scissorStack;
static glm::vec4 scissorArea;
static bool tryToMaximize(GLFWwindow* window, GLFWmonitor* monitor);
public:
static uint width;
static uint height;
@ -28,6 +33,8 @@ public:
static void setShouldClose(bool flag);
static void swapBuffers();
static void swapInterval(int interval);
static void toggleFullscreen();
static bool isFullscreen();
static void pushScissor(glm::vec4 area);
static void popScissor();
@ -36,6 +43,7 @@ public:
static void clear();
static void setBgColor(glm::vec3 color);
static double time();
static DisplaySettings* getSettings();
static glm::vec2 size() {
return glm::vec2(width, height);