Game object engine with physics and collision (CONTRIB module). More...
#include <snes/types.h>Go to the source code of this file.
Classes | |
| struct | t_objs |
| Game object data structure. More... | |
Macros | |
| #define | OB_MAX 80 |
| Maximum number of objects in the game. | |
| #define | OB_SIZE 64 |
| Object size in bytes. | |
| #define | OB_TYPE_MAX 64 |
| Maximum number of object types. | |
Typedefs | |
| typedef void(* | ObjInitFn) (u16 xp, u16 yp, u16 type, u16 minx, u16 maxx) |
| Init callback of an object type, called by objLoadObjects() per table entry. | |
| typedef void(* | ObjUpdateFn) (u16 idx) |
Update / refresh callback of an object type; idx is the slot index. | |
Functions | |
| void | objCollidMap (u16 objindex) |
| Check object collision with map tiles. | |
| void | objCollidMap1D (u16 objindex) |
| Check object collision with map (no gravity). | |
| void | objCollidMapWithSlopes (u16 objindex) |
| Check object collision with map tiles including slopes. | |
| u16 | objCollidObj (u16 idx1, u16 idx2) |
| Test collision between two objects. | |
| u16 | objGetCurrentId (void) |
| Handle of the last object objNew() created. | |
| u16 | objGetPointer (u16 objhandle) |
| Get pointer to an object from its handle. | |
| void | objInitEngine (void) |
| Initialize the object engine. | |
| void | objInitFriction1D (u16 friction) |
| Opt into friction for objCollidMap1D(). | |
| void | objInitFunctions (u8 objtype, ObjInitFn initfct, ObjUpdateFn updfct, ObjUpdateFn reffct) |
| Register callback functions for an object type. | |
| void | objInitGravity (u16 objgravity, u16 objfriction) |
| Set gravity and friction values. | |
| void | objKill (u16 objhandle) |
| Kill an object. | |
| void | objKillAll (void) |
| Kill all active objects. | |
| void | objKillCurrent (void) |
| Kill the object being updated. | |
| void | objLoadObjects (const u8 *sourceO) |
| Load objects from a data table. | |
| u16 | objNew (u8 objtype, u16 x, u16 y) |
| Create a new object. | |
| void | objRefreshAll (void) |
| Refresh sprites for all on-screen objects. | |
| void | objUpdateAll (void) |
| Update all active objects. | |
| void | objUpdateXY (u16 objindex) |
| Update object position from velocity. | |
Variables | |
| FAR t_objs | objWorkspace |
| Object workspace. | |
Game object engine with physics and collision (CONTRIB module).
Provides managed game objects with:
Because the object buffer resides in Bank $7E (not directly accessible from C), a workspace pattern is used. Before each C callback, the engine copies the current object's data to objWorkspace. After the callback returns, changes are copied back.
Based on: PVSnesLib object engine by Alekmaul Slope management: Nub1604 License: zlib (compatible with MIT)
| #define OB_MAX 80 |
Maximum number of objects in the game.
| #define OB_SIZE 64 |
Object size in bytes.
| #define OB_TYPE_MAX 64 |
Maximum number of object types.
Init callback of an object type, called by objLoadObjects() per table entry.
| typedef void(* ObjUpdateFn) (u16 idx) |
Update / refresh callback of an object type; idx is the slot index.
| void objCollidMap | ( | u16 | objindex | ) |
Check object collision with map tiles.
Tests collision in Y (gravity/jumping) then X (walls). Updates tilestand, tileabove, tilesprop, tilebprop in objWorkspace. Applies friction to X velocity. Applies gravity if airborne.
| objindex | Slot index (as received in an update callback) or the handle from objNew() — see "Index or handle" below |
| void objCollidMap1D | ( | u16 | objindex | ) |
Check object collision with map (no gravity).
For top-down movement without gravity.
| objindex | Slot index or handle |
| void objCollidMapWithSlopes | ( | u16 | objindex | ) |
Check object collision with map tiles including slopes.
Like objCollidMap but also handles slope tiles (T_SLOPEU1..T_SLOPEUD2).
| objindex | Slot index or handle |
Test collision between two objects.
Uses AABB (axis-aligned bounding box) collision.
idx and a handle both work. Before that a handle's id byte was shifted into the buffer offset and the routine silently worked on memory past the pool. Since 2026-10-05 an index of 80 or more (OB_MAX slots) returns at once — 0 here, no change elsewhere; before, it addressed the engine's own state past the pool.The id byte counts 1..255 and wraps (0 is never used): a handle kept across 255 or more objNew() calls can match a reused slot and pass as live. Re-fetch handles you keep for long, or check the object's type.
| idx1 | First object: slot index or handle |
| idx2 | Second object: slot index or handle |
| u16 objGetCurrentId | ( | void | ) |
Handle of the last object objNew() created.
Inside an init callback this is the object being created: objNew() has not returned yet, so its return value is not available there.
Get pointer to an object from its handle.
Validates the handle and populates objWorkspace with the object's data.
| objhandle | Object handle (from objNew() or objGetCurrentId()) |
| void objInitEngine | ( | void | ) |
Initialize the object engine.
Clears all object buffers, resets linked lists, and sets default gravity/friction values. Call once at game start.
| void objInitFriction1D | ( | u16 | friction | ) |
Opt into friction for objCollidMap1D().
objCollidMap1D() stops an object on a wall but never decelerates it otherwise (PVSnesLib parity; its FRICTION1D constant was defined and never used). A non-zero value here makes every objCollidMap1D() call move xvel AND yvel toward zero by that amount, clamped at zero. objInitEngine() resets it to 0 (off).
| friction | Deceleration per call, in 8.8 velocity units (0 = none; 0x0100, PVSnesLib's unused constant, is one pixel per frame) |
| void objInitFunctions | ( | u8 | objtype, |
| ObjInitFn | initfct, | ||
| ObjUpdateFn | updfct, | ||
| ObjUpdateFn | reffct ) |
Register callback functions for an object type.
Typed since 2026-09-19: pass the functions directly, no (void *) cast. A callback with the wrong signature is now a compile error instead of a corrupted stack at run time. A null callback is safe — the engine skips it.
| objtype | Object type index (0-63) |
| initfct | Init callback (or 0) |
| updfct | Update callback, called per frame (or 0) |
| reffct | Refresh callback, called for on-screen objects (or 0) |
Set gravity and friction values.
| objgravity | Gravity acceleration per frame (default: 41) |
| objfriction | Horizontal friction deceleration (default: 16) |
| void objKill | ( | u16 | objhandle | ) |
Kill an object.
| objhandle | Object handle |
| void objKillAll | ( | void | ) |
Kill all active objects.
| void objKillCurrent | ( | void | ) |
Kill the object being updated.
Call it inside an update callback. objUpdateAll() removes the object once the callback returns and its workspace has been written back, the only safe moment. It does nothing in a refresh callback until the next update pass.
| void objLoadObjects | ( | const u8 * | sourceO | ) |
Create a new object.
| objtype | Object type (0-63) |
| x | Initial X position in map pixels |
| y | Initial Y position in map pixels |
| void objRefreshAll | ( | void | ) |
Refresh sprites for all on-screen objects.
Calls the refresh function for objects that are on screen (-32 to 256 X, -32 to 224 Y). Useful to avoid flickering in scrolling games.
| void objUpdateAll | ( | void | ) |
Update all active objects.
Iterates through all active objects. For each object within the "virtual screen" (-64 to 320 X, -64 to 288 Y), calls the type's update function with objWorkspace pre-populated.
| void objUpdateXY | ( | u16 | objindex | ) |
Update object position from velocity.
Adds xvel/yvel to xpos/ypos (24-bit fixed point).
| objindex | Slot index (the one passed to your update callback) or the handle from objNew() |
Object workspace.
Single 64-byte workspace populated before each C callback. Read/write this in init, update, and refresh callbacks. Changes are automatically copied back to the object buffer.
It is a FAR object since 2026-10-09 (it sits behind the object pool, in bank $7E, so that the collision routines work on it without copying it): objWorkspace.field reads and writes as before, at the same cost; a pointer to it or to one of its fields is a FAR pointer.