Răsfoiți Sursa

Make hotreloading disablable

rexim 2 ani în urmă
părinte
comite
bfb9d92a68
5 a modificat fișierele cu 71 adăugiri și 47 ștergeri
  1. 19 0
      README.md
  2. 6 2
      build.sh
  3. 25 39
      src/musializer.c
  4. 7 1
      src/plug.c
  5. 14 5
      src/plug.h

+ 19 - 0
README.md

@@ -0,0 +1,19 @@
+# Musializer
+
+## Quick Start
+
+```console
+$ ./build.sh
+$ ./build/musualizer <song.ogg>
+```
+
+## Hot Reloading
+
+```console
+$ export HOTRELOAD=1
+$ export LD_LIBRARY_PATH="./build/:$LD_LIBRARY_PATH"
+$ ./build.sh
+$ ./build/musualizer <song.ogg>
+```
+
+Keep the app running. Rebuild with `./build.sh`. Hot reload by focusing on the window of the app and pressing <kbd>r</kbd>.

+ 6 - 2
build.sh

@@ -6,6 +6,10 @@ CFLAGS="-Wall -Wextra -ggdb `pkg-config --cflags raylib`"
 LIBS="`pkg-config --libs raylib` -lglfw -lm -ldl -lpthread"
 
 mkdir -p ./build/
-clang $CFLAGS -o ./build/libplug.so -fPIC -shared ./src/plug.c $LIBS
-clang $CFLAGS -o ./build/musializer ./src/musializer.c $LIBS -L./build/
+if [ ! -z "${HOTRELOAD}" ]; then
+    clang $CFLAGS -o ./build/libplug.so -fPIC -shared ./src/plug.c $LIBS
+    clang $CFLAGS -DHOTRELOAD -o ./build/musializer ./src/musializer.c $LIBS -L./build/
+else
+    clang $CFLAGS  -o ./build/musializer ./src/plug.c ./src/musializer.c $LIBS -L./build/
+fi
 clang -o ./build/fft ./src/fft.c -lm

+ 25 - 39
src/musializer.c

@@ -25,13 +25,18 @@ char *shift_args(int *argc, char ***argv)
 
 const char *libplug_file_name = "libplug.so";
 void *libplug = NULL;
-plug_hello_t plug_hello = NULL;
-plug_init_t plug_init = NULL;
-plug_pre_reload_t plug_pre_reload = NULL;
-plug_post_reload_t plug_post_reload = NULL;
-plug_update_t plug_update = NULL;
+
+#ifdef HOTRELOAD
+#define PLUG(name) name##_t *name = NULL;
+#else
+#define PLUG(name) name##_t name;
+#endif
+LIST_OF_PLUGS
+#undef PLUG
+
 Plug plug = {0};
 
+#ifdef HOTRELOAD
 bool reload_libplug(void)
 {
     if (libplug != NULL) dlclose(libplug);
@@ -42,43 +47,21 @@ bool reload_libplug(void)
         return false;
     }
 
-    plug_hello = dlsym(libplug, "plug_hello");
-    if (plug_hello == NULL) {
-        fprintf(stderr, "ERROR: could not find plug_hello symbol in %s: %s",
-                libplug_file_name, dlerror());
-        return false;
-    }
-
-    plug_init = dlsym(libplug, "plug_init");
-    if (plug_init == NULL) {
-        fprintf(stderr, "ERROR: could not find plug_init symbol in %s: %s",
-                libplug_file_name, dlerror());
-        return false;
-    }
-
-    plug_update = dlsym(libplug, "plug_update");
-    if (plug_update == NULL) {
-        fprintf(stderr, "ERROR: could not find plug_update symbol in %s: %s",
-                libplug_file_name, dlerror());
-        return false;
-    }
-
-    plug_pre_reload = dlsym(libplug, "plug_pre_reload");
-    if (plug_pre_reload == NULL) {
-        fprintf(stderr, "ERROR: could not find plug_pre_reload symbol in %s: %s",
-                libplug_file_name, dlerror());
-        return false;
-    }
-
-    plug_post_reload = dlsym(libplug, "plug_post_reload");
-    if (plug_post_reload == NULL) {
-        fprintf(stderr, "ERROR: could not find plug_post_reload symbol in %s: %s",
-                libplug_file_name, dlerror());
-        return false;
-    }
+    #define PLUG(name) \
+        name = dlsym(libplug, #name); \
+        if (name == NULL) { \
+            fprintf(stderr, "ERROR: could not find %s symbol in %s: %s", \
+                    #name, libplug_file_name, dlerror()); \
+            return false; \
+        }
+    LIST_OF_PLUGS
+    #undef PLUG
 
     return true;
 }
+#else
+#define reload_libplug() true
+#endif
 
 int main(int argc, char **argv)
 {
@@ -105,6 +88,9 @@ int main(int argc, char **argv)
             if (!reload_libplug()) return 1;
             plug_post_reload(&plug);
         }
+        if (IsKeyPressed(KEY_P)) {
+            plug_world();
+        }
         plug_update(&plug);
     }
 

+ 7 - 1
src/plug.c

@@ -59,6 +59,11 @@ void plug_hello(void)
     printf("Hello from Plugin\n");
 }
 
+void plug_world(void)
+{
+    printf("Foo Bar\n");
+}
+
 void plug_init(Plug *plug, const char *file_path)
 {
     plug->music = LoadMusicStream(file_path);
@@ -133,7 +138,8 @@ void plug_update(Plug *plug)
         }
         a /= (size_t) f1 - (size_t) f + 1;
         float t = a/max_amp;
-        DrawRectangle(m*cell_width, h/2 - h/2*t, cell_width, h/2*t, RED);
+        DrawRectangle(m*cell_width, h/2 - h/2*t, cell_width, h/2*t, GREEN);
+        // DrawCircle(m*cell_width, h/2, h/2*t, GREEN);
         m += 1;
     }
     EndDrawing();

+ 14 - 5
src/plug.h

@@ -8,10 +8,19 @@ typedef struct {
     Music music;
 } Plug;
 
-typedef void (*plug_hello_t)(void);
-typedef void (*plug_init_t)(Plug *plug, const char *file_path);
-typedef void (*plug_pre_reload_t)(Plug *plug);
-typedef void (*plug_post_reload_t)(Plug *plug);
-typedef void (*plug_update_t)(Plug *plug);
+typedef void (plug_hello_t)(void);
+typedef void (plug_world_t)(void);
+typedef void (plug_init_t)(Plug *plug, const char *file_path);
+typedef void (plug_pre_reload_t)(Plug *plug);
+typedef void (plug_post_reload_t)(Plug *plug);
+typedef void (plug_update_t)(Plug *plug);
+
+#define LIST_OF_PLUGS \
+    PLUG(plug_hello) \
+    PLUG(plug_world) \
+    PLUG(plug_init) \
+    PLUG(plug_pre_reload) \
+    PLUG(plug_post_reload) \
+    PLUG(plug_update)
 
 #endif // PLUG_H_