]> git.sur5r.net Git - c128-kasse/blob - util/mkfont
README: remove emulator configuration, we use -config vicerc
[c128-kasse] / util / mkfont
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use v5.10;
5
6 my ($fname, $array_name) = @ARGV;
7
8 die "Usage: $0 <file>\n" unless $fname;
9
10 my $fh;
11 open $fh, '<', $fname or die "$0: $!\n";
12
13 my $header = <$fh>;
14 die "$0: $fname: unknown file format\n" unless $header eq "P4\n";
15
16 my $len;
17 {
18     my $line = <$fh>;
19     chomp $line;
20     last if ($len) = $line =~ /^8 (\d+)$/;
21     redo if $line =~ /^#/;
22     die "$0: $fname: couldn't parse header\n";
23 }
24
25 local $/;
26 # read rest of file and return a list of the ascii value for each byte
27 my @bin = map { ord($_) } split //, <$fh>;
28
29 die "$0: $fname should have $len bytes image data, got: " . scalar(@bin) . "\n"
30     unless scalar(@bin) == $len;
31
32 say "/* autogenerated by util/mkfont */";
33 say "const unsigned char $array_name\[$len] = {";
34
35 # break up in max. 8 bytes per line
36 while (my @line = splice @bin, 0, 8) {
37     # seperated by commata, depending on how many bytes we have
38     my $hexify = join ', ', ('0x%02x') x scalar(@line);
39     printf "  $hexify" , @line;
40     # if there's more bytes left, seperate next block by a comma
41     print scalar(@bin) > 0 ? ",\n" : "\n";
42 }
43
44 say "};"