View Single Post
Old Dec 23rd, 2005, 3:47 PM   #1
niteice
Programmer
 
niteice's Avatar
 
Join Date: Aug 2005
Posts: 98
Rep Power: 4 niteice is on a distinguished road
Send a message via AIM to niteice
Simple string substitution..

My little utility generates a Quake3-style shader from a generic input file. The areas that need to be replaced are genericized as so:
textures/$DIR/$TEX_d

The script will open the generic version of the shader, read it in, and keep replacing the strings so long as there are files in the specified directory. Currently it's as follows:
#!/usr/local/bin/perl
#
# autoShader generates a generic shader for all the textures in a directory.
#

# definitions
$shader = 'auto_surf.shader';
$dir = 'test';

# subroutines
sub generateShader
{
        print "texture: @_\n";

        $outShader = $genericShader;

        print $outShader;
        # replace $DIR and $TEX
        $outShader =~ s/$DIR/$dir/;
        $outShader =~ s/$TEX/$file/;

        print $outShader;
}

print "----- autoShader -----\n";
print "using directory: $dir\n";

# read in the generic shader
open(INFO, $shader);
$genericShader = <INFO>;
close(INFO);

# loop through the directory
opendir(DIR, $dir) or die"error: cannot find directory $dir. perl output is below.\n$\n";
while (defined($file = readdir(DIR)))
{
        next if $file =~ /^\.\.?$/;     # skip . and ..
        generateShader($dir, $file);
}
closedir(DIR);
It seems to not be replacing the $DIR and $TEX tokens. Ideas?
niteice is offline   Reply With Quote