sgdk
vdp_spr.h File Reference

VDP Sprite support. More...

#include "config.h"
#include "types.h"
#include "dma.h"

Go to the source code of this file.

Classes

struct  VDPSprite
 VDP sprite definition structure replicating VDP hardware sprite. More...

Defines

#define SAT_MAX_SIZE   80
 Maximum size of Sprite Attribute Table (128 in VRAM but limited to 80 in VDP anyway)
#define SPRITE_SIZE(w, h)   ((((w) - 1) << 2) | ((h) - 1))
 Helper to define sprite size in sprite definition structure.

Functions

void VDP_resetSprites (void)
 Clear all sprites and reset VDP sprite allocation (if any).
void VDP_releaseAllSprites (void)
 Release all VDP sprite allocation.
s16 VDP_allocateSprites (u16 num)
 Allocate the specified number of hardware VDP sprites and link them together.

void VDP_releaseSprites (u16 index, u16 num)
 Release specified number of VDP sprites.
u16 VDP_getAvailableSprites (void)
 Returns the number of available VDP sprite from the SAT. WARNING: the SAT maximum size is 128 entries in VRAM but the VDP can only display up to 80 (H40 mode) or 64 sprites (H32 mode) at once on the screen.
s16 VDP_refreshHighestAllocatedSpriteIndex (void)
 Compute and return the highest index of currently allocated VDP sprite.
A value of -1 mean no VDP Sprite are allocated.
This value can be used to define the number of sprite to transfer with VDP_updateSprites(..) method.
void VDP_clearSprites (void)
 Clear all sprites.
void VDP_setSpriteFull (u16 index, s16 x, s16 y, u8 size, u16 attribut, u8 link)
 Set a sprite (use sprite list cache).
void VDP_setSprite (u16 index, s16 x, s16 y, u8 size, u16 attribut)
 Set a sprite (use sprite list cache).
void VDP_setSpritePosition (u16 index, s16 x, s16 y)
 Set sprite position (use sprite list cache).
void VDP_setSpriteSize (u16 index, u8 size)
 Set sprite size (use sprite list cache).
void VDP_setSpriteAttribut (u16 index, u16 attribut)
 Set sprite attributes (use sprite list cache).
void VDP_setSpriteLink (u16 index, u8 link)
 Set sprite link (use sprite list cache).
VDPSpriteVDP_linkSprites (u16 index, u16 num)
 Link sprites starting at the specified index.
Links are created in simple ascending order (1 --> 2 --> 3 --> ...)
void VDP_updateSprites (u16 num, TransferMethod tm)
 Send the cached sprite list to the VDP.
void VDP_setSpritePriority (u16 index, bool priority)
 Set sprite priority (use sprite list cache).
bool VDP_getSpritePriority (u16 index)
 Get sprite priority (use sprite list cache).
void VDP_setSpritePalette (u16 index, u16 palette)
 Set sprite palette (use sprite list cache).
u16 VDP_getSpritePalette (u16 index)
 Get sprite palette (use sprite list cache).
void VDP_setSpriteFlip (u16 index, bool flipH, bool flipV)
 Set sprite horizontal and vertical flip (use sprite list cache).
void VDP_setSpriteFlipH (u16 index, bool flipH)
 Set sprite horizontal flip (use sprite list cache).
void VDP_setSpriteFlipV (u16 index, bool flipV)
 Set sprite vertical flip (use sprite list cache).
bool VDP_getSpriteFlipH (u16 index)
 Get sprite horizontal flip (use sprite list cache).
bool VDP_getSpriteFlipV (u16 index)
 Get sprite vertical flip (use sprite list cache).
void VDP_setSpriteTile (u16 index, u16 tile)
 Set sprite tile index (use sprite list cache).
u16 VDP_getSpriteTile (u16 index)
 Get sprite tile index (use sprite list cache).

Variables

VDPSprite vdpSpriteCache [SAT_MAX_SIZE+16]
 VDP sprite cache.
VDPSpritelastAllocatedVDPSprite
 Pointer to last allocated sprite after calling VDP_allocateSprites(..) method.
