bind_context window function. move vsync option to swap_buffers.

This commit is contained in:
2018-09-28 23:13:57 +07:00
parent 383f17dac9
commit 041b067bde
6 changed files with 32 additions and 56 deletions

View File

@@ -86,6 +86,7 @@ if(APPLE)
find_library(Foundation Foundation) find_library(Foundation Foundation)
endif(APPLE) endif(APPLE)
set(GLFW_INSTALL OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

View File

@@ -33,7 +33,7 @@ namespace e2d
}; };
using event_listener_uptr = std::unique_ptr<event_listener>; using event_listener_uptr = std::unique_ptr<event_listener>;
public: public:
window(const v2u& size, str_view title, bool vsync, bool fullscreen); window(const v2u& size, str_view title, bool fullscreen);
~window() noexcept; ~window() noexcept;
void hide() noexcept; void hide() noexcept;
@@ -45,10 +45,7 @@ namespace e2d
bool focused() const noexcept; bool focused() const noexcept;
bool minimized() const noexcept; bool minimized() const noexcept;
bool vsync() const noexcept;
bool fullscreen() const noexcept; bool fullscreen() const noexcept;
bool toggle_vsync(bool yesno) noexcept;
bool toggle_fullscreen(bool yesno) noexcept; bool toggle_fullscreen(bool yesno) noexcept;
void hide_cursor() noexcept; void hide_cursor() noexcept;
@@ -65,7 +62,8 @@ namespace e2d
bool should_close() const noexcept; bool should_close() const noexcept;
void set_should_close(bool yesno) noexcept; void set_should_close(bool yesno) noexcept;
void swap_buffers() noexcept; void bind_context() noexcept;
void swap_buffers(bool vsync) noexcept;
static bool frame_tick() noexcept; static bool frame_tick() noexcept;
template < typename T, typename... Args > template < typename T, typename... Args >

View File

@@ -10,8 +10,7 @@ using namespace e2d;
int e2d_main() { int e2d_main() {
input& i = modules::initialize<input>(); input& i = modules::initialize<input>();
debug& d = modules::initialize<debug>(); debug& d = modules::initialize<debug>();
window& w = modules::initialize<window>( window& w = modules::initialize<window>(v2u{640, 480}, "Enduro2D", false);
v2u{640, 480}, "Enduro2D", true, false);
d.add_sink<debug_console_sink>(); d.add_sink<debug_console_sink>();
w.register_event_listener<window_input_source>(i); w.register_event_listener<window_input_source>(i);
@@ -24,7 +23,7 @@ int e2d_main() {
const keyboard& k = i.keyboard(); const keyboard& k = i.keyboard();
while ( !w.should_close() && !k.is_key_just_released(keyboard_key::escape) ) { while ( !w.should_close() && !k.is_key_just_released(keyboard_key::escape) ) {
i.frame_tick(); i.frame_tick();
w.swap_buffers(); w.swap_buffers(true);
window::frame_tick(); window::frame_tick();
} }
return 0; return 0;

View File

@@ -10,8 +10,7 @@ using namespace e2d;
int e2d_main() { int e2d_main() {
input& i = modules::initialize<input>(); input& i = modules::initialize<input>();
debug& d = modules::initialize<debug>(); debug& d = modules::initialize<debug>();
window& w = modules::initialize<window>( window& w = modules::initialize<window>(v2u{640, 480}, "Enduro2D", false);
v2u{640, 480}, "Enduro2D", true, false);
d.add_sink<debug_console_sink>(); d.add_sink<debug_console_sink>();
w.register_event_listener<window_input_source>(i); w.register_event_listener<window_input_source>(i);
@@ -24,7 +23,7 @@ int e2d_main() {
const keyboard& k = i.keyboard(); const keyboard& k = i.keyboard();
while ( !w.should_close() && !k.is_key_just_released(keyboard_key::escape) ) { while ( !w.should_close() && !k.is_key_just_released(keyboard_key::escape) ) {
i.frame_tick(); i.frame_tick();
w.swap_buffers(); w.swap_buffers(true);
window::frame_tick(); window::frame_tick();
} }
return 0; return 0;

View File

@@ -257,17 +257,15 @@ namespace e2d
window_uptr window; window_uptr window;
v2u virtual_size; v2u virtual_size;
str title; str title;
bool vsync = false;
bool fullscreen = false; bool fullscreen = false;
bool cursor_hidden = false; bool cursor_hidden = false;
char _pad[5]; char _pad[6];
public: public:
state(const v2u& size, str_view title, bool vsync, bool fullscreen) state(const v2u& size, str_view title, bool fullscreen)
: shared_state(glfw_state::get_shared_state()) : shared_state(glfw_state::get_shared_state())
, window(open_window_(size, make_utf8(title), vsync, fullscreen)) , window(open_window_(size, make_utf8(title), fullscreen))
, virtual_size(size) , virtual_size(size)
, title(title) , title(title)
, vsync(vsync)
, fullscreen(fullscreen) , fullscreen(fullscreen)
, cursor_hidden(false) , cursor_hidden(false)
{ {
@@ -309,7 +307,7 @@ namespace e2d
} }
private: private:
static window_uptr open_window_( static window_uptr open_window_(
const v2u& virtual_size, const str& title, bool vsync, bool fullscreen) noexcept const v2u& virtual_size, const str& title, bool fullscreen) noexcept
{ {
GLFWmonitor* monitor = glfwGetPrimaryMonitor(); GLFWmonitor* monitor = glfwGetPrimaryMonitor();
if ( !monitor ) { if ( !monitor ) {
@@ -333,10 +331,7 @@ namespace e2d
title.c_str(), title.c_str(),
fullscreen ? monitor : nullptr, fullscreen ? monitor : nullptr,
nullptr); nullptr);
if ( w ) { glfwMakeContextCurrent(w);
glfwMakeContextCurrent(w);
glfwSwapInterval(vsync ? 1 : 0);
}
return {w, glfwDestroyWindow}; return {w, glfwDestroyWindow};
} }
@@ -421,8 +416,8 @@ namespace e2d
// class window // class window
// //
window::window(const v2u& size, str_view title, bool vsync, bool fullscreen) window::window(const v2u& size, str_view title, bool fullscreen)
: state_(new state(size, title, vsync, fullscreen)) {} : state_(new state(size, title, fullscreen)) {}
window::~window() noexcept = default; window::~window() noexcept = default;
void window::hide() noexcept { void window::hide() noexcept {
@@ -467,25 +462,11 @@ namespace e2d
return GLFW_TRUE == glfwGetWindowAttrib(state_->window.get(), GLFW_ICONIFIED); return GLFW_TRUE == glfwGetWindowAttrib(state_->window.get(), GLFW_ICONIFIED);
} }
bool window::vsync() const noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
return state_->vsync;
}
bool window::fullscreen() const noexcept { bool window::fullscreen() const noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex); std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
return state_->fullscreen; return state_->fullscreen;
} }
bool window::toggle_vsync(bool yesno) noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window);
glfwMakeContextCurrent(state_->window.get());
glfwSwapInterval(yesno ? 1 : 0);
state_->vsync = yesno;
return true;
}
bool window::toggle_fullscreen(bool yesno) noexcept { bool window::toggle_fullscreen(bool yesno) noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex); std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
if ( state_->fullscreen == yesno ) { if ( state_->fullscreen == yesno ) {
@@ -579,9 +560,16 @@ namespace e2d
glfwSetWindowShouldClose(state_->window.get(), yesno ? GLFW_TRUE : GLFW_FALSE); glfwSetWindowShouldClose(state_->window.get(), yesno ? GLFW_TRUE : GLFW_FALSE);
} }
void window::swap_buffers() noexcept { void window::bind_context() noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex); std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window); E2D_ASSERT(state_->window);
glfwMakeContextCurrent(state_->window.get());
}
void window::swap_buffers(bool vsync) noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
E2D_ASSERT(state_->window);
glfwSwapInterval(vsync ? 1 : 0);
glfwSwapBuffers(state_->window.get()); glfwSwapBuffers(state_->window.get());
} }

