#include #include #include "renderwindow.h" #if defined(SDL_PLATFORM_LINUX) #include #include #endif RenderWindow InitRenderWindow(int width, int height, const char* title) { RenderWindow renderWindow = {0}; SDL_Init(SDL_INIT_VIDEO); renderWindow.window = SDL_CreateWindow(title, width, height, 0); renderWindow.wgpuInstance = wgpuCreateInstance(&(const WGPUInstanceDescriptor){ .requiredFeatureCount = 1, .requiredFeatures = (WGPUInstanceFeatureName[]) { WGPUInstanceFeatureName_TimedWaitAny } }); SDL_PropertiesID windowProperties = SDL_GetWindowProperties(renderWindow.window); #if defined(SDL_PLATFORM_LINUX) if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "x11") == 0) { Display* xdisplay = (Display *)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_X11_DISPLAY_POINTER, NULL); Window xwindow = (Window)SDL_GetNumberProperty(windowProperties, SDL_PROP_WINDOW_X11_WINDOW_NUMBER, 0); if (xdisplay && xwindow) { renderWindow.wgpuSurface = wgpuInstanceCreateSurface(renderWindow.wgpuInstance, &(const WGPUSurfaceDescriptor){ .nextInChain = (WGPUChainedStruct*)&(const WGPUSurfaceSourceXlibWindow) { .chain = (WGPUChainedStruct) { .next = NULL, .sType = WGPUSType_SurfaceSourceXlibWindow }, .display = xdisplay, .window = xwindow } }); } } else if (SDL_strcmp(SDL_GetCurrentVideoDriver(), "wayland") == 0) { struct wl_display* wl_display = (struct wl_display*)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WAYLAND_DISPLAY_POINTER, NULL); struct wl_surface* wl_surface = (struct wl_surface*)SDL_GetPointerProperty(windowProperties, SDL_PROP_WINDOW_WAYLAND_SURFACE_POINTER, NULL); if (wl_display && wl_surface) { renderWindow.wgpuSurface = wgpuInstanceCreateSurface(renderWindow.wgpuInstance, &(const WGPUSurfaceDescriptor){ .nextInChain = (WGPUChainedStruct*)&(const WGPUSurfaceSourceWaylandSurface) { .chain = (WGPUChainedStruct) { .next = NULL, .sType = WGPUSType_SurfaceSourceWaylandSurface }, .display = wl_display, .surface = wl_surface } }); } } #endif return renderWindow; } void QuitRenderWindow(RenderWindow* renderWindow) { wgpuSurfaceRelease(renderWindow->wgpuSurface); wgpuInstanceRelease(renderWindow->wgpuInstance); SDL_DestroyWindow(renderWindow->window); SDL_memset(renderWindow, 0, sizeof(RenderWindow)); SDL_Quit(); }