aboutsummaryrefslogtreecommitdiff
path: root/text2screen.pl
blob: 7dc2e52f3a3dc31667580d254562b90fc8303f17 (plain)
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
#!/usr/bin/perl -w

# Turn each line of stdin into one 40-byte line of Atari screen codes,
# truncating or padding with nulls as needed.

# Input format is plain text, except any character can be preceded
# by a \ to inverse it. No way to get a non-inverse \ in there, sorry.
# %V will be replaced by the contents of the VERSION file.

use bytes;

$linelen = $1 || 40; # 40 for normal playfield.

chomp($ver = `cat VERSION`);

while(<>) {
	chomp;

	s/%V/$ver/;
	s/\\(.)/chr(ord($1)|0x80)/ge;

	if(length > $linelen) {
		warn "$0: line $. truncated to $linelen characters!\n";
		substr($_, $linelen) = "";
	}

	my $blanks = $linelen;
	for(map { ord } split "", $_) {
		my $byte = $_ & 0x7f;
		my $inv =  $_ & 0x80;
#warn sprintf("\$_ %02x, \$byte %02x, \$inv %02x", $_, $byte, $inv);
		if($byte < 32) {
			$byte += 64;
		} elsif($byte >= 32 && $byte <= 96) {
			$byte -= 32;
		}
#warn sprintf("result: %02x", ($byte | $inv));
		print chr($byte | $inv);
		$blanks--;
	}
	print chr(0) x $blanks;
}