Platform and OS stability stuff
[vg.git] / src / vg / vg_lines.h
index e14d3381d3e4bf65942601afcb79a53ee052bb45..82f1814add1126f90df1dc98abfc3967f6be4515 100644 (file)
@@ -1,5 +1,10 @@
 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
 
+#ifndef VG_LINES_H
+#define VG_LINES_H
+
+#include "vg/vg.h"
+
 static int debug_lines_enable = 1;
 
 #ifdef VG_3D
@@ -76,6 +81,8 @@ vg_lines;
 
 static void vg_lines_init(void)
 {
+   vg_info( "vg_lines_init\n" );
+
    vg_convar_push( (struct vg_convar){
       .name = "debug_lines",
       .data = &debug_lines_enable,
@@ -85,47 +92,58 @@ static void vg_lines_init(void)
    });
    
    vg_shader_register( &_shader_lines );
-       
-       glGenVertexArrays( 1, &vg_lines.vao );
-       glGenBuffers( 1, &vg_lines.vbo );
-       glBindVertexArray( vg_lines.vao );
-       
-       glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
-       
-       vg_lines.cap = 50000;
-       vg_lines.buffer_size = vg_lines.cap * sizeof( struct vg_lines_vert );
-       
-       glBufferData( GL_ARRAY_BUFFER, vg_lines.buffer_size, NULL, GL_DYNAMIC_DRAW );
-       glBindVertexArray( vg_lines.vao );
-       
-       glVertexAttribPointer( 
-               0, 
-               sizeof( vg_lines.buffer[0].co ) / sizeof(float), 
-               GL_FLOAT, 
-               GL_FALSE, 
-               sizeof( struct vg_lines_vert ), 
-               (void *)0 
-       );
-       glEnableVertexAttribArray( 0 );
-       
-       glVertexAttribPointer( 
-               1, 
-               4, 
-               GL_UNSIGNED_BYTE, 
-               GL_TRUE, 
-               sizeof( struct vg_lines_vert ), 
-               (void*)(offsetof( struct vg_lines_vert, colour ))
-       );
-       glEnableVertexAttribArray( 1 );
-       vg_lines.buffer = malloc( vg_lines.buffer_size );
+
+   vg_acquire_thread_sync();
+   {
+      glGenVertexArrays( 1, &vg_lines.vao );
+      glGenBuffers( 1, &vg_lines.vbo );
+      glBindVertexArray( vg_lines.vao );
+      glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo );
+      
+      vg_lines.cap = 50000;
+      vg_lines.buffer_size = vg_lines.cap * sizeof( struct vg_lines_vert );
+      
+      glBufferData( GL_ARRAY_BUFFER, vg_lines.buffer_size, 
+                    NULL, GL_DYNAMIC_DRAW );
+      glBindVertexArray( vg_lines.vao );
+      VG_CHECK_GL_ERR();
+
+      /* Pointers */
+      glVertexAttribPointer( 
+         0, 
+         sizeof( vg_lines.buffer[0].co ) / sizeof(float), 
+         GL_FLOAT, 
+         GL_FALSE, 
+         sizeof( struct vg_lines_vert ), 
+         (void *)0 
+      );
+      glEnableVertexAttribArray( 0 );
+      
+      glVertexAttribPointer( 
+         1, 
+         4, 
+         GL_UNSIGNED_BYTE, 
+         GL_TRUE, 
+         sizeof( struct vg_lines_vert ), 
+         (void*)(offsetof( struct vg_lines_vert, colour ))
+      );
+      glEnableVertexAttribArray( 1 );
+
+      VG_CHECK_GL_ERR();
+
+      /* Alloc RAM */
+      vg_lines.buffer = vg_alloc( vg_lines.buffer_size );
+      vg_success( "done\n" );
+   }
+
+   vg_release_thread_sync();
 }
 
-static void vg_lines_free(void)
+static void vg_lines_free(void *nothing)
 {
        glDeleteVertexArrays( 1, &vg_lines.vao );
        glDeleteBuffers( 1, &vg_lines.vbo );
-       
-       free( vg_lines.buffer );
+       vg_free( vg_lines.buffer );
 }
 
 static void vg_lines_drawall( float* projection )
@@ -263,6 +281,20 @@ static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour )
    vg_line( p100, p010, colour );
 }
 
+static void vg_line_cross(v3f pos,u32 colour, float scale)
+{
+   v3f p0, p1;
+   v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 );
+   v3_add( (v3f){-scale,0.0f,0.0f}, pos, p1 );
+   vg_line( p0, p1, colour );
+   v3_add( (v3f){0.0f, scale,0.0f}, pos, p0 );
+   v3_add( (v3f){0.0f,-scale,0.0f}, pos, p1 );
+   vg_line( p0, p1, colour );
+   v3_add( (v3f){0.0f,0.0f, scale}, pos, p0 );
+   v3_add( (v3f){0.0f,0.0f,-scale}, pos, p1 );
+   vg_line( p0, p1, colour );
+}
+
 static void vg_line_pt3( v3f pt, float size, u32 colour )
 {
    boxf box =
@@ -273,3 +305,5 @@ static void vg_line_pt3( v3f pt, float size, u32 colour )
 
    vg_line_boxf( box, colour );
 }
+
+#endif /* VG_LINES_H */