.........1.........2.........3.........4.........5.........6.........7.........8
SCREEN COMPACTOR
by R Swailes
from Your Computer October 1984

R. Swailes presents a memory saving program for the Spectrum which
is designed to help adventure writers to draw pictures more efficiently.


THE ORIGINAL PURPOSE of this compactor program was to enable adventure game
writers to draw pictures quickly without wasting too much memory. Some adventure
games draw pictures for each location but take a long time about it. This program
is a compromise between slow pictures and minimum waste of memory and instant
pictures but with considerable waste of memory.

The average saving of memory is about 40 per cent, though for simple line
drawings it will be much higher. For very detailed pictures completely filling
the screen there will be a minimal saving.


Table 1
-------

List of locations
25021-A    25022-B
25047-A    25048-B
25148-A    25149-B

Values to be POKEd
 A   B
254  71  =  top 1/3 screen
252  79  =  top 2/3 screen
 0   88  =  whole screen
 0   91  =  whole screen + attributes


Begins at pixel one

The Spectrum SCREEN$ save begins at pixel
one and saves its value - 1= On, 0 = Off. The
first eight pixels, for example 0,0,1,1,1,1,0,0
are represented by binary 00111100 or 60
decimal. At the first screen location in
memory (16384), if PEEKed, would be seen the
value 60. The save continues through all
locations saving the pixel values in blocks of
eight. There are 49152 pixels - 49152/8 =
6144 bytes or 6K of memory.

	The compactor program relies on the fact
that most pictures have more background
than picture. Where background = eight
pixels off - BIN 00000000 = decimal 0.

	Even if saving a page of alpha-numerics,
economies can be made because the top line of
any 8 by 8 character is always blank, and since
the screen scan does the top row of the first
eight lines together - chapter 24 display file
- these can be compressed. The bottom lines
of each character are usually also blank -
except lower case letters with tails - and
similar savings can be made here.

	The machine code save "picture to code"
looks at the first screen location (16384) to see
if any of the eight pixels are on - giving a
value greater than zero. If so it saves the value
as normal. Should it be zero - after storing
the 0 - it checks the next location to see if
that is also zero and continues until a lit pixel
is found. The program then saves the number
of consecutive zeros. For example:
SCREEN$ Code:
64, 2, 0 0 0 0 0 0 0 0 0 12, 19 = 13 bytes.
Compacted Data:
64, 2, 0, 9, 0, 12, 19 = 7 bytes where 0 9 0
underlined.
	0 indicates start of a loop of zeros.
	9,0 Length of loop (low order byte first)
	0 x 256 + 9 = 9 Loop = 9)
Thus 0, 0, 24  24 x 256 + 0 = 6144 bytes = 6K
is a screen full of zeros in 3 bytes. To convert
the code back to a picture the reverse applies.

Type in listing 1 and RUN, then type in
Data 1. If all goes well then save to tape the
machine code for picture saving.

Amend listing 1 with listing 2 and RUN,
then type in Data 2. This will also save to tape
if it is correct. This is the machine code for
picture printing.


Clear the machine

Clear the machine and type in the
Demonstration Program and RUN. It will
load up the Data 1 and Data 2 CODE from
tape. It then expects a SCREEN$ to load in.
If you haven't a SCREEN$ handy on tape then
delete line 90 and insert 90 LIST 120.
Following the program and your SCREEN$ -
or listing - will be saved in code - in
memory. The length of the code and saving
can be seen. Press any key and the SCREEN$
instantly reappears.

A list of POKEs - Table 1 - is supplied in
case you don't want to save a whole screen.
Just amend the stated data in listings 1 and 2
and this will enable you to save either the Top
1/3, top 2/3, the whole screen or the whole and
attribute file.

You can draw and save several pictures one
after the other; just Poke 23728-9 - as lines
50, 60, 70 in the main program - with the
position in memory where the picture code is
to be stored. Save the picture with RAND USR
25000 and then PEEK 23728-9 - line 120 in
the main program - to find the length of the
picture code in memory and work out the next
picture code start position and repeat.


For all Spectrums

To recall any picture Poke 23728-9 - lines
150,160 in the main program - where
V = start position of picture code in memory
then RAND USR 25130.

The program was written for all Spectrums,
and the machine code is fully relocatable. For
48K machines instead of typing in numbers
25 etc. type in 35xxx or 45xxx.

Your picture code can be saved to tape once
stored in memory. See chapter 20, Spectrum
manual.

