diff options
Diffstat (limited to 'src/platform.cc')
| -rw-r--r-- | src/platform.cc | 67 |
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 |
