Commit 5e0329afde105ea6b44e18fda2c35beec1529d81

Thomas de Grivel 2023-12-02T18:10:42

wip sdl2

diff --git a/libc3/window/sdl2/configure b/libc3/window/sdl2/configure
index d69df62..a86f46c 100755
--- a/libc3/window/sdl2/configure
+++ b/libc3/window/sdl2/configure
@@ -44,6 +44,7 @@ LIBS="$LIBS -rpath ${PREFIX}/lib"
 config_asan
 config_gnu
 pkg_config gl
+pkg_config glu
 pkg_config sdl2
 pkg_config libbsd-overlay
 config_define PREFIX "\"${PREFIX}\""
diff --git a/libc3/window/sdl2/demo/configure b/libc3/window/sdl2/demo/configure
index 39666bc..832befc 100755
--- a/libc3/window/sdl2/demo/configure
+++ b/libc3/window/sdl2/demo/configure
@@ -50,6 +50,7 @@ CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic"
 config_asan
 config_gnu
 pkg_config gl
+pkg_config glu
 pkg_config sdl2
 LIBS="$LIBS"
 
diff --git a/libc3/window/sdl2/demo/window_sdl2_demo.c b/libc3/window/sdl2/demo/window_sdl2_demo.c
index 0d12e80..008249c 100644
--- a/libc3/window/sdl2/demo/window_sdl2_demo.c
+++ b/libc3/window/sdl2/demo/window_sdl2_demo.c
@@ -146,6 +146,12 @@ bool window_sdl2_demo_render (s_window_sdl2 *window,
   if (! seq->render(seq, window, context))
     return false;
   /* progress bar */
+  /*
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  glOrtho(0, window->w, window->h, 0, -1, 1);
+  glMatrixMode(GL_MODELVIEW);
+  glLoadIdentity();
   glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
   glRectd(19, window->h - 12,
           19 + (window->w - 40.0) * seq->t / seq->duration + 2,
@@ -154,6 +160,7 @@ bool window_sdl2_demo_render (s_window_sdl2 *window,
   glRectd(20, window->h - 11,
           20 + (window->w - 40.0) * seq->t / seq->duration,
           window->h - 11 + 2);
+  */
   return true;
 }
 
@@ -164,5 +171,6 @@ 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 8d0608f..95b697b 100644
--- a/libc3/window/sdl2/types.h
+++ b/libc3/window/sdl2/types.h
@@ -20,6 +20,7 @@
 
 #include <SDL.h>
 #include <GL/gl.h>
+#include <GL/glu.h>
 #include <libc3/types.h>
 #include "../types.h"
 
diff --git a/libc3/window/sdl2/window_sdl2.c b/libc3/window/sdl2/window_sdl2.c
index 9941b99..e1afa22 100644
--- a/libc3/window/sdl2/window_sdl2.c
+++ b/libc3/window/sdl2/window_sdl2.c
@@ -121,6 +121,10 @@ bool window_sdl2_run (s_window_sdl2 *window)
     }
     g_window_sdl2_initialized = true;
   }
+  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
+                      SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
+  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
+  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
   window->sdl_window = SDL_CreateWindow(window->title,
                                         window->x, window->y,
                                         window->w, window->h,
@@ -137,6 +141,16 @@ bool window_sdl2_run (s_window_sdl2 *window)
           SDL_GetError());
     return false;
   }
+  GLenum error = glGetError();
+  if (error != GL_NO_ERROR) {
+    fprintf(stderr, "OpenGL initialization error: %s\n", gluErrorString(error));
+    return false;
+  }
+  if (SDL_GL_MakeCurrent(window->sdl_window, context) < 0) {
+    warnx("window_sdl2_run: failed to make OpenGL context current: %s",
+          SDL_GetError());
+    return false;
+  }
   SDL_GL_SetSwapInterval(1);
   if (! window->load(window)) {
     warnx("window_sdl2_run: window->load => false");
@@ -186,13 +200,14 @@ bool window_sdl2_run (s_window_sdl2 *window)
         break;
       }
     }
-    if (! window->render(window, window->context)) {
+    glDrawBuffer(GL_BACK);
+    if (! window->render(window, NULL)) {
       warnx("window_sdl2_run: window->render -> false");
       quit = 1;
     }
     SDL_GL_SwapWindow(window->sdl_window);
   }
-  SDL_GL_DeleteContext(window->context);
+  SDL_GL_DeleteContext(context);
   SDL_DestroyWindow(window->sdl_window);
   return true;
 }