binary message system
[vg.git] / vg_ui.h
diff --git a/vg_ui.h b/vg_ui.h
index 0c4b4159bc5636419e86f430fe185aac93410adb..5296afc12e76b5f95c4f36c64e663483bda44904 100644 (file)
--- a/vg_ui.h
+++ b/vg_ui.h
@@ -1,8 +1,10 @@
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
+#if 0
 #ifndef VG_UI_H
 #define VG_UI_H
 
+#define VG_GAME
 #include "vg/vg.h"
 #include "vg/vg_tex.h"
 #include "vg/vg_shader.h"
@@ -73,6 +75,66 @@ static struct vg_shader _shader_ui =
    }
 };
 
+static struct vg_shader _shader_ui_image =
+{
+   .name = "[vg] ui_image",
+   .link = NULL,
+   .vs = 
+   {
+      .orig_file = NULL,
+      .static_src = 
+       "layout (location=0) in vec2 a_co;"
+       "layout (location=1) in vec2 a_uv;"
+       "layout (location=2) in vec4 a_colour;"
+       "layout (location=3) in vec4 a_clip;"
+       "uniform mat3 uPv;"
+       ""
+       "out vec2 aTexCoords;"
+       "out vec4 aColour;"
+       "out vec2 aWsp;"
+       "out vec4 aClip;"
+       ""
+       "void main()"
+       "{"
+               "gl_Position = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );"
+               "aTexCoords = a_uv * 0.0078125;"
+               "aColour = a_colour;"
+               
+               "aWsp = a_co;"
+               "aClip = a_clip;"
+       "}",
+   },
+   .fs = 
+   {
+      .orig_file = NULL,
+      .static_src = 
+       "uniform sampler2D uTexImage;"
+       "out vec4 FragColor;"
+       ""
+       "in vec2 aTexCoords;"
+       "in vec4 aColour;"
+       ""
+       "in vec2 aWsp;"
+       "in vec4 aClip;"
+
+       "void main()"
+       "{"
+               "float clip_blend = step( aWsp.x, aClip.z ) *"
+                         "step( aWsp.y, aClip.w ) *"
+                         "step( aClip.x, aWsp.x ) *"
+                         "step( aClip.y, aWsp.y ); "
+
+               "vec4 colour = texture( uTexImage, aTexCoords );"
+      "float value = dot(vec4(1.0),colour)*0.25;"
+
+      "vec3 col = vec3(pow(cos(value*3.14159265*2.0)*0.5+0.5,0.5))"
+                  "* vec3(step(value,0.5),0.3,step(1.0-value,0.5));"
+
+               "FragColor = vec4( col*4.0, clip_blend );"
+       "}"
+   }
+};
+
 typedef i16                            ui_px;
 typedef u32                            ui_colour;
 typedef ui_px                          ui_rect[4];
@@ -158,7 +220,7 @@ struct
 } 
 static vg_uictx;
 
