Search
lxdream.org :: lxdream/src/tools/genglsl.c :: diff
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/genglsl.c
changeset 1234:1b836bf92653
prev1229:dc935eee9767
next1235:8da2f3dad9c0
author nkeynes
date Fri Feb 24 17:31:18 2012 +1000 (8 years ago)
permissions -rw-r--r--
last change Add some real option processing to genglsl and let it accept multiple glsl
input files (basically concatenate them together)
file annotate diff log raw
1.1 --- a/src/tools/genglsl.c Thu Feb 23 19:43:24 2012 +1000
1.2 +++ b/src/tools/genglsl.c Fri Feb 24 17:31:18 2012 +1000
1.3 @@ -7,7 +7,7 @@
1.4 * a C file with appropriate escaping, as well as program definitions
1.5 * written as #program <name> = <shader1> <shader2> ... <shaderN>
1.6 *
1.7 - * Copyright (c) 2007-2010 Nathan Keynes.
1.8 + * Copyright (c) 2007-2012 Nathan Keynes.
1.9 *
1.10 * This program is free software; you can redistribute it and/or modify
1.11 * it under the terms of the GNU General Public License as published by
1.12 @@ -26,6 +26,7 @@
1.13 #include <stdio.h>
1.14 #include <stdlib.h>
1.15 #include <string.h>
1.16 +#include <getopt.h>
1.17 #include <glib/gstrfuncs.h>
1.18 #include <glib/glist.h>
1.19
1.20 @@ -144,7 +145,7 @@
1.21 }
1.22
1.23
1.24 -static struct glsldata *readInput( const char *filename )
1.25 +static void readInput( const char *filename, glsldata_t result )
1.26 {
1.27 char buf[MAX_LINE];
1.28 size_t current_size = 0, current_posn = 0;
1.29 @@ -153,13 +154,17 @@
1.30 FILE *f = fopen( filename, "ro" );
1.31 if( f == NULL ) {
1.32 fprintf( stderr, "Error: unable to open input file '%s': %s\n", filename, strerror(errno) );
1.33 - exit(1);
1.34 + exit(2);
1.35 }
1.36
1.37 shader_t shader = NULL;
1.38 - glsldata_t result = g_malloc0(sizeof(struct glsldata));
1.39 - assert( result != NULL );
1.40 - result->filename = strdup(filename);
1.41 + if( result->filename == NULL ) {
1.42 + result->filename = g_strdup(filename);
1.43 + } else {
1.44 + const gchar *tmp = result->filename;
1.45 + result->filename = g_strdup_printf("%s, %s", tmp, filename);
1.46 + g_free((gchar *)tmp);
1.47 + }
1.48
1.49 while( fgets(buf, sizeof(buf), f) != NULL ) {
1.50 if( strlen(buf) == 0 )
1.51 @@ -220,8 +225,6 @@
1.52 }
1.53
1.54 fclose(f);
1.55 - linkPrograms(result);
1.56 - return result;
1.57 }
1.58
1.59 /**
1.60 @@ -433,7 +436,7 @@
1.61 fclose(f);
1.62 }
1.63
1.64 -const char *makeExtension(const char *basename, const char *ext)
1.65 +static const char *makeExtension(const char *basename, const char *ext)
1.66 {
1.67 const char *oldext = strrchr(basename, '.');
1.68 if( oldext == NULL ) {
1.69 @@ -443,29 +446,65 @@
1.70 }
1.71 }
1.72
1.73 +static char *option_list = "hi:o:";
1.74 +static struct option long_option_list[] = {
1.75 + { "help", no_argument, NULL, 'h' },
1.76 + { "interface", required_argument, 'i' },
1.77 + { "output", required_argument, NULL, 'o' },
1.78 + { NULL, 0, 0, 0 } };
1.79 +
1.80 +static void usage() {
1.81 + fprintf( stderr, "Usage: genglsl <glsl-source-list> [-o output.def] [-i output.h]\n");
1.82 +}
1.83 int main( int argc, char *argv[] )
1.84 {
1.85 - if( argc < 2 ) {
1.86 - fprintf( stderr, "Usage: genglsl <glsl-source-file> [output.c [output.h]]\n");
1.87 + const char *output_file = NULL;
1.88 + const char *iface_file = NULL;
1.89 + int opt;
1.90 +
1.91 + while( (opt = getopt_long( argc, argv, option_list, long_option_list, NULL )) != -1 ) {
1.92 + switch( opt ) {
1.93 + case 'h':
1.94 + usage();
1.95 + exit(0);
1.96 + break;
1.97 + case 'i':
1.98 + if( iface_file != NULL ) {
1.99 + fprintf( stderr, "Error: at most one interface file can be supplied\n" );
1.100 + usage();
1.101 + exit(1);
1.102 + }
1.103 + iface_file = optarg;
1.104 + break;
1.105 + case 'o':
1.106 + if( output_file != NULL ) {
1.107 + fprintf( stderr, "Error: at most one output file can be supplied\n" );
1.108 + usage();
1.109 + exit(1);
1.110 + }
1.111 + output_file = optarg;
1.112 + }
1.113 + }
1.114 +
1.115 + if( optind == argc ) {
1.116 + usage();
1.117 exit(1);
1.118 }
1.119
1.120 - glsldata_t data = readInput(argv[1]);
1.121 -
1.122 - const char *sourcefile, *ifacefile;
1.123 - if( argc > 2 ) {
1.124 - sourcefile = argv[2];
1.125 - } else {
1.126 - sourcefile = makeExtension(argv[1], ".def");
1.127 + if( output_file == NULL ) {
1.128 + output_file = makeExtension(argv[optind], ".def");
1.129 + }
1.130 + if( iface_file == NULL ) {
1.131 + iface_file = makeExtension(output_file, ".h");
1.132 }
1.133
1.134 - if( argc > 3 ) {
1.135 - ifacefile = argv[3];
1.136 - } else {
1.137 - ifacefile = makeExtension(sourcefile, ".h");
1.138 + glsldata_t data = g_malloc0(sizeof(struct glsldata));
1.139 + while( optind < argc ) {
1.140 + readInput(argv[optind++], data);
1.141 }
1.142 + linkPrograms(data);
1.143
1.144 - writeSource( sourcefile, data );
1.145 - writeInterface( ifacefile, data );
1.146 + writeSource( output_file, data );
1.147 + writeInterface( iface_file, data );
1.148 return 0;
1.149 }
.