This can be used to do the link from the last allocated VDP sprite.
s16 highestVDPSpriteIndex
 Highest index of allocated VDP sprite since the last call to VDP_resetSprites() or VDP_releaseAllSprites().
A value of -1 mean no VDP Sprite were allocated..
This can be used to define the number of sprite to transfer with VDP_updateSprites(..) method.
WARNING: this value is not correctly updated on sprite release operation so it may gives an higher index than reality.
You can ue currently VDP_refreshHighestAllocatedSpriteIndex() method to force recomputation of highest index (costs a bit of time).

Detailed Description

VDP Sprite support.

Author:
Stephane Dallongeville
Date:
08/2011

This unit provides methods to allocate and manipulate VDP Sprite at low level.
The Sega Genesis VDP can handle up to 80 simultanous sprites of 4x4 tiles (32x32 pixels).


Define Documentation

#define SPRITE_SIZE (   w,
 
)    ((((w) - 1) << 2) | ((h) - 1))

Helper to define sprite size in sprite definition structure.

Parameters:
wsprite width (in tile).
hsprite height (in tile).

Function Documentation

s16 VDP_allocateSprites ( u16  num)

Allocate the specified number of hardware VDP sprites and link them together.

Parameters:
numNumber of VDP sprite to allocate (need to be > 0)
Returns:
the first VDP sprite index where allocation was made.
-1 if there is not enough available VDP sprite remaining.
WARNING: VDP can display up to 80 sprites at once on screen in H40 mode only, in H32 mode it's limited to 64 sprites even if we allow to allocate up to 80 (SAT size).

This method allocates the specified number of VDP sprite and returns the index of the first allocated sprite in VDP sprite table (see vdpSpriteCache).
Sprites are linked together using link field (last sprite ends with link 0).
If there is not enough available VDP sprites the allocation operation fails and return -1. NOTE: The last sprite from the allocated list can be retrieved with lastAllocatedVDPSprite, this is to avoid parsing all the list to find it, if we want to link it to a specific sprite for instance.

See also:
VDP_releaseSprites(..)
u16 VDP_getAvailableSprites ( void  )

Returns the number of available VDP sprite from the SAT. WARNING: the SAT maximum size is 128 entries in VRAM but the VDP can only display up to 80 (H40 mode) or 64 sprites (H32 mode) at once on the screen.

See also:
VDP_allocateSprites(..)
VDP_releaseSprites(..)
bool VDP_getSpriteFlipH ( u16  index)

