From bf46e44e1cb6027a072819a2a3aa3be32651f6e1 Mon Sep 17 00:00:00 2001 From: skal Date: Tue, 3 Feb 2026 18:44:41 +0100 Subject: refactor: Task #20 - Platform & Code Hygiene - Consolidated all WebGPU shims and platform-specific logic into src/platform.h. - Refactored platform_init to return PlatformState by value and platform_poll to automatically refresh time and aspect_ratio. - Removed STL dependencies (std::map, std::vector, std::string) from AssetManager and Procedural subsystems. - Fixed Windows cross-compilation by adjusting include paths and linker flags in CMakeLists.txt and updating build_win.sh. - Removed redundant direct inclusions of GLFW/glfw3.h and WebGPU headers across the project. - Applied clang-format and updated documentation. handoff(Gemini): Completed Task #20 and 20.1. Platform abstraction is now unified, and core paths are STL-free. Windows build is stable. --- src/platform.h | 106 +++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 76 insertions(+), 30 deletions(-) (limited to 'src/platform.h') diff --git a/src/platform.h b/src/platform.h index 152a38d..0a98850 100644 --- a/src/platform.h +++ b/src/platform.h @@ -1,7 +1,76 @@ // Handles windowing, input, and native surface creation. +// Consolidates platform-specific shims for WebGPU. #pragma once +#include +#include + +// WebGPU specific headers and shims +#if defined(DEMO_CROSS_COMPILE_WIN32) +#include +#include + +#define WGPUOptionalBool_True true +#define WGPUOptionalBool_False false +#define WGPUSType_ShaderSourceWGSL WGPUSType_ShaderModuleWGSLDescriptor +#define WGPU_DEPTH_SLICE_UNDEFINED 0xffffffff +#define WGPUSurfaceGetCurrentTextureStatus_SuccessOptimal \ + WGPUSurfaceGetCurrentTextureStatus_Success +#define WGPUSurfaceGetCurrentTextureStatus_SuccessSuboptimal \ + WGPUSurfaceGetCurrentTextureStatus_Success +#define WGPUCallbackMode_WaitAnyOnly 0 + +typedef WGPUShaderModuleWGSLDescriptor WGPUShaderSourceWGSL; + +static inline const char* str_view(const char* str) { + return str; +} +static inline const char* label_view(const char* str) { + return str; +} + +static inline void platform_wgpu_wait_any(WGPUInstance instance) { + wgpuInstanceProcessEvents(instance); +} +static inline void +platform_wgpu_set_error_callback(WGPUDevice device, + WGPUErrorCallback callback) { + wgpuDeviceSetUncapturedErrorCallback(device, callback, nullptr); +} + +#else +#include +#include + +static inline WGPUStringView str_view(const char* str) { + if (!str) + return {nullptr, 0}; + return {str, strlen(str)}; +} + +static inline WGPUStringView label_view(const char* str) { +#if !defined(STRIP_ALL) + if (!str) + return {nullptr, 0}; + return {str, strlen(str)}; +#else + (void)str; + return {nullptr, 0}; +#endif +} + +static inline void platform_wgpu_wait_any(WGPUInstance instance) { + wgpuInstanceWaitAny(instance, 0, nullptr, 0); +} +static inline void +platform_wgpu_set_error_callback(WGPUDevice device, + WGPUUncapturedErrorCallback callback) { + // Handled in descriptor for new API, but provided for compatibility if needed + // elsewhere +} +#endif + // Forward declare GLFWwindow to avoid including the full header here. struct GLFWwindow; @@ -9,46 +78,23 @@ struct PlatformState { GLFWwindow* window = nullptr; int width = 1280; int height = 720; + float aspect_ratio = 1.0f; + double time = 0.0; bool is_fullscreen = false; // Store windowed geometry for fullscreen toggle int windowed_x = 0, windowed_y = 0, windowed_w = 0, windowed_h = 0; }; -void platform_init(PlatformState* state, bool fullscreen, int* width_ptr, - int* height_ptr); +// Refactored platform API +PlatformState platform_init(bool fullscreen, int width, int height); void platform_shutdown(PlatformState* state); void platform_poll(PlatformState* state); bool platform_should_close(PlatformState* state); void platform_toggle_fullscreen(PlatformState* state); -// Inline getters for direct access -inline GLFWwindow* platform_get_window(PlatformState* state) { - return state->window; -} -inline int platform_get_width(PlatformState* state) { - return state->width; -} -inline int platform_get_height(PlatformState* state) { - return state->height; -} -inline float platform_get_aspect_ratio(PlatformState* state) { - if (state->height == 0) - return 1.0f; - return (float)state->width / (float)state->height; -} - -// glfwGetTime is a simple global query, so it doesn't need the state struct. -// Include the header directly to get the proper linkage. -#include -inline double platform_get_time() { - return glfwGetTime(); -} - // WebGPU specific surface creation -#if defined(DEMO_CROSS_COMPILE_WIN32) -#include -#else -#include -#endif WGPUSurface platform_create_wgpu_surface(WGPUInstance instance, PlatformState* state); + +// Global time query (if needed without state) +double platform_get_time(); \ No newline at end of file -- cgit v1.2.3