rendering improvements
This commit is contained in:
parent
789bb307d5
commit
7f46ac13fa
14 changed files with 296 additions and 179 deletions
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue