// This file is part of the 64k demo project. // It implements platform-specific windowing and input using GLFW. // Handles fullscreen toggling and native surface creation for WebGPU. #include "platform.h" #include "glfw3webgpu.h" #include static GLFWwindow *window = nullptr; static int windowed_x, windowed_y, windowed_w, windowed_h; static bool g_is_fullscreen = false; 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(); } } } void platform_init_window(bool fullscreen) { glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); window = glfwCreateWindow(1280, 720, "demo64k", nullptr, nullptr); glfwSetKeyCallback(window, glfw_key_callback); g_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); GLFWmonitor *monitor = glfwGetPrimaryMonitor(); const GLFWvidmode *mode = glfwGetVideoMode(monitor); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } } void platform_shutdown() { glfwDestroyWindow(window); glfwTerminate(); } void platform_poll() { glfwPollEvents(); } bool platform_should_close() { return glfwWindowShouldClose(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); GLFWmonitor *monitor = glfwGetPrimaryMonitor(); const GLFWvidmode *mode = glfwGetVideoMode(monitor); glfwSetWindowMonitor(window, monitor, 0, 0, mode->width, mode->height, mode->refreshRate); } else { glfwSetWindowMonitor(window, nullptr, windowed_x, windowed_y, windowed_w, windowed_h, 0); } } GLFWwindow *platform_get_window() { return window; } double platform_get_time() { return glfwGetTime(); } WGPUSurface platform_create_wgpu_surface(WGPUInstance instance) { return glfwCreateWindowWGPUSurface(instance, window); }