SPRITE DESIGNER
by Robert Newman
from Your Computer, January 1984

PSEUDOSPRITES
Open up a new world of possibilities with Robert Newman's sprite designer

A NUMBER OF home computers are now provided with sprite graphics, usually via
special hardware such as the VIC chip in the Commodore 64. The ZX Spectrum
unfortunately lacks such hardware, and so I have written a machine-code
routine to emulate this feature. A sprite is a user-defined graphic character
up to 16 by 16 points which can be positioned anywhere upon the screen and
made to move very easily.

Normally a program in which a number of objects are required to move about
the screen will be heavily involved in the mundane operations of erasing at
the old position, updating co-ordinates, printing at the new position,
checking for collisions, testing for the edge of the screen, etc. One
advantage of sprite graphics is that all of these operations are carried out
automatically by the sprite mover routine, so that your program is free to go
on with its other tasks.

The routine can handle up to eight sprites at a time. Associated with each
sprite is a set of attributes which tell the routine how far to move the
sprite at each step, how fast to move it, how many moves to make, and what to
do if the sprite hits the edge of the screen or another object.

Each sprite has a collision flag which can be tested to check whether it has
hit anything. Movement of the sprites is made independent of the user program
because the machine-code routine is driven by interrupts.

Every 1/50 second the Z-80 microprocessor receives an interrupt which makes
it stop whatever it is doing and carry out an interrupt service routine which
normally just increments system variable FRAMES and scans the keyboard.
However, the interrupt can be diverted - as described by Nigel Dore in the
May 1983 issue of Your Computer - and in this case it is made to include the
sprite mover routine.

By doing this, your BASIC program will be slowed down somewhat, according to
how many sprites are being moved, and how fast they are moving. In spite of
this, a program which uses sprite graphics will run faster than one which
relies upon normal printing to achieve the same effects.

The machine code, sprite attributes and graphics characters occupy about
1.4K, and I have chosen to locate them below the Sinclair user-graphics. The
machine code - Listing 1 or 2 depending upon memory size - can be entered via
the program in Listing 3.

Take care, since a single mistake in the machine code will almost certainly
make the computer crash when you try to run it. Check the data after entry -
you can make a note of any mistakes and corrcct them with a POKE - and SAVE
the routine with the command:
	SAVE "sprmov48k" CODE 64551,611
or
	SAVE "sprmov16k" CODE 31770,611

You can now enter the sprite demonstration program in Listing 4. This sets up
the graphic character and programs the sprite attributes so that the eight
sprites move away from the centre of the screen in random directions with
different speeds.

At fIrst they will bounce off the edges of the screen, but by pressing ENTER
you can make them wrap around. Thus a sprite disappearing on the left-hand
edge of the screen will reappear at the right-hand side. Then, by pressing
ENTER again you can make tnem stop moving as soon as they collide with each
other.

I will now explain how to use the routine in your own programs. To do so, you
need to POKE values into two areas of memory - the graphic character and
sprite attribute areas.

In the graphics area you can store up to 16 different user-defined graphics,
each made up of 16 by 16 points. When designing your charactcrs you will need
to use a grid.

If your character does not fill the grid, make sure that the unused areas
come at the right hand side and the bottom, and make a note of the area that
you have used - width x height - because you will need these values later.
Now divide each line of the grid into two halves The left-hand group gives
you a value for the first byte of data for each line, the right-hand group
gives the second data byte.

Chapter 14 of the user manual tells you how to work out the value for each
byte. You can either work with binary numbers or convert to decimal.

Each character takes up 32 bytes, and these should be stored line by line,
with the top line first. As stated earlier, the graphics area can hold 16
characters. On the 48K machine character number 1 would be stored in
addresses 64000-64031, character 2 from 64032-64063, etc.

In order to simplify the process of designing your sprite graphic characters,
I have written the BASIC program in Listing 5. This program lets you design
your sprite graphics, store them in the graphics area of memory, retrieve
characters from memory and edit them, and SAVE your sprite graphics to tape.

The cursor keys are used to move around with the 16 by 16 grid, and by
pressing I you can ink in a particular square. Pressing P will change an
inked square back to paper. At the right-hand side of the screen, the
contents of the graphics area is displayed so that you can see all the
characters that you have set up.

