diff --git a/headers/enduro2d/core/window.hpp b/headers/enduro2d/core/window.hpp index 024846b9..80034864 100644 --- a/headers/enduro2d/core/window.hpp +++ b/headers/enduro2d/core/window.hpp @@ -27,13 +27,16 @@ namespace e2d void restore() noexcept; void minimize() noexcept; - bool vsync() const noexcept; - bool fullscreen() const noexcept; - bool visible() const noexcept; bool focused() const noexcept; bool minimized() const noexcept; + bool vsync() const noexcept; + bool fullscreen() const noexcept; + + bool toggle_vsync(bool yesno) noexcept; + bool toggle_fullscreen(bool yesno) noexcept; + v2u real_size() const noexcept; v2u virtual_size() const noexcept; diff --git a/samples/sources/sample_00/sample_00.cpp b/samples/sources/sample_00/sample_00.cpp index a8e48cea..c7825875 100644 --- a/samples/sources/sample_00/sample_00.cpp +++ b/samples/sources/sample_00/sample_00.cpp @@ -8,11 +8,21 @@ using namespace e2d; int main() { - modules::initialize(); - the().add_sink(); + modules::initialize() + .add_sink(); - window w{{640, 480}, "Enduro2D", false, false}; - while ( !w.should_close() ) { + window& w = modules::initialize( + v2u{640, 480}, "Enduro2D", false, false); + + for ( std::size_t i = 0; i < 2; ++i ) { + w.swap_buffers(); + window::poll_events(); + std::this_thread::sleep_for(time::to_chrono(make_seconds(2))); + w.toggle_fullscreen(!w.fullscreen()); + } + + auto current_time = time::now_s(); + while ( !w.should_close() && current_time + make_seconds(5ll) < time::now_s() ) { w.swap_buffers(); window::poll_events(); } diff --git a/sources/enduro2d/core/window_impl/window_glfw.cpp b/sources/enduro2d/core/window_impl/window_glfw.cpp index 2ea72741..5bdaec23 100644 --- a/sources/enduro2d/core/window_impl/window_glfw.cpp +++ b/sources/enduro2d/core/window_impl/window_glfw.cpp @@ -122,6 +122,7 @@ namespace e2d fullscreen ? monitor : nullptr, nullptr); if ( w ) { + glfwMakeContextCurrent(w); glfwSwapInterval(vsync ? 1 : 0); } return {w, glfwDestroyWindow}; @@ -156,16 +157,6 @@ namespace e2d glfwIconifyWindow(state_->window.get()); } - bool window::vsync() const noexcept { - std::lock_guard guard(state_->mutex); - return state_->vsync; - } - - bool window::fullscreen() const noexcept { - std::lock_guard guard(state_->mutex); - return state_->fullscreen; - } - bool window::visible() const noexcept { std::lock_guard guard(state_->mutex); E2D_ASSERT(state_->window); @@ -184,6 +175,52 @@ namespace e2d return glfwGetWindowAttrib(state_->window.get(), GLFW_ICONIFIED); } + bool window::vsync() const noexcept { + std::lock_guard guard(state_->mutex); + return state_->vsync; + } + + bool window::fullscreen() const noexcept { + std::lock_guard guard(state_->mutex); + return state_->fullscreen; + } + + bool window::toggle_vsync(bool yesno) noexcept { + std::lock_guard guard(state_->mutex); + glfwMakeContextCurrent(state_->window.get()); + glfwSwapInterval(yesno ? 1 : 0); + state_->vsync = yesno; + return true; + } + + bool window::toggle_fullscreen(bool yesno) noexcept { + std::lock_guard guard(state_->mutex); + if ( state_->fullscreen == yesno ) { + return true; + } + GLFWmonitor* monitor = glfwGetPrimaryMonitor(); + if ( !monitor ) { + return false; + } + const GLFWvidmode* video_mode = glfwGetVideoMode(monitor); + if ( !video_mode ) { + return false; + } + v2i real_size = yesno + ? make_vec2(video_mode->width, video_mode->height) + : state_->virtual_size.cast_to(); + glfwSetWindowMonitor( + state_->window.get(), + yesno ? monitor : nullptr, + yesno ? 0 : math::max(0, video_mode->width / 2 - real_size.x / 2), + yesno ? 0 : math::max(0, video_mode->height / 2 - real_size.y / 2), + real_size.x, + real_size.y, + yesno ? video_mode->refreshRate : 0); + state_->fullscreen = yesno; + return true; + } + v2u window::real_size() const noexcept { std::lock_guard guard(state_->mutex); E2D_ASSERT(state_->window); diff --git a/sources/enduro2d/core/window_impl/window_none.cpp b/sources/enduro2d/core/window_impl/window_none.cpp index f0aa4e74..29a7a59b 100644 --- a/sources/enduro2d/core/window_impl/window_none.cpp +++ b/sources/enduro2d/core/window_impl/window_none.cpp @@ -54,16 +54,6 @@ namespace e2d state_->minimized = true; } - bool window::vsync() const noexcept { - std::lock_guard guard(state_->mutex); - return state_->vsync; - } - - bool window::fullscreen() const noexcept { - std::lock_guard guard(state_->mutex); - return state_->fullscreen; - } - bool window::visible() const noexcept { std::lock_guard guard(state_->mutex); return state_->visible; @@ -79,6 +69,28 @@ namespace e2d return state_->minimized; } + bool window::vsync() const noexcept { + std::lock_guard guard(state_->mutex); + return state_->vsync; + } + + bool window::fullscreen() const noexcept { + std::lock_guard guard(state_->mutex); + return state_->fullscreen; + } + + bool window::toggle_vsync(bool yesno) noexcept { + std::lock_guard guard(state_->mutex); + state_->vsync = yesno; + return true; + } + + bool window::toggle_fullscreen(bool yesno) noexcept { + std::lock_guard guard(state_->mutex); + state_->fullscreen = yesno; + return true; + } + v2u window::real_size() const noexcept { std::lock_guard guard(state_->mutex); return state_->virtual_size;