Commit 0c1e2386e0ec432caea94f1fc76d1a7bd5549127

Thomas de Grivel 2024-01-24T21:39:28

wip

diff --git a/.ic3_history b/.ic3_history
index afdab9c..1888b1f 100644
--- a/.ic3_history
+++ b/.ic3_history
@@ -1,7 +1,3 @@
-1.001f
-1.0010f
-1.00010f
-1.000010f
 1.0000010f
 1.00000010f
 1.00000001f
@@ -97,3 +93,7 @@ dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 16)
 dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 32)
 dlopen("libc3/window/.libs/libc3_window.so.0.0", (U32) 0)
 dlopen("/home/dx/c/thodg/c3-lang/c3/libc3/window/.libs/libc3_window.so.0.0", (U32) 0)
+getenv("_")
+getenv("SHELL")
+getenv("HOME")
+getenv("USER")
diff --git a/c3s/configure b/c3s/configure
index fa750c2..4b263c6 100755
--- a/c3s/configure
+++ b/c3s/configure
@@ -39,8 +39,9 @@ OBJECTS_DEBUG="$(c2ext .debug.lo "$SOURCES")"
 echo "OBJECTS_DEBUG = $OBJECTS_DEBUG" >> ${CONFIG_MK}
 
 # Common config for all targets
-CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic"
 CPPFLAGS="-I../libffi/include $CPPFLAGS"
+CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic -fPIC"
+LDFLAGS="$LDFLAGS -rdynamic"
 config_asan
 config_gnu
 pkg_config libbsd-overlay
@@ -57,7 +58,7 @@ LOCAL_LIBS_ASAN="$LIBC3_ASAN"
 LIBS_ASAN="$LOCAL_LIBS_ASAN $LIBS"
 
 # Coverage config
-CFLAGS_COV="$CFLAGS -ftest-coverage -fprofile-arcs"
+CFLAGS_COV="$CFLAGS --coverage"
 LDFLAGS_COV="$LDFLAGS"
 LIBC3_COV=../libc3/libc3_cov.la
 LOCAL_LIBS_COV="$LIBC3_COV"
diff --git a/ic3/configure b/ic3/configure
index 75006cf..9b48bd4 100755
--- a/ic3/configure
+++ b/ic3/configure
@@ -26,8 +26,9 @@ PROG_DEBUG=ic3_debug
 . ./sources.sh
 
 # Common config for all targets
-CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic"
 CPPFLAGS="-I../libffi/include $CPPFLAGS"
+CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic -fPIC"
+LDFLAGS="$LDFLAGS -rdynamic"
 config_asan
 config_gnu
 pkg_config libbsd-overlay
@@ -72,8 +73,8 @@ LOCAL_LIBS_ASAN="$LIBC3_ASAN"
 LIBS_ASAN="$LOCAL_LIBS_ASAN $LIBS"
 
 # Coverage config
-CFLAGS_COV="$CFLAGS -ftest-coverage -fprofile-arcs -fprofile-generate"
-LDFLAGS_COV="$LDFLAGS --coverage"
+CFLAGS_COV="$CFLAGS --coverage"
+LDFLAGS_COV="$LDFLAGS"
 LIBC3_COV=../libc3/libc3_cov.la
 LOCAL_LIBS_COV="$LIBC3_COV"
 LIBS_COV="$LOCAL_LIBS_COV $LIBS"
diff --git a/lib/c3/0.1/c3.facts b/lib/c3/0.1/c3.facts
index cfd6439..65f6c01 100644
--- a/lib/c3/0.1/c3.facts
+++ b/lib/c3/0.1/c3.facts
@@ -188,5 +188,7 @@ replace {C3.fib, :fn, fn { (0) { 1 }
                            (x) { fib(x - 1) + fib(x - 2) } }}
 add {C3, :symbol, C3.facts_next_id}
 replace {C3.facts_next_id, :cfn, cfn Uw "c3_facts_next_id" (Result)}
+add {C3, :symbol, C3.getenv}
+replace {C3.getenv, :cfn, cfn Str "c3_getenv" (Str, Result)}
 add {C3, :symbol, C3.dlopen}
-replace {C3.dlopen, :cfn, cfn Ptr "dlopen" (Char*, U32)}
+replace {C3.dlopen, :cfn, cfn Ptr "c3_dlopen" (Char*)}
diff --git a/libc3/c3.c b/libc3/c3.c
index 0c6ee99..44ba7c4 100644
--- a/libc3/c3.c
+++ b/libc3/c3.c
@@ -11,12 +11,14 @@
  * THIS SOFTWARE.
  */
 #include <assert.h>
+#include <dlfcn.h>
 #include <err.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include "buf.h"
 #include "c3_main.h"
 #include "env.h"
+#include "str.h"
 #include "sym.h"
 
 const s_str g_c3_base_binary = {{NULL}, 2, {"01"}};
@@ -42,6 +44,12 @@ void c3_clean (s_env *env)
   sym_delete_all();
 }
 
