window module wip

This commit is contained in:
2018-09-20 03:41:40 +07:00
parent a8f73821ca
commit b1fa2158d3
4 changed files with 94 additions and 14 deletions

View File

@@ -19,7 +19,7 @@ namespace e2d
class window final : public module<window> {
public:
window(const v2u& size, str_view title, bool fullscreen);
window(const v2u& size, str_view title, bool vsync, bool fullscreen);
~window() noexcept;
void hide() noexcept;
@@ -27,6 +27,13 @@ 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;
v2u real_size() const noexcept;
v2u virtual_size() const noexcept;

View File

@@ -11,7 +11,7 @@ int main() {
modules::initialize<debug>();
the<debug>().add_sink<debug_console_sink>();
window w{{640, 480}, "Enduro2D", false};
window w{{640, 480}, "Enduro2D", false, false};
while ( !w.should_close() ) {
w.swap_buffers();
window::poll_events();

View File

@@ -76,13 +76,17 @@ namespace e2d
window_uptr window;
v2u virtual_size;
str title;
bool vsync = false;
bool fullscreen = false;
std::mutex mutex;
public:
state(const v2u& size, str_view title, bool fullscreen)
state(const v2u& size, str_view title, bool vsync, bool fullscreen)
: shared_state(glfw_state::get_shared_state())
, window(open_window_(size, make_utf8(title), fullscreen))
, window(open_window_(size, make_utf8(title), vsync, fullscreen))
, virtual_size(size)
, title(title)
, vsync(vsync)
, fullscreen(fullscreen)
{
if ( !window ) {
throw bad_window_operation();
@@ -95,7 +99,7 @@ namespace e2d
}
private:
static window_uptr open_window_(
const v2u& virtual_size, const str& title, bool fullscreen) noexcept
const v2u& virtual_size, const str& title, bool vsync, bool fullscreen) noexcept
{
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
if ( !monitor ) {
@@ -117,12 +121,15 @@ namespace e2d
title.c_str(),
fullscreen ? monitor : nullptr,
nullptr);
if ( w ) {
glfwSwapInterval(vsync ? 1 : 0);
}
return {w, glfwDestroyWindow};
}
};
window::window(const v2u& size, str_view title, bool fullscreen)
: state_(new state(size, title, fullscreen)) {}
window::window(const v2u& size, str_view title, bool vsync, bool fullscreen)
: state_(new state(size, title, vsync, fullscreen)) {}
window::~window() noexcept = default;
void window::hide() noexcept {
@@ -149,6 +156,34 @@ 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);
return glfwGetWindowAttrib(state_->window.get(), GLFW_VISIBLE);
}
bool window::focused() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
E2D_ASSERT(state_->window);
return glfwGetWindowAttrib(state_->window.get(), GLFW_FOCUSED);
}
bool window::minimized() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
E2D_ASSERT(state_->window);
return glfwGetWindowAttrib(state_->window.get(), GLFW_ICONIFIED);
}
v2u window::real_size() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
E2D_ASSERT(state_->window);

View File

@@ -12,43 +12,81 @@ namespace e2d
{
class window::state final : private e2d::noncopyable {
public:
v2u size;
v2u virtual_size;
str title;
bool vsync = false;
bool fullscreen = false;
bool should_close = false;
bool visible = true;
bool focused = true;
bool minimized = false;
std::mutex mutex;
public:
state(const v2u& size, str_view title, bool fullscreen)
: size(size)
state(const v2u& size, str_view title, bool vsync, bool fullscreen)
: virtual_size(size)
, title(make_utf8(title))
, vsync(vsync)
, fullscreen(fullscreen) {}
~state() noexcept = default;
};
window::window(const v2u& size, str_view title, bool fullscreen)
: state_(new state(size, title, fullscreen)) {}
window::window(const v2u& size, str_view title, bool vsync, bool fullscreen)
: state_(new state(size, title, vsync, fullscreen)) {}
window::~window() noexcept = default;
void window::hide() noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
state_->visible = false;
}
void window::show() noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
state_->visible = true;
}
void window::restore() noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
state_->minimized = false;
}
void window::minimize() noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
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;
}
bool window::focused() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->focused;
}
bool window::minimized() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->minimized;
}
v2u window::real_size() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->size;
return state_->virtual_size;
}
v2u window::virtual_size() const noexcept {
std::lock_guard<std::mutex> guard(state_->mutex);
return state_->size;
return state_->virtual_size;
}
const str& window::title() const noexcept {