home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Falcon 030 Power 2
/
F030_POWER2.iso
/
ST_STE
/
MAGS
/
ICTARI07.ARJ
/
ictari.07
/
ASSEMBLY
/
SPRITES
/
M_SPRITE.DOC
< prev
next >
Wrap
Text File
|
1993-09-03
|
6KB
|
120 lines
SUB-ROUTINE NAME m_sprite
BRIEF DESCRIPTION Display moveable sprite
FILENAME SPRITE.S
OTHER RESOURCES
LANGUAGE Assembler (Devpac)
AUTHOR Peter Hibbs
ENTRY PARAMETERS d0=x co-ordinate of hot spot (0-319)
d1=y co-ordinate of hot spot (0-199)
d2=sprite number (1-nn)
(screen) holds screen start address
(spr_buffer..) holds last copy
(sprite_tab..) holds sprite info
EXIT PARAMETERS Sprite displayed on screen.
DETAILS -
This routine moves a sprite to the co-ordinates defined by d0 and d1 and
restores the screen data beneath the sprite. The sprite data is generated
by the Neochrome Master art program which has a facility to generate a
sprite file. The sprite data is first loaded from disk by the sprite_init
sub-routine which also generates a mask file and a look-up table for the
sprite addresses, offsets, etc. See data sheet for sprite_init for
information on setting up the system.
The m_sprite sub-routine consists of three main sections, the code which
restores the old screen area, the code which saves the new screen area
and the code to display the sprite itself.
When the routine is called it first restores the area of screen
underneath the sprite which is stored in a temporary buffer called
spr_buffer (unless the buffer is empty as it would be the first time the
sprite is displayed in which case it is skipped).
When the screen has been 'repaired' (or skipped) the screen area for the
new position of the sprite (defined by the x/y co-ords in d0/d1) is saved
in the sprite buffer. The screen data is stored in the buffer along with
the address and size of the area of screen saved. The first 4 bytes hold
the screen address of the start of the sprite data, if these bytes are
set to 0000 the routine will assume the buffer is empty and not attempt
to copy the data to screen (the program should ensure that these bytes
are set to zero before the sprites are used or the program will crash).
The next 4 bytes define the width and height of the sprite and are used
to determine the size of area to be copied. The following n bytes are the
screen data, the number will vary depending on the size of the sprite.
The routine then displays the sprite on screen at the specified co-
ordinates.
The 'hot spot' for a sprite is normally the top left pixel of the sprite
rectangle (regardless of the shape of the visible area of the sprite) and
this is the pixel defined by the d0/d1 registers. It is possible,
however, to change the hot spot (to the centre point of the sprite for
example) with the SPR_CHCK.PRG program (see SPRITE.TXT document file).
If an invalid co-ordinate is passed to the routine (i.e. d0 greater than
319 or d1 greater than 199) the screen area will be restored but the
sprite image will not be displayed. This option thus allows the sprite to
be erased at any time without corrupting the screen.
Clearing the first 4 bytes of the spr_buffer to zero will effectively
erase the screen data and may be used when changing sprite images.
One limitation of the sub-routine is that no 'clipping' is done on the
left and right edges of the sprite. If the sprite is displayed at the
extreme right of the screen, part of it will appear on the left edge. If
it is displayed at the left edge with the hot spot set to the centre of
the sprite, it will disappear since the x co-ordinate will be a negative
value which is invalid. To add clipping code is extremely complicated and
would slow the sprite display somewhat. If a sprite is moved by the user
the program could prevent this problem by limiting the movement of the
sprite to avoid the edges. The top and bottom edges are clipped since
this crashes the program if data is written to an area outside the
screen.
Since there is only one sprite buffer available, it is only possible to
have one moveable sprite on screen at any one time. If there were more
than one it would be extremely difficult to ensure that the correct data
was being redrawn to the screen if the two (or more) sprites were to
cross each others paths. If the program requires a number of sprites on
screen at the same time, the s_sprite sub-routine should be used (which
is much faster) and screen switching employed to give the impression of
movement. The sprites should be copied to the screen during one frame
blanking period and displayed during the next and the screens switched
each period.
The size of the spr_buffer can be calculated with the following formula.
buffer size (bytes)=(width in bytes+4)*(height in pixels)+8
For example, a sprite of 8 bytes wide by 10 pixels high would require a
buffer of (8+4)*(10)+8=128 bytes, say 150 (just in case). These values
can be obtained by using the SPR_CHCK.PRG program supplied. Note that if
several different sprites are to be used, the largest one should be used
to calculate the buffer size.
Where a sprite is moved it will almost certainly be necessary to
synchronise the display with the frame blanking signal to prevent
flicker. A typical piece of code is shown below.
bsr sprite_init set up table and buffers
tst.l d0 check for error
bmi error_label branch if load error
clr.l spr_buffer ensure buffer is clear
label bsr ms_posn fetch x/y co-ords from mouse
move d2,d3 button state copied to d3
bsr vsync wait for frame blanking signal
move #10,d2 use sprite number 10
bsr m_sprite display sprite
tst d3 check if button pressed
beq label loop if no button pressed
.. ..
(ms_posn sub-routine returns immediately with the current x co-ord in d0,
the y co-ord in d1 and the mouse button state in d2).