feat: on frame listener

This commit is contained in:
2026-02-13 13:53:47 +01:00
parent 7f9de651d4
commit 2912ebb91c
5 changed files with 40 additions and 12 deletions

View File

@@ -3,6 +3,7 @@
#define H_WINDOW_ #define H_WINDOW_
#include <cstddef> #include <cstddef>
#include <functional>
#include <EGL/egl.h> #include <EGL/egl.h>
#include <EGL/eglext.h> #include <EGL/eglext.h>
@@ -10,6 +11,8 @@
#define UNUSED(x) (void)(x) #define UNUSED(x) (void)(x)
typedef std::function<void()> IFrameListener;
class WindowImpl { class WindowImpl {
public: public:
WindowImpl() = default; WindowImpl() = default;
@@ -19,6 +22,7 @@ public:
WindowImpl(WindowImpl&&) = delete; WindowImpl(WindowImpl&&) = delete;
protected: protected:
virtual bool Dispatch() = 0; virtual bool Dispatch() = 0;
virtual void OnFrame(IFrameListener fn) = 0;
virtual size_t GetWidth() const = 0; virtual size_t GetWidth() const = 0;
virtual size_t GetHeight() const = 0; virtual size_t GetHeight() const = 0;
@@ -39,6 +43,10 @@ public:
size_t GetWidth() const; size_t GetWidth() const;
size_t GetHeight() const; size_t GetHeight() const;
public:
void Run();
void OnFrame(IFrameListener fn);
private: private:
WindowImpl* m_impl; WindowImpl* m_impl;

View File

@@ -10,6 +10,7 @@ public:
public: public:
bool Dispatch() override; bool Dispatch() override;
void OnFrame(IFrameListener fn) override;
size_t GetWidth() const override; size_t GetWidth() const override;
size_t GetHeight() const override; size_t GetHeight() const override;
@@ -45,5 +46,7 @@ private:
bool m_running = false; bool m_running = false;
size_t m_width, m_height; size_t m_width, m_height;
IFrameListener m_on_frame;
}; };

View File

@@ -5,18 +5,22 @@
#define UNUSED(x) (void)(x) #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) { int main(int argc, char **argv) {
UNUSED(argc); UNUSED(argc);
UNUSED(argv); UNUSED(argv);
Renderer::GetInstance(); Renderer::GetInstance();
Window window(720, 480); window.OnFrame(HandleFrame);
printf("[APP]: starting window"); printf("[APP]: starting window\n");
while (window.Running()) { window.Run();
printf("[APP]: width = %zu, height = %zu\n", window.GetWidth(), window.GetHeight());
}
return 0; return 0;
} }

View File

@@ -22,3 +22,7 @@ size_t Window::GetHeight() const { return m_impl->GetHeight(); }
bool Window::Running() const { return m_running && m_impl->Dispatch(); } 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()) {}; }

View File

@@ -1,6 +1,8 @@
#include <stdio.h> #include <stdio.h>
#include <wayland-client-core.h>
#include "state/wayland.h" #include "state/wayland.h"
#include "window.h"
#include "window/wayland.h" #include "window/wayland.h"
#include "renderer.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.close = xtoplevel_handle_close;
m_toplevel_listener.wm_capabilities = xtoplevel_handle_wm_capabilities; m_toplevel_listener.wm_capabilities = xtoplevel_handle_wm_capabilities;
xdg_toplevel_add_listener(m_toplevel, &m_toplevel_listener, this); xdg_toplevel_add_listener(m_toplevel, &m_toplevel_listener, this);
wl_surface_commit(m_wsurface);
m_running = true;
} }
WaylandWindowImpl::~WaylandWindowImpl() { m_running = false; } WaylandWindowImpl::~WaylandWindowImpl() { m_running = false; }
bool WaylandWindowImpl::Dispatch() { void WaylandWindowImpl::OnFrame(IFrameListener fn) {
return m_running && m_on_frame = fn;
wl_display_dispatch(WaylandState::GetInstance()->m_display) != -1;
} }
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 { size_t WaylandWindowImpl::GetWidth() const {
return m_width; 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); 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"); GLint angle_loc = glGetUniformLocation(window->m_program, "angle");
static float angle = 0.0f; static float angle = 0.0f;