The sprite attribute area consists of eight sets of 15 bytes. On the 48K
machine the attributes for sprite 1 are stored from 65200-65214, sprite 2
from 65215-65229, etc. The meaning of each byte is described below;
addresses given are for sprite 1; add 15 for sprite 2, 30 for sprite 3, etc.

X position (65200/32420): This is the x coordinate of the top left-hand
corner of the sprite. Values range from 0 to 255. You should set this to
wherever you wish the sprite to appear. Once the sprite starts moving the
value is updated to reflect its actual position.

Y position (65201/32421): Y co-ordinate of the top left-hand corner of the
sprite. Possible values range from 0 to 175.

X movement (65202/32422): This tells the routine how many points to move the
sprite in the X direction at each step. Values up to 127 move it from left to
right. Values from 128 to 255 move it right to left, but the value is
interpreted in 2's complement - so 255 means -1, 254 means -2, etc. If you
are unsure of 2's complements, you can use negative numbers in a POKE
statement to obtain the correct value - thus POKE 65202,-2. To avoid jerky
motion I would advise you to use values between -10 and +10.

Y movement (65203/32423): As above, but for displacements in the Y direction.
Positive values move it upwards, negative ones downwards.

Speed (65204/32424): This tells the routine how many interrupts should occur
before the sprite is moved. Values may range from 1 to 255, and low values
give least motion. Too low a value might make the sprite flicker, however.

Edge action (65205/32425): This tells the routine what to do when the sprite
hits the edge of the screen: 0 = stop moving; 1 = continue, with wrap-around;
2 = bounce.

Collision action (65206/32426); This tells the routine what to do when the
sprite hits another object: 0 = stop moving; 1 = continue moving.

Number of move (65207/32427): Values from 1 to 254 will move the sprite by
that many steps. A value of 0 means that the sprite remains stationary. A
value of 255 means keep moving continually. However, if the sprite hits the
edge of the screen when edge action = 0 or another object when collide action
= 0, the sprite will stop and the number of moves will be set to 0.

Off/on (65208/32428): A value of 0 means that the sprite is switched off. A
value of 1 switches it on. You should only switch a sprite on after all its
other attributes have been set.

Collision flag (65209/32429): This byte can he PEEKed, and it returns values
of: 0 = no collision; 1 = sprite is at the edge of the screen; 128 = sprite
has hit another object; 129 = sprite is at edge, and has hit something.

Graphic character address (65210,11/32430,31): These two bytes contain the
address where thc sprite graphic character is stored. 65210/32430 contains
the low byte of the address; ie. remainder from (address/256); 65231/32431
contains the high byte; ie. INT(address/256).

65212/32432: This byte is used by the mover routine and should not be POKEd.

Width (65213/32433): This tells the routine how wide - 1 to 16 points - the
sprite graphic character is. It needs to know this so as to tell when it has
reached the edge.

Height (65214/32434): This tells the routine how many lines you have used in
the sprite graphic character - 1 to 16 - so that it knows when it hits the
bottom of he screen.

After setting up your graphics characters and attributes, you can switch on
the sprite mover routine with the command:
	RAND USR 65148 (or 32367)
This diverts the interrupt service routine so as to include the machine code.
Sprites can be switched off, and interrupts restored to normal with:
	RAND USR 65155 (or 32374)

There is no provision within the routine for selecting sprite colours.
Instead, the sprites are printed in INK 9, PAPER 9 - they take their colour
from whatever is already on the screen. For example, when passing over an
area of the screen which had been printed in blue ink on yellow paper, the
sprite would appear blue. Note also that while spritcs are switched on, you
can clear the screen without erasing the sprites. You can use this fact to
change sprite colours as is done in the demonstration program in Listing 4.

This routine offers a number of advantages to BASIC programmers, providing
features which can only be obtained from machine code. The speed of movement
and ability to use high resolution graphics mean that arcade action games can
now be written in BASIC.

If anyone would like a cassette tape which contains a copy of the machine code
and sprite graphic character generator, together with a number of
demonstration programs, they should send 3 to [address deleted].
