Personal Computer News


Sprite Control

 
Published in Personal Computer News #104

Your sprites are organised for faster, more efficient use with Jonathan Whitchley's routines.

Sprite Control

Your sprites are organised for faster, more efficient use with Jonathan Whitchley's routines

Setting up and controlling sprites on the Commodore 64 needn't be difficult: it's quite possible to write routines that ease yor load considerably.

The code presented here gives you the facility to set up all the sprite information in one go without a lot of peeks and pokes. It is considerably faster and more convenient than normal Basic. You may specify as few or as many parameters as you wish, as by default the others remain unchanged. This helps to avoid dealing with a large number of parameters when you want to change just a few.

In Use

The command lets you specify:

  1. Which sprite you are setting up (1-8)
  2. Its x-position on the screen (0-511)
  3. Its y-position on the screen (0-255)
  4. The sprite colour (0-15)
  5. The address of the sprite data (0-65535)
  6. Expanded or unexpanded horizontally (0-1)
  7. Expanded or unexpanded vertically (0-1)
  8. Which has priority, screen text or sprite (0-1)
  9. High-resolution or multi-colour mode (0-1)
  10. Detect sprite collisions (0-1)
  11. Detect sprite data collisions (0-1)

You can also specify whether you want to turn the sprite on or off, or simply set up the information ready to switch on the sprite later in your program.

The command syntax is: SYS 53000,a,b,c,d,e, etc, and up to twelve parameters may be given.

You don't have to enter all twelve parameters as whatever you specify is used to update the sprite information. For example, you could write SYS 53000,a,b which leaves the sprite information c to k unchanged. If, at the end of the string of parameters, you place a ;1 the sprite is switched on. If you put a ;0 the sprite is turned off. If you don't put a ; the sprite information is updated, but no attempt is made to turn the sprite on or off.

Here are some examples:

SYS 53000,2,300,126;1   sprite No 2 is moved to position 300,126 and turned on
SYS 53000,4,80,90,1 sprite No 4 is moved to position 80,90. Sprite colour is white. If the sprite is off, it's left off: if it's on, it's left on.
SYS 53000,7,60,50,5,960,1,0,0,1;1; sprite No 7 is moved to 60,50. It is colour 5 (green) and the sprite data starts from address 960. The sprite is expanded horizontally but not vertically. The sprite appears behind any screen text and in multi-colour mode. The sprite will be switched on.
10 FOR K=30 TO 100
20 SYS 53000,1,K,2.2*K;1
33 NEXT K
sprite No 1 moves across the screen the equation y=2.2*K
SYS 53000;0 turns off the previously specific sprite

(The parameters may be any expression).

The parameters must go in the order a,b,c etc. This could be awkward if you wanted to change one of the later parameters and leave the others unchanged. If you give the value -1 to a parameter, the command skips on to the next one, leaving the original value unaltered.

SYS 53000,1,-1,100,3,896,-1,1   this leaves the x position unchanged, but moves the sprite to the y position 100, changes the colour to cyan and to the sprite data stored at 896. The sprite is expanded vertically.

Handy Points

The SYS 53000 command may be used directly or in a program.

The sprite data pointer is calculated from the parameter e. This parameter may be either the sprite data address in memory (0-65535) or the relative address (0-16384) from the base bank address. The SYS 53000 command finds the current address of the screen each time, so the correct sprite pointer is calculated and located correfctly, no matter which bank or relative screen position you may change to. The address of the sprite data should be a multiple of 64; if you give any other value, the address is rounded down to the nearest value.

The sprite is turned on in the time when the raster display is off-screen. This gets rid of the annoying jerky sprite movement that you usually see when moving sprites from Basic.

The collision parameters j and k set up the appropriate register and clear it from any previous collisions. You need only to peek the appropriate bits in register $D019 to find out if a subsequent collision occurs.

All parameters are calculated in modulo arithmetic. For example, if you specify a colour of 16, it wraps around to colour 0; if you specify a y position of 256, it will read as position 1. Parameters f onwards should be -1, 0 or 1. However, any negative number > -65536 is read as -1, any positive number < 65536 is read as 1.

For parameters f to k:

f 0 = unexpanded
g 0 = unexpanded
h 0 = sprite overlays text
i 0 = high-resolution
j 0 = collision is not detected
k 0 = collision is not detected

The SYS 53000 command changes the particular sprite information without affecting any of the other sprites.

If you are in bank 3, don't put your screen at $CC00; keep your sprite data at > $E000 (dec 57344) or < $CF00 (dec 52992). The video controller reads the sprite data happily from the RAM under the ROM.

If you wish to load in this utlity program from your own program, the first few lines of Basic should be along the lines:

  10 IF FLG THEN 30
  20 FLG=1:DEV%=PEEK(186):LB=PEEK(45):HB=PEEK(46):LOAD"UTILITY",DEV%,1
  30 POKE 45,LB:POKE 46,HB:CLR
  40 REM your program starts here

If you are using tape, save your own program and then the utility program immediately after. It makes life easier.

Getting Started

Type in Program 1, then make sure your tape recorder or disk drive is connected and the routine will save automatically. Your sprite routine should now be ready to be called from within your own progams. If you want a demonstration, type in Program 2. It will call on you to load your saved program, and will then produce the demonstration.

Jonathan Whitchley