medium sized dollop
[vg.git] / src / vg / vg_loader.h
diff --git a/src/vg/vg_loader.h b/src/vg/vg_loader.h
new file mode 100644 (file)
index 0000000..2ba707e
--- /dev/null
@@ -0,0 +1,350 @@
+/*
+ * Copyright 2021-2022 (C) Mount0 Software, Harry Godden - All Rights Reserved
+ * -----------------------------------------------------------------------------
+ *
+ * Splash / load screen
+ *
+ * -----------------------------------------------------------------------------
+ */
+
+#ifndef VG_LOADER_H
+#define VG_LOADER_H
+
+#include "common.h"
+
+static struct vg_loader
+{
+   /* Synchronization */
+   vg_semaphore sem_loading;
+   vg_mutex mux_status;
+
+   enum loader_status
+   {
+      k_loader_status_loading,
+      k_loader_status_complete,
+      k_loader_status_fail
+   }
+   status;
+
+   /* Shutdown steps */
+   struct loader_free_step
+   {
+      void (*fn_free)(void *);
+      void *data;
+   }
+   *step_buffer;
+   u32 step_count, step_cap, step_action;
+}
+vg_loader;
+
+static int vg_loader_init(void)
+{
+   vg_semaphore_init( &vg_loader.sem_loading, 0 );
+   vg_mutex_init( &vg_loader.mux_status );
+   return 1;
+}
+
+static void vg_loader_free(void)
+{
+   vg_semaphore_wait( &vg_loader.sem_loading );
+   vg_semaphore_free( &vg_loader.sem_loading );
+   vg_mutex_free( &vg_loader.mux_status );
+
+   for( int i=0; i<vg_loader.step_count; i++ )
+   {
+      struct loader_free_step *step = 
+         &vg_loader.step_buffer[vg_loader.step_count -1 -i];
+
+      step->fn_free( step->data );
+   }
+
+   free( vg_loader.step_buffer );
+}
+
+static enum loader_status vg_loader_status(void)
+{
+   enum loader_status answer;
+
+   vg_mutex_lock( &vg_loader.mux_status );
+   answer = vg_loader.status;
+   vg_mutex_unlock( &vg_loader.mux_status );
+
+   return answer;
+}
+
+static float hue_to_rgb( float p, float q, float t )
+{
+   if(t < 0.0f) t += 1.0f;
+   if(t > 1.0f) t -= 1.0f;
+   if(t < 1.0f/6.0f) return p + (q - p) * 6.0f * t;
+   if(t < 1.0f/2.0f) return q;
+   if(t < 2.0f/3.0f) return p + (q - p) * (2.0f/3.0f - t) * 6.0f;
+   return p;
+}
+
+static void vg_loader_render(void)
+{
+   float h = vg_fractf(vg_time*0.1),
+         s = 0.2f,
+         l = 0.1f, //* (0.5f+vg_fractf(vg_time*40.0)*0.5f),
+         q = l < 0.5f ? l * (1.0f + s) : l + s - l * s,
+         p = 2.0f * l - q,
+         r = hue_to_rgb( p, q, h + 1.0f/3.0f ),
+         g = hue_to_rgb( p, q, h ),
+         b = hue_to_rgb( p, q, h - 1.0f/3.0f );
+
+   glClearColor( r, g, b, 1.0f );
+   glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
+
+
+
+   ui_begin( &ui_global_ctx, vg_window_x, vg_window_y );
+
+
+   int const fh = 14;
+   int lines_screen_max = ((vg_window_y/fh)-2),
+       lines_max_draw = VG_MIN( lines_screen_max, vg_list_size(vg_log.buffer) ),
+       lines_to_draw  = VG_MIN( lines_max_draw, vg_log.buffer_line_count );
+
+       int ptr = vg_log.buffer_line_current;
+       
+       ui_global_ctx.cursor[0] = 0;
+       ui_global_ctx.cursor[1] = lines_to_draw*fh;
+   ui_global_ctx.cursor[3] = fh;
+       ui_fill_x( &ui_global_ctx );
+
+   for( int i=0; i<lines_to_draw; i ++ )
+   {
+      ptr --;
+
+      if( ptr < 0 )
+         ptr = vg_list_size( vg_log.buffer )-1;
+      
+      ui_text( &ui_global_ctx, ui_global_ctx.cursor, 
+            vg_log.buffer[ptr], vg_console.scale, 0 );
+
+      ui_global_ctx.cursor[1] -= fh*vg_console.scale;
+   }
+
+   ui_resolve( &ui_global_ctx );
+   ui_draw( &ui_global_ctx, NULL );
+}
+
+
+static int vg_load_full(void);
+
+static void vg_loader_thread(void * nothing)
+{
+   /* Run client loader */
+   int res = vg_load_full();
+
+   /* Propogate status */
+   vg_mutex_lock( &vg_loader.mux_status );
+   if( res )
+   {
+      vg_loader.status = k_loader_status_complete;
+   }
+   else
+   {
+      vg_loader.status = k_loader_status_fail;
+   }
+   vg_mutex_unlock( &vg_loader.mux_status );
+
+   vg_semaphore_post( &vg_loader.sem_loading );
+}
+
+static void vg_loader_start(void)
+{
+   vg_thread_run( vg_loader_thread, NULL );
+}
+
+static void vg_free_libc_malloced( void *data )
+{
+   free( data );
+}
+
+static int vg_loader_push_free_step( struct loader_free_step step )
+{
+   void *buf = buffer_reserve( vg_loader.step_buffer, vg_loader.step_count, 
+                              &vg_loader.step_cap, 1, 
+                              sizeof( struct loader_free_step ) );
+
+   if( !buf )
+      return 0;
+
+   vg_loader.step_buffer = buf;
+   vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
+   return 1;
+}
+/*
+ * Schedule something to be freed
+ */
+__attribute__((warn_unused_result))
+static int vg_loader_highwater( void( *fn_free )(void *), void *data )
+{
+   if( fn_free )
+   {
+      struct loader_free_step step;
+      step.data = data;
+      step.fn_free = fn_free;
+
+      if( !vg_loader_push_free_step( step ) )
+         return 0;
+   }
+   else
+   {
+      if( data )
+      {
+         struct loader_free_step step;
+         step.data = data;
+         step.fn_free = vg_free_libc_malloced;
+
+         if( !vg_loader_push_free_step( step ) )
+            return 0;
+      }
+   }
+
+   vg_mutex_lock( &vg.mux_engine_status );
+
+   if( !vg.engine_running )
+   {
+      vg_mutex_unlock( &vg.mux_engine_status );
+      return 0;
+   }
+
+   vg_mutex_unlock( &vg.mux_engine_status );
+   return 1;
+}
+
+#endif /* VG_LOADER_H */
+
+#if 0
+#ifndef LOADER_H
+#define LOADER_H
+
+#include "common.h"
+
+static struct loader
+{
+   MUTEX_TYPE mux;
+
+   struct loader_step
+   {
+      int (*fn_load)(void);
+      void (*fn_free)(void);
+
+      int require_opengl;
+      const char *name;
+   }
+   *step_buffer;
+   u32 step_count, step_cap, step_action, 
+       low_water_mark; /* What is the minumum number of systems we can have? */
+
+   enum loader_status
+   {
+      k_loader_status_loading,
+      k_loader_status_complete,
+      k_loader_status_fail
+   }
+   status;
+
+   int cancel;
+}
+loader;
+
+static void loader_add_stage( struct loader_step step )
+{
+   loader.step_buffer = buffer_reserve( loader.step_buffer, loader.step_count, 
+                                        &loader.step_cap,
+                                        1, sizeof( struct loader_step ) );
+
+   loader.step_buffer[ loader.step_count ++ ] = step;
+}
+
+static void loader_insert_stage( struct loader_step step )
+{
+   
+}
+
+static void loader_cancel(void)
+{
+   MUTEX_LOCK( loader.mux );
+   loader.cancel = 1;
+   MUTEX_UNLOCK( loader.mux );
+}
+
+static void loader_worker_thread( void *nothing )
+{
+   while(1)
+   {
+      vg_sleep_ms( 1000.0 );
+      vg_info( "... loader ....\n" );
+
+      if( loader.cancel )
+      {
+         return;
+      }
+   }
+}
+
+static void loader_begin(void)
+{
+   if( loader.step_count == 0 )
+   {
+      loader.status = k_loader_status_complete;
+      return;
+   }
+
+   loader.status = k_loader_status_loading;
+   vg_thread_run( loader_worker_thread, NULL );
+}
+
+static void loader_free(void)
+{
+   /* TODO */
+   for( int i=0; i<loader.step_count; i++ )
+   {
+      struct loader_step *step = &loader.step_buffer[ loader.step_count -i -1 ];
+      if( step->fn_free )
+         step->fn_free();
+   }
+}
+
+/*
+ * Returns 0 if loading is not happening
+ * Returns 1 if we are loading something
+ */
+static int loader_update(void)
+{
+   MUTEX_LOCK( loader.mux );
+
+   if( loader.status == k_loader_status_complete )
+   {
+      MUTEX_UNLOCK( loader.mux );
+      return 0;
+   }
+   else
+   {
+      struct loader_step *cstep = &loader.step_buffer[ loader.step_action ];
+
+      if( cstep->require_opengl )
+      {
+         if( !cstep->fn_load() )
+         {
+            loader.cancel = 1;
+            MUTEX_UNLOCK( loader.mux );
+            
+            loader_free();
+            vg_exit();
+         }
+
+         loader.step_action ++;
+      }
+
+      MUTEX_UNLOCK( loader.mux );
+      return 1;
+   }
+}
+
+#endif /* LOADER_H */
+#endif