diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-02 09:28:28 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-02 09:28:28 +0100 |
| commit | 61139c8d9d655e07964d63ec1f5a091a7e8ab7d0 (patch) | |
| tree | 4cf3694702f24e96c1c76a339f7b3e4d4fdf5e70 /src/platform.h | |
| parent | 0b0067cb0a8db5ea5178501a12aacdef436a9845 (diff) | |
refactor(platform): Encapsulate state in PlatformState struct
- Replaced all global static variables in the platform layer with a single PlatformState struct.
- Updated all platform function signatures to accept a pointer to this struct, making the implementation stateless and more modular.
- Refactored main.cc, tests, and tools to instantiate and pass the PlatformState struct.
- This improves code organization and removes scattered global state.
Diffstat (limited to 'src/platform.h')
| -rw-r--r-- | src/platform.h | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/src/platform.h b/src/platform.h index 38a0568..2d2fc9e 100644 --- a/src/platform.h +++ b/src/platform.h @@ -1,25 +1,46 @@ -// This file is part of the 64k demo project. -// It defines the platform abstraction layer for windowing and input. -// Provides a consistent interface for GLFW-based operations. +// Handles windowing, input, and native surface creation. #pragma once -#include <webgpu.h> - // Forward declare GLFWwindow to avoid including the full header here. struct GLFWwindow; -void platform_init_window(bool fullscreen, int* width_ptr, int* height_ptr); -void platform_shutdown(); -void platform_poll(); -bool platform_should_close(); +struct PlatformState { + GLFWwindow* window = nullptr; + int width = 1280; + int height = 720; + 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); +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; +} -GLFWwindow* platform_get_window(); -double platform_get_time(); +// glfwGetTime is a simple global query, so it doesn't need the state struct. +// Include the header directly to get the proper linkage. +#include <GLFW/glfw3.h> +inline double platform_get_time() { + return glfwGetTime(); +} -// Add dimension getters -int platform_get_width(); -int platform_get_height(); -float platform_get_aspect_ratio(); -WGPUSurface platform_create_wgpu_surface(WGPUInstance instance); +// WebGPU specific surface creation +#if defined(DEMO_CROSS_COMPILE_WIN32) +#include <webgpu/webgpu.h> +#else +#include <wgpu.h> +#endif +WGPUSurface platform_create_wgpu_surface(WGPUInstance instance, PlatformState* state); |
