summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorskal <pascal.massimino@gmail.com>2026-02-02 09:19:17 +0100
committerskal <pascal.massimino@gmail.com>2026-02-02 09:19:17 +0100
commit0b0067cb0a8db5ea5178501a12aacdef436a9845 (patch)
tree362322b45ffde32346351dfa0710d3a470dd6e88 /src
parent275fe655fd7272edd9fa49439d47f449231ce445 (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')
-rw-r--r--src/main.cc21
-rw-r--r--src/platform.cc44
-rw-r--r--src/platform.h10
-rw-r--r--src/tests/test_3d_render.cc9
4 files changed, 69 insertions, 15 deletions
diff --git a/src/main.cc b/src/main.cc
index 8cedfce..f7136ef 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -130,7 +130,10 @@ float* generate_tone(float* buffer, float freq) {
int main(int argc, char** argv) {
bool fullscreen_enabled = false;
- double seek_time = 0.0;
+ float seek_time = 0.0f;
+ int* width_ptr = nullptr;
+ int* height_ptr = nullptr;
+ int custom_width, custom_height;
#if !defined(STRIP_ALL)
for (int i = 1; i < argc; ++i) {
@@ -139,6 +142,12 @@ int main(int argc, char** argv) {
} else if (strcmp(argv[i], "--seek") == 0 && i + 1 < argc) {
seek_time = atof(argv[i + 1]);
++i;
+ } else if (strcmp(argv[i], "--resolution") == 0 && i + 1 < argc) {
+ const char* res_str = argv[++i];
+ if (sscanf(res_str, "%dx%d", &custom_width, &custom_height) == 2) {
+ width_ptr = &custom_width;
+ height_ptr = &custom_height;
+ }
}
}
#else
@@ -147,10 +156,8 @@ int main(int argc, char** argv) {
fullscreen_enabled = true;
#endif /* STRIP_ALL */
- platform_init_window(fullscreen_enabled);
- int width, height;
- glfwGetFramebufferSize(platform_get_window(), &width, &height);
- gpu_init(platform_get_window(), width, height);
+ platform_init_window(fullscreen_enabled, width_ptr, height_ptr);
+ gpu_init(platform_get_window(), platform_get_width(), platform_get_height());
audio_init();
// Register drum assets
@@ -234,9 +241,7 @@ int main(int argc, char** argv) {
update_game_logic(current_time);
- int width, height;
- glfwGetFramebufferSize(platform_get_window(), &width, &height);
- float aspect_ratio = (float)width / (float)height;
+ float aspect_ratio = platform_get_aspect_ratio();
// Adjusted multiplier for visuals (preventing constant 1.0 saturation)
float raw_peak = synth_get_output_peak();
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);
}
diff --git a/src/platform.h b/src/platform.h
index 9cfaf00..38a0568 100644
--- a/src/platform.h
+++ b/src/platform.h
@@ -6,16 +6,20 @@
#include <webgpu.h>
+// Forward declare GLFWwindow to avoid including the full header here.
struct GLFWwindow;
-void platform_init_window(bool fullscreen);
+void platform_init_window(bool fullscreen, int* width_ptr, int* height_ptr);
void platform_shutdown();
void platform_poll();
bool platform_should_close();
-void platform_toggle_fullscreen();
-
GLFWwindow* platform_get_window();
double platform_get_time();
+// Add dimension getters
+int platform_get_width();
+int platform_get_height();
+float platform_get_aspect_ratio();
+
WGPUSurface platform_create_wgpu_surface(WGPUInstance instance);
diff --git a/src/tests/test_3d_render.cc b/src/tests/test_3d_render.cc
index e4477a0..0a809c4 100644
--- a/src/tests/test_3d_render.cc
+++ b/src/tests/test_3d_render.cc
@@ -174,8 +174,13 @@ void gen_periodic_noise(uint8_t* buffer, int w, int h, const float* params,
}
int main() {
- platform_init_window(false);
- init_wgpu();
+ printf("Running 3D Renderer Test...\n");
+
+ platform_init_window(false, nullptr, nullptr);
+ gpu_init(platform_get_window(), platform_get_width(), platform_get_height());
+
+ // Create Renderer and Scene
+ Renderer3D renderer;
g_renderer.init(g_device, g_queue, g_format);
g_renderer.resize(g_width, g_height);