summaryrefslogtreecommitdiff
path: root/src/platform.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-03 18:44:41 +0100
committerskal <pascal.massimino@gmail.com>2026-02-03 18:44:41 +0100
commitbf46e44e1cb6027a072819a2a3aa3be32651f6e1 (patch)
tree21267e7ef52fd91e7b99271ed87e275e91b3de3c /src/platform.cc
parent815c428dea14a6a1ea5c421c400985d0c14d473d (diff)
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.
Diffstat (limited to 'src/platform.cc')
-rw-r--r--src/platform.cc67
1 files changed, 46 insertions, 21 deletions
diff --git a/src/platform.cc b/src/platform.cc
index 5d5c082..fbd1d51 100644
--- a/src/platform.cc
+++ b/src/platform.cc
@@ -14,6 +14,9 @@ static void framebuffer_size_callback(GLFWwindow* window, int width,
if (state) {
state->width = width;
state->height = height;
+ if (height > 0) {
+ state->aspect_ratio = (float)width / (float)height;
+ }
}
}
@@ -32,37 +35,49 @@ static void glfw_key_callback(GLFWwindow* window, int key, int scancode,
// --- Public API Implementation ---
-void platform_init(PlatformState* state, bool fullscreen, int* width_ptr,
- int* height_ptr) {
- if (width_ptr && height_ptr) {
- state->width = *width_ptr;
- state->height = *height_ptr;
- }
+PlatformState platform_init(bool fullscreen, int width, int height) {
+ PlatformState state = {};
+ state.width = width;
+ state.height = height;
+ state.is_fullscreen = fullscreen;
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- state->window = glfwCreateWindow(state->width, state->height, "demo64k",
- nullptr, nullptr);
-
- // Store our state pointer in the window for callbacks
- glfwSetWindowUserPointer(state->window, state);
+ state.window =
+ glfwCreateWindow(state.width, state.height, "demo64k", nullptr, nullptr);
// Immediately query the actual framebuffer size for high-DPI displays
- glfwGetFramebufferSize(state->window, &state->width, &state->height);
+ glfwGetFramebufferSize(state.window, &state.width, &state.height);
+ if (state.height > 0) {
+ state.aspect_ratio = (float)state.width / (float)state.height;
+ }
- glfwSetKeyCallback(state->window, glfw_key_callback);
- glfwSetFramebufferSizeCallback(state->window, framebuffer_size_callback);
+ // Store our state in a static pointer or use the window pointer?
+ // We'll use a pointer to a persistent state.
+ // For this demo, we can assume the PlatformState persists in main().
+ // But we return by value. This is a bit tricky with callbacks.
+ // We'll fix the callbacks in platform_poll if needed, or just let main pass
+ // the pointer back.
+
+ glfwSetWindowUserPointer(state.window,
+ nullptr); // Will be set in main's state
+
+ glfwSetKeyCallback(state.window, glfw_key_callback);
+ glfwSetFramebufferSizeCallback(state.window, framebuffer_size_callback);
- state->is_fullscreen = fullscreen;
if (fullscreen) {
- glfwGetWindowPos(state->window, &state->windowed_x, &state->windowed_y);
- glfwGetWindowSize(state->window, &state->windowed_w, &state->windowed_h);
+ glfwGetWindowPos(state.window, &state.windowed_x, &state.windowed_y);
+ glfwGetWindowSize(state.window, &state.windowed_w, &state.windowed_h);
GLFWmonitor* monitor = glfwGetPrimaryMonitor();
const GLFWvidmode* mode = glfwGetVideoMode(monitor);
- glfwSetWindowMonitor(state->window, monitor, 0, 0, mode->width,
- mode->height, mode->refreshRate);
+ glfwSetWindowMonitor(state.window, monitor, 0, 0, mode->width, mode->height,
+ mode->refreshRate);
}
+
+ state.time = glfwGetTime();
+
+ return state;
}
void platform_shutdown(PlatformState* state) {
@@ -73,7 +88,16 @@ void platform_shutdown(PlatformState* state) {
}
void platform_poll(PlatformState* state) {
+ // Ensure the window has the current state pointer for callbacks
+ if (glfwGetWindowUserPointer(state->window) != state) {
+ glfwSetWindowUserPointer(state->window, state);
+ }
+
glfwPollEvents();
+ state->time = glfwGetTime();
+ if (state->height > 0) {
+ state->aspect_ratio = (float)state->width / (float)state->height;
+ }
}
bool platform_should_close(PlatformState* state) {
@@ -102,5 +126,6 @@ WGPUSurface platform_create_wgpu_surface(WGPUInstance instance,
return glfwCreateWindowWGPUSurface(instance, state->window);
}
-// Note: platform_get_* functions are now inline in the header.
-// platform_get_time() remains global.
+double platform_get_time() {
+ return glfwGetTime();
+} \ No newline at end of file