View File

@@ -18,19 +18,17 @@ namespace e2d
std::recursive_mutex rmutex; std::recursive_mutex rmutex;
v2u virtual_size; v2u virtual_size;
str title; str title;
bool vsync = false;
bool fullscreen = false; bool fullscreen = false;
bool cursor_hidden = false; bool cursor_hidden = false;
bool should_close = false; bool should_close = false;
bool visible = true; bool visible = true;
bool focused = true; bool focused = true;
bool minimized = false; bool minimized = false;
char _pad[1]; char _pad[2];
public: public:
state(const v2u& size, str_view title, bool vsync, bool fullscreen) state(const v2u& size, str_view title, bool fullscreen)
: virtual_size(size) : virtual_size(size)
, title(make_utf8(title)) , title(make_utf8(title))
, vsync(vsync)
, fullscreen(fullscreen) {} , fullscreen(fullscreen) {}
~state() noexcept = default; ~state() noexcept = default;
@@ -45,8 +43,8 @@ namespace e2d
} }
}; };
window::window(const v2u& size, str_view title, bool vsync, bool fullscreen) window::window(const v2u& size, str_view title, bool fullscreen)
: state_(new state(size, title, vsync, fullscreen)) {} : state_(new state(size, title, fullscreen)) {}
window::~window() noexcept = default; window::~window() noexcept = default;
void window::hide() noexcept { void window::hide() noexcept {
@@ -98,22 +96,11 @@ namespace e2d
return state_->minimized; return state_->minimized;
} }
bool window::vsync() const noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
return state_->vsync;
}
bool window::fullscreen() const noexcept { bool window::fullscreen() const noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex); std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
return state_->fullscreen; return state_->fullscreen;
} }
bool window::toggle_vsync(bool yesno) noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
state_->vsync = yesno;
return true;
}
bool window::toggle_fullscreen(bool yesno) noexcept { bool window::toggle_fullscreen(bool yesno) noexcept {
std::lock_guard<std::recursive_mutex> guard(state_->rmutex); std::lock_guard<std::recursive_mutex> guard(state_->rmutex);
state_->fullscreen = yesno; state_->fullscreen = yesno;
@@ -170,7 +157,11 @@ namespace e2d
state_->should_close = yesno; state_->should_close = yesno;
} }
void window::swap_buffers() noexcept { void window::bind_context() noexcept {
}
void window::swap_buffers(bool vsync) noexcept {
E2D_UNUSED(vsync);
} }
bool window::frame_tick() noexcept { bool window::frame_tick() noexcept {