make initial window size depend on UI scaling factor default tip

Mon, 28 Jul 2025 23:11:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 28 Jul 2025 23:11:30 +0200
changeset 231
0da563c4e39c
parent 230
02090b2d147e

make initial window size depend on UI scaling factor

src/ascension/ui.h file | annotate | diff | comparison | revisions
src/ascension/window.h file | annotate | diff | comparison | revisions
src/ui.c file | annotate | diff | comparison | revisions
src/window.c file | annotate | diff | comparison | revisions
test/snake/snake.c file | annotate | diff | comparison | revisions
--- a/src/ascension/ui.h	Sun Jul 27 23:54:33 2025 +0200
+++ b/src/ascension/ui.h	Mon Jul 28 23:11:30 2025 +0200
@@ -51,8 +51,9 @@
  *
  * @note you need to call this function again when the window changes the display
  * // TODO: check if there is an SDL event when the window changes the display and implement a reaction
+ * @return the scaling factor that was applied
  */
-void asc_ui_scale_auto(void);
+float asc_ui_scale_auto(void);
 
 /**
  * Adds a node to the active window's UI.
--- a/src/ascension/window.h	Sun Jul 27 23:54:33 2025 +0200
+++ b/src/ascension/window.h	Mon Jul 28 23:11:30 2025 +0200
@@ -44,6 +44,7 @@
 #define ASC_MAX_SCENES 8u
 #endif // ASC_MAX_SCENES
 
+// TODO: remove AscWindowSettings (we can set most of the stuff after creation)
 typedef struct asc_window_settings_s {
     AscGLContextSettings glsettings;
     asc_vec2u dimensions;
@@ -148,5 +149,39 @@
  */
 asc_vec2u asc_window_display_resolution(void);
 
+/**
+ * Returns the window's UI scaling factor.
+ *
+ * @param index the window index
+ * @return the current UI scale of the window
+ */
+float asc_window_ui_scale(unsigned int index);
+
+/**
+ * Sets the UI scaling factor for a window.
+ *
+ * Use asc_ui_scale() to change the scaling factor for the active window.
+ *
+ * @param index the window index
+ * @param scale the new UI scale
+ */
+void asc_window_set_ui_scale(unsigned int index, float scale);
+
+/**
+ * Sets the window title.
+ *
+ * @param index the window index
+ * @param title the new title
+ */
+void asc_window_set_title(unsigned int index, const char *title);
+
+/**
+ * Sets the window size.
+ *
+ * @param index the window index
+ * @param size the new size of the window
+ */
+void asc_window_set_size(unsigned int index, asc_vec2u size);
+
 #endif /* ASCENSION_WINDOW_H */
 
--- a/src/ui.c	Sun Jul 27 23:54:33 2025 +0200
+++ b/src/ui.c	Mon Jul 28 23:11:30 2025 +0200
@@ -43,13 +43,16 @@
     return asc_active_window->ui_scale;
 }
 
-void asc_ui_scale_auto(void) {
-    asc_vec2u res = asc_window_display_resolution();
+float asc_ui_scale_auto(void) {
+    const asc_vec2u res = asc_window_display_resolution();
+    float scale;
     if (res.width > 3100) {
-        asc_ui_scale(2.0f);
+        scale = 2.0f;
     } else if (res.width > 2000) {
-        asc_ui_scale(1.5f);
+        scale = 1.5f;
     } else {
-        return asc_ui_scale(1.f);
+        scale = 1.f;
     }
+    asc_ui_scale(scale);
+    return scale;
 }
--- a/src/window.c	Sun Jul 27 23:54:33 2025 +0200
+++ b/src/window.c	Mon Jul 28 23:11:30 2025 +0200
@@ -29,6 +29,7 @@
 #include "ascension/window.h"
 #include "ascension/context.h"
 
+#include <assert.h>
 #include <GL/glew.h>
 
 void asc_window_settings_init_defaults(AscWindowSettings *settings) {
@@ -210,3 +211,19 @@
     }
     return ASC_VEC2U(dm.w, dm.h);
 }
+
+float asc_window_ui_scale(unsigned int index) {
+    assert(asc_context.windows[index].window != NULL);
+    return asc_context.windows[index].ui_scale;
+}
+
+void asc_window_set_title(unsigned index, const char *title) {
+    assert(asc_context.windows[index].window != NULL);
+    SDL_SetWindowTitle(asc_context.windows[index].window, title);
+}
+
+void asc_window_set_size(unsigned index, asc_vec2u size) {
+    assert(asc_context.windows[index].window != NULL);
+    asc_context.windows[index].dimensions = size;
+    SDL_SetWindowSize(asc_context.windows[index].window, (int)size.width, (int)size.height);
+}
--- a/test/snake/snake.c	Sun Jul 27 23:54:33 2025 +0200
+++ b/test/snake/snake.c	Mon Jul 28 23:11:30 2025 +0200
@@ -271,14 +271,13 @@
     // initialize globals
     init_globals();
 
-    // create window
+    // create the window
     AscWindowSettings settings;
     asc_window_settings_init_defaults(&settings);
-    settings.title = "Snake";
-    settings.dimensions = ASC_VEC2U(900, 600);
-    // TODO: resize window depending on ui scale factor
     asc_window_initialize(0, &settings);
-    asc_ui_scale_auto();
+    asc_window_set_title(0, "Snake");
+    float ui_scale = asc_ui_scale_auto();
+    asc_window_set_size(0, ASC_VEC2U(700+ui_scale*200, 700));
 
     // load textures
     init_textures();

mercurial