refactor: minor window module refactors
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
ee4483bfbb
commit
60ae1d52f7
4 changed files with 54 additions and 46 deletions
|
@ -12,7 +12,7 @@ public:
|
|||
Mirror()
|
||||
{
|
||||
get_window().set_properties(
|
||||
WindowProperties {
|
||||
Window::Properties {
|
||||
.title = "Mirror",
|
||||
.size = glm::uvec2(1280u, 720u),
|
||||
.vsync = true,
|
||||
|
|
|
@ -16,18 +16,23 @@ public:
|
|||
|
||||
~lWindow() override;
|
||||
|
||||
lWindow(lWindow &&) = delete;
|
||||
|
||||
lWindow(const lWindow &) = delete;
|
||||
|
||||
auto operator=(lWindow &&) -> lWindow & = delete;
|
||||
|
||||
auto operator=(const lWindow &) -> lWindow & = delete;
|
||||
|
||||
void poll_events() override;
|
||||
|
||||
void on_event(const Event &event) override;
|
||||
|
||||
void set_properties(
|
||||
const WindowProperties &properties,
|
||||
bool overrideVisibility = false
|
||||
) override;
|
||||
void set_properties(const Properties &properties, bool overrideVisibility = false) override;
|
||||
|
||||
void set_title(const std::string &title) override;
|
||||
|
||||
void set_size(const glm::uvec2 &size, bool additive = false) override;
|
||||
void set_size(const glm::uvec2 &size) override;
|
||||
|
||||
void set_v_sync(bool vsync, bool toggle = false) override;
|
||||
|
||||
|
@ -39,13 +44,17 @@ public:
|
|||
}
|
||||
|
||||
private:
|
||||
void on_window_resize(const WindowResizedEvent &event);
|
||||
|
||||
void bind_glfw_events();
|
||||
|
||||
GLFWwindow *m_handle { nullptr };
|
||||
|
||||
std::function<void(Event &)> m_event_callback;
|
||||
|
||||
void on_window_resize(const WindowResizedEvent &event);
|
||||
Properties m_properties {};
|
||||
|
||||
void bind_glfw_events();
|
||||
bool b_Closed {};
|
||||
};
|
||||
|
||||
} // namespace lt
|
||||
|
|
|
@ -6,41 +6,42 @@ namespace lt {
|
|||
|
||||
class Event;
|
||||
|
||||
struct WindowProperties
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
struct Properties
|
||||
{
|
||||
std::string title;
|
||||
|
||||
glm::uvec2 size;
|
||||
|
||||
bool vsync, visible;
|
||||
};
|
||||
};
|
||||
|
||||
class Window
|
||||
{
|
||||
public:
|
||||
static Scope<Window> create(const std::function<void(Event &)> &callback);
|
||||
|
||||
Window() = default;
|
||||
|
||||
virtual ~Window();
|
||||
|
||||
Window(Window &&) = delete;
|
||||
|
||||
Window(const Window &) = delete;
|
||||
|
||||
Window &operator=(const Window &) = delete;
|
||||
auto operator=(Window &&) -> Window & = delete;
|
||||
|
||||
virtual ~Window();
|
||||
auto operator=(const Window &) -> Window & = delete;
|
||||
|
||||
virtual void poll_events() = 0;
|
||||
|
||||
virtual void on_event(const Event &event) = 0;
|
||||
|
||||
virtual void set_properties(
|
||||
const WindowProperties &properties,
|
||||
bool affectVisibility = false
|
||||
) = 0;
|
||||
virtual void set_properties(const Properties &properties, bool affectVisibility = false) = 0;
|
||||
|
||||
virtual void set_title(const std::string &title) = 0;
|
||||
|
||||
/** pass 0 for width or height for single dimension resizing */
|
||||
virtual void set_size(const glm::uvec2 &size, bool additive = false) = 0;
|
||||
virtual void set_size(const glm::uvec2 &size) = 0;
|
||||
|
||||
void close()
|
||||
{
|
||||
|
@ -51,7 +52,7 @@ public:
|
|||
|
||||
virtual void set_visibility(bool visible, bool toggle = false) = 0;
|
||||
|
||||
[[nodiscard]] auto get_properties() const -> const WindowProperties &
|
||||
[[nodiscard]] auto get_properties() const -> const Properties &
|
||||
{
|
||||
return m_properties;
|
||||
}
|
||||
|
@ -84,7 +85,13 @@ public:
|
|||
virtual auto get_handle() -> void * = 0;
|
||||
|
||||
protected:
|
||||
WindowProperties m_properties {};
|
||||
auto get_properties_handle() -> Properties &
|
||||
{
|
||||
return m_properties;
|
||||
}
|
||||
|
||||
private:
|
||||
Properties m_properties {};
|
||||
|
||||
bool b_Closed {};
|
||||
};
|
||||
|
|
|
@ -8,8 +8,7 @@
|
|||
|
||||
namespace lt {
|
||||
|
||||
Window::~Window()
|
||||
= default;
|
||||
Window::~Window() = default;
|
||||
|
||||
auto Window::create(const std::function<void(Event &)> &callback) -> Scope<Window>
|
||||
{
|
||||
|
@ -17,19 +16,18 @@ auto Window::create(const std::function<void(Event &)> &callback) -> Scope<Windo
|
|||
}
|
||||
|
||||
lWindow::lWindow(std::function<void(Event &)> callback)
|
||||
: m_handle(glfwCreateWindow(1u, 1u, "", nullptr, nullptr)), m_event_callback(std::move(std::move(callback)))
|
||||
: m_handle(glfwCreateWindow(1u, 1u, "", nullptr, nullptr))
|
||||
, m_event_callback(std::move(std::move(callback)))
|
||||
{
|
||||
// init glfw
|
||||
ensure(glfwInit(), "lWindow::lWindow: failed to initialize 'glfw'");
|
||||
ensure(glfwInit(), "Failed to initialize 'glfw'");
|
||||
|
||||
// create window
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
|
||||
|
||||
|
||||
ensure(m_handle, "lWindow::lWindow: failed to create 'GLFWwindow'");
|
||||
ensure(m_handle, "Failed to create 'GLFWwindow'");
|
||||
|
||||
glfwSetWindowUserPointer(m_handle, &m_event_callback);
|
||||
bind_glfw_events();
|
||||
|
@ -56,6 +54,8 @@ void lWindow::on_event(const Event &event)
|
|||
case EventType::WindowResized:
|
||||
on_window_resize(dynamic_cast<const WindowResizedEvent &>(event));
|
||||
break;
|
||||
|
||||
default:
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -64,8 +64,7 @@ void lWindow::on_window_resize(const WindowResizedEvent &event)
|
|||
m_properties.size = event.get_size();
|
||||
}
|
||||
|
||||
void lWindow::
|
||||
set_properties(const WindowProperties &properties, bool overrideVisibility /* = false */)
|
||||
void lWindow::set_properties(const Properties &properties, bool overrideVisibility /* = false */)
|
||||
{
|
||||
// save the visibility status and re-assign if 'overrideVisibility' is false
|
||||
auto visible = overrideVisibility ? properties.visible : m_properties.visible;
|
||||
|
@ -86,17 +85,10 @@ void lWindow::set_title(const std::string &title)
|
|||
glfwSetWindowTitle(m_handle, title.c_str());
|
||||
}
|
||||
|
||||
void lWindow::set_size(const glm::uvec2 &size, bool additive /* = false */)
|
||||
void lWindow::set_size(const glm::uvec2 &size)
|
||||
{
|
||||
m_properties.size.x = size.x == 0u ? m_properties.size.x :
|
||||
additive ? m_properties.size.x + size.x :
|
||||
size.x;
|
||||
m_properties.size.y = size.y == 0u ? m_properties.size.y :
|
||||
additive ? m_properties.size.y + size.y :
|
||||
size.y;
|
||||
|
||||
|
||||
glfwSetWindowSize(m_handle, size.x, size.y);
|
||||
m_properties.size = size;
|
||||
glfwSetWindowSize(m_handle, static_cast<int>(size.x), static_cast<int>(size.y));
|
||||
}
|
||||
|
||||
void lWindow::set_v_sync(bool vsync, bool toggle /* = false */)
|
||||
|
|
Loading…
Add table
Reference in a new issue