Commit c5ca3757a3137bf53483f6d89eb50b4a33b414a6

Thomas de Grivel 2023-12-03T23:43:16

get compatible with high dpi display

diff --git a/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo.app/Contents/Info.plist b/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo.app/Contents/Info.plist
index a0b071b..16d4085 100644
--- a/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo.app/Contents/Info.plist
+++ b/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo.app/Contents/Info.plist
@@ -8,5 +8,7 @@
 	<string>c3</string>
 	<key>CFBundleIdentifier</key>
 	<string>io.kmx.c3-window-sdl2-demo</string>
+        <key>NSHighResolutionCapable</key>
+        <true/>
 </dict>
 </plist>
diff --git a/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo_debug.app/Contents/Info.plist b/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo_debug.app/Contents/Info.plist
index a0b071b..16d4085 100644
--- a/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo_debug.app/Contents/Info.plist
+++ b/libc3/window/sdl2/demo/macos/c3_window_sdl2_demo_debug.app/Contents/Info.plist
@@ -8,5 +8,7 @@
 	<string>c3</string>
 	<key>CFBundleIdentifier</key>
 	<string>io.kmx.c3-window-sdl2-demo</string>
+        <key>NSHighResolutionCapable</key>
+        <true/>
 </dict>
 </plist>
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 1cfbdc4..1156a6a 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -79,14 +79,14 @@ bool window_sdl2_demo_key (s_window_sdl2 *window, SDL_Keysym *keysym)
                                   SDL_WINDOW_FULLSCREEN_DESKTOP)) {
         warnx("window_sdl2_demo_key: SDL_SetWindowFullscreen(:desktop): %s",
               SDL_GetError());
-        return true;
+        SDL_MaximizeWindow(window->sdl_window);
       }
     }
     else {
       if (SDL_SetWindowFullscreen(window->sdl_window, 0)) {
         warnx("window_sdl2_demo_key: SDL_SetWindowFullscreen(0): %s",
               SDL_GetError());
-        return true;
+        SDL_RestoreWindow(window->sdl_window);
       }
     }
     window->fullscreen = ! window->fullscreen;
@@ -203,7 +203,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window, void *context)
   /* 2D */
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
-  glOrtho(0, window->w, 0, window->h, -1, 1);
+  glOrtho(0, window->gl_w, 0, window->gl_h, -1, 1);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glDisable(GL_DEPTH_TEST);
@@ -232,6 +232,5 @@ bool window_sdl2_demo_resize (s_window_sdl2 *window,
   (void) window;
   (void) w;
   (void) h;
-  glViewport(0, 0, w, h);
   return true;
 }
diff --git a/libc3/window/sdl2/types.h b/libc3/window/sdl2/types.h
index eef4516..d38aa58 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -100,6 +100,8 @@ struct window_sdl2 {
   uw                   sequence_count;
   uw                   sequence_pos;
   const s8            *title;
+  uw gl_w;
+  uw gl_h;
 };
 
 #endif /* LIBC3_WINDOW_SDL2_TYPES_H */
diff --git a/libc3/window/sdl2/window_sdl2.c b/libc3/window/sdl2/window_sdl2.c
index 913373c..814de77 100644
--- a/libc3/window/sdl2/window_sdl2.c
+++ b/libc3/window/sdl2/window_sdl2.c
@@ -109,9 +109,11 @@ bool window_sdl2_resize_default (s_window_sdl2 *window, uw w, uw h)
 
 bool window_sdl2_run (s_window_sdl2 *window)
 {
+  SDL_GLContext context;
+  s32 gl_w;
+  s32 gl_h;
   int quit = 0;
   SDL_Event sdl_event;
-  SDL_GLContext context;
   assert(window);
   if (! g_window_sdl2_initialized) {
     if (SDL_Init(SDL_INIT_VIDEO)) {
@@ -139,6 +141,7 @@ bool window_sdl2_run (s_window_sdl2 *window)
   window->sdl_window = SDL_CreateWindow(window->title,
                                         window->x, window->y,
                                         window->w, window->h,
+                                        SDL_WINDOW_ALLOW_HIGHDPI |
                                         SDL_WINDOW_OPENGL |
                                         SDL_WINDOW_RESIZABLE);
   if (! window->sdl_window) {
@@ -162,6 +165,22 @@ bool window_sdl2_run (s_window_sdl2 *window)
           SDL_GetError());
     return false;
   }
+  SDL_Renderer *renderer = SDL_GetRenderer(window->sdl_window);
+  gl_w = window->w;
+  gl_h = window->h;
+  SDL_GetRendererOutputSize(renderer,
+                            &gl_w, &gl_h);
+  window->gl_w = gl_w;
+  window->gl_h = gl_h;
+  if (window->gl_w != window->w) {
+    double scale_w = (double) gl_w / (double) window->w;
+    double scale_h = (double) gl_h / (double) window->h;
+    if (fabs(scale_w - scale_h) > DBL_EPSILON)
+      warnx("window_sdl2_run: width scale != height scale\n");
+    printf("window_sdl2_run: scale_w %f scale_h %f\n", scale_w,
+           scale_h);
+    SDL_RenderSetScale(renderer, scale_w, scale_h);
+}
   SDL_GL_SetSwapInterval(1);
   if (! window->load(window)) {
     warnx("window_sdl2_run: window->load => false");
@@ -205,6 +224,11 @@ bool window_sdl2_run (s_window_sdl2 *window)
           }
           window->w = sdl_event.window.data1;
           window->h = sdl_event.window.data2;
+          SDL_GetRendererOutputSize(SDL_GetRenderer(window->sdl_window),
+                                    &gl_w, &gl_h);
+          window->gl_w = gl_w;
+          window->gl_h = gl_h;
+          glViewport(0, 0, gl_w, gl_h);
         }
         break;
       default: