window module wip

This commit is contained in:
2018-09-20 05:27:30 +07:00
parent 291e264df4
commit 231864b587
4 changed files with 89 additions and 27 deletions

View File

@@ -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;

View File

@@ -8,11 +8,21 @@
using namespace e2d;
int main() {
modules::initialize<debug>();
the<debug>().add_sink<debug_console_sink>();
modules::initialize<debug>()
.add_sink<debug_console_sink>();
window w{{640, 480}, "Enduro2D", false, false};
while ( !w.should_close() ) {
window& w = modules::initialize<window>(
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();
}

View File

@@ -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<std::mutex> guard(state_->mutex);
return state_->vsync;
}
bool window::fullscreen() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->fullscreen;
}
bool window::visible() const noexcept {
std::lock_guard<std::mutex> 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<std::mutex> guard(state_->mutex);
return state_->vsync;
}
bool window::fullscreen() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->fullscreen;
}
bool window::toggle_vsync(bool yesno) noexcept {
std::lock_guard<std::mutex> 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<std::mutex> 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<i32>();
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<std::mutex> guard(state_->mutex);
E2D_ASSERT(state_->window);

View File

@@ -54,16 +54,6 @@ namespace e2d
state_->minimized = true;
}
bool window::vsync() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->vsync;
}
bool window::fullscreen() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->fullscreen;
}
bool window::visible() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->visible;
@@ -79,6 +69,28 @@ namespace e2d
return state_->minimized;
}
bool window::vsync() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->vsync;
}
bool window::fullscreen() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->fullscreen;
}
bool window::toggle_vsync(bool yesno) noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
state_->vsync = yesno;
return true;
}
bool window::toggle_fullscreen(bool yesno) noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
state_->fullscreen = yesno;
return true;
}
v2u window::real_size() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->virtual_size;