summaryrefslogtreecommitdiff
path: root/src/platform.cc
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-02 09:28:28 +0100
committerskal <pascal.massimino@gmail.com>2026-02-02 09:28:28 +0100
commit61139c8d9d655e07964d63ec1f5a091a7e8ab7d0 (patch)
tree4cf3694702f24e96c1c76a339f7b3e4d4fdf5e70 /src/platform.cc
parent0b0067cb0a8db5ea5178501a12aacdef436a9845 (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.cc')
-rw-r--r--src/platform.cc123
1 files changed, 48 insertions, 75 deletions
diff --git a/src/platform.cc b/src/platform.cc
index 464003f..8df9992 100644
--- a/src/platform.cc
+++ b/src/platform.cc
@@ -4,120 +4,93 @@
#include "platform.h"
#include "glfw3webgpu.h"
-
#include <GLFW/glfw3.h>
-static GLFWwindow* window = nullptr;
-static int windowed_x, windowed_y, windowed_w, windowed_h;
-static bool g_is_fullscreen = false;
-
-// --- New state for framebuffer size ---
-static int g_width = 1280;
-static int g_height = 720;
-
-// Forward declaration for callback
-void platform_toggle_fullscreen();
-
// --- Callbacks ---
-static void framebuffer_size_callback(GLFWwindow* cb_window, int width, int height) {
- (void)cb_window; // Unused
- g_width = width;
- g_height = height;
- // Note: We would trigger a swap chain resize here, but our demo handles it implicitly.
+static void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
+ PlatformState* state = (PlatformState*)glfwGetWindowUserPointer(window);
+ if (state) {
+ state->width = width;
+ state->height = height;
+ }
}
-static void glfw_key_callback(GLFWwindow* cb_window, int key, int scancode,
- int action, int mods) {
- if (action == GLFW_PRESS) {
- if (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) {
- glfwSetWindowShouldClose(cb_window, GLFW_TRUE);
- } else if (key == GLFW_KEY_F) {
- platform_toggle_fullscreen();
+static void glfw_key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
+ PlatformState* state = (PlatformState*)glfwGetWindowUserPointer(window);
+ if (action == GLFW_PRESS) {
+ if (key == GLFW_KEY_ESCAPE || key == GLFW_KEY_Q) {
+ glfwSetWindowShouldClose(window, GLFW_TRUE);
+ } else if (key == GLFW_KEY_F) {
+ if (state) platform_toggle_fullscreen(state);
+ }
}
- }
}
-void platform_init_window(bool fullscreen, int* width_ptr, int* height_ptr) {
+// --- Public API Implementation ---
+
+void platform_init(PlatformState* state, bool fullscreen, int* width_ptr, int* height_ptr) {
if (width_ptr && height_ptr) {
- g_width = *width_ptr;
- g_height = *height_ptr;
+ state->width = *width_ptr;
+ state->height = *height_ptr;
}
glfwInit();
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
- window = glfwCreateWindow(g_width, g_height, "demo64k", nullptr, nullptr);
+ state->window = glfwCreateWindow(state->width, state->height, "demo64k", nullptr, nullptr);
+
+ // Store our state pointer in the window for callbacks
+ glfwSetWindowUserPointer(state->window, state);
// Immediately query the actual framebuffer size for high-DPI displays
- glfwGetFramebufferSize(window, &g_width, &g_height);
+ glfwGetFramebufferSize(state->window, &state->width, &state->height);
- glfwSetKeyCallback(window, glfw_key_callback);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
+ glfwSetKeyCallback(state->window, glfw_key_callback);
+ glfwSetFramebufferSizeCallback(state->window, framebuffer_size_callback);
- g_is_fullscreen = fullscreen;
+ state->is_fullscreen = fullscreen;
if (fullscreen) {
- // Save current windowed mode dimensions before going fullscreen
- glfwGetWindowPos(window, &windowed_x, &windowed_y);
- glfwGetWindowSize(window, &windowed_w, &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(window, monitor, 0, 0, mode->width, mode->height,
- mode->refreshRate);
+ glfwSetWindowMonitor(state->window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
}
}
-void platform_shutdown() {
- glfwDestroyWindow(window);
+void platform_shutdown(PlatformState* state) {
+ if (state->window) {
+ glfwDestroyWindow(state->window);
+ }
glfwTerminate();
}
-void platform_poll() {
+void platform_poll(PlatformState* state) {
glfwPollEvents();
}
-bool platform_should_close() {
- return glfwWindowShouldClose(window);
+bool platform_should_close(PlatformState* state) {
+ return glfwWindowShouldClose(state->window);
}
-void platform_toggle_fullscreen() {
- g_is_fullscreen = !g_is_fullscreen;
- if (g_is_fullscreen) {
- glfwGetWindowPos(window, &windowed_x, &windowed_y);
- glfwGetWindowSize(window, &windowed_w, &windowed_h);
+void platform_toggle_fullscreen(PlatformState* state) {
+ state->is_fullscreen = !state->is_fullscreen;
+ if (state->is_fullscreen) {
+ 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(window, monitor, 0, 0, mode->width, mode->height,
- mode->refreshRate);
+ glfwSetWindowMonitor(state->window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate);
} else {
- glfwSetWindowMonitor(window, nullptr, windowed_x, windowed_y, windowed_w,
- windowed_h, 0);
+ glfwSetWindowMonitor(state->window, nullptr, state->windowed_x, state->windowed_y, state->windowed_w, state->windowed_h, 0);
}
}
-GLFWwindow* platform_get_window() {
- return window;
+WGPUSurface platform_create_wgpu_surface(WGPUInstance instance, PlatformState* state) {
+ return glfwCreateWindowWGPUSurface(instance, state->window);
}
-double platform_get_time() {
- return glfwGetTime();
-}
-
-// --- New dimension getters ---
-int platform_get_width() {
- return g_width;
-}
-
-int platform_get_height() {
- return g_height;
-}
-
-float platform_get_aspect_ratio() {
- if (g_height == 0) return 1.0f;
- return (float)g_width / (float)g_height;
-}
-
-WGPUSurface platform_create_wgpu_surface(WGPUInstance instance) {
- return glfwCreateWindowWGPUSurface(instance, window);
-}
+// Note: platform_get_* functions are now inline in the header.
+// platform_get_time() remains global.