command line, multisampling, optimisations
[csRadar.git] / csRadar.c
index 43b9c0eff82b6ccbd6ebcf53320127007f5ee242..2bde6027f01b26565f53b92a57d957aeb061f202 100644 (file)
--- a/csRadar.c
+++ b/csRadar.c
@@ -6,6 +6,7 @@
 #include <time.h>
 
 // CSR lib
+#include "csrOpt.h"
 #include "csrTypes.h"
 #include "csrMath.h"
 #include "csrMem.h"
 #include "vmf.h"
 
 // CSR main
-#include "csrDraw.h"
 #include "csr32f.h"
+#include "csrDraw.h"
 
+#define CSR_VERSION "0.0.1"
 
-//#include "stretchy_buffer.h"
+// gcc -Wall -fsanitize=address csRadar.c -o csRadar -lm
 
 int main( int argc, char *argv[] )
 {
-       if( argc == 2 )
+       char *arg;
+       char *strings[ 20 ];
+       int num_strings = 0;
+       
+       float padding = 128.f;
+       u32 resolution = 1024;
+       int standard_layers = 0;
+       int write_txt = 1;
+       int multi_sample = 1;
+       char output_path[ 512 ];                // Full path eg. /home/harry/my_map.vmf
+       char vmf_name[ 128 ];                   // Just the base name eg. my_map
+       int output_set = 0;
+
+       while( csr_argp( argc, argv ) )
        {
-               fs_set_gameinfo( "/home/harry/SteamLibrary/steamapps/common/Counter-Strike Global Offensive/csgo/gameinfo.txt" );
-               
-               printf( "read: %s\n", argv[1] );
-               //vdf_node *node = vdf_open_file( argv[1] );
-               
-               vmf_map *map = vmf_init( argv[1], 1 );
-               
-               csr_target target;
-               
-               csr_create_target( &target, 1024, 1024 );
-               csr_rt_clear( &target );
-               
-               csr_filter filter_layout =
+               if( (arg = csr_arg()) )
                {
-                       .classname = NULL,
-                       .visgroup = "tar_layout"
-               };
+                       if( num_strings == 20 )
+                       {
+                               fprintf( stderr, "Too many arguments! Max 20\n" );
+                               fs_exit();
+                               exit(0);
+                       }
+                       
+                       strings[ num_strings ++ ] = arg;
+               }
                
-               csr_filter filter_buyzone =
+               if( (arg = csr_opt_arg( 'o' )) )
                {
-                       .classname = "func_buyzone",
-                       .visgroup = NULL
-               };
+                       strcpy( output_path, arg );
+                       csr_path_winunix( output_path );
+                       
+                       output_set = 1;
+               }
                
-               csr_filter filter_setup = 
+               if( (arg = csr_opt_arg( 'g' )) )
                {
-                       .classname = NULL,
-                       .visgroup = NULL,
-                       .compute_bounds_only = 1
-               };
-               
-               draw_vmf_group( &target, map, map->root, &filter_setup, NULL, NULL );
-               csr_auto_fit( &target, 128.f );
-               
-               draw_vmf_group( &target, map, map->root, NULL, NULL, NULL );
-               
-               float *rgba_test = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
+                       fs_set_gameinfo( arg );
+               }
                
-               for( int l = 0; l < 1024; l ++ )
+               if( (arg = csr_opt_arg( 'r' )) )
                {
-                       for( int x = 0; x < 1024; x ++ )
-                       {
-                               float *dst = &rgba_test[ (l*1024+x)*3 ];
-                               csr_frag *src = &target.fragments[ ((1023-l)*1024+x) ];
-                               
-                               dst[0] = src->co[0];
-                               dst[1] = src->co[1];
-                               dst[2] = src->co[2];
-                       }
+                       resolution = atoi( arg );
                }
                
