simplify gitignore
[vg.git] / src / vg / vg_loader.h
index 6e3882af4c2bd5696f7c09e4b451e172136e483e..72e4c3cad37fe8fb24309669df887197fb8ae0bd 100644 (file)
 
 #include "common.h"
 
+static struct vg_shader _shader_loader = 
+{
+   .name = "[vg] loader",
+   .link = NULL,
+   .vs = 
+   {
+      .orig_file = NULL,
+      .static_src = ""
+      "layout (location=0) in vec2 a_co;"
+      "out vec2 aUv;"
+      "void main()"
+      "{"
+         "gl_Position = vec4(a_co*2.0-1.0,0.0,1.0);"
+         "aUv = a_co;"
+      "}"
+   },
+   .fs = 
+   {
+      .orig_file = NULL,
+      .static_src = 
+      
+      "out vec4 FragColor;"
+      "uniform float uTime;"
+      "in vec2 aUv;"
+      
+      "void main()"
+      "{"
+         "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
+         "float grad = 1.0-(aUv.y*0.5+0.5);"
+         "float fmt1 = step( 0.5, grad+dither );"
+
+         "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
+
+         "FragColor = vec4(col*grad*fmt1,1.0);"
+      "}"
+   }
+};
+
 static struct vg_loader
 {
    /* Shutdown steps */
@@ -20,19 +58,38 @@ static struct vg_loader
       void (*fn_free)(void *);
       void *data;
    }
-   *step_buffer;
-   u32 step_count, step_cap, step_action;
+   step_buffer[16];
+   u32 step_count, step_action;
+
+   GLuint vao, vbo;
 }
 vg_loader;
 
-static int vg_loader_init(void)
+VG_STATIC void vg_loader_init(void)
 {
-   return 1;
+   float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
+                    0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f };
+
+   glGenVertexArrays( 1, &vg_loader.vao );
+   glGenBuffers( 1, &vg_loader.vbo );
+   glBindVertexArray( vg_loader.vao );
+   glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo );
+   glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW );
+   glBindVertexArray( vg_loader.vao );
+   glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 );
+   glEnableVertexAttribArray( 0 );
+
+   VG_CHECK_GL_ERR();
+
+   if( !vg_shader_compile( &_shader_loader ) )
+      vg_fatal_exit_loop( "failed to compile shader" );
 }
 
-static void vg_loader_free(void)
+VG_STATIC void vg_loader_free(void)
 {
    vg_info( "vg_loader_free\n" );
+   glDeleteVertexArrays( 1, &vg_loader.vao );
+   glDeleteBuffers( 1, &vg_loader.vbo );
 
    for( int i=0; i<vg_loader.step_count; i++ )
    {
@@ -43,11 +100,10 @@ static void vg_loader_free(void)
       step->fn_free( step->data );
    }
 
-   vg_free( vg_loader.step_buffer );
    vg_info( "done\n" );
 }
 
-static float hue_to_rgb( float p, float q, float t )
+VG_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;
@@ -57,36 +113,22 @@ static float hue_to_rgb( float p, float q, float t )
    return p;
 }
 
-static void vg_loader_render(void)
+VG_STATIC void vg_render_log(void)
 {
-   float h = vg_randf(),
-         s = 0.7f,
-         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 );
-
+   ui_begin( vg.window_x, vg.window_y );
+   vg_mutex_lock( &log_print_mutex );
 
    int const fh = 14;
-   int lines_screen_max = ((vg_window_y/fh)-2),
+   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 );
+       vg_uictx.cursor[0] = 0;
+       vg_uictx.cursor[1] = lines_to_draw*fh;
+   vg_uictx.cursor[3] = fh;
+       ui_fill_x();
 
    for( int i=0; i<lines_to_draw; i ++ )
    {
@@ -95,20 +137,36 @@ static void vg_loader_render(void)
       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_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
+      vg_uictx.cursor[1] -= fh;
    }
 
-   ui_resolve( &ui_global_ctx );
-   ui_draw( &ui_global_ctx, NULL );
+   vg_mutex_unlock( &log_print_mutex );
+
+   ui_resolve();
+   ui_draw( NULL );
+}
+
+VG_STATIC void vg_loader_render(void)
+{
+   glViewport( 0,0, vg.window_x, vg.window_y );
+   glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+   glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
+   glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
+
+   glUseProgram( _shader_loader.id );
+       glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
+
+   glBindVertexArray( vg_loader.vao );
+   glDrawArrays( GL_TRIANGLES, 0, 6 );
+
+   vg_render_log();
 }
 
 
-static void vg_load_full(void);
+VG_STATIC void vg_load_full(void);
 
-static void vg_loader_thread(void * nothing)
+VG_STATIC void vg_loader_thread(void * nothing)
 {
    vg_thread_info.gl_context_level = 0;
    vg_thread_info.purpose = k_thread_purpose_loader;
@@ -119,31 +177,30 @@ static void vg_loader_thread(void * nothing)
    vg_semaphore_post( &vg.sem_loader );
 }
 
-static void vg_loader_start(void)
+VG_STATIC void vg_loader_start(void)
 {
    vg_semaphore_wait( &vg.sem_loader );
    vg_thread_run( vg_loader_thread, NULL );
 }
 
-static void vg_free_libc_malloced( void *data )
+/* this is maybe probably unused now */
+VG_STATIC void vg_free_libc_malloced( void *data )
 {
-   vg_free( data );
+   free( data );
 }
 
-static void vg_loader_push_free_step( struct loader_free_step step )
+VG_STATIC void vg_loader_push_free_step( struct loader_free_step step )
 {
-   vg_loader.step_buffer = 
-      buffer_reserve( vg_loader.step_buffer, vg_loader.step_count, 
-                              &vg_loader.step_cap, 1, 
-                              sizeof( struct loader_free_step ) );
+   if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
+      vg_fatal_exit_loop( "Too many free steps" );
 
    vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
 }
 
 /*
- * Schedule something to be ran now, freed later
+ * Schedule something to be ran now, freed later. Checks in with engine status
  */
-static void vg_loader_highwater( void( *fn_load )(void), 
+VG_STATIC void vg_loader_highwater( void( *fn_load )(void), 
                                  void( *fn_free )(void *), void *data )
 {
    if( fn_load )