OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
object.h
Go to the documentation of this file.
1
48
49#ifndef OPENSNES_OBJECT_H
50#define OPENSNES_OBJECT_H
51
52#include <snes/types.h>
53
54/*============================================================================
55 * Constants
56 *============================================================================*/
57
59#define OB_MAX 80
60
62#define OB_TYPE_MAX 64
63
65#define OB_SIZE 64
66
67/*============================================================================
68 * Object Structure (64 bytes, packed)
69 *============================================================================*/
70
113
114/*============================================================================
115 * Compile-time struct layout assertions
116 * Must match the .STRUCT t_objs in object.asm exactly.
117 *============================================================================*/
118
119_Static_assert(sizeof(t_objs) == 64, "t_objs must be 64 bytes");
120_Static_assert(__builtin_offsetof(t_objs, xpos) == 16, "xpos offset mismatch");
121_Static_assert(__builtin_offsetof(t_objs, ypos) == 19, "ypos offset mismatch");
122_Static_assert(__builtin_offsetof(t_objs, xvel) == 34, "xvel offset mismatch");
123_Static_assert(__builtin_offsetof(t_objs, yvel) == 36, "yvel offset mismatch");
124_Static_assert(__builtin_offsetof(t_objs, action) == 46, "action offset mismatch");
125_Static_assert(__builtin_offsetof(t_objs, type) == 4, "type offset mismatch");
126_Static_assert(__builtin_offsetof(t_objs, width) == 26, "width offset mismatch");
127_Static_assert(__builtin_offsetof(t_objs, height) == 28, "height offset mismatch");
128_Static_assert(__builtin_offsetof(t_objs, status) == 48, "status offset mismatch");
129_Static_assert(__builtin_offsetof(t_objs, onscreen) == 56, "onscreen offset mismatch");
130
131/*============================================================================
132 * Exported Variables
133 *============================================================================*/
134
135/* --- Bank $00 SLOT 1 (C-accessible, < $2000) --- */
136
149extern FAR t_objs objWorkspace;
150
151/* Three more variables were exported here until 2026-10-03 (API decision D4:
152 * a public header must not claim unprefixed names):
153 * objgetid -> objGetCurrentId()
154 * objtokill -> objKillCurrent() (was `objtokill = 1;`)
155 * objptr -> the return value of objGetPointer()
156 * Assembly reads obj_current_id, obj_kill_flag and obj_ptr. */
157
158/* --- Bank $7E SLOT 2 (NOT C-accessible, ASM-only) --- */
159/* The actual object buffer array lives in Bank $7E:
160 * objbuffers[OB_MAX] — 80 x 64 = 5120 bytes
161 * objfctinit/upd/ref — function pointer tables (256 bytes each)
162 * The workspace is owner-tracked (since 2026-09-19): it remembers which slot
163 * it mirrors, flushes to that slot before any reload (objNew, objGetPointer,
164 * objKill), and every write-back goes to the owner. A callback may therefore
165 * look at another object without corrupting its own record.
166 *
167 * Access these ONLY through the workspace pattern:
168 * objGetPointer(handle) copies objbuffers[N] → objWorkspace
169 * Engine functions copy objWorkspace → objbuffers[N] after callbacks
170 */
171
172/*============================================================================
173 * Functions
174 *============================================================================*/
175
182void objInitEngine(void);
183
190void objInitGravity(u16 objgravity, u16 objfriction);
191
204void objInitFriction1D(u16 friction);
205
207typedef void (*ObjInitFn)(u16 xp, u16 yp, u16 type, u16 minx, u16 maxx);
208
210typedef void (*ObjUpdateFn)(u16 idx);
211
224void objInitFunctions(u8 objtype, ObjInitFn initfct, ObjUpdateFn updfct, ObjUpdateFn reffct);
225
240u16 objNew(u8 objtype, u16 x, u16 y);
241
253
270
279void objKillCurrent(void);
280
291void objKill(u16 objhandle);
292
296void objKillAll(void);
297
306void objLoadObjects(const u8 *sourceO);
307
315void objUpdateAll(void);
316
324void objRefreshAll(void);
325
338void objCollidMap(u16 objindex);
339
348
356void objCollidMap1D(u16 objindex);
357
383
394void objUpdateXY(u16 objindex);
395
396#endif /* OPENSNES_OBJECT_H */
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
#define FAR
Place a global in bank $7E WRAM, outside the 8 KB bank-0 band.
Definition types.h:130
void objKillAll(void)
Kill all active objects.
void objInitFunctions(u8 objtype, ObjInitFn initfct, ObjUpdateFn updfct, ObjUpdateFn reffct)
Register callback functions for an object type.
void(* ObjInitFn)(u16 xp, u16 yp, u16 type, u16 minx, u16 maxx)
Init callback of an object type, called by objLoadObjects() per table entry.
Definition object.h:207
void objKill(u16 objhandle)
Kill an object.
void objCollidMap1D(u16 objindex)
Check object collision with map (no gravity).
void objCollidMap(u16 objindex)
Check object collision with map tiles.
u16 objNew(u8 objtype, u16 x, u16 y)
Create a new object.
void objInitEngine(void)
Initialize the object engine.
u16 objGetCurrentId(void)
Handle of the last object objNew() created.
void objKillCurrent(void)
Kill the object being updated.
u16 objGetPointer(u16 objhandle)
Get pointer to an object from its handle.
void objCollidMapWithSlopes(u16 objindex)
Check object collision with map tiles including slopes.
void objInitFriction1D(u16 friction)
Opt into friction for objCollidMap1D().
void(* ObjUpdateFn)(u16 idx)
Update / refresh callback of an object type; idx is the slot index.
Definition object.h:210
void objLoadObjects(const u8 *sourceO)
Load objects from a data table.
FAR t_objs objWorkspace
Object workspace.
void objInitGravity(u16 objgravity, u16 objfriction)
Set gravity and friction values.
void objUpdateAll(void)
Update all active objects.
void objRefreshAll(void)
Refresh sprites for all on-screen objects.
u16 objCollidObj(u16 idx1, u16 idx2)
Test collision between two objects.
void objUpdateXY(u16 objindex)
Update object position from velocity.
static const s16 xpos[4]
ROM source for the 16x16 sprite sheet tile data.
Definition main.c:53
Game object data structure.
Definition object.h:77
u8 type
Definition object.h:80
u16 parentID
Definition object.h:107
u16 sprframe
Definition object.h:87
u8 sprflip
Definition object.h:85
u8 onscreen
Definition object.h:110
u8 tempo
Definition object.h:104
u8 sprrefresh
Definition object.h:109
s16 xvel
Definition object.h:96
u16 height
Definition object.h:93
s16 yvel
Definition object.h:97
u16 width
Definition object.h:92
u16 sprblk3216
Definition object.h:84
u16 tilebprop
Definition object.h:101
u16 yofs
Definition object.h:91
u8 hitpoints
Definition object.h:108
u16 tilesprop
Definition object.h:100
u8 nID
Definition object.h:81
u8 count
Definition object.h:105
u16 prev
Definition object.h:78
u8 status
Definition object.h:103
u8 xpos[3]
Definition object.h:88
u8 sprpal
Definition object.h:86
u16 sprid3216
Definition object.h:83
u16 xmax
Definition object.h:95
u16 xofs
Definition object.h:90
u8 dir
Definition object.h:106
u16 tileabove
Definition object.h:99
u16 sprnum
Definition object.h:82
u8 objnotused[7]
Definition object.h:111
u16 tilestand
Definition object.h:98
u8 ypos[3]
Definition object.h:89
u16 action
Definition object.h:102
u16 next
Definition object.h:79
u16 xmin
Definition object.h:94
OpenSNES Standard Types.