diff --git a/examples/HelloUI/main.cpp b/examples/HelloUI/main.cpp index 0e17e85..be87e72 100644 --- a/examples/HelloUI/main.cpp +++ b/examples/HelloUI/main.cpp @@ -4,13 +4,13 @@ import std; using namespace Crafter; int main() { - Window window("HelloWindow", 1280, 720); + Window window(1280, 720); - UiElement& element = window.elements.emplace_back( - 0.5, //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor - 0.5, //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor + UiElement& element = window.CreateElement( 2, //bufferWidth: the width of this elements pixel buffer 1, //bufferHeight: the height of this elements pixel buffer + 0.5, //anchorX: relative position where this elements x anchor (top-left) is placed to its parent x anchor + 0.5, //anchorY: relative position where this elements y anchor (top-left) is placed to its parent y anchor 0.5f, //relativeSizeX: the relative x size this element should be scaled to compared to its parent 0.5f, //relativeSizeY: the relative y size this element should be scaled to compared to its parent 0.5, //anchorOffsetX: the amount this element's anchor should be offset from the top left corner (0.5 to in the middle) @@ -19,25 +19,12 @@ int main() { false //ignoreScaling: wether this element ignores the scaling of the window, if true its size will be scaled according to the window scale ); - // UiElement& element = window.elements.emplace_back( - // 0.5, - // 0.5, - // 2, - // 1, - // uint32_t(100), //absoluteSizeX: the absolute x size in pixels this element should be scaled to - // uint32_t(100), //absoluteSizeY: the absolute x size in pixels this element should be scaled to - // 0.5, - // 0.5, - // 0, - // false - // ); + // EventListener clickListener(&element.onMouseLeftClick, [](MousePoint point){ + // // Print the coordinates where the user clicked relative to the element's top left corner. + // std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y) << std::endl; + // }); - EventListener clickListener(&element.onMouseLeftClick, [](MousePoint point){ - // Print the coordinates where the user clicked relative to the element's top left corner. - std::cout << std::format("Clicked on X:{} Y:{}!", point.x, point.y) << std::endl; - }); - - element.buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; - window.scale = 1; + element.buffer.buffer = {{255, 0, 0 ,255}, {0, 255, 0 ,255}}; + window.framebuffer.Render(); window.StartSync(); } diff --git a/examples/HelloWindow/README.md b/examples/HelloWindow/README.md index c49bd66..6582c22 100644 --- a/examples/HelloWindow/README.md +++ b/examples/HelloWindow/README.md @@ -2,7 +2,7 @@ ## Description -This example demonstrates the minimal code needed to create a window and show it on the screen. +This example demonstrates the minimal code needed to create a window and show it on the screen. This uses the Framebuffer and Title capability. ## Expected Result @@ -11,7 +11,9 @@ A empty window with the title "HelloWindow" shows onscreen. ## Highlighted Code Snippet ```cpp -WindowWaylandWayland window("HelloWindow", 1280, 720); +Window window; +window.framebuffer.Create(1280, 720); +window.title.Set("HelloWindow"); window.StartSync(); ``` diff --git a/examples/HelloWindow/main.cpp b/examples/HelloWindow/main.cpp index 6ba59c1..1a1774c 100644 --- a/examples/HelloWindow/main.cpp +++ b/examples/HelloWindow/main.cpp @@ -3,18 +3,6 @@ import std; using namespace Crafter; int main() { - /* - This creates a window titled "HelloWindow" with a size of 1280x720 pixels. - The WindowWaylandWayland class is a specialized window implementation - that uses the Wayland display server protocol and renderer (hence the name "WaylandWayland"). - */ - Window window("HelloWindow", 1280, 720); - - /* - This starts the window’s main event loop, allowing it to respond to user input and system events. - The window will remain open and responsive until it is closed. - You can hook into various events through the event system. - This call blocks the current thread; to run the event loop asynchronously, use StartAsync instead. - */ + Window window(1280, 720, "HelloWindow"); window.StartSync(); } diff --git a/implementations/Crafter.Graphics-Shm.cpp b/implementations/Crafter.Graphics-Shm.cpp new file mode 100644 index 0000000..48f6ecf --- /dev/null +++ b/implementations/Crafter.Graphics-Shm.cpp @@ -0,0 +1,76 @@ +/* +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 +#include +#include +#include +module Crafter.Graphics:Shm_impl; +import :Shm; +import std; + +using namespace Crafter; + +void Crafter::randname(char *buf) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + long r = ts.tv_nsec; + for (int i = 0; i < 6; ++i) { + buf[i] = 'A'+(r&15)+(r&16)*2; + r >>= 5; + } +} + +int Crafter::anonymous_shm_open(void) { + char name[] = "/hello-wayland-XXXXXX"; + int retries = 100; + + do { + randname(name + strlen(name) - 6); + + --retries; + // shm_open guarantees that O_CLOEXEC is set + int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); + if (fd >= 0) { + shm_unlink(name); + return fd; + } + } while (retries > 0 && errno == EEXIST); + + return -1; +} + +int Crafter::create_shm_file(off_t size) { + int fd = anonymous_shm_open(); + if (fd < 0) { + return fd; + } + + if (ftruncate(fd, size) < 0) { + close(fd); + return -1; + } + + return fd; +} diff --git a/implementations/Crafter.Graphics-UiElement.cpp b/implementations/Crafter.Graphics-UiElement.cpp index b6d317e..88484bc 100644 --- a/implementations/Crafter.Graphics-UiElement.cpp +++ b/implementations/Crafter.Graphics-UiElement.cpp @@ -18,99 +18,12 @@ 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; -#define STB_IMAGE_IMPLEMENTATION -#include "../lib/stb_image.h" -#include "../lib/stb_truetype.h" module Crafter.Graphics:UiElement_impl; import :UiElement; -import :Font; import std; using namespace Crafter; -UiElement::UiElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), useRelativeSize(true), ignoreScaling(ignoreScaling) { +UiElementBase::UiElementBase(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), ignoreScaling(ignoreScaling) { -} - -UiElement::UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t absoluteWidth, std::uint32_t absoluteHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), bufferWidth(bufferWidth), bufferHeight(bufferHeight), absoluteWidth(absoluteWidth), absoluteHeight(absoluteHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), buffer(bufferWidth*bufferHeight), useRelativeSize(false), ignoreScaling(ignoreScaling) { - -} - -UiElement::UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), bufferWidth(bufferWidth), bufferHeight(bufferHeight), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), buffer(bufferWidth*bufferHeight), useRelativeSize(true), ignoreScaling(ignoreScaling) { - -} - -UiElement::UiElement(const std::filesystem::path& image, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : anchorX(anchorX), anchorY(anchorY), relativeWidth(relativeWidth), relativeHeight(relativeHeight), anchorOffsetX(anchorOffsetX), anchorOffsetY(anchorOffsetY), z(z), useRelativeSize(true), ignoreScaling(ignoreScaling) { - std::filesystem::path abs = std::filesystem::absolute(image); - int xSize; - int ySize; - unsigned char* bgData = stbi_load(abs.string().c_str(), &xSize, &ySize, nullptr, 4); - - bufferWidth = xSize; - bufferHeight = ySize; - - buffer.resize(bufferWidth*bufferHeight); - - for(std::uint_fast32_t x = 0; x < xSize; x++) { - for(std::uint_fast32_t y = 0; y < ySize; y++) { - std::uint_fast32_t idx = (x*ySize+y)*4; - buffer[x*ySize+y].r = bgData[idx]; - buffer[x*ySize+y].g = bgData[idx+1]; - buffer[x*ySize+y].b = bgData[idx+2]; - buffer[x*ySize+y].a = bgData[idx+3]; - } - } -} - -TextElement::TextElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, const Font& font, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX, float anchorOffsetY, float z, bool ignoreScaling) : UiElement(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling) { - RenderText(text, size, color, font); -} - -void TextElement::RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, const Font& font) { - float scale = stbtt_ScaleForPixelHeight(&font.font, size); - - int baseline = (int)(font.ascent * scale); - - bufferWidth = 0; - bufferHeight = (int)((font.ascent -font.descent) * scale); - for (const char c : text) { - int advance, lsb; - stbtt_GetCodepointHMetrics(&font.font, c, &advance, &lsb); - bufferWidth += (int)(advance * scale); - } - - buffer.resize(bufferWidth * bufferHeight); - - int x = 0; - for (std::uint_fast32_t i = 0; i < text.size(); i++) { - int codepoint = text[i]; - - int ax; - int lsb; - stbtt_GetCodepointHMetrics(&font.font, codepoint, &ax, &lsb); - - int c_x1, c_y1, c_x2, c_y2; - stbtt_GetCodepointBitmapBox(&font.font, codepoint, scale, scale, &c_x1, &c_y1, &c_x2, &c_y2); - - int w = c_x2 - c_x1; - int h = c_y2 - c_y1; - - std::vector bitmap(w * h); - stbtt_MakeCodepointBitmap(&font.font, bitmap.data(), w, h, w, scale, scale, codepoint); - - for (int j = 0; j < h; j++) { - for (int i = 0; i < w; i++) { - int bufIndex = ((baseline + j + c_y1) * bufferWidth + (x + i + c_x1)); - unsigned char val = bitmap[j * w + i]; - buffer[bufIndex] = {color.r, color.g, color.b, val}; - } - } - - x += (int)(ax * scale); - - if (i + 1 < text.size()) { - x += (int)stbtt_GetCodepointKernAdvance(&font.font, codepoint, text[i+1] * scale); - } - } } \ No newline at end of file diff --git a/implementations/Crafter.Graphics-UiElementBuffer.cpp b/implementations/Crafter.Graphics-UiElementBuffer.cpp new file mode 100644 index 0000000..f0b4f77 --- /dev/null +++ b/implementations/Crafter.Graphics-UiElementBuffer.cpp @@ -0,0 +1,54 @@ +/* +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 Crafter.Graphics:UiElementBuffer_impl; +import :UiElementBuffer; +import :UiElement; +import :WindowFramebuffer; +import std; + +namespace Crafter { + UiElementBuffer::UiElementBuffer(UiElementBase& parent, std::uint_fast32_t width, std::uint_fast32_t height) : parent(parent), width(width), height(height) { + + } + + void UiElementBuffer::DrawNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const { + for (std::uint_fast32_t y = 0; y < dstHeight; y++) { + std::uint_fast32_t srcY = y * height / dstHeight; + for (std::uint_fast32_t x = 0; x < dstWidth; x++) { + std::uint_fast32_t srcX = x * width / dstWidth; + dst[y * dstWidth + x] = buffer[srcY * width + srcX]; + } + } + } + + ScaleData UiElementBuffer::ScaleElement(const UiElementBase& element, ScaleData own, WindowFramebuffer& window) const { + ScaleData data; + if(element.ignoreScaling) { + data.width = element.relativeWidth*own.width; + data.height = element.relativeHeight*own.height; + } else { + data.width = element.relativeWidth*own.width*window.scale; + data.height = element.relativeHeight*own.height*window.scale; + } + data.x = ((element.anchorX*own.width)-(element.anchorOffsetX*data.width))+own.x; + data.y = ((element.anchorY*own.height)-(element.anchorOffsetY*data.height))+own.y; + return data; + } +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-Window.cpp b/implementations/Crafter.Graphics-Window.cpp deleted file mode 100644 index 76e39ed..0000000 --- a/implementations/Crafter.Graphics-Window.cpp +++ /dev/null @@ -1,605 +0,0 @@ -/* -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 as published by the Free Software Foundation; either -version 3.0 of the License, or (at your option) any later version. - -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 -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "../lib/xdg-shell-client-protocol.h" -#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h" -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -module Crafter.Graphics:Window_impl; -import :Window; -import std; -import Crafter.Event; -using namespace Crafter; - - -void randname(char *buf) { - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - long r = ts.tv_nsec; - for (int i = 0; i < 6; ++i) { - buf[i] = 'A'+(r&15)+(r&16)*2; - r >>= 5; - } -} - -int anonymous_shm_open(void) { - char name[] = "/hello-wayland-XXXXXX"; - int retries = 100; - - do { - randname(name + strlen(name) - 6); - - --retries; - // shm_open guarantees that O_CLOEXEC is set - int fd = shm_open(name, O_RDWR | O_CREAT | O_EXCL, 0600); - if (fd >= 0) { - shm_unlink(name); - return fd; - } - } while (retries > 0 && errno == EEXIST); - - return -1; -} - -int create_shm_file(off_t size) { - int fd = anonymous_shm_open(); - if (fd < 0) { - return fd; - } - - if (ftruncate(fd, size) < 0) { - close(fd); - return -1; - } - - return fd; -} - -int counter = 0; - -void ScaleBitmapR8G8B8(Pixel_BU8_GU8_RU8_AU8* dst, const Pixel_BU8_GU8_RU8_AU8* src, std::uint32_t srcWidth, std::uint32_t srcHeight, std::uint32_t dstWidth, std::uint32_t dstHeight) { - for (std::uint32_t y = 0; y < dstHeight; y++) { - std::uint32_t srcY = y * srcHeight / dstHeight; - for (std::uint32_t x = 0; x < dstWidth; x++) { - std::uint32_t srcX = x * srcWidth / dstWidth; - const Pixel_BU8_GU8_RU8_AU8* srcPixel = src + (srcY * srcWidth + srcX); - Pixel_BU8_GU8_RU8_AU8* dstPixel = dst + (y * dstWidth + x); - dstPixel[0] = srcPixel[0]; - } - } -} - -Window::Window(std::string name, std::uint32_t width, std::uint32_t height) : name(name), width(width), height(height) { - display = wl_display_connect(NULL); - if (display == NULL) { - std::cerr << "failed to create display" << std::endl; - } - - wl_registry* registry = wl_display_get_registry(display); - wl_registry_add_listener(registry, ®istry_listener, this); - if (wl_display_roundtrip(display) == -1) { - exit(EXIT_FAILURE); - } - if (shm == NULL || compositor == NULL || xdgWmBase == NULL) { - std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl; - exit(EXIT_FAILURE); - } - - surface = wl_compositor_create_surface(compositor); - xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface); - xdgToplevel = xdg_surface_get_toplevel(xdgSurface); - cb = wl_surface_frame(surface); - - wl_callback_add_listener(cb, &surface_frame_listener, this); - xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this); - xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this); - wl_surface_commit(surface); - - xdg_toplevel_set_title(xdgToplevel, name.c_str()); - - while (wl_display_dispatch(display) != -1 && !configured) {} - - - wl_surface_commit(surface); - - zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel); - zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); - - // Create a wl_buffer, attach it to the surface and commit the surface - int stride = width * 4; - int size = stride * height; - - // Allocate a shared memory file with the right size - int fd = create_shm_file(size); - if (fd < 0) { - fprintf(stderr, "creating a buffer file for %d B failed: %m\n", size); - } - - // Map the shared memory file - 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); - } - - // Create a wl_buffer from our shared memory file descriptor - 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); - wl_shm_pool_destroy(pool); - - // Now that we've mapped the file and created the wl_buffer, we no longer - // need to keep file descriptor opened - close(fd); - - if (buffer == NULL) { - exit(EXIT_FAILURE); - } - - wl_surface_attach(surface, buffer, 0, 0); - wl_surface_commit(surface); -} - -ScaleData Window::ScaleElement(const UiElement& element) { - ScaleData data; - if(element.ignoreScaling) { - if(element.useRelativeSize) { - data.width = element.relativeWidth*width; - data.height = element.relativeHeight*height; - } else { - data.width = element.absoluteWidth; - data.height = element.absoluteHeight; - } - } else { - if(element.useRelativeSize) { - data.width = element.relativeWidth*width*scale; - data.height = element.relativeHeight*height*scale; - } else { - data.width = element.absoluteWidth*scale; - data.height = element.absoluteHeight*scale; - } - } - data.x = (element.anchorX*width)-(element.anchorOffsetX*data.width); - data.y = (element.anchorY*height)-(element.anchorOffsetY*data.height); - return data; -} - -ScaleData ScaleElement(const UiElement& element, const ScaleData& parent, const Window* window) { - ScaleData data; - if(element.ignoreScaling) { - if(element.useRelativeSize) { - data.width = element.relativeWidth*parent.width; - data.height = element.relativeHeight*parent.height; - } else { - data.width = element.absoluteWidth; - data.height = element.absoluteHeight; - } - } else { - if(element.useRelativeSize) { - data.width = element.relativeWidth*parent.width*window->scale; - data.height = element.relativeHeight*parent.height*window->scale; - } else { - data.width = element.absoluteWidth*window->scale; - data.height = element.absoluteHeight*window->scale; - } - } - data.x = ((element.anchorX*parent.width)-(element.anchorOffsetX*data.width))+parent.x; - data.y = ((element.anchorY*parent.height)-(element.anchorOffsetY*data.height))+parent.y; - return data; -} - -static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) { - xdg_wm_base_pong(xdg_wm_base, serial); -} - -xdg_wm_base_listener xdgWmBaseListener = { -.ping = xdg_wm_base_handle_ping, -}; - -void RenderElements(Window* window, const UiElement& parent, const ScaleData& parentScale) { - std::vector drawOrder; - drawOrder.reserve(parent.children.size()); - for (const std::unique_ptr& e : parent.children) drawOrder.push_back(e.get()); - std::sort(drawOrder.begin(), drawOrder.end(), [](UiElement* a, UiElement* b){ return a->z < b->z; }); - - for(const UiElement* element : drawOrder) { - ScaleData data = ScaleElement(*element, parentScale, window); - std::vector scaled(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 y = data.y; y - data.y < data.height; y++) { - if (x >= 0 && x < window->width && y >= 0 && y < window->height) { - Pixel_BU8_GU8_RU8_AU8& dst = window->framebuffer[y * window->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); - } -} - -void Window::wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) -{ - wl_callback_destroy(cb); - Window* window = reinterpret_cast(data); - cb = wl_surface_frame(window->surface); - wl_callback_add_listener(cb, &Window::surface_frame_listener, window); - - std::vector drawOrder; - drawOrder.reserve(window->elements.size()); - for (UiElement& e : window->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) { - ScaleData data = window->ScaleElement(*element); - std::vector scaled(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 y = data.y; y - data.y < data.height; y++) { - if (x >= 0 && x < window->width && y >= 0 && y < window->height) { - Pixel_BU8_GU8_RU8_AU8& dst = window->framebuffer[y * window->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); - } - - - wl_surface_attach(window->surface, window->buffer, 0, 0); - wl_surface_damage(window->surface, 0, 0, window->width, window->height); - wl_surface_commit(window->surface); -} - -wl_callback_listener Window::surface_frame_listener = { - .done = wl_surface_frame_done, -}; - -void Window::pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) { - Window* window = reinterpret_cast(data); - if (button == BTN_LEFT) { - if(state == WL_POINTER_BUTTON_STATE_PRESSED) { - window->mouseLeftHeld = true; - window->onMouseLeftClick.Invoke(window->currentMousePos); - for(UiElement& element : window->elements) { - ScaleData data = window->ScaleElement(element); - if(window->currentMousePos.x >= data.x && window->currentMousePos.x <= data.x+data.width && window->currentMousePos.y > data.y && window->currentMousePos.y < data.y+data.height) { - element.onMouseLeftClick.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); - } - } - } else { - window->mouseLeftHeld = false; - window->onMouseLeftRelease.Invoke(window->currentMousePos); - for(UiElement& element : window->elements) { - ScaleData data = window->ScaleElement(element); - if(window->currentMousePos.x >= data.x && window->currentMousePos.x <= data.x+data.width && window->currentMousePos.y > data.y && window->currentMousePos.y < data.y+data.height) { - element.onMouseLeftRelease.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); - } - } - } - } else if(button == BTN_RIGHT){ - if(state == WL_POINTER_BUTTON_STATE_PRESSED) { - window->mouseRightHeld = true; - window->onMouseRightClick.Invoke(window->currentMousePos); - for(UiElement& element : window->elements) { - ScaleData data = window->ScaleElement(element); - if(window->currentMousePos.x >= data.x && window->currentMousePos.x <= data.x+data.width && window->currentMousePos.y > data.y && window->currentMousePos.y < data.y+data.height) { - element.onMouseRightClick.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); - } - } - } else { - window->mouseRightHeld = true; - window->onMouseRightRelease.Invoke(window->currentMousePos); - for(UiElement& element : window->elements) { - ScaleData data = window->ScaleElement(element); - if(window->currentMousePos.x >= data.x && window->currentMousePos.x <= data.x+data.width && window->currentMousePos.y > data.y && window->currentMousePos.y < data.y+data.height) { - element.onMouseRightRelease.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); - } - } - } - } -} - -void Window::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) { - Window* window = reinterpret_cast(data); - MousePoint pos = {wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y)}; - window->lastMousePos = window->currentMousePos; - window->currentMousePos = pos; - window->mouseDelta = {window->currentMousePos.x-window->lastMousePos.x, window->currentMousePos.y-window->lastMousePos.y}; - window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta}); - for(UiElement& element : window->elements) { - ScaleData data = window->ScaleElement(element); - if(window->currentMousePos.x >= data.x && window->currentMousePos.x <= data.x+data.width && window->currentMousePos.y > data.y && window->currentMousePos.y < data.y+data.height) { - element.onMouseMove.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); - if(!(window->lastMousePos.x >= data.x && window->lastMousePos.x <= data.x+data.width && window->lastMousePos.y > data.y && window->lastMousePos.y < data.y+data.height)) { - element.onMouseEnter.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); - } - } else if(window->lastMousePos.x >= data.x && window->lastMousePos.x <= data.x+data.width && window->lastMousePos.y > data.y && window->lastMousePos.y < data.y+data.height) { - element.onMouseLeave.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); - } - } -} - -void Window::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, uint serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) { - Window* window = reinterpret_cast(data); - window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta}); -} - -void Window::PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) { - Window* window = reinterpret_cast(data); - window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta}); -} - -void Window::PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) { - -} - -wl_pointer_listener Window::pointer_listener = { -.enter = Window::PointerListenerHandleEnter, -.leave = Window::PointerListenerHandleLeave, -.motion = Window::PointerListenerHandleMotion, -.button = Window::pointer_handle_button, -.axis = Window::PointerListenerHandleAxis, -}; - -xkb_keymap* xkb_keymap; -xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); -xkb_state* xkb_state; - - -void keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) { - if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { - close(fd); - fprintf(stderr, "Unsupported keymap format\n"); - return; - } - - void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); - if (map == MAP_FAILED) { - close(fd); - perror("mmap"); - return; - } - - xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - xkb_keymap = xkb_keymap_new_from_string(xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS); - munmap(map, size); - close(fd); - - xkb_state = xkb_state_new(xkb_keymap); -} - -void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) { - -} - -void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) { - -} - -void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { - if (!xkb_state) { - return; - } - - Window* window = reinterpret_cast(data); - - xkb_keycode_t keycode = key + 8; - xkb_keysym_t keysym = xkb_state_key_get_one_sym(xkb_state, keycode); - - char utf8[8] = {0}; - int len = xkb_keysym_to_utf8(keysym, utf8, sizeof(utf8)); - if (len != 0) { - char keypress = utf8[0]; - if(state == WL_KEYBOARD_KEY_STATE_PRESSED) { - if(window->heldkeys[keypress]) { - window->onKeyHold[keypress].Invoke(); - window->onAnyKeyHold.Invoke(keypress); - } else{ - window->heldkeys[keypress] = true; - window->onKeyDown[keypress].Invoke(); - window->onAnyKeyDown.Invoke(keypress); - } - } else{ - window->heldkeys[keypress] = false; - window->onKeyUp[keypress].Invoke(); - window->onAnyKeyUp.Invoke(keypress); - } - - } else { - // // fallback for keys like Return, Escape, etc. - // char name[64]; - // if (xkb_keysym_get_name(keysym, name, sizeof(name)) > 0) { - // printf("Key %s pressed (non-printable or multi-char)\n", name); - // } - } -} - -void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { - -} - -void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) { - -} - -wl_keyboard_listener Window::keyboard_listener = { - .keymap = keyboard_keymap, - .enter = keyboard_enter, - .leave = keyboard_leave, - .key = keyboard_key, - .modifiers = keyboard_modifiers, - .repeat_info = keyboard_repeat_info, -}; - -void Window::seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) { - Window* window = reinterpret_cast(data); - window->seat = seat; - if (capabilities & WL_SEAT_CAPABILITY_POINTER) { - wl_pointer* pointer = wl_seat_get_pointer(seat); - wl_pointer_add_listener(pointer, &pointer_listener, window); - } - if (capabilities & WL_SEAT_CAPABILITY_KEYBOARD) { - wl_keyboard* keyboard = wl_seat_get_keyboard(seat); - wl_keyboard_add_listener(keyboard, &keyboard_listener, window); - } -} - -wl_seat_listener Window::seat_listener = { -.capabilities = seat_handle_capabilities, -}; - -void Window::handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) { - Window* window = reinterpret_cast(data); - if (strcmp(interface, wl_shm_interface.name) == 0) { - window->shm = reinterpret_cast(wl_registry_bind(registry, name, &wl_shm_interface, 1)); - } else if (strcmp(interface, wl_seat_interface.name) == 0) { - wl_seat* seat = reinterpret_cast(wl_registry_bind(registry, name, &wl_seat_interface, 1)); - wl_seat_add_listener(seat, &seat_listener, window); - } else if (compositor == NULL && strcmp(interface, wl_compositor_interface.name) == 0) { - compositor = reinterpret_cast(wl_registry_bind(registry, name, &wl_compositor_interface, 1)); - } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { - window->xdgWmBase = reinterpret_cast(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1)); - xdg_wm_base_add_listener(window->xdgWmBase, &xdgWmBaseListener, NULL); - } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) { - window->manager = reinterpret_cast(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1)); - } -} - -static void handle_global_remove(void* data, wl_registry* registry, uint32_t name) { - - -} - -wl_registry_listener Window::registry_listener = { -.global = Window::handle_global, -.global_remove = handle_global_remove, -}; - -static void noop5(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){ - -} - -void Window::xdg_toplevel_handle_close(void* data, xdg_toplevel*) { - Window* window = reinterpret_cast(data); - window->onClose.Invoke(); - window->open = false; -} - -xdg_toplevel_listener Window::xdg_toplevel_listener = { -.configure = noop5, -.close = Window::xdg_toplevel_handle_close, -}; - -void Window::xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) { - Window* window = reinterpret_cast(data); - // The compositor configures our surface, acknowledge the configure event - xdg_surface_ack_configure(xdg_surface, serial); - - if (window->configured) { - // If this isn't the first configure event we've received, we already - // have a buffer attached, so no need to do anything. Commit the - // surface to apply the configure acknowledgement. - wl_surface_commit(window->surface); - } - - window->configured = true; -} - -xdg_surface_listener Window::xdg_surface_listener = { -.configure = xdg_surface_handle_configure, -}; - -Window::~Window() { - xdg_toplevel_destroy(xdgToplevel); - xdg_surface_destroy(xdgSurface); - wl_surface_destroy(surface); - wl_buffer_destroy(buffer); -} - -void Window::StartSync() { - while (open && wl_display_dispatch(display) != -1) { - - } -} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-WindowFramebuffer.cpp b/implementations/Crafter.Graphics-WindowFramebuffer.cpp new file mode 100644 index 0000000..b70df04 --- /dev/null +++ b/implementations/Crafter.Graphics-WindowFramebuffer.cpp @@ -0,0 +1,55 @@ +/* +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 Crafter.Graphics:WindowFramebuffer_impl; +import :WindowFramebuffer; +import :Window; +import std; +import :Types; +import :UiElement; +import Crafter.Event; + +namespace Crafter { + WindowFramebuffer::WindowFramebuffer(WindowBase& window) : window(window) { + + } + + WindowFramebuffer::WindowFramebuffer(WindowBase& window, std::uint_fast32_t width, std::uint_fast32_t height) : window(window) { + Create(width, height); + } + + WindowFramebuffer::~WindowFramebuffer() { + Destroy(); + } + + ScaleData WindowFramebuffer::ScaleElement(const UiElementBase& element) { + ScaleData data; + if(element.ignoreScaling) { + data.width = element.relativeWidth*width; + data.height = element.relativeHeight*height; + } else { + data.width = element.relativeWidth*width*scale; + data.height = element.relativeHeight*height*scale; + } + data.x = ((element.anchorX*width)-(element.anchorOffsetX*data.width)); + data.y = ((element.anchorY*height)-(element.anchorOffsetY*data.height)); + return data; + } + +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-WindowFramebuffer_wayland.cpp b/implementations/Crafter.Graphics-WindowFramebuffer_wayland.cpp new file mode 100644 index 0000000..70a88cb --- /dev/null +++ b/implementations/Crafter.Graphics-WindowFramebuffer_wayland.cpp @@ -0,0 +1,107 @@ +/* +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); + } + } +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-WindowTitle_wayland.cpp b/implementations/Crafter.Graphics-WindowTitle_wayland.cpp new file mode 100644 index 0000000..4b22807 --- /dev/null +++ b/implementations/Crafter.Graphics-WindowTitle_wayland.cpp @@ -0,0 +1,42 @@ +/* +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 "../lib/xdg-shell-client-protocol.h" + +module Crafter.Graphics:WindowTitle_impl; +import :WindowTitle; +import :Window; +import std; +import :Types; +import :Shm; +import Crafter.Event; + +namespace Crafter { + WindowTitle::WindowTitle(WindowBase& window) : window(window) { + + } + WindowTitle::WindowTitle(WindowBase& window, const std::string_view title) : window(window) { + Set(title); + } + void WindowTitle::Set(const std::string_view title) { + xdg_toplevel_set_title(window.xdgToplevel, title.data()); + } +} \ No newline at end of file diff --git a/implementations/Crafter.Graphics-Window_wayland.cpp b/implementations/Crafter.Graphics-Window_wayland.cpp new file mode 100644 index 0000000..cb0ff8f --- /dev/null +++ b/implementations/Crafter.Graphics-Window_wayland.cpp @@ -0,0 +1,66 @@ +/* +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; + +#ifdef CRAFTER_GRAPHICS_WAYLAND +#include +#include +#include +#include +#include +#include "../lib/xdg-shell-client-protocol.h" +#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif + +export module Crafter.Graphics:Window_impl; +import :Window; +import std; +import :WindowKeyboard; +import :WindowFramebuffer; +import :Types; +import :Shm; +import Crafter.Event; + +using namespace Crafter; + +void WindowBase::StartSync() { + while (open && wl_display_dispatch(display) != -1) { + std::cout << "bruh3" << std::endl; + } +} + +void WindowBase::StartUpdate(void* window) { + cb = wl_surface_frame(surface); + wl_callback_add_listener(cb, &surface_frame_listener, window); +} + +void WindowBase::StopUpdate() { + onUpdate.listeners.clear(); +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-Shm.cppm b/interfaces/Crafter.Graphics-Shm.cppm new file mode 100644 index 0000000..81745d3 --- /dev/null +++ b/interfaces/Crafter.Graphics-Shm.cppm @@ -0,0 +1,31 @@ +/* +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 + +export module Crafter.Graphics:Shm; +import std; + +export namespace Crafter { + void randname(char *buf); + int anonymous_shm_open(); + int create_shm_file(off_t size); +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-Types.cppm b/interfaces/Crafter.Graphics-Types.cppm index 7e1c53d..b2dd823 100644 --- a/interfaces/Crafter.Graphics-Types.cppm +++ b/interfaces/Crafter.Graphics-Types.cppm @@ -32,6 +32,13 @@ namespace Crafter { MousePoint currentMousePos; MousePoint mouseDelta; }; + + export struct ScaleData { + std::int_fast32_t x; + std::int_fast32_t y; + std::int_fast32_t width; + std::int_fast32_t height; + }; export struct __attribute__((packed)) Pixel_BU8_GU8_RU8_AU8 { std::uint8_t b; diff --git a/interfaces/Crafter.Graphics-UiElement.cppm b/interfaces/Crafter.Graphics-UiElement.cppm index 9e070f5..d854668 100644 --- a/interfaces/Crafter.Graphics-UiElement.cppm +++ b/interfaces/Crafter.Graphics-UiElement.cppm @@ -1,12 +1,11 @@ /* Crafter®.Graphics Copyright (C) 2025 Catcrafts® -Catcrafts.net +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 as published by the Free Software Foundation; either -version 3.0 of the License, or (at your option) any later version. +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 @@ -19,50 +18,38 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ export module Crafter.Graphics:UiElement; +import :UiElementBuffer; +import :UiElementMouse; import std; import Crafter.Event; import :Types; -namespace Crafter { - export class Font; - export class UiElement { +export namespace Crafter { + class Font; + class UiElementBase { public: - Event onMouseMove; - Event onMouseEnter; - Event onMouseLeave; - Event onMouseRightClick; - Event onMouseLeftClick; - Event onMouseRightHold; - Event onMouseLeftHold; - Event onMouseRightRelease; - Event onMouseLeftRelease; float z; float anchorX; float anchorY; - bool useRelativeSize; bool ignoreScaling; - std::uint32_t bufferWidth; - std::uint32_t bufferHeight; - std::uint32_t absoluteWidth; - std::uint32_t absoluteHeight; float relativeWidth; float relativeHeight; float anchorOffsetX; float anchorOffsetY; - std::vector buffer; - std::vector> children; + std::vector> children; - UiElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false); - UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, std::uint32_t absoluteWidth, std::uint32_t absoluteHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false); - UiElement(float anchorX, float anchorY, std::uint32_t bufferWidth, std::uint32_t bufferHeight, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false); - UiElement(const std::filesystem::path& image, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false); - UiElement(UiElement&&) noexcept = default; - UiElement& operator=(UiElement&&) noexcept = default; + UiElementBase(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false); + UiElementBase(UiElementBase&&) noexcept = default; + UiElementBase& operator=(UiElementBase&&) noexcept = default; }; - export class TextElement : public UiElement { + template + class UiElement : public UiElementBase{ public: - TextElement(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, const Font& font, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false); - void RenderText(const std::string_view text, float size, Pixel_BU8_GU8_RU8_AU8 color, const Font& font); + std::conditional_t buffer; + std::conditional_t mouse; + UiElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) : UiElementBase(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling = false) {} + UiElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires (Buffer) : UiElementBase(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), buffer(*this) {} + UiElement(std::uint_fast32_t width, std::uint_fast32_t height, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires (Buffer) : UiElementBase(anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling), buffer(*this, width, height) {} }; } \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-UiElementBuffer.cppm b/interfaces/Crafter.Graphics-UiElementBuffer.cppm new file mode 100644 index 0000000..356e2a7 --- /dev/null +++ b/interfaces/Crafter.Graphics-UiElementBuffer.cppm @@ -0,0 +1,51 @@ +/* +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 +*/ + +export module Crafter.Graphics:UiElementBuffer; +import std; +import :Types; + +export namespace Crafter { + class UiElementBase; + class WindowFramebuffer; + class UiElementBuffer { + public: + UiElementBase& parent; + std::uint_fast32_t width; + std::uint_fast32_t height; + std::vector buffer; + UiElementBuffer(UiElementBase& parent); + UiElementBuffer(UiElementBase& parent, std::uint_fast32_t width, std::uint_fast32_t height); + + void DrawNearestNeighbour(Pixel_BU8_GU8_RU8_AU8* dst, std::uint_fast32_t dstWidth, std::uint_fast32_t dstHeight) const; + ScaleData ScaleElement(const UiElementBase& element, ScaleData own, WindowFramebuffer& window) const; + + void Create(std::uint_fast32_t width, std::uint_fast32_t height); + void Destroy(); + + void Write(Pixel_BU8_GU8_RU8_AU8* pixels); + void Write(std::uint_fast32_t x, std::uint_fast32_t y, Pixel_BU8_GU8_RU8_AU8 pixel); + + Pixel_BU8_GU8_RU8_AU8 Read(std::uint_fast32_t x, std::uint_fast32_t y); + Pixel_BU8_GU8_RU8_AU8* Read(); + + Pixel_BU8_GU8_RU8_AU8* Get(); + void Store(); + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-UiElementMouse.cppm b/interfaces/Crafter.Graphics-UiElementMouse.cppm new file mode 100644 index 0000000..9dce4d1 --- /dev/null +++ b/interfaces/Crafter.Graphics-UiElementMouse.cppm @@ -0,0 +1,37 @@ +/* +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 +*/ +export module Crafter.Graphics:UiElementMouse; +import std; +import :Types; +import Crafter.Event; + +export namespace Crafter { + class UiElementMouse { + public: + Event onMouseMove; + Event onMouseEnter; + Event onMouseLeave; + Event onMouseRightClick; + Event onMouseLeftClick; + Event onMouseRightHold; + Event onMouseLeftHold; + Event onMouseRightRelease; + Event onMouseLeftRelease; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-Window.cppm b/interfaces/Crafter.Graphics-Window.cppm index 08e1c24..ca85c22 100644 --- a/interfaces/Crafter.Graphics-Window.cppm +++ b/interfaces/Crafter.Graphics-Window.cppm @@ -1,12 +1,11 @@ /* Crafter®.Graphics Copyright (C) 2025 Catcrafts® -Catcrafts.net +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 as published by the Free Software Foundation; either -version 3.0 of the License, or (at your option) any later version. +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 @@ -20,93 +19,396 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA module; -#include +#ifdef CRAFTER_GRAPHICS_WAYLAND +#include +#include +#include +#include #include #include "../lib/xdg-shell-client-protocol.h" #include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif export module Crafter.Graphics:Window; import std; -import Crafter.Event; -import :UiElement; -import :Types; +import :WindowBase; +import :WindowKeyboard; +import :WindowMouse; +import :WindowTitle; +import :WindowFramebuffer; export namespace Crafter { - struct ScaleData { - std::int32_t x; - std::int32_t y; - std::int32_t width; - std::int32_t height; - }; - - class Window { + template + class Window : public WindowBase { public: - Pixel_BU8_GU8_RU8_AU8* framebuffer = nullptr; - Event onMouseRightClick; - Event onMouseLeftClick; - Event onMouseRightHold; - Event onMouseLeftHold; - Event onMouseRightRelease; - Event onMouseLeftRelease; - Event onMouseMove; - Event onMouseEnter; - Event onMouseLeave; - Event onMouseScroll; - Event onClose; - MousePoint currentMousePos; - MousePoint lastMousePos; - MousePoint mouseDelta; - bool mouseLeftHeld = false; - bool mouseRightHeld = false; - bool heldkeys[255] = {}; - Event onKeyDown[255]; - Event onKeyHold[255]; - Event onKeyUp[255]; - Event onAnyKeyDown; - Event onAnyKeyHold; - Event onAnyKeyUp; - std::vector elements; - std::string name; - std::uint32_t width; - std::uint32_t height; - float scale = 1; - bool open = true; + std::conditional_t framebuffer; + std::conditional_t title; + std::conditional_t keyboard; + std::conditional_t mouse; + Window() requires (Framebuffer && !Title && !Keyboard && !Mouse) : framebuffer(*this) {} + Window(std::uint_fast32_t width, std::uint_fast32_t height) requires (Framebuffer && !Title && !Keyboard && !Mouse) : framebuffer(*this, width, height) {} - Window(std::string name, std::uint32_t width, std::uint32_t height); - ~Window(); - void StartSync(); - ScaleData ScaleElement(const UiElement& element); + Window() requires (!Framebuffer &&!Title && !Keyboard && !Mouse) : title(*this) {} + Window(const std::string_view title) requires (!Framebuffer &&!Title && !Keyboard && !Mouse) : title(*this, title) {} - protected: - bool configured = false; - wl_shm* shm = NULL; - wl_seat* seat = NULL; - xdg_toplevel* xdgToplevel = NULL; - xdg_wm_base* xdgWmBase = NULL; - zxdg_decoration_manager_v1* manager = NULL; - wl_surface* surface = NULL; - wl_buffer* buffer = NULL; - wl_buffer* backBuffer = NULL; - xdg_surface* xdgSurface = NULL; - wl_display* display = NULL; - wl_callback* cb = nullptr; - inline static wl_compositor* compositor = NULL; - static wl_pointer_listener pointer_listener; - static wl_keyboard_listener keyboard_listener; - static wl_seat_listener seat_listener; - static wl_registry_listener registry_listener; - static xdg_surface_listener xdg_surface_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 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 PointerListenerHandleLeave(void*, wl_pointer*, std::uint32_t, wl_surface*); - static void xdg_toplevel_handle_close(void* data, xdg_toplevel*); - static void handle_global(void* data, wl_registry* registry, std::uint32_t name, const char* interface, std::uint32_t version); - static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state); - static void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities); - static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial); + Window(std::uint_fast32_t width, std::uint_fast32_t height, const std::string_view title) requires (Framebuffer && Title && !Keyboard && !Mouse) : framebuffer(*this, width, height), title(*this, title) {} + + ~Window() { + std::cout << "destr" << std::endl; + #ifdef CRAFTER_GRAPHICS_WAYLAND + xdg_toplevel_destroy(xdgToplevel); + xdg_surface_destroy(xdgSurface); + wl_surface_destroy(surface); + #endif + } + + // template + // UiElement& AddElement(float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires(Buffer == true) { + + // } + template + UiElement& CreateElement(std::uint_fast32_t width, std::uint_fast32_t height, float anchorX, float anchorY, float relativeWidth, float relativeHeight, float anchorOffsetX = 0.5, float anchorOffsetY = 0.5, float z = 0, bool ignoreScaling = false) requires(UiBuffer && Framebuffer) { + std::unique_ptr> element = std::make_unique>(width, height, anchorX, anchorY, relativeWidth, relativeHeight, anchorOffsetX, anchorOffsetY, z, ignoreScaling); + UiElement* ptr = element.get(); + framebuffer.elements.push_back(&element->buffer); + if constexpr(UiMouse) { + mouse.elements.push_back(&element->mouse); + } + elements.emplace_back(std::move(element)); + return *ptr; + } + template + void RemoveElement(UiElement& element) { + if constexpr(UiBuffer) { + framebuffer.elements.erase(std::find(framebuffer.elements.begin(), framebuffer.elements.end(), &element.buffer)); + } + for(std::uint_fast32_t i = 0; i < elements.size(); i++) { + if(elements[i].get() == &element) { + elements.erase(elements.begin() + i); + break; + } + } + } + + private: + #ifdef CRAFTER_GRAPHICS_WAYLAND + void InitWayland() { + std::cout << "bruh" << std::endl; + display = wl_display_connect(NULL); + if (display == NULL) { + std::cerr << "failed to create display" << std::endl; + } + + wl_registry* registry = wl_display_get_registry(display); + wl_registry_add_listener(registry, ®istry_listener, this); + if (wl_display_roundtrip(display) == -1) { + exit(EXIT_FAILURE); + } + if (shm == NULL || compositor == NULL || xdgWmBase == NULL) { + std::cerr << "no wl_shm, wl_compositor or xdg_wm_base support" << std::endl; + exit(EXIT_FAILURE); + } + + surface = wl_compositor_create_surface(compositor); + xdgSurface = xdg_wm_base_get_xdg_surface(xdgWmBase, surface); + xdgToplevel = xdg_surface_get_toplevel(xdgSurface); + cb = wl_surface_frame(surface); + + //wl_callback_add_listener(cb, &surface_frame_listener, this); + xdg_surface_add_listener(xdgSurface, &xdg_surface_listener, this); + xdg_toplevel_add_listener(xdgToplevel, &xdg_toplevel_listener, this); + wl_surface_commit(surface); + + while (wl_display_dispatch(display) != -1 && !configured) {} + + wl_surface_commit(surface); + + zxdg_toplevel_decoration_v1* decoration = zxdg_decoration_manager_v1_get_toplevel_decoration(manager, xdgToplevel); + zxdg_toplevel_decoration_v1_set_mode(decoration, ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); + + wl_surface_commit(surface); + std::cout << "bruh" << std::endl; + open = true; + } + + static void wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time) { + wl_callback_destroy(cb); + Window* window = reinterpret_cast*>(data); + if(window->onUpdate.listeners.size() > 0) { + cb = wl_surface_frame(window->surface); + wl_callback_add_listener(cb, &WindowBase::surface_frame_listener, window); + } + } + constexpr static wl_callback_listener surface_frame_listener = { + .done = wl_surface_frame_done, + }; + + static void xdg_wm_base_handle_ping(void* data, xdg_wm_base* xdg_wm_base, std::uint32_t serial) { + xdg_wm_base_pong(xdg_wm_base, serial); + } + + constexpr static xdg_wm_base_listener xdgWmBaseListener = { + .ping = xdg_wm_base_handle_ping, + }; + + static void pointer_handle_button(void* data, wl_pointer* pointer, std::uint32_t serial, std::uint32_t time, std::uint32_t button, std::uint32_t state) { + Window* window = reinterpret_cast<*>(data); + if (button == BTN_LEFT) { + if(state == WL_POINTER_BUTTON_STATE_PRESSED) { + window->mouseLeftHeld = true; + window->mouse.onMouseLeftClick.Invoke(window->mouse.currentMousePos); + if constexpr (Framebuffer) { + for(UiElementMouse& element : window->mouse.elements) { + ScaleData data = window->framebuffer.ScaleElement(element); + if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) { + element.onMouseLeftClick.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y}); + } + } + } + } else { + window->mouse.mouseLeftHeld = false; + window->mouse.onMouseLeftRelease.Invoke(window->mouse.currentMousePos); + for(UiElement& element : window->mouse.elements) { + ScaleData data = window->mouse.ScaleElement(element); + if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) { + element.onMouseLeftRelease.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y}); + } + } + } + } else if(button == BTN_RIGHT){ + if(state == WL_POINTER_BUTTON_STATE_PRESSED) { + window->mouse.mouseRightHeld = true; + window->mouse.onMouseRightClick.Invoke(window->mouse.currentMousePos); + for(UiElement& element : window->mouse.elements) { + ScaleData data = window->mouse.ScaleElement(element); + if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) { + element.onMouseRightClick.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y}); + } + } + } else { + window->mouse.mouseRightHeld = true; + window->mouse.onMouseRightRelease.Invoke(window->mouse.currentMousePos); + for(UiElement& element : window->mouse.elements) { + ScaleData data = window->mouse.ScaleElement(element); + if(window->mouse.currentMousePos.x >= data.x && window->mouse.currentMousePos.x <= data.x+data.width && window->mouse.currentMousePos.y > data.y && window->mouse.currentMousePos.y < data.y+data.height) { + element.onMouseRightRelease.Invoke({window->mouse.currentMousePos.x-data.x, window->mouse.currentMousePos.y-data.y}); + } + } + } + } + } + + static void PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, uint time, wl_fixed_t surface_x, wl_fixed_t surface_y) { + * window = reinterpret_cast<*>(data); + MousePoint pos = {wl_fixed_to_double(surface_x), wl_fixed_to_double(surface_y)}; + window->mouse.lastMousePos = window->currentMousePos; + window->currentMousePos = pos; + window->mouseDelta = {window->currentMousePos.x-window->lastMousePos.x, window->currentMousePos.y-window->lastMousePos.y}; + window->onMouseMove.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta}); + for(UiElement& element : window->elements) { + ScaleData data = window->ScaleElement(element); + if(window->currentMousePos.x >= data.x && window->currentMousePos.x <= data.x+data.width && window->currentMousePos.y > data.y && window->currentMousePos.y < data.y+data.height) { + element.onMouseMove.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); + if(!(window->lastMousePos.x >= data.x && window->lastMousePos.x <= data.x+data.width && window->lastMousePos.y > data.y && window->lastMousePos.y < data.y+data.height)) { + element.onMouseEnter.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.y}); + } + } else if(window->lastMousePos.x >= data.x && window->lastMousePos.x <= data.x+data.width && window->lastMousePos.y > data.y && window->lastMousePos.y < data.y+data.height) { + element.onMouseLeave.Invoke({window->currentMousePos.x-data.x, window->currentMousePos.y-data.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) { + * window = reinterpret_cast<*>(data); + window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta}); + } + + static void PointerListenerHandleLeave(void* data, wl_pointer*, std::uint32_t, wl_surface*) { + * window = reinterpret_cast<*>(data); + window->onMouseEnter.Invoke({window->lastMousePos, window->currentMousePos, window->mouseDelta}); + } + + static void PointerListenerHandleAxis(void*, wl_pointer*, std::uint32_t, std::uint32_t, wl_fixed_t value) { + + } + + constexpr static wl_pointer_listener pointer_listener = { + .enter = PointerListenerHandleEnter, + .leave = PointerListenerHandleLeave, + .motion = PointerListenerHandleMotion, + .button = pointer_handle_button, + .axis = PointerListenerHandleAxis, + }; + + xkb_keymap* xkb_keymap; + xkb_context* xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + xkb_state* xkb_state; + + + static void keyboard_keymap(void *data, wl_keyboard *keyboard, uint32_t format, int fd, uint32_t size) { + if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { + close(fd); + fprintf(stderr, "Unsupported keymap format\n"); + return; + } + + void *map = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + if (map == MAP_FAILED) { + close(fd); + perror("mmap"); + return; + } + + xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + xkb_keymap = xkb_keymap_new_from_string(xkb_context, (const char *)map, XKB_KEYMAP_FORMAT_TEXT_V1,XKB_KEYMAP_COMPILE_NO_FLAGS); + munmap(map, size); + close(fd); + + xkb_state = xkb_state_new(xkb_keymap); + } + + static void keyboard_enter(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface, wl_array *keys) { + + } + + static void keyboard_leave(void *data, wl_keyboard *keyboard, uint32_t serial, wl_surface *surface) { + + } + + static void keyboard_key(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t time, uint32_t key, uint32_t state) { + if (!xkb_state) { + return; + } + + Window* window = reinterpret_cast*>(data); + + xkb_keycode_t keycode = key + 8; + xkb_keysym_t keysym = xkb_state_key_get_one_sym(xkb_state, keycode); + + char utf8[8] = {0}; + int len = xkb_keysym_to_utf8(keysym, utf8, sizeof(utf8)); + if (len != 0) { + char keypress = utf8[0]; + if(state == WL_KEYBOARD_KEY_STATE_PRESSED) { + if(window->heldkeys[keypress]) { + window->onKeyHold[keypress].Invoke(); + window->onAnyKeyHold.Invoke(keypress); + } else{ + window->heldkeys[keypress] = true; + window->onKeyDown[keypress].Invoke(); + window->onAnyKeyDown.Invoke(keypress); + } + } else{ + window->heldkeys[keypress] = false; + window->onKeyUp[keypress].Invoke(); + window->onAnyKeyUp.Invoke(keypress); + } + + } else { + // // fallback for keys like Return, Escape, etc. + // char name[64]; + // if (xkb_keysym_get_name(keysym, name, sizeof(name)) > 0) { + // printf("Key %s pressed (non-printable or multi-char)\n", name); + // } + } + } + + static void keyboard_modifiers(void *data, wl_keyboard *keyboard, uint32_t serial, uint32_t mods_depressed, uint32_t mods_latched, uint32_t mods_locked, uint32_t group) { + + } + + static void keyboard_repeat_info(void *data, wl_keyboard *keyboard, int32_t rate, int32_t delay) { + + } + + constexpr static wl_keyboard_listener keyboard_listener = { + .keymap = keyboard_keymap, + .enter = keyboard_enter, + .leave = keyboard_leave, + .key = keyboard_key, + .modifiers = keyboard_modifiers, + .repeat_info = keyboard_repeat_info, + }; + + void seat_handle_capabilities(void* data, wl_seat* seat, uint32_t capabilities) { + Window* window = reinterpret_cast*>(data); + window->seat = seat; + if constexpr(Mouse) { + wl_pointer* pointer = wl_seat_get_pointer(seat); + wl_pointer_add_listener(pointer, &pointer_listener, window); + } + if constexpr(Keyboard) { + wl_keyboard* keyboard = wl_seat_get_keyboard(seat); + wl_keyboard_add_listener(keyboard, &keyboard_listener, window); + } + } + constexpr static wl_seat_listener seat_listener = { + .capabilities = seat_handle_capabilities, + }; + + + static void handle_global(void *data, wl_registry *registry, std::uint32_t name, const char *interface, std::uint32_t version) { + Window* window = reinterpret_cast*>(data); + if (strcmp(interface, wl_shm_interface.name) == 0) { + window->shm = reinterpret_cast(wl_registry_bind(registry, name, &wl_shm_interface, 1)); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + if constexpr (Keyboard || Mouse) { + wl_seat* seat = reinterpret_cast(wl_registry_bind(registry, name, &wl_seat_interface, 1)); + wl_seat_add_listener(seat, &seat_listener, window); + } + } else if (compositor == NULL && strcmp(interface, wl_compositor_interface.name) == 0) { + compositor = reinterpret_cast(wl_registry_bind(registry, name, &wl_compositor_interface, 1)); + } else if (strcmp(interface, xdg_wm_base_interface.name) == 0) { + window->xdgWmBase = reinterpret_cast(wl_registry_bind(registry, name, &xdg_wm_base_interface, 1)); + xdg_wm_base_add_listener(window->xdgWmBase, &xdgWmBaseListener, NULL); + } else if (strcmp(interface, zxdg_decoration_manager_v1_interface.name) == 0) { + window->manager = reinterpret_cast(wl_registry_bind(registry, name, &zxdg_decoration_manager_v1_interface, 1)); + } + } + void handle_global_remove(void* data, wl_registry* registry, uint32_t name) {} + constexpr wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = handle_global_remove, + }; + + static void xdg_surface_handle_configure(void* data, xdg_surface* xdg_surface, std::uint32_t serial) { + Window* window = reinterpret_cast*>(data); + // The compositor configures our surface, acknowledge the configure event + xdg_surface_ack_configure(xdg_surface, serial); + + if (window->configured) { + // If this isn't the first configure event we've received, we already + // have a buffer attached, so no need to do anything. Commit the + // surface to apply the configure acknowledgement. + wl_surface_commit(window->surface); + } + + window->configured = true; + } + constexpr xdg_surface_listener xdg_surface_listener = { + .configure = xdg_surface_handle_configure, + }; + + void noop5(void*, xdg_toplevel*, std::int32_t, std::int32_t, wl_array*){} + static void xdg_toplevel_handle_close(void* data, xdg_toplevel*) { + WindowBase* window = reinterpret_cast(data); + window->onClose.Invoke(); + window->open = false; + } + constexpr xdg_toplevel_listener xdg_toplevel_listener = { + .configure = noop5, + .close = WindowBase::xdg_toplevel_handle_close, + }; + #endif }; } \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-WindowBase.cppm b/interfaces/Crafter.Graphics-WindowBase.cppm new file mode 100644 index 0000000..84ee4d4 --- /dev/null +++ b/interfaces/Crafter.Graphics-WindowBase.cppm @@ -0,0 +1,72 @@ +/* +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; + +#ifdef CRAFTER_GRAPHICS_WAYLAND +#include +#include "../lib/xdg-shell-client-protocol.h" +#include "../lib/wayland-xdg-decoration-unstable-v1-client-protocol.h" +#include +#include +#include +#include +#endif + +export module Crafter.Graphics:WindowBase; +import std; +import :Types; +import Crafter.Event; + +export namespace Crafter { + enum WindowCapabilities { + CRAFTER_WINDOWCAPTABILITIES_FRAMEBUFFER = 0b1, + CRAFTER_WINDOWCAPTABILITIES_KEYBOARD = 0b10, + CRAFTER_WINDOWCAPTABILITIES_MOUSE = 0b100, + }; + + class UiElementBase; + class WindowBase { + public: + Event onClose; + Event onUpdate; + bool open; + std::vector> elements; + WindowBase(WindowBase&) = delete; + WindowBase(WindowBase&&) = delete; + WindowBase& operator=(const WindowBase&) = delete; + void StartSync(); + void StartUpdate(void* window); + void StopUpdate(); + #ifdef CRAFTER_GRAPHICS_WAYLAND + WindowBase(); + ~WindowBase(); + #endif + protected: + friend class WindowKeyboard; + friend class WindowFramebuffer; + friend class WindowTitle; + #ifdef CRAFTER_GRAPHICS_WAYLAND + wl_surface* surface = nullptr; + wl_callback* cb = nullptr; + static void wl_surface_frame_done(void* data, struct wl_callback *cb, uint32_t time); + static wl_callback_listener surface_frame_listener; + #endif + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-WindowFramebuffer.cppm b/interfaces/Crafter.Graphics-WindowFramebuffer.cppm new file mode 100644 index 0000000..42d459e --- /dev/null +++ b/interfaces/Crafter.Graphics-WindowFramebuffer.cppm @@ -0,0 +1,61 @@ +/* +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; + +#ifdef CRAFTER_GRAPHICS_WAYLAND +#include +#endif + +export module Crafter.Graphics:WindowFramebuffer; +import std; +import :Types; +import :UiElement; +import :WindowBase; +import Crafter.Event; + +export namespace Crafter { + class WindowBase; + class UiElementBase; + class UiElementBuffer; + class WindowFramebuffer { + public: + std::vector elements; + Pixel_BU8_GU8_RU8_AU8* framebuffer; + std::uint_fast32_t width; + std::uint_fast32_t height; + float scale = 1; + WindowFramebuffer(WindowBase& window); + WindowFramebuffer(WindowBase& window, std::uint_fast32_t width, std::uint_fast32_t height); + WindowFramebuffer(WindowFramebuffer&) = delete; + WindowFramebuffer(WindowFramebuffer&&) = delete; + WindowFramebuffer& operator=(const WindowFramebuffer&) = delete; + ScaleData ScaleElement(const UiElementBase& element); + + void Render(); + void Create(std::uint_fast32_t width, std::uint_fast32_t height); + void Resize(std::uint_fast32_t width, std::uint_fast32_t height); + void Destroy(); + ~WindowFramebuffer(); + private: + WindowBase& window; + #ifdef CRAFTER_GRAPHICS_WAYLAND + wl_buffer* buffer; + #endif + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-WindowKeyboard.cppm b/interfaces/Crafter.Graphics-WindowKeyboard.cppm new file mode 100644 index 0000000..d209a9a --- /dev/null +++ b/interfaces/Crafter.Graphics-WindowKeyboard.cppm @@ -0,0 +1,35 @@ +/* +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 +*/ + +export module Crafter.Graphics:WindowKeyboard; +import std; +import Crafter.Event; + +export namespace Crafter { + class WindowKeyboard { + public: + bool heldkeys[255] = {}; + Event onKeyDown[255]; + Event onKeyHold[255]; + Event onKeyUp[255]; + Event onAnyKeyDown; + Event onAnyKeyHold; + Event onAnyKeyUp; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-WindowMouse.cppm b/interfaces/Crafter.Graphics-WindowMouse.cppm new file mode 100644 index 0000000..718835e --- /dev/null +++ b/interfaces/Crafter.Graphics-WindowMouse.cppm @@ -0,0 +1,46 @@ +/* +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 +*/ + +export module Crafter.Graphics:WindowKeyboard; +import std; +import Crafter.Event; + +export namespace Crafter { + class UiElementMouse; + class WindowKeyboard { + public: + std::vector elements; + Event onMouseRightClick; + Event onMouseLeftClick; + Event onMouseRightHold; + Event onMouseLeftHold; + Event onMouseRightRelease; + Event onMouseLeftRelease; + Event onMouseMove; + Event onMouseEnter; + Event onMouseLeave; + Event onMouseScroll; + Event onClose; + MousePoint currentMousePos; + MousePoint lastMousePos; + MousePoint mouseDelta; + bool mouseLeftHeld = false; + bool mouseRightHeld = false; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics-WindowTitle.cppm b/interfaces/Crafter.Graphics-WindowTitle.cppm new file mode 100644 index 0000000..9a772e0 --- /dev/null +++ b/interfaces/Crafter.Graphics-WindowTitle.cppm @@ -0,0 +1,40 @@ +/* +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; + +#ifdef CRAFTER_GRAPHICS_WAYLAND +#include +#endif + +export module Crafter.Graphics:WindowTitle; +import std; +import :Types; +import Crafter.Event; + +export namespace Crafter { + class WindowBase; + class WindowTitle { + public: + WindowTitle(WindowBase& window); + WindowTitle(WindowBase& window, const std::string_view title); + void Set(const std::string_view title); + private: + WindowBase& window; + }; +} \ No newline at end of file diff --git a/interfaces/Crafter.Graphics.cppm b/interfaces/Crafter.Graphics.cppm index 3163190..c0a0fae 100644 --- a/interfaces/Crafter.Graphics.cppm +++ b/interfaces/Crafter.Graphics.cppm @@ -22,8 +22,13 @@ export module Crafter.Graphics; export import :Window; export import :UiElement; +export import :UiElementBuffer; +export import :UiElementMouse; export import :Types; export import :Font; +export import :WindowKeyboard; +export import :WindowFramebuffer; +export import :Shm; // export import :WindowWaylandVulkan; // export import :VulkanBuffer; diff --git a/project.json b/project.json index 4d7d13a..db93ce9 100644 --- a/project.json +++ b/project.json @@ -3,66 +3,22 @@ "configurations": [ { "name": "base", - "implementations": ["implementations/Crafter.Graphics-Window", "implementations/Crafter.Graphics-UiElement", "implementations/Crafter.Graphics-Font"], - "interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-UiElement", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font"], + "implementations": ["implementations/Crafter.Graphics-Font", "implementations/Crafter.Graphics-Shm", "implementations/Crafter.Graphics-WindowFramebuffer", "implementations/Crafter.Graphics-WindowFramebuffer", "implementations/Crafter.Graphics-UiElement"], + "interfaces": ["interfaces/Crafter.Graphics-Window", "interfaces/Crafter.Graphics-WindowBase", "interfaces/Crafter.Graphics", "interfaces/Crafter.Graphics-Types", "interfaces/Crafter.Graphics-Font", "interfaces/Crafter.Graphics-WindowKeyboard", "interfaces/Crafter.Graphics-WindowFramebuffer", "interfaces/Crafter.Graphics-WindowTitle", "interfaces/Crafter.Graphics-Shm", "interfaces/Crafter.Graphics-UiElement", "interfaces/Crafter.Graphics-UiElementBuffer", "interfaces/Crafter.Graphics-UiElementMouse"], "type": "library" }, { "name": "wayland", - "implementations": [], + "implementations": ["implementations/Crafter.Graphics-Window_wayland", "implementations/Crafter.Graphics-WindowFramebuffer_wayland", "implementations/Crafter.Graphics-WindowTitle_wayland", "implementations/Crafter.Graphics-UiElementBuffer"], "interfaces": [], "libs": ["wayland-client", "xkbcommon"], "c_files": ["lib/xdg-shell-protocol", "lib/wayland-xdg-decoration-unstable-v1-client-protocol"], - "extends": ["base"] - }, - { - "name": "vulkan", - "implementations": ["implementations/Crafter.Graphics-VulkanDevice", "implementations/Crafter.Graphics-WindowWaylandVulkan"], - "interfaces": ["interfaces/Crafter.Graphics-WindowWayland", "interfaces/Crafter.Graphics-WindowWaylandWayland", "interfaces/Crafter.Graphics-VulkanDevice", "interfaces/Crafter.Graphics-VulkanPipeline", "interfaces/Crafter.Graphics-VulkanShader", "interfaces/Crafter.Graphics-WindowWaylandVulkan", "interfaces/Crafter.Graphics-VulkanBuffer", "interfaces/Crafter.Graphics-Mesh", "interfaces/Crafter.Graphics-MeshShader", "interfaces/Crafter.Graphics-HeightmapShader","interfaces/Crafter.Graphics-VoxelShader", "interfaces/Crafter.Graphics-VulkanTexture", "interfaces/Crafter.Graphics-TextureShader", "interfaces/Crafter.Graphics-DescriptorSet"], - "libs": ["vulkan"], - "shaders": [ + "extends": ["base"], + "defines": [ { - "path":"src/shader/MeshShaderXYZ.glsl", - "type":13, - "entrypoint":"main" - }, - { - "path":"src/shader/MeshShaderXYZUV.glsl", - "type":13, - "entrypoint":"main" - }, - { - "path":"src/shader/MeshShaderXYZRGBA.glsl", - "type":13, - "entrypoint":"main" - }, - { - "path":"src/shader/MeshShaderHeightmapRGBA.glsl", - "type":13, - "entrypoint":"main" - }, - { - "path":"src/shader/MeshShaderMixedVoxelGrid.glsl", - "type":13, - "entrypoint":"main" - }, - { - "path":"src/shader/FragmentShaderSolidWhite.glsl", - "type":4, - "entrypoint":"main" - }, - { - "path":"src/shader/FragmentShaderTexture.glsl", - "type":4, - "entrypoint":"main" - }, - { - "path":"src/shader/FragmentShaderVertexColor.glsl", - "type":4, - "entrypoint":"main" + "name": "CRAFTER_GRAPHICS_WAYLAND" } - ], - "extends": ["wayland"] + ] }, { "name": "deps",