Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 159 lines (120 sloc) 3.605 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
#!/usr/bin/perl -w

# Since the sdl_const.pl and gl_const.pl scripts with 2.0-beta2 don't seem to
# work at all, this script takes SDL/Constans.pm and OpenGL/Constants.pm (as
# original from 2.0-beta2) and fixes them up, and moves them into ../lib/

# I already did this for 1.20.2, so you need to run this only if you intent
# to rebuild the .pm files.

# See http://Bloodgate.com/perl/sdl/sdl_perl.html

use strict;

##############################################################################

my $sdl = read_file('SDL/Constants.pm');

# remove 'main::' on subs
$sdl =~ s/sub main::([\w]+)/sub $1/g;

# turn on () on subs to make peep optimizer to inline them
#$sdl =~ s/sub ([\w]+)\s+{/sub $1 () {/g;

write_file( '../lib/SDL/Constants.pm', insert_export($sdl) );

undef $sdl;

##############################################################################

my $gl = read_file('OpenGL/Constants.pm');

# remove 'main::' on subs
$gl =~ s/sub main::([\w]+)/sub $1/g;

# turn on () on subs to make peep optimizer to inline them
#$gl =~ s/sub ([\w]+)\s+{/sub $1 () {/g;

write_file(
'../lib/SDL/OpenGL/Constants.pm',
insert_export( $gl, grep_constants() )
);

1;

sub read_file {
my $file = shift;

print "Reading $file...";
my $FILE;
open $FILE, $file or die("Cannot read $file: $!\n");
local $/; # slurp mode
my $doc = <$FILE>;
close $FILE;
print "done.\n";
$doc;
}

sub write_file {
my ( $file, $txt ) = @_;

print "Writing $file...";
my $FILE;
open $FILE, ">$file" or die("Cannot write $file: $!\n");
print $FILE $txt;
close $FILE;
print "done.\n";
}

sub insert_export {
my $txt = shift;

my @sub = ();

# gather all sub names
$txt =~ s/sub ([\w]+)\s+/push @sub, $1; 'sub ' . $1 . ' '/eg;

# if we got a second list of names, parse it and include anything that isn't
# alreay in

my $add = "";
if ( ref( $_[0] ) eq 'ARRAY' ) {
my $const = shift;
foreach my $c ( sort @$const ) {
if ( grep ( /^$c->[0]$/, @sub ) == 0 ) {
print "Adding $c->[0] $c->[1]\n";
$add .= "sub $c->[0] () { $c->[1] }\n";
push @sub, $c->[0];
}
}
$add .= "\n";
}

# SDL/Constants.pm contains doubles :-( So filter them out.
my @sorted = sort @sub;
my $last;
@sub = ();
my @doubles;
foreach my $cur (@sorted) {
if ( defined $last && $last eq $cur ) {

# double!
push @doubles, $last;
} else {
push @sub, $last if defined $last;
}
$last = $cur;
}
foreach my $cur (@doubles) {
$txt =~ s/\bsub $cur.*//g; # remove
}

my $export = "require Exporter;\nuse strict;\n";
$export .= "use vars qw/\$VERSION \@ISA \@EXPORT/;";
$export .= "\n\@ISA = qw/Exporter/;\n";

# this makes Exporter export the symbols from SDL::Constants to whatever
# package used SDL::Constants (usually SDL::Event.pm)
my $pack;
if ( $txt =~ /SDL::Constants/ ) {
$txt =~ s/SDL::Constants/SDL::Event/g;
$pack = 'SDL::Event';
}
if ( $txt =~ /SDL::OpenGL::Constants/ ) {
$txt =~ s/SDL::OpenGL::Constants/SDL::OpenGL/g;
$pack = 'SDL::OpenGL';
}
$export .= "\nsub import { $pack\->export_to_level(1, \@_); }\n";

$export .= "\n\@EXPORT = qw/\n";

my $line = "\t";
foreach my $s ( sort @sub ) {
if ( length($line) + length($s) > 70 ) {
$export .= "$line\n";
$line = "\t";
}
$line .= "$s ";
}
$export .= "$line /;\n";

# insert Exporter section
$txt =~ s/sub/$export\n\n$add\nsub/;

$txt;
}

sub grep_constants {

# grep all the OpenGL constants from SDL and return them

my $sdl_gl = read_file('/usr/include/GL/gl.h');

my @const = ();
$sdl_gl =~ s/^#define (GL_.*?)\s+(0x[\da-fA-F]+)/push @const,[$1,$2];/egm;

\@const;
}

END { print "\n"; }
Something went wrong with that request. Please try again.