Get sprite horizontal flip (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
bool VDP_getSpriteFlipV ( u16  index)

Get sprite vertical flip (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
u16 VDP_getSpritePalette ( u16  index)

Get sprite palette (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
bool VDP_getSpritePriority ( u16  index)

Get sprite priority (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
u16 VDP_getSpriteTile ( u16  index)

Get sprite tile index (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
VDPSprite* VDP_linkSprites ( u16  index,
u16  num 
)

Link sprites starting at the specified index.
Links are created in simple ascending order (1 --> 2 --> 3 --> ...)

Parameters:
indexIndex of the first sprite we want to link (should be < SAT_MAX_SIZE).
numNumber of link to create (if you want to link 2 sprites you should use 1 here)
Returns:
The last linked sprite
s16 VDP_refreshHighestAllocatedSpriteIndex ( void  )

Compute and return the highest index of currently allocated VDP sprite.
A value of -1 mean no VDP Sprite are allocated.
This value can be used to define the number of sprite to transfer with VDP_updateSprites(..) method.

See also:
VDP_allocateSprites(..)
VDP_releaseSprites(..)
highestVDPSpriteIndex
void VDP_releaseSprites ( u16  index,
u16  num 
)

Release specified number of VDP sprites.

Parameters:
indexThe index of the first VDP sprite to release (0 <= index < SAT_MAX_SIZE)
numNumber of VDP sprite to release (should be > 0)

This method release the specified number of VDP sprite from the specified index using the link field information to determine which sprites to release when more than 1 sprite is released.

See also:
VDP_allocateSprites(..)
void VDP_setSprite ( u16  index,
s16  x,
s16  y,
u8  size,
u16  attribut 
)

Set a sprite (use sprite list cache).

Parameters:
indexIndex of the sprite to set (should be < SAT_MAX_SIZE).
xSprite position X on screen.
ySprite position Y on screen.
sizeSprite size (see SPRITE_SIZE() macro).
attributSprite tile attributes (see TILE_ATTR_FULL() macro).
See also:
VDP_setSpriteFull(..)
VDP_updateSprites(..)
void VDP_setSpriteAttribut ( u16  index,
u16  attribut 
)

Set sprite attributes (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
attributSprite tile attributes (see TILE_ATTR_FULL() macro).
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpriteFlip ( u16  index,
bool  flipH,
bool  flipV 
)

Set sprite horizontal and vertical flip (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
flipHHorizontal flip. Valid values: 0 -> normal, 1 -> flipped.
flipVVertical flip. Valid values: 0 -> normal, 1 -> flipped.
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpriteFlipH ( u16  index,
bool  flipH 
)

Set sprite horizontal flip (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
flipHHorizontal flip. Valid values: 0 -> normal, 1 -> flipped.
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpriteFlipV ( u16  index,
bool  flipV 
)

Set sprite vertical flip (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
flipVVertical flip. Valid values: 0 -> normal, 1 -> flipped.
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpriteFull ( u16  index,
s16  x,
s16  y,
u8  size,
u16  attribut,
u8  link 
)

Set a sprite (use sprite list cache).

Parameters:
indexIndex of the sprite to set (should be < SAT_MAX_SIZE).
xSprite position X on screen.
ySprite position Y on screen.
sizeSprite size (see SPRITE_SIZE() macro).
attributSprite tile attributes (see TILE_ATTR_FULL() macro).
linkSprite link (index of next sprite, 0 for end)
Be careful to not modify link made by VDP_allocateSprite(..), use VDP_setSprite(..) instead in that case.
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpriteLink ( u16  index,
u8  link 
)

Set sprite link (use sprite list cache).

Parameters:
indexIndex of the sprite to modify link (should be < SAT_MAX_SIZE).
linkSprite link (index of next sprite, 0 for end).
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpritePalette ( u16  index,
u16  palette 
)

Set sprite palette (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
palettepalette index. Valid values: [0, 3]
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpritePosition ( u16  index,
s16  x,
s16  y 
)

Set sprite position (use sprite list cache).

Parameters:
indexIndex of the sprite to modify position (should be < SAT_MAX_SIZE).
xSprite position X.
ySprite position Y.
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpritePriority ( u16  index,
bool  priority 
)

Set sprite priority (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
prioritysprite priority. Valid values: 0 -> low, 1 -> high
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpriteSize ( u16  index,
u8  size 
)

Set sprite size (use sprite list cache).

Parameters:
indexIndex of the sprite to modify size (should be < SAT_MAX_SIZE).
sizeSprite size (see SPRITE_SIZE() macro).
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_setSpriteTile ( u16  index,
u16  tile 
)

Set sprite tile index (use sprite list cache).

Parameters:
indexIndex of the sprite to modify attributes (should be < SAT_MAX_SIZE).
tileTile index. Valid values: [0, 2047].
See also:
VDP_setSprite(..)
VDP_updateSprites(..)
void VDP_updateSprites ( u16  num,
TransferMethod  tm 
)

Send the cached sprite list to the VDP.

Parameters:
numNumber of sprite to transfer starting at index 0 (max = MAX_SPRITE).
If you use dynamic VDP Sprite allocation you may use 'highestVDPSpriteIndex + 1' here
tmTransfer method.
Accepted values are:
  • CPU
  • DMA
  • DMA_QUEUE
  • DMA_QUEUE_COPY
See also:
highestVDPSpriteIndex
VDP_refreshHighestAllocatedSpriteIndex()

Variable Documentation

Highest index of allocated VDP sprite since the last call to VDP_resetSprites() or VDP_releaseAllSprites().
A value of -1 mean no VDP Sprite were allocated..
This can be used to define the number of sprite to transfer with VDP_updateSprites(..) method.
WARNING: this value is not correctly updated on sprite release operation so it may gives an higher index than reality.
You can ue currently VDP_refreshHighestAllocatedSpriteIndex() method to force recomputation of highest index (costs a bit of time).

See also:
VDP_refreshHighestAllocatedSpriteIndex()
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines