Search
lxdream.org :: lxdream/src/tools/genglsl.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/tools/genglsl.c
changeset 561:533f6b478071
prev405:570d93abb5b7
next736:a02d1475ccfd
author nkeynes
date Tue Jan 01 05:08:38 2008 +0000 (12 years ago)
branchlxdream-mmu
permissions -rw-r--r--
last change Enable Id keyword on all source files
file annotate diff log raw
nkeynes@405
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@405
     3
 *
nkeynes@405
     4
 * Trivial tool to take two shader source files and dump them out in
nkeynes@405
     5
 * a C file with appropriate escaping.
nkeynes@405
     6
 *
nkeynes@405
     7
 * Copyright (c) 2007 Nathan Keynes.
nkeynes@405
     8
 *
nkeynes@405
     9
 * This program is free software; you can redistribute it and/or modify
nkeynes@405
    10
 * it under the terms of the GNU General Public License as published by
nkeynes@405
    11
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@405
    12
 * (at your option) any later version.
nkeynes@405
    13
 *
nkeynes@405
    14
 * This program is distributed in the hope that it will be useful,
nkeynes@405
    15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@405
    16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@405
    17
 * GNU General Public License for more details.
nkeynes@405
    18
 */
nkeynes@405
    19
nkeynes@405
    20
#include <stdio.h>
nkeynes@405
    21
#include <stdlib.h>
nkeynes@405
    22
nkeynes@405
    23
/**
nkeynes@405
    24
 * Copy input to output, quoting " characters as we go.
nkeynes@405
    25
 */
nkeynes@405
    26
void writeShader( FILE *out, FILE *in )
nkeynes@405
    27
{
nkeynes@405
    28
    int ch;
nkeynes@405
    29
nkeynes@405
    30
    while( (ch = fgetc(in)) != EOF ) {
nkeynes@405
    31
	if( ch == '\"' ) {
nkeynes@405
    32
	    fputc( '\\', out );
nkeynes@405
    33
	} else if( ch == '\n') {
nkeynes@405
    34
	    fputs( "\\n\\", out );
nkeynes@405
    35
	}
nkeynes@405
    36
	fputc( ch, out );
nkeynes@405
    37
    }
nkeynes@405
    38
}
nkeynes@405
    39
nkeynes@405
    40
int main( int argc, char *argv[] )
nkeynes@405
    41
{
nkeynes@405
    42
    if( argc != 4 ) {
nkeynes@405
    43
	fprintf( stderr, "Usage: genglsl <vertex-shader-file> <fragment-shader-file> <output-file>\n");
nkeynes@405
    44
	exit(1);
nkeynes@405
    45
    }
nkeynes@405
    46
nkeynes@405
    47
    FILE *vsin = fopen( argv[1], "ro" );
nkeynes@405
    48
    if( vsin == NULL ) {
nkeynes@405
    49
	perror( "Unable to open vertex shader source" );
nkeynes@405
    50
	exit(2);
nkeynes@405
    51
    }
nkeynes@405
    52
nkeynes@405
    53
    FILE *fsin = fopen( argv[2], "ro" );
nkeynes@405
    54
    if( fsin == NULL ) {
nkeynes@405
    55
	perror( "Unable to open fragment shader source" );
nkeynes@405
    56
	exit(2);
nkeynes@405
    57
    }
nkeynes@405
    58
nkeynes@405
    59
    FILE *out = fopen( argv[3], "wo" );
nkeynes@405
    60
    if( out == NULL ) {
nkeynes@405
    61
	perror( "Unable to open output file" );
nkeynes@405
    62
	exit(2);
nkeynes@405
    63
    }
nkeynes@405
    64
nkeynes@405
    65
    fprintf( out, "/**\n * This file is automatically generated - do not edit\n */\n\n" );
nkeynes@405
    66
    fprintf( out, "const char *glsl_vertex_shader_src = \"" );
nkeynes@405
    67
nkeynes@405
    68
    writeShader( out, vsin );
nkeynes@405
    69
nkeynes@405
    70
    fprintf( out, "\";\n\n" );
nkeynes@405
    71
    fprintf( out, "const char *glsl_fragment_shader_src = \"" );
nkeynes@405
    72
    writeShader( out, fsin );
nkeynes@405
    73
    fprintf( out, "\";\n\n" );
nkeynes@405
    74
    fclose( fsin );
nkeynes@405
    75
    fclose( vsin );
nkeynes@405
    76
    fclose( out );
nkeynes@405
    77
    return 0;
nkeynes@405
    78
}
.