rendering improvements

This commit is contained in:
Jorijn van der Graaf 2026-03-12 01:07:46 +01:00
commit 7f46ac13fa
14 changed files with 296 additions and 179 deletions

View file

@ -340,10 +340,12 @@ void Device::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, std
if(element) {
if(Device::focusedWindow->currentMousePos.x >= element->scaled.position.x && Device::focusedWindow->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->currentMousePos.y > element->scaled.position.y && Device::focusedWindow->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
element->onMouseMove.Invoke();
if(!(Device::focusedWindow->lastMousePos.x >= element->scaled.position.x && Device::focusedWindow->lastMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->lastMousePos.y > element->scaled.position.y && Device::focusedWindow->lastMousePos.y < element->scaled.position.y+element->scaled.size.y)) {
if(!element->mouseHover) {
element->mouseHover = true;
element->onMouseEnter.Invoke();
}
} else if(Device::focusedWindow->lastMousePos.x >= element->scaled.position.x && Device::focusedWindow->lastMousePos.x <= element->scaled.position.x+element->scaled.size.x && Device::focusedWindow->lastMousePos.y > element->scaled.position.y && Device::focusedWindow->lastMousePos.y < element->scaled.position.y+element->scaled.size.y) {
} else if(element->mouseHover) {
element->mouseHover = false;
element->onMouseLeave.Invoke();
}
}
@ -352,8 +354,12 @@ void Device::PointerListenerHandleMotion(void* data, wl_pointer* wl_pointer, std
}
void Device::PointerListenerHandleEnter(void* data, wl_pointer* wl_pointer, std::uint32_t serial, wl_surface* surface, wl_fixed_t surface_x, wl_fixed_t surface_y) {
Device::wlPointer = wl_pointer;
for(Window* window : windows) {
if(window->surface == surface) {
if(window->cursorSurface != nullptr) {
wl_pointer_set_cursor(wl_pointer, serial, window->cursorSurface, 0, 0);
}
focusedWindow = window;
window->onMouseEnter.Invoke();
return;

View file

@ -29,7 +29,7 @@ using namespace Crafter;
Font::Font(const std::filesystem::path& fontFilePath) {
// 1. Load the font file into memory
std::ifstream fontFile("inter.ttf", std::ios::binary | std::ios::ate);
std::ifstream fontFile(fontFilePath, std::ios::binary | std::ios::ate);
if (!fontFile.is_open()) {
std::cerr << "Failed to open font file\n";
}

View file

@ -57,11 +57,6 @@ void GridElement::UpdatePositionScaled(RendertargetBase& window) {
}
}
void GridElement::UpdatePosition(RendertargetBase& window) {
ScaleElement(window.transform);
UpdatePositionScaled(window);
}
void GridElement::UpdatePosition(RendertargetBase& window, Transform2D& parent) {
ScaleElement(parent);
UpdatePositionScaled(window);

View file

@ -25,9 +25,9 @@ module Crafter.Graphics:RenderingElement3D_impl;
import :RenderingElement3D;
import std;
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
using namespace Crafter;
#ifdef CRAFTER_GRAPHICS_RENDERER_VULKAN
std::vector<RenderingElement3D*> RenderingElement3D::elements;

View file

@ -36,13 +36,6 @@ Transform2D::Transform2D(Anchor2D anchor) : anchor(anchor) {
}
void Transform2D::UpdatePosition(RendertargetBase& window) {
ScaleElement(window.transform);
for(Transform2D* child : children) {
child->UpdatePosition(window, *this);
}
}
void Transform2D::UpdatePosition(RendertargetBase& window, Transform2D& parent) {
ScaleElement(parent);
for(Transform2D* child : children) {

View file

@ -69,7 +69,6 @@ using namespace Crafter;
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
#ifdef CRAFTER_GRAPHICS_RENDERER_SOFTWARE
void randname(char *buf) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
@ -113,7 +112,6 @@ int create_shm_file(off_t size) {
return fd;
}
#endif
#endif
#ifdef CRAFTER_GRAPHICS_WINDOW_WIN32
CrafterKeys vk_to_crafter_key(WPARAM vk)
@ -304,10 +302,12 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
if(element) {
if(window->currentMousePos.x >= element->scaled.position.x && window->currentMousePos.x <= element->scaled.position.x+element->scaled.size.x && window->currentMousePos.y > element->scaled.position.y && window->currentMousePos.y < element->scaled.position.y+element->scaled.size.y) {
element->onMouseMove.Invoke();
if(!(window->lastMousePos.x >= element->scaled.position.x && window->lastMousePos.x <= element->scaled.position.x+element->scaled.size.x && window->lastMousePos.y > element->scaled.position.y && window->lastMousePos.y < element->scaled.position.y+element->scaled.size.y)) {
if(!element->mouseHover) {
element->mouseHover = true;
element->onMouseEnter.Invoke();
}
} else if(window->lastMousePos.x >= element->scaled.position.x && window->lastMousePos.x <= element->scaled.position.x+element->scaled.size.x && window->lastMousePos.y > element->scaled.position.y && window->lastMousePos.y < element->scaled.position.y+element->scaled.size.y) {
} else if(element->mouseHover) {
element->mouseHover = false
element->onMouseLeave.Invoke();
}
}
@ -380,7 +380,7 @@ Window::Window(std::uint32_t width, std::uint32_t height, const std::string_view
}
#ifdef CRAFTER_GRAPHICS_RENDERER_SOFTWARE
Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height(height) : Rendertarget(width, height) {
Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height(height), renderer(width, height) {
#else
Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height(height) {
#endif
@ -420,9 +420,7 @@ Window::Window(std::uint32_t width, std::uint32_t height) : width(width), height
}
// Map the shared memory file
renderer.buffer = reinterpret_cast<Vector<std::uint8_t, 4>*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
renderer.sizeX = width;
renderer.sizeY = height;
renderer.buffer = reinterpret_cast<Vector<std::uint8_t, 4, 4>*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (renderer.buffer == MAP_FAILED) {
throw std::runtime_error("mmap failed");
}
@ -563,6 +561,61 @@ void Window::SetTitle(const std::string_view title) {
#endif
}
void Window::SetCusorImage(std::uint16_t sizeX, std::uint16_t sizeY) {
new (&cursorRenderer) Rendertarget<std::uint8_t, 4, 4>(sizeX, sizeY);
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
if(cursorSurface == nullptr) {
cursorSurface = wl_compositor_create_surface(Device::compositor);
} else {
wl_buffer_destroy(cursorWlBuffer);
munmap(cursorRenderer.buffer, cursorBufferOldSize);
}
int stride = sizeX * 4;
int size = stride * sizeY;
cursorBufferOldSize = size;
// Allocate a shared memory file with the right size
int fd = create_shm_file(size);
if (fd < 0) {
throw std::runtime_error(std::format("creating a buffer file for {}B failed", size));
}
cursorRenderer.buffer = reinterpret_cast<Vector<std::uint8_t, 4, 4>*>(mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0));
if (cursorRenderer.buffer == MAP_FAILED) {
throw std::runtime_error("mmap failed");
}
wl_shm_pool *pool = wl_shm_create_pool(Device::shm, fd, size);
cursorWlBuffer = wl_shm_pool_create_buffer(pool, 0, sizeX, sizeY, stride, WL_SHM_FORMAT_ARGB8888);
wl_shm_pool_destroy(pool);
close(fd);
wl_surface_attach(cursorSurface, cursorWlBuffer, 0, 0);
wl_surface_damage(cursorSurface, 0, 0, sizeX, sizeY);
wl_surface_commit(cursorSurface);
#endif
}
void Window::SetCusorImageDefault() {
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
wl_buffer_destroy(cursorWlBuffer);
wl_surface_destroy(cursorSurface);
cursorSurface = nullptr;
#endif
}
void Window::UpdateCursorImage() {
cursorRenderer.Render();
for(std::uint32_t i = 0; i < cursorBufferOldSize / 4; i++) {
std::swap(cursorRenderer.buffer[i].b, cursorRenderer.buffer[i].r);
}
wl_surface_attach(cursorSurface, cursorWlBuffer, 0, 0);
wl_surface_damage(cursorSurface, 0, 0, 9999999, 99999999);
wl_surface_commit(cursorSurface);
}
void Window::StartSync() {
#ifdef CRAFTER_GRAPHICS_WINDOW_WAYLAND
while (open && wl_display_dispatch(Device::display) != -1) {