-#define UI_GLYPH_SPACING_X 9
+#define UI_GLYPH_SPACING_X 8
 
 static GLuint ui_glyph_texture = 0;
 static ui_colourset ui_default_colours = {
@@ -167,10 +229,11 @@ static ui_colourset ui_default_colours = {
        .active = 0xffad9f9e
 };
 
-VG_STATIC void ui_init_context(void)
+VG_STATIC void _vg_ui_init(void)
 {
-   if( !vg_shader_compile( &_shader_ui ) ) 
-      vg_fatal_exit_loop( "Failed to compile ui shader" );
+   if( !vg_shader_compile( &_shader_ui ) ||
+       !vg_shader_compile( &_shader_ui_image ) ) 
+      vg_fatal_error( "Failed to compile ui shader" );
 
    /*
     * Vertex buffer
@@ -179,6 +242,7 @@ VG_STATIC void ui_init_context(void)
 
    vg_uictx.max_indices = 20000;
    vg_uictx.max_verts = 30000;
+   vg_uictx.colours = &ui_default_colours;
        
        /* Generate the buffer we are gonna be drawing to */
    glGenVertexArrays( 1, &vg_uictx.vao );
@@ -226,7 +290,7 @@ VG_STATIC void ui_init_context(void)
 
        /* Alloc RAM default context */
    u32 vert_size = vg_uictx.max_verts*sizeof(struct ui_vert),
-       inds_size = vg_uictx.max_indices*sizeof(u16);
+       inds_size = vg_align8( vg_uictx.max_indices*sizeof(u16) );
    
    vg_uictx.vertex_buffer = vg_linear_alloc( vg_mem.rtmemory, vert_size );
    vg_uictx.indice_buffer = vg_linear_alloc( vg_mem.rtmemory, inds_size );
@@ -237,20 +301,17 @@ VG_STATIC void ui_init_context(void)
    
        /* Load default font */
    u32 compressed[] = {
-      #include "vg/vg_pxfont.h"
+      #include "vg/vg_pxfont_thin.h"
    };
 
    u32 pixels = 0, total = 256*256, data = 0;
    u8 image[256*256];
    
-   while( pixels < total )
-   {
-      for( int b = 31; b >= 0; b-- )
-      {
-         image[ pixels ++ ] = (compressed[data] & (0x1 << b))? 0xff: 0x00;
+   while( pixels < total ){
+      for( int b = 31; b >= 0; b-- ){
+         image[ pixels ++ ] = (compressed[data] & (0x1u << b))? 0xffu: 0x00u;
          
-         if( pixels >= total )
-         {
+         if( pixels >= total ){
             total = 0;
             break;
          }
@@ -264,8 +325,10 @@ VG_STATIC void ui_init_context(void)
                  GL_RED, GL_UNSIGNED_BYTE, image );
 
    VG_CHECK_GL_ERR();
-   vg_tex2d_clamp();
-   vg_tex2d_nearest();
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
+   glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
 }
 
 static struct ui_vert *ui_fill_rect_uv( ui_rect rect, u32 colour, ui_px uv[4] );
@@ -320,6 +383,15 @@ VG_STATIC void ui_draw( m3x3f view_override )
        
        glDrawElements( GL_TRIANGLES, num_indices_normal, 
          GL_UNSIGNED_SHORT, (void*)(0) );
+
+
+   /* images */
+   glUseProgram( _shader_ui_image.id );
+       glUniformMatrix3fv( glGetUniformLocation( _shader_ui_image.id, "uPv" ), 1, 
+         GL_FALSE, (float *)view_override );
+
+       glActiveTexture( GL_TEXTURE1 );
+       glUniform1i( glGetUniformLocation( _shader_ui_image.id, "uTexImage" ), 1 );
        
        /* Draw image elements */
        for( int i = 0; i < vg_uictx.image_count; i ++ )
@@ -581,16 +653,13 @@ VG_STATIC void ui_text( ui_px pos[2],
        text_cursor[2] = 8*scale;
        text_cursor[3] = 14*scale;
 
-       while( (c = *(_c ++)) )
-       {
-               if( c == '\n' )
-               {
+       while( (c = *(_c ++)) ){
+               if( c == '\n' ){
                        text_cursor[1] += 14*scale;
                        text_cursor[0] = pos[0] + ui_text_line_offset( _c, scale, align );
                        continue;
                }
-               else if( c >= 33 )
-               {
+               else if( c >= 33 ){
                        u8 glyph_base[2];
                        u8 glyph_index = c;
                        glyph_base[0] = glyph_index & 0xf;
@@ -607,20 +676,15 @@ VG_STATIC void ui_text( ui_px pos[2],
                glyph_base[1]+8
             });
                }
-               else if( c == '\x1B' )
-               {
+               else if( c == '\x1B' ){
                        _c ++;
                        u16 colour_id = 0;
-                       for( int i = 0; i < 3; i ++ )
-                       {
-                               if( _c[i] )
-                               {
-                                       if( _c[i] == 'm' )
-                                       {
+                       for( int i = 0; i < 3; i ++ ){
+                               if( _c[i] ){
+                                       if( _c[i] == 'm' ){
                                                _c = _c + i + 1;
                                                
-                                               switch( colour_id )
-                                               {
+                                               switch( colour_id ){
                                                        case '0': current_colour = 0x00ffffff; break;
                                                        case '3'|'1'<<8: current_colour = 0x00201fee; break;
                                                        case '3'|'2'<<8: current_colour = 0x0037e420; break;
@@ -636,8 +700,7 @@ VG_STATIC void ui_text( ui_px pos[2],
                                        
                                        colour_id |= _c[i] << (i*8);
                                } 
-                               else
-                               {
+                               else{
                                        _c = _c +i;
                                        break;
                                }
@@ -645,8 +708,7 @@ VG_STATIC void ui_text( ui_px pos[2],
 
          continue;
                }
-      else if( c == '\t' )
-      {
+      else if( c == '\t' ){
          text_cursor[0] += UI_GLYPH_SPACING_X*scale*4;
          continue;
       }
@@ -916,3 +978,4 @@ VG_STATIC void ui_checkbox( struct ui_checkbox *cb )
 }
 
 #endif /* VG_UI_H */
+#endif