OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
sprite.h
Go to the documentation of this file.
1
35
36#ifndef OPENSNES_SPRITE_H
37#define OPENSNES_SPRITE_H
38
39#include <snes/types.h>
40
41/* Removed on 2026-10-05 (1.0 plan, lot C): oamDrawMeta, oamDrawMetaFlip.
42 * The replacements are in docs/UPGRADING.md; `make check-upgrade` names them. */
43
44/*============================================================================
45 * Constants
46 *============================================================================*/
47
49#define MAX_SPRITES 128
50
52#define OBJ_SIZE8_L16 0
53#define OBJ_SIZE8_L32 1
54#define OBJ_SIZE8_L64 2
55#define OBJ_SIZE16_L32 3
56#define OBJ_SIZE16_L64 4
57#define OBJ_SIZE32_L64 5
58
60#define OBJ_SIZE_TO_REG(size) ((size) << 5)
61
63#define OBJ_BASE(vram_addr) (((vram_addr) >> 13) & 0x07)
64
75#define OBJSEL(size, vram_addr) ((u8)(OBJ_SIZE_TO_REG(size) | OBJ_BASE(vram_addr)))
76
78#define OBJ_CGRAM_BASE 128
79
81#define OBJ_CGRAM_PAL(n) (OBJ_CGRAM_BASE + (n) * 16)
82
84#define PALETTE_16_SIZE 32
85
87#define OAM_Y_OFFSCREEN 0xE0
88
90#define OAM_EXT_OFFSET 512
91
93#define OAM_EXT_SIZE 32
94
96#define OAM_BUFFER_SIZE 544
97
99#define OBJ_HIDE_Y 240
100
102#define OBJ_SMALL 0
103#define OBJ_LARGE 1
104
105/*============================================================================
106 * Dynamic Sprite Engine Constants
107 *============================================================================*/
108
110#define OBJ_SPRITE32 1
111#define OBJ_SPRITE16 2
112#define OBJ_SPRITE8 4
113
115#define OBJ_QUEUELIST_SIZE 128
116
118#define MAXSPRTRF (7 * 6)
119
120/*============================================================================
121 * Dynamic Sprite Structure
122 *============================================================================*/
123
155
156/*============================================================================
157 * Compile-time struct layout assertions
158 * Must match the oambuffer layout in sprite_dynamic.asm exactly.
159 *============================================================================*/
160
161_Static_assert(sizeof(t_sprites) == 16, "t_sprites must be 16 bytes");
162_Static_assert(__builtin_offsetof(t_sprites, oamx) == 0, "oamx offset mismatch");
163_Static_assert(__builtin_offsetof(t_sprites, oamy) == 2, "oamy offset mismatch");
164_Static_assert(__builtin_offsetof(t_sprites, oamframeid) == 4, "oamframeid offset mismatch");
165_Static_assert(__builtin_offsetof(t_sprites, oamattribute) == 6, "oamattribute offset mismatch");
166_Static_assert(__builtin_offsetof(t_sprites, oamrefresh) == 7, "oamrefresh offset mismatch");
167_Static_assert(__builtin_offsetof(t_sprites, oamgfxaddr) == 8, "oamgfxaddr offset mismatch");
168_Static_assert(__builtin_offsetof(t_sprites, oamgfxbank) == 10, "oamgfxbank offset mismatch");
169
182#define OAM_SET_GFX(id, gfx) do { \
183 oambuffer[id].oamgfxaddr = (u16)(u32)(const void *)(gfx); \
184 oambuffer[id].oamgfxbank = (u8)((u32)(const void *)(gfx) >> 16); \
185} while(0)
186
187/* OAM_SET_GFX_BANK(id, gfx, bank) was removed on 2026-10-05: OAM_SET_GFX() reads
188 * the bank from its pointer (docs/UPGRADING.md). */
189
190/* --- Bank $00 SLOT 1 (C-accessible, < $2000) --- */
191
199extern t_sprites oambuffer[128];
200
201/*============================================================================
202 * Initialization
203 *============================================================================*/
204
211#define OAM_DEFAULT_SIZE OBJ_SIZE8_L16
213#define OAM_DEFAULT_TILE_BASE 0
214
239void oamInit(u16 size, u16 name_base);
240
257#define OBJ_NAME_BASE(vram_word_addr) (((vram_word_addr) >> 13) & 0x07)
258
280void oamInitGfxSet(const u8 *tileSource, u16 tileSize, const u8 *tilePalette,
281 u16 paletteSize, u8 paletteEntry, u16 vramAddr, u8 oamSize);
282
283/*============================================================================
284 * Sprite Properties
285 *============================================================================*/
286
287/* Sprite ids are u16 in every function below. The setters other than oamSet()
288 * took a u8 until 2026-09-21: an id of 256 was truncated to 0 BEFORE the
289 * `id >= 128` check could refuse it, and silently overwrote sprite 0. Same
290 * stack slot either way — no ABI change, and a u8 variable still converts. */
291
325void oamSet(u16 id, u16 x, u16 y, u16 tile, u16 palette, u16 priority, u16 flags);
326
349void oamSetAttr(u16 id, u16 x, u16 y, u16 tile, u16 attr);
350
357void oamSetX(u16 id, u16 x);
358
365void oamSetY(u16 id, u16 y);
366
374void oamSetXY(u16 id, u16 x, u16 y);
375
388
400
408typedef struct {
409 const s16 *x;
410 const s16 *y;
411 const u8 *tile;
412 const u8 *attr;
417 const u8 *order;
418 const u8 *big;
421
422/* Not under the host's syntax check (clang, 8-byte pointers): the layout
423 * that matters is cc65816's, 4 bytes a pointer. */
424#ifndef __clang__
425_Static_assert(__builtin_offsetof(OamWorldBatch, y) == 4, "OamWorldBatch.y offset (sprite_world.asm)");
426_Static_assert(__builtin_offsetof(OamWorldBatch, tile) == 8, "OamWorldBatch.tile offset (sprite_world.asm)");
427_Static_assert(__builtin_offsetof(OamWorldBatch, attr) == 12, "OamWorldBatch.attr offset (sprite_world.asm)");
428_Static_assert(__builtin_offsetof(OamWorldBatch, visible) == 16, "OamWorldBatch.visible offset (sprite_world.asm)");
429_Static_assert(__builtin_offsetof(OamWorldBatch, first_id) == 20, "OamWorldBatch.first_id offset (sprite_world.asm)");
430_Static_assert(__builtin_offsetof(OamWorldBatch, count) == 21, "OamWorldBatch.count offset (sprite_world.asm)");
431_Static_assert(__builtin_offsetof(OamWorldBatch, size) == 22, "OamWorldBatch.size offset (sprite_world.asm)");
432_Static_assert(__builtin_offsetof(OamWorldBatch, order) == 24, "OamWorldBatch.order offset (sprite_world.asm)");
433_Static_assert(__builtin_offsetof(OamWorldBatch, big) == 28, "OamWorldBatch.big offset (sprite_world.asm)");
434_Static_assert(__builtin_offsetof(OamWorldBatch, size_big) == 32, "OamWorldBatch.size_big offset (sprite_world.asm)");
435#endif
436
523
530void oamSetTile(u16 id, u16 tile);
531
532/* Note: there is deliberately no oamSetVisible(id, show) — SNES sprite
533 * visibility is Y-position-based, so a "show" call can't know which Y to
534 * restore and could only be a silent no-op (which the removed v0.x
535 * function was). Use oamHide() to hide and oamSetY()/oamSet() with a
536 * valid Y to show. */
537
567void oamHide(u16 id);
568
586void oamHideRange(u16 first, u16 count);
587
595void oamSetSize(u16 id, u16 large);
596
597/*============================================================================
598 * OAM Update
599 *============================================================================*/
600
611void oamUpdate(void);
612
618void oamClear(void);
619
620/*============================================================================
621 * Metasprites
622 *============================================================================*/
623
646
649
651#define METASPR_ITEM(dx, dy, tile, attr) { (dx), (dy), (tile), (attr), 0 }
652
654#define METASPR_TERM { -128, 0, 0, 0, 0 }
655
657#define metasprite_end (-128)
658
660#define OBJ_PAL(pal) ((pal) << 1)
661
663#define OBJ_PRIO(prio) ((prio) << 4)
664
666#define OBJ_FLIPX 0x40
667
669#define OBJ_FLIPY 0x80
670
672#define OBJ_NAMETABLE_HIGH 0x01
673
692
735 const MetaspriteStyle *style, u8 flip);
736
737/*============================================================================
738 * Dynamic Sprite Engine
739 *============================================================================*/
740
759
781
795void oamDynamicSetSize(u16 id, u8 size);
796
813
838
839/*============================================================================
840 * Dynamic Metasprite Engine
841 *============================================================================*/
842
888void oamMetaDrawDyn(u16 id, s16 x, s16 y,
889 const MetaspriteItem *meta, const u8 *gfxptr, u8 size_class);
890
891/*============================================================================
892 * Fast Macro Sprite API
893 *
894 * Inline alternatives to oamSet/oamSetXY: they write directly to oamMemory[]
895 * without a function call.
896 *
897 * The macros below write the OAM bytes in place, from C. They are NOT
898 * cheaper than the assembly setters: measured per sprite (devtools/libbench,
899 * SlowROM), oamSetFast() 2,590 master cycles against oamSet() 2,030 and
900 * oamSetAttr() 1,580, oamSetXYFast() 1,920 against oamSetXY() 1,330. They
901 * stay for code that wants the writes inline; for speed, call the setters.
902 *
903 * Note: cc65816 does not truly inline 'static inline' functions — they
904 * become separate SUPERFREE sections with global labels that conflict
905 * across translation units; hence macros.
906 *
907 * oamMemory[] and oam_update_flag are declared in <snes/system.h>
908 * (included automatically via <snes.h>).
909 *
910 * Usage:
911 * // Pre-compute attribute byte once at init, then per frame:
912 * u8 attr = OAM_ATTR(tile, palette, priority, flags);
913 * oamSetAttr(id, x, y, tile, attr); // the cheapest full update
914 *
915 * // the inline forms
916 * oamSetFast(id, x, y, tile, palette, priority, flags);
917 * oamSetXYFast(id, x, y);
918 *============================================================================*/
919
929#define OAM_ATTR(_tile, _pal, _prio, _fl) \
930 ((u8)(((_fl) & 0xC0) | \
931 (((_prio) & 0x03) << 4) | \
932 (((_pal) & 0x07) << 1) | \
933 (((_tile) >> 8) & 0x01)))
934
941#define OAM_XHI_MASK(_slot) \
942 ((u8)((_slot) == 0 ? 0x01 : (_slot) == 1 ? 0x04 : \
943 (_slot) == 2 ? 0x10 : 0x40))
944
959#define oamSetFast(_id, _x, _y, _tile, _pal, _prio, _fl) do { \
960 u16 _off = (u16)(_id) << 2; \
961 oamMemory[_off + 0] = (u8)((_x) & 0xFF); \
962 oamMemory[_off + 1] = (u8)((_y) & 0xFF); \
963 oamMemory[_off + 2] = (u8)((_tile) & 0xFF); \
964 oamMemory[_off + 3] = OAM_ATTR(_tile, _pal, _prio, _fl); \
965 u16 _ext = 512 + ((u16)(_id) >> 2); \
966 u16 _sl = (u16)(_id) & 0x03; \
967 u8 _xhi = OAM_XHI_MASK(_sl); \
968 if ((_x) & 0x100) \
969 oamMemory[_ext] |= _xhi; \
970 else \
971 oamMemory[_ext] &= ~_xhi; \
972 oam_update_flag = 1; \
973} while(0)
974
985#define oamSetXYFast(_id, _x, _y) do { \
986 u16 _off = (u16)(_id) << 2; \
987 oamMemory[_off + 0] = (u8)((_x) & 0xFF); \
988 oamMemory[_off + 1] = (u8)((_y) & 0xFF); \
989 u16 _ext = 512 + ((u16)(_id) >> 2); \
990 u16 _sl = (u16)(_id) & 0x03; \
991 u8 _xhi = OAM_XHI_MASK(_sl); \
992 if ((_x) & 0x100) \
993 oamMemory[_ext] |= _xhi; \
994 else \
995 oamMemory[_ext] &= ~_xhi; \
996 oam_update_flag = 1; \
997} while(0)
998
1005typedef struct {
1006 const s16 *x;
1007 const s16 *y;
1008 const u8 *tile;
1009 const u8 *attr;
1012} OamBatch;
1013
1014#ifndef __clang__
1015_Static_assert(__builtin_offsetof(OamBatch, y) == 4, "OamBatch.y offset (sprite_batch.asm)");
1016_Static_assert(__builtin_offsetof(OamBatch, tile) == 8, "OamBatch.tile offset (sprite_batch.asm)");
1017_Static_assert(__builtin_offsetof(OamBatch, attr) == 12, "OamBatch.attr offset (sprite_batch.asm)");
1018_Static_assert(__builtin_offsetof(OamBatch, first_id) == 16, "OamBatch.first_id offset (sprite_batch.asm)");
1019_Static_assert(__builtin_offsetof(OamBatch, count) == 17, "OamBatch.count offset (sprite_batch.asm)");
1020#endif
1021
1053void oamSetBatch(const OamBatch *batch);
1054
1055#endif /* OPENSNES_SPRITE_H */
u16 frame
Definition main.c:94
BGnSC keeps vramAddr
Definition background.h:140
u16 flip
Frame counter for scroll speed throttle (scrolls every 3 frames).
Definition main.c:69
signed short s16
16-bit signed integer (-32768 to 32767)
Definition types.h:50
unsigned short u16
16-bit unsigned integer (0 to 65535)
Definition types.h:53
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
void oamDynamicSetSize(u16 id, u8 size)
Override the dispatched pixel size for a dynamic sprite slot.
void oamInit(u16 size, u16 name_base)
Initialize the sprite (OAM) system.
void oamSetY(u16 id, u16 y)
Set sprite Y position.
t_sprites oambuffer[128]
Dynamic sprite buffer (128 entries, 2048 bytes).
void oamSetSize(u16 id, u16 large)
Set sprite size (large/small).
void oamDynamicInit(const OamDynamicConfig *cfg)
Initialize the dynamic sprite engine from a config struct.
void oamDynamicDraw(u16 id)
Draw a dynamic sprite — engine picks the size routine.
u16 oamGetX(u16 id)
Read back a sprite's X position.
void oamSet(u16 id, u16 x, u16 y, u16 tile, u16 palette, u16 priority, u16 flags)
Set sprite properties.
void oamHideRange(u16 first, u16 count)
Park a run of sprites off screen in one call.
void oamSetBatch(const OamBatch *batch)
Write the OAM entries of N sprites from parallel arrays, in one call.
void oamSetXY(u16 id, u16 x, u16 y)
Set sprite position.
void oamClear(void)
Clear all sprites.
void oamUpdate(void)
Copy OAM buffer to hardware.
void oamInitGfxSet(const u8 *tileSource, u16 tileSize, const u8 *tilePalette, u16 paletteSize, u8 paletteEntry, u16 vramAddr, u8 oamSize)
Initialize sprite graphics and palette (PVSnesLib compatible).
void oamHide(u16 id)
Park a sprite off screen.
u8 oamGetY(u16 id)
Read back a sprite's Y position.
void oamPlaceWorld(const OamWorldBatch *batch, u16 cam_x, u16 cam_y)
Place a batch of world-space sprites: camera, culling, OAM, in one call.
void oamDynamicDrainQueue(void)
Block until the dynamic-sprite VRAM tile queue is empty.
u16 oamDrawMetasprite(u16 startId, s16 x, s16 y, const MetaspriteItem *frame, const MetaspriteStyle *style, u8 flip)
Draw one frame of a metasprite.
void oamSetTile(u16 id, u16 tile)
Set sprite tile.
MetaspriteItem t_metasprite
PVSnesLib compatibility typedef.
Definition sprite.h:648
void oamMetaDrawDyn(u16 id, s16 x, s16 y, const MetaspriteItem *meta, const u8 *gfxptr, u8 size_class)
Draw a dynamic metasprite — engine picks the size routine.
void oamSetAttr(u16 id, u16 x, u16 y, u16 tile, u16 attr)
Set a sprite whose attribute byte the game keeps composed.
void oamSetX(u16 id, u16 x)
Set sprite X position.
u16 cam_y
Definition main.c:91
u16 cam_x
Probe oracles: the camera, the shards placed this frame, the resident in the front slot.
Definition main.c:90
Metasprite item structure (PVSnesLib compatible).
Definition sprite.h:639
u16 tile
Definition sprite.h:642
u8 reserved
Definition sprite.h:644
s16 dx
Definition sprite.h:640
s16 dy
Definition sprite.h:641
u8 attr
Definition sprite.h:643
What stays the same from one frame of a metasprite to the next.
Definition sprite.h:680
u8 height
Definition sprite.h:689
u16 baseTile
Definition sprite.h:681
u8 pieceSize
Definition sprite.h:684
u8 width
Definition sprite.h:687
u8 basePalette
Definition sprite.h:682
u8 size
Definition sprite.h:683
A batch of sprites in screen coordinates, for oamSetBatch().
Definition sprite.h:1005
const u8 * attr
Definition sprite.h:1009
const u8 * tile
Definition sprite.h:1008
const s16 * x
Definition sprite.h:1006
u8 count
Definition sprite.h:1011
u8 first_id
Definition sprite.h:1010
const s16 * y
Definition sprite.h:1007
Configuration for the dynamic sprite engine.
Definition sprite.h:748
u16 vramLarge
Definition sprite.h:749
u16 vramSmall
Definition sprite.h:753
u16 slotSmallInit
Definition sprite.h:756
u8 sizeMode
Definition sprite.h:757
u16 slotLargeInit
Definition sprite.h:755
A batch of sprites in world coordinates, for oamPlaceWorld().
Definition sprite.h:408
u8 size_big
Definition sprite.h:419
const u8 * big
Definition sprite.h:418
const s16 * x
Definition sprite.h:409
u8 count
Definition sprite.h:415
u8 * visible
Definition sprite.h:413
const u8 * order
Definition sprite.h:417
const u8 * attr
Definition sprite.h:412
u8 size
Definition sprite.h:416
u8 first_id
Definition sprite.h:414
const u8 * tile
Definition sprite.h:411
const s16 * y
Definition sprite.h:410
Dynamic sprite state structure (16 bytes, PVSnesLib compatible).
Definition sprite.h:143
u16 oamframeid
Definition sprite.h:146
u8 _pad
Definition sprite.h:151
s16 oamx
Definition sprite.h:144
u16 _reserved2
Definition sprite.h:153
s16 oamy
Definition sprite.h:145
u16 oamgfxaddr
Definition sprite.h:149
u16 _reserved1
Definition sprite.h:152
u8 oamgfxbank
Definition sprite.h:150
u8 oamattribute
Definition sprite.h:147
u8 oamrefresh
Definition sprite.h:148
static const u8 palette[]
Definition main.c:83
OpenSNES Standard Types.