-               csr_32f_write( "hello.pfm", 1024, 1024, rgba_test );
-               
-               csr_rt_free( &target );
-               
-               free( rgba_test );
-               vmf_free( map );
-               fs_exit();
-               
-               /*
-               vmf_solid solid_main;
-               
-               
-               solidgen_ctx_init( &solid_main );
-                       
-               vdf_node *world = vdf_next( node, "world", NULL );
-               
-               vdf_foreach( world, "solid", brush )
+               if( (arg = csr_long_opt_arg( "padding" )) )
                {
-                       solidgen_push( &solid_main, brush );
+                       padding = atof( arg );
                }
 
-               clock_t t;
-               t = clock();
-               printf("Timer starts\n");
-
-               csr_frag *image = (csr_frag *)csr_malloc( 1024*1024*sizeof(csr_frag) );
-               clear_depth( image, 1024, 1024 );
-
-               for( int i = 0; i < csr_sb_count( solid_main.indices )/3; i ++ )
+               if( csr_opt( 'v' ) || csr_long_opt( "version" ) )
+               {
+                       printf( "csRadar version: " CSR_VERSION "\n" );
+                       return 0;
+               }
+               
+               if( csr_opt( 'h' ) || csr_long_opt( "help" ) )
+               {
+                       // Display help
+                       printf
+                       ( 
+                               "csRadar Copyright (C) 2021 Harry Godden (hgn)\n"
+                               "\n"
+                               "Usage: ./csRadar map.vmf -g \"/gamedir/gameinfo.txt\" layout cover\n"
+                               "   VMF file is first, then any other arguments (eg. layout, cover), will specify\n"
+                               "   visgroups to be rendered into individual files\n"
+                               "   No visgroups specified will simply draw everything\n"
+                               "\n"
+                               "Options:\n"
+                               "   -g <gameinfo.txt path>         Required if you are loading models\n"
+                               "   -r 1024                        Output resolution\n"
+                               "   -o <output>                    Specify output name/path\n"
+                               "   --padding=128                  When cropping radar, add padding units to border\n"
+                               "   --standard-layers              Use standard TAR layers/groups\n"
+                               "   --no-txt                       Don't create matching radar txt\n"
+                               "   --multi-sample=                [ none, 2, 4, 4r, 8kn (default), 16c ]\n"
+                               "\n"
+                               "   -v --version                   Display program version\n"
+                               "   -h --help                      Display this help text\n"
+                       );
+                       
+                       return 0;
+               }
+       }
+       
+       if( num_strings )
+       {
+               vmf_map *map = vmf_init( strings[0], 1 );
+       
+               if( map )
                {
-                       u32 * base = solid_main.indices + i*3;
+                       // Path handling
+                       if( !output_set )
+                       {
+                               strcpy( output_path, strings[0] );
+                               csr_stripext( output_path );
+                       }
+                       
+                       char *base_name;
+                       if( !(base_name = csr_findext( output_path, '/' ) ))
+                       {
+                               base_name = output_path;
+                       }
                        
-                       vmf_vert tri[3];
+                       strcpy( vmf_name, base_name );
+                       
+                       printf( "output_path: '%s'\nvmf_name: '%s'\n", output_path, vmf_name );
+                       
+                       
+                       // Main
+                                       
+                       csr_target target;
+                       
+                       csr_create_target( &target, resolution, resolution );
+                       csr_rt_clear( &target );
+                       
+                       // Compute bounds
+                       csr_filter filter = 
+                       {
+                               .classname = NULL,
+                               .visgroup = NULL,
+                               .compute_bounds_only = 1
+                       };
+                       draw_vmf_group( &target, map, map->root, &filter, NULL, NULL );
+                       csr_auto_fit( &target, padding );
+                       filter.compute_bounds_only = 0;
+                       
+                       if( num_strings == 1 )
+                       {
+                               // Draw everything
+                               draw_vmf_group( &target, map, map->root, NULL, NULL, NULL );
+                               csr_rt_save_buffers( &target, output_path, "all" );
+                       }
+                       else
+                       {
+                               // Draw groups
+                               for( int i = 1; i < num_strings; i ++ )
+                               {
+                                       filter.visgroup = strings[ i ];
+                                       
+                                       draw_vmf_group( &target, map, map->root, NULL, NULL, NULL );
+                                       csr_rt_save_buffers( &target, output_path, strings[i] );
+                                       
+                                       csr_rt_clear( &target );
+                               }
+                       }
                        
-                       tri[0] = solid_main.verts[ base[0] ];
-                       tri[1] = solid_main.verts[ base[1] ];
-                       tri[2] = solid_main.verts[ base[2] ];
+                       if( write_txt )
+                       {
+                               char txt_path[ 512 ];
+                               
+                               strcpy( txt_path, output_path );
+                               strcat( txt_path, ".txt" );
+                               
+                               csr_write_txt( txt_path, vmf_name, &target );
+                       }
                        
-                       draw_buffers( image, 1024, 1024, (v4f){ -1000.f, -1000.f, 1000.f, 1000.f }, tri, 1 );
+                       csr_rt_free( &target );
+                       vmf_free( map );
                }
-               
-               printf("Timer ends \n");
-               t = clock() - t;
-               double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
-               printf("Tracing took %f seconds to execute\n", time_taken);
-               
-               float *rgba_test = (float *)csr_malloc( 1024*1024*sizeof(float)*3 );
-               
-               for( int i = 0; i < 1024*1024; i ++ )
+               else
                {
-                       rgba_test[i*3+0] = image[i].qa;
-                       rgba_test[i*3+1] = image[i].qb;
-                       rgba_test[i*3+2] = image[i].depth;
+                       fprintf( stderr, "Could not load VMF\n" );
                }
-               
-               csr_32f_write( "hello.pfm", 1024, 1024, rgba_test );
-               
-               free( rgba_test );
-               free( image );
-               
-               solidgen_to_obj( &solid_main, "hello.obj" );
-               
-               vdf_free_r( node );
-               */
        }
+       else
+       {
+               fprintf( stderr, "Missing required argument: mapfile\n" );
+       }
+       
+       fs_exit();
+
+       return 0;
 }