+void * c3_dlopen (const s_str *path)
+{
+  assert(path);
+  return dlopen(path->ptr.pchar, RTLD_GLOBAL);
+}
+
 void c3_exit (sw code)
 {
   exit((int) code);
@@ -49,10 +57,22 @@ void c3_exit (sw code)
 
 uw * c3_facts_next_id (uw *dest)
 {
+  assert(dest);
   *dest = g_c3_env.facts.next_id;
   return dest;
 }
 
+s_str * c3_getenv (const s_str *name, s_str *dest)
+{
+  char *p;
+  assert(name);
+  assert(dest);
+  p = getenv(name->ptr.pchar);
+  if (! p)
+    return NULL;
+  return str_init_1(dest, NULL, p);
+}
+
 s_env * c3_init (s_env *env, int argc, char **argv)
 {
   if (! env)
diff --git a/libc3/c3_main.h b/libc3/c3_main.h
index a4f016c..596599c 100644
--- a/libc3/c3_main.h
+++ b/libc3/c3_main.h
@@ -25,10 +25,13 @@ extern const s_str g_c3_bases_hexadecimal[2];
 extern const char *g_c3_license;
 extern sw          g_c3_exit_code;
 
-/* stack-allocation compatible functions */
+/* Stack-allocation compatible functions, call c3_clean after use. */
 s_env * c3_init (s_env *env, int argc, char **argv);
 void c3_clean (s_env *env);
 
+/* Observers. */
+s_str * c3_getenv (const s_str *name, s_str *dest);
+
 /* debug */
 void c3_break (void);
 
diff --git a/libc3/configure b/libc3/configure
index 4d911fb..439f8f6 100755
--- a/libc3/configure
+++ b/libc3/configure
@@ -38,9 +38,8 @@ OBJECTS_DEBUG="$(c2ext .debug.lo "$LO_SOURCES")"
 
 # Common config for all targets
 CPPFLAGS="-I../libffi/include $CPPFLAGS"
-CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic"
-LDFLAGS="--shared ${LDFLAGS}"
-LIBS="${LIBS} -lm -pthread -rpath ${PREFIX}/lib"
+CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic -fPIC"
+LDFLAGS="--shared $LDFLAGS -rdynamic"
 config_asan
 config_gnu
 pkg_config libbsd-overlay
@@ -52,7 +51,7 @@ config_include sha1_h sys/types.h sha1.h HAVE_SHA1_H
 config_define PREFIX "\"${PREFIX}\""
 config_have_stat_mtim
 update_config_h
-LIBS="$LIBS ../libffi/libffi.la"
+LIBS="$LIBS ../libffi/libffi.la -lm -pthread -rpath ${PREFIX}/lib"
 
 if ! [ -f ../ucd2c/ucd.c ]; then
     touch ../ucd2c/ucd.c
@@ -64,9 +63,9 @@ LDFLAGS_ASAN="$LDFLAGS"
 LIBS_ASAN="$LIBS"
 
 # Coverage config
-CFLAGS_COV="$CFLAGS -ftest-coverage -fprofile-arcs -fprofile-generate"
-LDFLAGS_COV="$LDFLAGS --coverage"
-LIBS_COV="$LIBS"
+CFLAGS_COV="$CFLAGS --coverage"
+LDFLAGS_COV="$LDFLAGS"
+LIBS_COV="$LIBS -lgcov"
 
 # Debug config
 CFLAGS_DEBUG="$CFLAGS -DDEBUG -O0 -g"
diff --git a/libc3/window/sdl2/gl_ortho.c b/libc3/window/sdl2/gl_ortho.c
index 7e05d65..161f373 100644
--- a/libc3/window/sdl2/gl_ortho.c
+++ b/libc3/window/sdl2/gl_ortho.c
@@ -40,13 +40,16 @@ static const char * g_gl_ortho_fragment_shader_src =
   "in vec3 iFragNormal;\n"
   "in vec2 iTexCoord;\n"
   "out vec4 oFragColor;\n"
+  "uniform vec4 uColor;\n"
   "uniform bool uEnableTex2D;\n"
   "uniform sampler2D uTex2D;\n"
-  "uniform vec4 uColor;\n"
   "void main() {\n"
   "  vec4 texColor = texture(uTex2D, iTexCoord);\n"
   "  if (uEnableTex2D) {\n"
-  "    oFragColor = texColor * uColor;\n"
+  "    oFragColor = vec4(texColor[0] * uColor[0],\n"
+  "                      texColor[1] * uColor[1],\n"
+  "                      texColor[2] * uColor[2],\n"
+  "                      texColor[3] * uColor[3]);\n"
   "  }\n"
   "  else\n"
   "    oFragColor = uColor;\n"
@@ -210,7 +213,6 @@ void gl_ortho_rect (s_gl_ortho *ortho, f32 x, f32 y, f32 w, f32 h)
 
 void gl_ortho_render (s_gl_ortho *ortho)
 {
-  const s_rgba color = {1, 1, 1, 1};
   GLenum error;
   assert(ortho);
   assert(glGetError() == GL_NO_ERROR);
@@ -234,7 +236,7 @@ void gl_ortho_render (s_gl_ortho *ortho)
   assert(glGetError() == GL_NO_ERROR);
   glUniform1i(ortho->gl_tex2d_loc, 0);
   assert(glGetError() == GL_NO_ERROR);
-  glUniform4fv(ortho->gl_color_loc, 1, &color.r);
+  glUniform4f(ortho->gl_color_loc, 1.0f, 1.0f, 1.0f, 1.0f);
   assert(glGetError() == GL_NO_ERROR);
   glDepthRange(ortho->clip_z_near, ortho->clip_z_far);
   assert(glGetError() == GL_NO_ERROR);
diff --git a/libc3/window/sdl2/gl_sprite.c b/libc3/window/sdl2/gl_sprite.c
index a6d96f8..cd8e4e7 100644
--- a/libc3/window/sdl2/gl_sprite.c
+++ b/libc3/window/sdl2/gl_sprite.c
@@ -267,6 +267,7 @@ s_gl_sprite * gl_sprite_init (s_gl_sprite *sprite, const char *path,
     str_clean(&tmp.real_path);
     return NULL;
   }
+  glActiveTexture(GL_TEXTURE0);
   i = 0;
   y = 0;
   while (i < tmp.frame_count && y < dim_y) {
diff --git a/libc3/window/sdl2/gl_text.c b/libc3/window/sdl2/gl_text.c
index e42c7d1..0e31752 100644
--- a/libc3/window/sdl2/gl_text.c
+++ b/libc3/window/sdl2/gl_text.c
@@ -125,17 +125,19 @@ bool gl_text_render_to_texture (s_gl_text *text)
     }
     glyph = font->ft_face->glyph;
     i = 0;
-    while (i < glyph->bitmap.width) {
+    while (i < glyph->bitmap.rows) {
+      data_y = i + glyph->bitmap_top;
+      //printf("\n");
       j = 0;
-      while (j < glyph->bitmap.rows) {
-        data_x = x + i;
-        data_y = j + glyph->bitmap_top;
+      while (j < glyph->bitmap.width) {
+        data_x = x + j;
         data_pixel = data + (data_y * data_w + data_x) * 4;
-        u8 value = glyph->bitmap.buffer[j * glyph->bitmap.width + i];
+        u8 value = glyph->bitmap.buffer[i * glyph->bitmap.width + j];
         data_pixel[0] = 255;
         data_pixel[1] = 255;
         data_pixel[2] = 255;
-        data_pixel[3] = value;
+        data_pixel[3] = (u8) value;
+        //printf("%s", (const char *[]) {" ", ".", ":", "#", "░", "▒", "▓", "█"}[value / 32]);
         j++;
       }
       i++;
@@ -145,12 +147,15 @@ bool gl_text_render_to_texture (s_gl_text *text)
   }
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data_w, data_h, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
   assert(glGetError() == GL_NO_ERROR);
+  glGenerateMipmap(GL_TEXTURE_2D);
+  assert(glGetError() == GL_NO_ERROR);
   free(data);
   glBindTexture(GL_TEXTURE_2D, 0);
   text->pix_w = data_w;
   text->pix_h = data_h;
   text->pt_w = text->pix_w * text->font->point_per_pixel;
   text->pt_h = text->pix_h * text->font->point_per_pixel;
+  //printf("\n");
   return true;
 }
 
diff --git a/libtommath b/libtommath
index 8b03179..83f74f9 160000
--- a/libtommath
+++ b/libtommath
@@ -1 +1 @@
-Subproject commit 8b03179ac3d1dd6ea4f91deba769c79c0e21fc43
+Subproject commit 83f74f992b786cee20f4be8a583e0152935dc75c
diff --git a/test/configure b/test/configure
index d3e137e..744af2f 100755
--- a/test/configure
+++ b/test/configure
@@ -39,8 +39,9 @@ OBJECTS_DEBUG="$(c2ext .debug.lo "$SOURCES")"
 echo "OBJECTS_DEBUG = $OBJECTS_DEBUG" >> ${CONFIG_MK}
 
 # Common config for all targets
-CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic"
 CPPFLAGS="-I../libffi/include $CPPFLAGS"
+CFLAGS="$CFLAGS -W -Wall -Werror -std=c99 -pedantic -fPIC"
+LDFLAGS="$LDFLAGS -rdynamic"
 config_asan
 config_gnu
 pkg_config libbsd-overlay
@@ -56,7 +57,7 @@ LIBC3_ASAN=../libc3/libc3_asan.la
 LIBS_ASAN="$LIBC3_ASAN $LIBS"
 
 # Coverage config
-CFLAGS_COV="$CFLAGS -ftest-coverage -fprofile-arcs -fprofile-generate"
+CFLAGS_COV="$CFLAGS --coverage"
 LDFLAGS_COV="$LDFLAGS"
 LIBC3_COV=../libc3/libc3_cov.la
 LIBS_COV="$LIBC3_COV $LIBS"