From 2912ebb91c3ed8c60e50fb3dc2055e6e2634e69b Mon Sep 17 00:00:00 2001 From: admin Date: Fri, 13 Feb 2026 13:53:47 +0100 Subject: [PATCH] feat: on frame listener --- include/window.h | 8 ++++++++ include/window/wayland.h | 3 +++ src/main.cpp | 14 +++++++++----- src/window.cpp | 4 ++++ src/window/wayland.cpp | 23 ++++++++++++++++------- 5 files changed, 40 insertions(+), 12 deletions(-) diff --git a/include/window.h b/include/window.h index 0cefaa9..e5813fa 100644 --- a/include/window.h +++ b/include/window.h @@ -3,6 +3,7 @@ #define H_WINDOW_ #include +#include #include #include @@ -10,6 +11,8 @@ #define UNUSED(x) (void)(x) +typedef std::function IFrameListener; + class WindowImpl { public: WindowImpl() = default; @@ -19,6 +22,7 @@ public: WindowImpl(WindowImpl&&) = delete; protected: virtual bool Dispatch() = 0; + virtual void OnFrame(IFrameListener fn) = 0; virtual size_t GetWidth() const = 0; virtual size_t GetHeight() const = 0; @@ -39,6 +43,10 @@ public: size_t GetWidth() const; size_t GetHeight() const; +public: + void Run(); + void OnFrame(IFrameListener fn); + private: WindowImpl* m_impl; diff --git a/include/window/wayland.h b/include/window/wayland.h index 8b5e207..0ab69b1 100644 --- a/include/window/wayland.h +++ b/include/window/wayland.h @@ -10,6 +10,7 @@ public: public: bool Dispatch() override; + void OnFrame(IFrameListener fn) override; size_t GetWidth() const override; size_t GetHeight() const override; @@ -45,5 +46,7 @@ private: bool m_running = false; size_t m_width, m_height; + + IFrameListener m_on_frame; }; diff --git a/src/main.cpp b/src/main.cpp index e2220ac..415bc87 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,18 +5,22 @@ #define UNUSED(x) (void)(x) +static Window window(720, 480); + +void HandleFrame() { + printf("[APP]: width = %zu, height = %zu\n", window.GetWidth(), window.GetHeight()); +} + int main(int argc, char **argv) { UNUSED(argc); UNUSED(argv); Renderer::GetInstance(); - Window window(720, 480); + window.OnFrame(HandleFrame); - printf("[APP]: starting window"); - while (window.Running()) { - printf("[APP]: width = %zu, height = %zu\n", window.GetWidth(), window.GetHeight()); - } + printf("[APP]: starting window\n"); + window.Run(); return 0; } diff --git a/src/window.cpp b/src/window.cpp index 615ac1c..96ca78a 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -22,3 +22,7 @@ size_t Window::GetHeight() const { return m_impl->GetHeight(); } bool Window::Running() const { return m_running && m_impl->Dispatch(); } +void Window::OnFrame(IFrameListener fn) { m_impl->OnFrame(fn); } + +void Window::Run() { printf("[DEBUG(Window)]: Run() starts\n"); m_running = true; while(Running()) {}; } + diff --git a/src/window/wayland.cpp b/src/window/wayland.cpp index 4b73b81..1ee5612 100644 --- a/src/window/wayland.cpp +++ b/src/window/wayland.cpp @@ -1,6 +1,8 @@ #include +#include #include "state/wayland.h" +#include "window.h" #include "window/wayland.h" #include "renderer.h" @@ -88,19 +90,24 @@ WaylandWindowImpl::WaylandWindowImpl(size_t width, size_t height) m_toplevel_listener.close = xtoplevel_handle_close; m_toplevel_listener.wm_capabilities = xtoplevel_handle_wm_capabilities; xdg_toplevel_add_listener(m_toplevel, &m_toplevel_listener, this); - - wl_surface_commit(m_wsurface); - - m_running = true; } WaylandWindowImpl::~WaylandWindowImpl() { m_running = false; } -bool WaylandWindowImpl::Dispatch() { - return m_running && - wl_display_dispatch(WaylandState::GetInstance()->m_display) != -1; +void WaylandWindowImpl::OnFrame(IFrameListener fn) { + m_on_frame = fn; } +bool WaylandWindowImpl::Dispatch() { + wl_surface_commit(m_wsurface); + + m_running = true; + + auto display = WaylandState::GetInstance()->m_display; + + // wl_display_dispatch_pending(display); + return wl_display_dispatch(display) != -1; } + size_t WaylandWindowImpl::GetWidth() const { return m_width; } @@ -125,6 +132,8 @@ void WaylandWindowImpl::handle_frame_callback(void *data, wl_callback_add_listener(frame_callback, &window->m_frame_listener, window); + if (window->m_on_frame) window->m_on_frame(); + GLint angle_loc = glGetUniformLocation(window->m_program, "angle"); static float angle = 0.0f;