refactor: minor window module refactors
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
light7734 2025-07-15 15:11:49 +03:30
parent ee4483bfbb
commit 60ae1d52f7
Signed by: light7734
GPG key ID: 8C30176798F1A6BA
4 changed files with 54 additions and 46 deletions

View file

@ -12,7 +12,7 @@ public:
Mirror() Mirror()
{ {
get_window().set_properties( get_window().set_properties(
WindowProperties { Window::Properties {
.title = "Mirror", .title = "Mirror",
.size = glm::uvec2(1280u, 720u), .size = glm::uvec2(1280u, 720u),
.vsync = true, .vsync = true,

View file

@ -16,18 +16,23 @@ public:
~lWindow() override; ~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 poll_events() override;
void on_event(const Event &event) override; void on_event(const Event &event) override;
void set_properties( void set_properties(const Properties &properties, bool overrideVisibility = false) override;
const WindowProperties &properties,
bool overrideVisibility = false
) override;
void set_title(const std::string &title) 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; void set_v_sync(bool vsync, bool toggle = false) override;
@ -39,13 +44,17 @@ public:
} }
private: private:
void on_window_resize(const WindowResizedEvent &event);
void bind_glfw_events();
GLFWwindow *m_handle { nullptr }; GLFWwindow *m_handle { nullptr };
std::function<void(Event &)> m_event_callback; 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 } // namespace lt

View file

@ -6,7 +6,11 @@ namespace lt {
class Event; class Event;
struct WindowProperties
class Window
{
public:
struct Properties
{ {
std::string title; std::string title;
@ -15,32 +19,29 @@ struct WindowProperties
bool vsync, visible; bool vsync, visible;
}; };
class Window
{
public:
static Scope<Window> create(const std::function<void(Event &)> &callback); static Scope<Window> create(const std::function<void(Event &)> &callback);
Window() = default; Window() = default;
virtual ~Window();
Window(Window &&) = delete;
Window(const 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 poll_events() = 0;
virtual void on_event(const Event &event) = 0; virtual void on_event(const Event &event) = 0;
virtual void set_properties( virtual void set_properties(const Properties &properties, bool affectVisibility = false) = 0;
const WindowProperties &properties,
bool affectVisibility = false
) = 0;
virtual void set_title(const std::string &title) = 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) = 0;
virtual void set_size(const glm::uvec2 &size, bool additive = false) = 0;
void close() void close()
{ {
@ -51,7 +52,7 @@ public:
virtual void set_visibility(bool visible, bool toggle = false) = 0; 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; return m_properties;
} }
@ -84,7 +85,13 @@ public:
virtual auto get_handle() -> void * = 0; virtual auto get_handle() -> void * = 0;
protected: protected:
WindowProperties m_properties {}; auto get_properties_handle() -> Properties &
{
return m_properties;
}
private:
Properties m_properties {};
bool b_Closed {}; bool b_Closed {};
}; };

View file

@ -8,8 +8,7 @@
namespace lt { namespace lt {
Window::~Window() Window::~Window() = default;
= default;
auto Window::create(const std::function<void(Event &)> &callback) -> Scope<Window> 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) 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(), "Failed to initialize 'glfw'");
ensure(glfwInit(), "lWindow::lWindow: failed to initialize 'glfw'");
// create window
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); 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); glfwSetWindowUserPointer(m_handle, &m_event_callback);
bind_glfw_events(); bind_glfw_events();
@ -56,6 +54,8 @@ void lWindow::on_event(const Event &event)
case EventType::WindowResized: case EventType::WindowResized:
on_window_resize(dynamic_cast<const WindowResizedEvent &>(event)); on_window_resize(dynamic_cast<const WindowResizedEvent &>(event));
break; break;
default:
} }
} }
@ -64,8 +64,7 @@ void lWindow::on_window_resize(const WindowResizedEvent &event)
m_properties.size = event.get_size(); m_properties.size = event.get_size();
} }
void lWindow:: void lWindow::set_properties(const Properties &properties, bool overrideVisibility /* = false */)
set_properties(const WindowProperties &properties, bool overrideVisibility /* = false */)
{ {
// save the visibility status and re-assign if 'overrideVisibility' is false // save the visibility status and re-assign if 'overrideVisibility' is false
auto visible = overrideVisibility ? properties.visible : m_properties.visible; 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()); 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 : m_properties.size = size;
additive ? m_properties.size.x + size.x : glfwSetWindowSize(m_handle, static_cast<int>(size.x), static_cast<int>(size.y));
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);
} }
void lWindow::set_v_sync(bool vsync, bool toggle /* = false */) void lWindow::set_v_sync(bool vsync, bool toggle /* = false */)