OpenSNES
Modern Open-Source SNES Development SDK
Loading...
Searching...
No Matches
asset.h File Reference

Opt-in asset bundle convention — package tiles + palette (+ optional tilemap) into a typed value, load it with one function call. More...

#include <snes/types.h>
#include <snes/background.h>
#include <snes/dma.h>

Go to the source code of this file.

Classes

struct  BgAsset
 A full background bundle: tileset + tilemap + map size. More...
struct  GfxAsset
 A tileset bundle: graphics + palette + color depth. More...

Macros

#define DECLARE_BG_ASSET(name, color_mode_, map_size_)
 Declare a static const BgAsset from opensnes-tileset-style symbol pairs.
#define DECLARE_GFX_ASSET(name, color_mode_)
 Declare a static const GfxAsset from <name>_tiles / <name>_pal symbol pairs (the names opensnes-tileset and opensnes-sprite write).

Functions

void bgLoad (u8 bg, const BgAsset *asset, u8 palette_slot, u16 tiles_vram, u16 map_vram)
 Load a full background bundle: tiles, palette and tilemap in one call. (The BG_LOAD macro this used to be paired with was deleted in the H3 audit.).
void gfxLoad (u8 bg, const GfxAsset *asset, u8 palette_slot, u16 tiles_vram)
 Load a tileset (tiles + palette) and configure a BG's tile graphics pointer. (It used to be described as the typed-value variant of a GFX_LOAD macro; that macro was deleted in the H3 audit and this is the only interface now.).

Detailed Description

Opt-in asset bundle convention — package tiles + palette (+ optional tilemap) into a typed value, load it with one function call.

One of the three "framework opt-ins" promised by PHILOSOPHY.md (alongside gameloop and scene). This module removes a recurring piece of SNES boilerplate: declaring six extern symbols (begin/end pairs for tiles, palette, tilemap), computing three sizes by subtraction at every call site, and threading them through an 8-parameter bgInitTileSet() followed by a separate dmaCopyVram() for the map.

Before
extern u8 tiles[], tiles_end[];
extern u8 palette[], palette_end[];
extern u8 tilemap[], tilemap_end[];
bgSetMapPtr(0, 0x0000, SC_32x32);
bgInitTileSet(0, tiles, palette, 0,
tiles_end - tiles,
palette_end - palette,
BG_16COLORS, 0x4000);
dmaCopyVram(tilemap, 0x0000, tilemap_end - tilemap);
void bgInitTileSet
Definition background.h:209
void bgSetMapPtr
Definition background.h:140
static u16 tilemap[896]
Generated 32x28 tilemap (krom's fixed pattern — see buildTilemap).
Definition main.c:56
void dmaCopyVram(const u8 *source, u16 vramAddr, u16 size)
Copy data to VRAM (PVSnesLib compatible).
#define BG_16COLORS
Definition background.h:47
#define SC_32x32
Definition background.h:36
unsigned char u8
8-bit unsigned integer (0 to 255)
Definition types.h:47
static const u8 palette[]
Definition main.c:83
After
// bg_tiles, bg_pal, bg_map (each with _end labels) come from
// the res/bg.inc that opensnes-tileset convert writes.
bgLoad(0, &bg, 0, 0x4000, 0x0000);
void bgLoad(u8 bg, const BgAsset *asset, u8 palette_slot, u16 tiles_vram, u16 map_vram)
Load a full background bundle: tiles, palette and tilemap in one call. (The BG_LOAD macro this used t...
#define DECLARE_BG_ASSET(name, color_mode_, map_size_)
Declare a static const BgAsset from opensnes-tileset-style symbol pairs.
Definition asset.h:212

DECLARE_BG_ASSET is constructor sugar — it expands to the six extern declarations and a static const BgAsset initialised from those symbols. Users who want to instantiate the struct manually can do so; the macro is a convenience, the struct is the contract.

The macros expect the symbol naming convention <name>_tiles, <name>_pal, <name>_map plus their _end siblings. opensnes-tileset convert writes these symbols itself, in a <name>.inc that already holds a ready DECLARE_BG_ASSET when the map is 32x32, 64x32, 32x64 or 64x64; hand-written assembly that exports the same labels works too.

Why a typed value

The asset is a first-class value: pass it to a function, store an array of them as a level table indexed at runtime, swap assets at scene boundaries. Each callsite is one bgLoad/gfxLoad call, no positional-parameter shuffle.

What this module does NOT do
  • It does not configure the background mode (setMode), the main screen mask (setMainScreen), or the screen brightness (setScreenOn). The caller still owns the high-level init order.
  • It does not handle sprite asset bundles. Sprites have their own CGRAM offset (128) and OAM conventions; out of scope here.
  • It does not handle the Tiled mapdata pipeline (tilesetdef, tilesetatt, large levels). Those still go through mapLoad().
Performance
gfxLoad and bgLoad are thin C wrappers over bgInitTileSet, dmaCopyVram, and bgSetMapPtr. The compiler emits one indirect load per struct field; on the order of 30 cycles of overhead per call. All real work is done by the underlying DMA, which is unchanged.
Modules required
asset (transitively pulls dma and background).
See also
background.h, dma.h

Macro Definition Documentation

◆ DECLARE_BG_ASSET

#define DECLARE_BG_ASSET ( name,
color_mode_,
map_size_ )
Value:
extern const u8 name##_tiles[], name##_tiles_end[]; \
extern const u8 name##_pal[], name##_pal_end[]; \
extern const u8 name##_map[], name##_map_end[]; \
static const BgAsset name = { \
.gfx = { \
.tiles = name##_tiles, \
.tiles_end = name##_tiles_end, \
.palette = name##_pal, \
.palette_end = name##_pal_end, \
.color_mode = (color_mode_), \
}, \
.tilemap = name##_map, \
.tilemap_end = name##_map_end, \
.map_size = (map_size_), \
}
A full background bundle: tileset + tilemap + map size.
Definition asset.h:129
u8 map_size
Tilemap layout. Use one of SC_32x32, SC_64x32, SC_32x64, SC_64x64.
Definition asset.h:140
const u8 * tilemap_end
One past the end of the tilemap data.
Definition asset.h:135

Declare a static const BgAsset from opensnes-tileset-style symbol pairs.

Same convention as DECLARE_GFX_ASSET, plus <name>_map / <name>_map_end for the tilemap blob.

bgLoad(0, &scene, 0, 0x4000, 0x0000);
u8 scene
Definition main.c:195

◆ DECLARE_GFX_ASSET

#define DECLARE_GFX_ASSET ( name,
color_mode_ )
Value:
extern const u8 name##_tiles[], name##_tiles_end[]; \
extern const u8 name##_pal[], name##_pal_end[]; \
static const GfxAsset name = { \
.tiles = name##_tiles, \
.tiles_end = name##_tiles_end, \
.palette = name##_pal, \
.palette_end = name##_pal_end, \
.color_mode = (color_mode_), \
}
A tileset bundle: graphics + palette + color depth.
Definition asset.h:104
const u8 * tiles_end
One past the end of tile graphics. Size is computed at load time as tiles_end - tiles.
Definition asset.h:109
u16 color_mode
Color depth selector. Use one of: BG_4COLORS, BG_4COLORS0 (Mode 0), BG_16COLORS, BG_256COLORS....
Definition asset.h:120
const u8 * palette
Pointer to BGR555 palette data.
Definition asset.h:111
const u8 * palette_end
One past the end of the palette data.
Definition asset.h:113

