3 # Since the sdl_const.pl and gl_const.pl scripts with 2.0-beta2 don't seem to
4 # work at all, this script takes SDL/Constans.pm and OpenGL/Constants.pm (as
5 # original from 2.0-beta2) and fixes them up, and moves them into ../lib/
7 # I already did this for 1.20.2, so you need to run this only if you intent
8 # to rebuild the .pm files.
10 # See http://Bloodgate.com/perl/sdl/sdl_perl.html
14 ##############################################################################
16 my $sdl = read_file('SDL/Constants.pm');
18 # remove 'main::' on subs
19 $sdl =~ s/sub main::([\w]+)/sub $1/g;
21 # turn on () on subs to make peep optimizer to inline them
22 #$sdl =~ s/sub ([\w]+)\s+{/sub $1 () {/g;
24 write_file('../lib/SDL/Constants.pm',insert_export($sdl));
28 ##############################################################################
30 my $gl = read_file('OpenGL/Constants.pm');
32 # remove 'main::' on subs
33 $gl =~ s/sub main::([\w]+)/sub $1/g;
35 # turn on () on subs to make peep optimizer to inline them
36 #$gl =~ s/sub ([\w]+)\s+{/sub $1 () {/g;
39 ('../lib/SDL/OpenGL/Constants.pm',insert_export($gl,grep_constants()));
47 print "Reading $file...";
49 open $FILE, $file or die ("Cannot read $file: $!\n");
50 local $/; # slurp mode
61 print "Writing $file...";
63 open $FILE, ">$file" or die ("Cannot write $file: $!\n");
74 # gather all sub names
75 $txt =~ s/sub ([\w]+)\s+/push @sub, $1; 'sub ' . $1 . ' '/eg;
77 # if we got a second list of names, parse it and include anything that isn't
81 if (ref($_[0]) eq 'ARRAY')
84 foreach my $c (sort @$const)
86 if ( grep (/^$c->[0]$/, @sub) == 0)
88 print "Adding $c->[0] $c->[1]\n";
89 $add .= "sub $c->[0] () { $c->[1] }\n";
96 # SDL/Constants.pm contains doubles :-( So filter them out.
97 my @sorted = sort @sub;
98 my $last; @sub = (); my @doubles;
99 foreach my $cur (@sorted)
101 if (defined $last && $last eq $cur)
108 push @sub, $last if defined $last;
112 foreach my $cur (@doubles)
114 $txt =~ s/\bsub $cur.*//g; # remove
117 my $export = "require Exporter;\nuse strict;\n";
118 $export .= "use vars qw/\$VERSION \@ISA \@EXPORT/;";
119 $export .= "\n\@ISA = qw/Exporter/;\n";
121 # this makes Exporter export the symbols from SDL::Constants to whatever
122 # package used SDL::Constants (usually SDL::Event.pm)
124 if ($txt =~ /SDL::Constants/)
126 $txt =~ s/SDL::Constants/SDL::Event/g;
127 $pack = 'SDL::Event';
129 if ($txt =~ /SDL::OpenGL::Constants/)
131 $txt =~ s/SDL::OpenGL::Constants/SDL::OpenGL/g;
132 $pack = 'SDL::OpenGL';
134 $export .= "\nsub import { $pack\->export_to_level(1, \@_); }\n";
136 $export .= "\n\@EXPORT = qw/\n";
139 foreach my $s (sort @sub)
141 if (length($line)+length($s) > 70)
143 $export .= "$line\n"; $line = "\t";
147 $export .= "$line /;\n";
149 # insert Exporter section
150 $txt =~ s/sub/$export\n\n$add\nsub/;
157 # grep all the OpenGL constants from SDL and return them
159 my $sdl_gl = read_file('/usr/include/GL/gl.h');
162 $sdl_gl =~ s/^#define (GL_.*?)\s+(0x[\da-fA-F]+)/push @const,[$1,$2];/egm;