diff options
| author | skal <pascal.massimino@gmail.com> | 2026-02-02 09:19:17 +0100 |
|---|---|---|
| committer | skal <pascal.massimino@gmail.com> | 2026-02-02 09:19:17 +0100 |
| commit | 0b0067cb0a8db5ea5178501a12aacdef436a9845 (patch) | |
| tree | 362322b45ffde32346351dfa0710d3a470dd6e88 /src/platform.cc | |
| parent | 275fe655fd7272edd9fa49439d47f449231ce445 (diff) | |
feat(platform): Fix high-DPI scaling and add resolution option
- Fixed a 'squished' viewport bug on high-DPI (Retina) displays by querying the framebuffer size in pixels instead of using the window size in points.
- Centralized window dimension management within the platform layer.
- Added a '--resolution WxH' command-line option to allow specifying a custom window size at startup. This option is stripped in STRIP_ALL builds.
- Updated all test and tool executables to use the new platform API.
Diffstat (limited to 'src/platform.cc')
| -rw-r--r-- | src/platform.cc | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/platform.cc b/src/platform.cc index ff45964..464003f 100644 --- a/src/platform.cc +++ b/src/platform.cc @@ -11,6 +11,22 @@ 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 glfw_key_callback(GLFWwindow* cb_window, int key, int scancode, int action, int mods) { if (action == GLFW_PRESS) { @@ -22,11 +38,21 @@ static void glfw_key_callback(GLFWwindow* cb_window, int key, int scancode, } } -void platform_init_window(bool fullscreen) { +void platform_init_window(bool fullscreen, int* width_ptr, int* height_ptr) { + if (width_ptr && height_ptr) { + g_width = *width_ptr; + g_height = *height_ptr; + } + glfwInit(); glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); - window = glfwCreateWindow(1280, 720, "demo64k", nullptr, nullptr); + window = glfwCreateWindow(g_width, g_height, "demo64k", nullptr, nullptr); + + // Immediately query the actual framebuffer size for high-DPI displays + glfwGetFramebufferSize(window, &g_width, &g_height); + glfwSetKeyCallback(window, glfw_key_callback); + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); g_is_fullscreen = fullscreen; if (fullscreen) { @@ -78,6 +104,20 @@ 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); } |