Declare a static const GfxAsset from <name>_tiles / <name>_pal symbol pairs (the names opensnes-tileset and opensnes-sprite write).

Expands to extern declarations for <name>_tiles/<name>_tiles_end and <name>_pal/<name>_pal_end, plus a static const GfxAsset populated with the right pointers and color mode.

gfxLoad(0, &player, 0, 0x4000);
void gfxLoad(u8 bg, const GfxAsset *asset, u8 palette_slot, u16 tiles_vram)
Load a tileset (tiles + palette) and configure a BG's tile graphics pointer. (It used to be described...
#define DECLARE_GFX_ASSET(name, color_mode_)
Declare a static const GfxAsset from <name>_tiles / <name>_pal symbol pairs (the names opensnes-tiles...
Definition asset.h:189

Function Documentation

◆ bgLoad()

void bgLoad ( u8 bg,
const BgAsset * asset,
u8 palette_slot,
u16 tiles_vram,
u16 map_vram )

Load a full background bundle: tiles, palette and tilemap in one call. (The BG_LOAD macro this used to be paired with was deleted in the H3 audit.).

Composes gfxLoad() with bgSetMapPtr() and a dmaCopyVram() of the tilemap. After the call the BG is fully addressable; the caller still controls when to enable it on the screen.

Parameters
bgBackground number 0-3.
assetBundle to load. Must be non-NULL.
palette_slotPalette slot index (see gfxLoad).
tiles_vramVRAM word address for the tile graphics.
map_vramVRAM word address for the tilemap.

◆ gfxLoad()

void gfxLoad ( u8 bg,
const GfxAsset * asset,
u8 palette_slot,
u16 tiles_vram )

Load a tileset (tiles + palette) and configure a BG's tile graphics pointer. (It used to be described as the typed-value variant of a GFX_LOAD macro; that macro was deleted in the H3 audit and this is the only interface now.).

Parameters
bgBackground number 0-3.
assetBundle to load. Must be non-NULL.
palette_slotPalette slot index (0-7 for 16-color, etc.).
tiles_vramVRAM word address for the tile graphics (4 KB aligned).