Skip to content

4.3 Software sprites

6502Nerd edited this page Oct 5, 2021 · 14 revisions

Software sprite system

One thing which is always a problem for low speed 8 bit computers like the Oric is moving stuff about on the screen. Some computers like the Atari and C64 provided help by providing hardware sprites which didn't burden the CPU with drawing images and restoring backgrounds.

There is no hardware sprite support in the Oric hardware, which makes it challenging to create games which have more than a few moving objects without a lot of slow down in BASIC.

So dflat provides a software sprite system which allows easy positioning of screen objects. The main compromise of this is that it is only for the low resolution screen and positions are on character boundaries - so strictly for chunky movement. The advantage of this is that the sprite system is quite fast and does not take up much memory.

Key features

The sprite system has the following features;

  • Up to 32 sprites on the screen
  • Sprite 0 is the highest priority (basically it is drawn last)
  • Keeps track of the background, so doesn't disturb non-sprite objects
  • Position with x,y screen coordinate
  • Uses unused 256 byte portion of font memory (0xb400)
  • Core engine is around 200 bytes of ROM code, so even if not used often, is not taking up much space!

How it works

To demystify a little, how the sprite system works is worth knowing;

  • A single sprite has the following attributes
    • newX : new x position (0-39) with -1 indicating the sprite is not active
    • newY : new y position (0-27)
    • curX : current x position (0-39)
    • curX : current y position (0-27)
    • char : which character should be displayed for the sprite
    • back : what was on the screen before the sprite was positioned (used to restore when the sprite moves)
  • When initialised all sprites as disabled by having newX and curX position of -1
  • When a sprite is put or moved on the screen, it is always the newX and newY positions which are set
  • When the sprite system does an update (needs to be requested), the following happens
    • Erase all sprites with background (if needed - check for any position changes in new and current and active status)
    • Save background character of all erases and active sprites
    • Draw all active sprites (even if new is same as current).

dflat sprite commands

These are the core commands to use from dflat;

  • sprinit : Initialises the sprite system - all sprites are set to inactive. Note - issuing the text command also initialises sprites as part of entering text mode
  • sprupd : update all sprites (restore backgrounds, plot in new positions as appropriate)
  • sprchar s:<int>,c:<int> : set sprite s character to c (only takes effect on the next sprupd)
  • sprpos s:<int>,x:<int>,y:<int> : set sprits s to position x,y with x=-1 disabling it (only takes effect on the next sprupd)
  • sprmulti x[],y[] : set all sprites to position given by two 32 element arrays contain x and y positions

A word on the sprmulti command; This is very useful in dflat as it allows sprite positions to be held in two 32 element arrays and then written very fast to the sprite system. Without this dflat would have to go through each sprite to update the position which is much slower.

How to use

To summarise, the sprite system needs to be used in a specific sequence to make best use of it:

  • Initialise the system by spritinit or text
  • Draw the static background
  • Position all the required sprites and set the char
  • In the dflat update loop: change all sprite positions, then issue a sprupd
  • Avoid changing the background over which sprites may roam, will confuse the system