This commit is contained in:
Jorijn van der Graaf 2025-11-16 20:42:48 +01:00
commit e795ab880c
6 changed files with 89 additions and 24 deletions

View file

@ -11,8 +11,8 @@ A window with a red colored square.
## Highlighted Code Snippet ## Highlighted Code Snippet
```cpp ```cpp
for(uint32_t x = 0; x < 1280; x++) { for(std::uint_fast32_t x = 0; x < 1280; x++) {
for(uint32_t y = 0; y < 720; y++) { for(std::uint_fast32_t y = 0; y < 720; y++) {
window.framebuffer[x*720+y].r = 255; window.framebuffer[x*720+y].r = 255;
window.framebuffer[x*720+y].g = 0; window.framebuffer[x*720+y].g = 0;
window.framebuffer[x*720+y].b = 0; window.framebuffer[x*720+y].b = 0;

View file

@ -5,8 +5,8 @@ using namespace Crafter;
int main() { int main() {
WindowWaylandWayland window("HelloWindow", 1280, 720); WindowWaylandWayland window("HelloWindow", 1280, 720);
for(uint32_t x = 0; x < 1280; x++) { for(std::uint_fast32_t x = 0; x < 1280; x++) {
for(uint32_t y = 0; y < 720; y++) { for(std::uint_fast32_t y = 0; y < 720; y++) {
window.framebuffer[x*720+y].r = 255; window.framebuffer[x*720+y].r = 255;
window.framebuffer[x*720+y].g = 0; window.framebuffer[x*720+y].g = 0;
window.framebuffer[x*720+y].b = 0; window.framebuffer[x*720+y].b = 0;
@ -15,8 +15,8 @@ int main() {
} }
//Semi transparant version: //Semi transparant version:
// for(uint32_t x = 0; x < 1280; x++) { // for(std::uint_fast32_t x = 0; x < 1280; x++) {
// for(uint32_t y = 0; y < 720; y++) { // for(std::uint_fast32_t = 0; y < 720; y++) {
// window.framebuffer[x*720+y].r = 128;//alpha channel must be premultiplied // window.framebuffer[x*720+y].r = 128;//alpha channel must be premultiplied
// window.framebuffer[x*720+y].g = 0; // window.framebuffer[x*720+y].g = 0;
// window.framebuffer[x*720+y].b = 0; // window.framebuffer[x*720+y].b = 0;

View file

@ -55,6 +55,34 @@ xdg_wm_base_listener xdgWmBaseListener = {
.ping = xdg_wm_base_handle_ping, .ping = xdg_wm_base_handle_ping,
}; };
void WindowWayland::wl_surface_frame_done(void *data, struct wl_callback *cb, uint32_t time)
{
/* Destroy this callback */
wl_callback_destroy(cb);
/* Request another frame */
struct client_state *state = data;
cb = wl_surface_frame(state->wl_surface);
wl_callback_add_listener(cb, &wl_surface_frame_listener, state);
/* Update scroll amount at 24 pixels per second */
if (state->last_frame != 0) {
int elapsed = time - state->last_frame;
state->offset += elapsed / 1000.0 * 24;
}
/* Submit a frame for this event */
struct wl_buffer *buffer = draw_frame(state);
wl_surface_attach(state->wl_surface, buffer, 0, 0);
wl_surface_damage_buffer(state->wl_surface, 0, 0, INT32_MAX, INT32_MAX);
wl_surface_commit(state->wl_surface);
state->last_frame = time;
}
wl_callback_listener WindowWayland::surface_frame_listener = {
.done = wl_surface_frame_done,
};
void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) { void WindowWayland::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) {
WindowWayland* window = reinterpret_cast<WindowWayland*>(data); WindowWayland* window = reinterpret_cast<WindowWayland*>(data);
@ -333,12 +361,15 @@ WindowWayland::WindowWayland(std::string name, std::uint32_t width, std::uint32_
surface = wl_compositor_create_surface(compositor); surface = wl_compositor_create_surface(compositor);
xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface); xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface);
xdgToplevel = xdg_surface_get_toplevel(xdgSurface); xdgToplevel = xdg_surface_get_toplevel(xdgSurface);
cb = wl_surface_frame(surface);
+ wl_callback_add_listener(cb, &wl_surface_frame_listener, this);
xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this); xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this);
xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this); xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this);
wl_surface_commit(surface); wl_surface_commit(surface);
xdg_toplevel_set_title(xdg_toplevel, name.c_str());
while (wl_display_dispatch(display) != -1 && !configured) { while (wl_display_dispatch(display) != -1 && !configured) {
// This space intentionally left blank // This space intentionally left blank
} }
@ -353,5 +384,6 @@ WindowWayland::~WindowWayland() {
xdg_toplevel_destroy(xdgToplevel); xdg_toplevel_destroy(xdgToplevel);
xdg_surface_destroy(xdgSurface); xdg_surface_destroy(xdgSurface);
wl_surface_destroy(surface); wl_surface_destroy(surface);
wl_buffer_destroy(buffer); wl_buffer_destroy(frontBuffer);
wl_buffer_destroy(backBuffer);
} }

View file

@ -121,26 +121,32 @@ WindowWaylandWayland::WindowWaylandWayland(std::string name, std::uint32_t width
} }
// Map the shared memory file // Map the shared memory file
framebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); frontFramebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (framebuffer == MAP_FAILED) { if (frontFramebuffer == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n");
close(fd);
}
backFramebuffer = reinterpret_cast<Pixel_BU8_GU8_RU8_AU8*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (backFramebuffer == MAP_FAILED) {
fprintf(stderr, "mmap failed: %m\n"); fprintf(stderr, "mmap failed: %m\n");
close(fd); close(fd);
} }
// Create a wl_buffer from our shared memory file descriptor // Create a wl_buffer from our shared memory file descriptor
wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size);
buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888); frontBuffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
backBuffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool); wl_shm_pool_destroy(pool);
// Now that we've mapped the file and created the wl_buffer, we no longer // Now that we've mapped the file and created the wl_buffer, we no longer
// need to keep file descriptor opened // need to keep file descriptor opened
close(fd); close(fd);
if (buffer == NULL) { if (frontBuffer == NULL || backBuffer == NULL) {
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
wl_surface_attach(surface, buffer, 0, 0); wl_surface_attach(surface, frontBuffer, 0, 0);
wl_surface_commit(surface); wl_surface_commit(surface);
} }
@ -155,20 +161,42 @@ void WindowWaylandWayland::StartAsync() {
void WindowWaylandWayland::StartSync() { void WindowWaylandWayland::StartSync() {
while (open && wl_display_dispatch(display) != -1) { while (open && wl_display_dispatch(display) != -1) {
wl_surface_attach(surface, buffer, 0, 0); std::vector<UiElement*> drawOrder;
for(const UiElement& element : elements) { drawOrder.reserve(elements.size());
ScaleData data = ScaleElement(element); for (UiElement& e : elements) drawOrder.push_back(&e);
std::sort(drawOrder.begin(), drawOrder.end(), [](UiElement* a, UiElement* b){ return a->z < b->z; });
for(const UiElement* element : drawOrder) {
std::cout << element->bufferWidth << std::endl;
ScaleData data = ScaleElement(*element);
std::vector<Pixel_BU8_GU8_RU8_AU8> scaled(data.width*data.height); std::vector<Pixel_BU8_GU8_RU8_AU8> scaled(data.width*data.height);
ScaleBitmapR8G8B8(scaled.data(), element.buffer.data(), element.bufferWidth, element.bufferHeight, data.width, data.height); ScaleBitmapR8G8B8(scaled.data(), element->buffer.data(), element->bufferWidth, element->bufferHeight, data.width, data.height);
for (std::int32_t x = data.x; x - data.x < data.width; x++) { for (std::int32_t x = data.x; x - data.x < data.width; x++) {
for (std::int32_t y = data.y; y - data.y < data.height; y++) { for (std::int32_t y = data.y; y - data.y < data.height; y++) {
if(x > 0 && x < width && y > 0 && y < height) { if (x >= 0 && x < width && y >= 0 && y < height) {
framebuffer[y*width+x] = scaled[(y-data.y)*data.width+(x-data.x)]; Pixel_BU8_GU8_RU8_AU8& dst = backFramebuffer[y * width + x];
const Pixel_BU8_GU8_RU8_AU8& src = scaled[(y - data.y) * data.width + (x - data.x)];
float srcA = src.a / 255.0f;
float dstA = dst.a / 255.0f;
float outA = srcA + dstA * (1.0f - srcA);
if (outA > 0.0f) {
dst.r = static_cast<uint8_t>((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA);
dst.g = static_cast<uint8_t>((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA);
dst.b = static_cast<uint8_t>((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA);
dst.a = static_cast<uint8_t>(outA * 255);
} }
} }
} }
wl_surface_damage(surface, data.x, data.y, data.width, data.height);
} }
}
wl_surface_attach(surface, backBuffer, 0, 0);
wl_surface_damage(surface, 0, 0, width, height);
wl_surface_commit(surface); wl_surface_commit(surface);
std::swap(frontFramebuffer, backFramebuffer);
std::swap(frontBuffer, backBuffer);
} }
} }

View file

@ -62,9 +62,11 @@ export namespace Crafter {
xdg_wm_base* xdgWmBase = NULL; xdg_wm_base* xdgWmBase = NULL;
zxdg_decoration_manager_v1* manager = NULL; zxdg_decoration_manager_v1* manager = NULL;
wl_surface* surface = NULL; wl_surface* surface = NULL;
wl_buffer* buffer = NULL; wl_buffer* frontBuffer = NULL;
wl_buffer* backBuffer = NULL;
xdg_surface* xdgSurface = NULL; xdg_surface* xdgSurface = NULL;
wl_display* display = NULL; wl_display* display = NULL;
wl_callback* cb = nullptr;
inline static wl_compositor* compositor = NULL; inline static wl_compositor* compositor = NULL;
static wl_pointer_listener pointer_listener; static wl_pointer_listener pointer_listener;
static wl_keyboard_listener keyboard_listener; static wl_keyboard_listener keyboard_listener;
@ -72,6 +74,8 @@ export namespace Crafter {
static wl_registry_listener registry_listener; static wl_registry_listener registry_listener;
static xdg_surface_listener xdg_surface_listener; static xdg_surface_listener xdg_surface_listener;
static xdg_toplevel_listener xdg_toplevel_listener; static xdg_toplevel_listener xdg_toplevel_listener;
static wl_callback_listener surface_frame_listener;
static void wl_surface_frame_done(void *data, wl_callback *cb, uint32_t time);
static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y); static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y);
static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value); static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value);
static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y); static void PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y);

View file

@ -34,7 +34,8 @@ export namespace Crafter {
/** /**
* @brief Framebuffer for the window using the BGRA 8-bit unsigned pixel format, use this for direct drawing to the window. * @brief Framebuffer for the window using the BGRA 8-bit unsigned pixel format, use this for direct drawing to the window.
*/ */
Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr; Pixel_BU8_GU8_RU8_AU8* frontFramebuffer = nullptr;
Pixel_BU8_GU8_RU8_AU8* backFramebuffer = nullptr;
/** /**
* @brief Constructs a new WindowWaylandWayland object. * @brief Constructs a new WindowWaylandWayland object.
* *