/* Crafter®.Graphics Copyright (C) 2025 Catcrafts® catcrafts.net This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 3.0 as published by the Free Software Foundation; This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ module; #include #include #include #include #include module Crafter.Graphics:WindowFramebuffer_wayland_impl; import :WindowFramebuffer; import :Window; import :UiElement; import std; import :Types; import :Shm; import Crafter.Event; namespace Crafter { void WindowFramebuffer::Create(std::uint_fast32_t width, std::uint_fast32_t height) { this->width = width; this->height = height; int stride = width * 4; int size = stride * height; int fd = create_shm_file(size); if (fd < 0) { fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); } framebuffer = reinterpret_cast(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)); if (framebuffer == MAP_FAILED) { fprintf(stderr, "mmap failed: %m\n"); close(fd); } wl_shm_pool *pool = wl_shm_create_pool(window.shm, fd, size); buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride, WL_SHM_FORMAT_ARGB8888); wl_shm_pool_destroy(pool); close(fd); if (buffer == NULL) { exit(EXIT_FAILURE); } wl_surface_attach(window.surface, buffer, 0, 0); wl_surface_commit(window.surface); } void WindowFramebuffer::Resize(std::uint_fast32_t width, std::uint_fast32_t height) { } void WindowFramebuffer::Destroy() { wl_buffer_destroy(buffer); } void WindowFramebuffer::Render() { std::vector drawOrder; drawOrder.reserve(elements.size()); for (const UiElementBuffer* e : elements) drawOrder.push_back(e); std::sort(drawOrder.begin(), drawOrder.end(), [](const UiElementBuffer* a, const UiElementBuffer* b){ return a->parent.z < b->parent.z; }); for(const UiElementBuffer* element : drawOrder) { ScaleData data = ScaleElement(element->parent); std::vector scaled(data.width*data.height); element->DrawNearestNeighbour(scaled.data(), data.width, data.height); 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++) { if (x >= 0 && x < width && y >= 0 && y < height) { Pixel_BU8_GU8_RU8_AU8& dst = framebuffer[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((src.r * srcA + dst.r * dstA * (1.0f - srcA)) / outA); dst.g = static_cast((src.g * srcA + dst.g * dstA * (1.0f - srcA)) / outA); dst.b = static_cast((src.b * srcA + dst.b * dstA * (1.0f - srcA)) / outA); dst.a = static_cast(outA * 255); } } } } //RenderElements(window, *element, data); } } }