Loading...
Searching...
No Matches
main.c File Reference

Scrolling platformer with object engine and enemy AI. More...

#include <snes.h>
#include <snes/map.h>
#include <snes/object.h>
#include "mario.h"
#include "goomba.h"
#include "koopatroopa.h"

Functions

int main (void)
 Main entry point – scrolling platformer with map and object engines.
 
void objRegisterTypes (void)
 Register all object type callbacks (Mario, Goomba, Koopa) with correct bank bytes.
 

Variables

u8 mapmario
 Map data (tile indices for the scrolling level layout)
 
u16 nbobjects
 Running count of active objects in the level.
 
u8 objmario
 Object layer data (spawn positions and type IDs for entities)
 
u8 palsprite
 Shared sprite palette for all entity types (BGR555)
 
u8 tileset
 BG1 tileset tile data (4bpp) – start label.
 
u8 tilesetatt
 Tile attribute table (collision flags per tile index)
 
u8 tilesetdef
 Tile definition table (visual properties per tile index)
 
u8 tilesetend
 BG1 tileset tile data – end label (for size calculation)
 
u8 tilesetpal
 BG1 tileset palette (BGR555, 16 colors)
 

Detailed Description

Scrolling platformer with object engine and enemy AI.

Demonstrates the OpenSNES map engine and object engine working together to create a scrolling platformer with multiple entity types. The map engine handles tile-based scrolling and VRAM streaming, while the object engine manages entity lifecycle, physics, collision callbacks, and per-frame update dispatch.

Three object types are registered via assembly callbacks (for correct bank byte resolution): Mario (player-controlled), Goomba (walks and reverses at walls), and Koopa Troopa (patrols with shell behavior). Object spawn positions are loaded from the map's object layer.

The dynamic sprite engine handles animated 16x16 sprites with automatic VRAM tile management, freeing the game logic from manual tile uploads.

Port of the PVSnesLib mapandobjects example by Alekmaul.

SNES Concepts
  • Map engine: tile scrolling with mapLoad/mapUpdate/mapVblank pipeline
  • Object engine: entity registration, spawn from map data, update callbacks
  • Dynamic sprite engine: automatic VRAM tile allocation for animated sprites
  • Multiple sprite palettes via CGRAM bank indexing
  • SC_64x32 tilemap for horizontal scrolling levels
  • Assembly bank byte resolution for cross-bank function pointers
What to Observe
  • Mario walks and jumps through a scrolling level
  • Goombas walk back and forth, reversing at obstacles
  • Koopa Troopas patrol with distinct movement patterns
  • Camera scrolls to follow Mario's position
  • All entities animate independently with the dynamic sprite engine
Modules Used
console, sprite, sprite_dynamic, sprite_lut, dma, input, background, map, object
See also
map.h, object.h, sprite.h, background.h, input.h

Function Documentation

◆ main()

int main ( void  )

Main entry point – scrolling platformer with map and object engines.

Initializes the map engine (BG1 tileset + tilemap), object engine (entity registration and spawning), and dynamic sprite engine (animated 16x16 sprites). The main loop follows the standard OpenSNES game pipeline:

Active display: mapUpdate() + objUpdateAll() + sprite end-frame VBlank: mapVblank() + oamVramQueueUpdate()

Object callbacks (Mario, Goomba, Koopa Troopa) handle their own physics, collision, animation, and sprite drawing within objUpdateAll().

Returns
0 (never reached – infinite game loop)

◆ objRegisterTypes()

void objRegisterTypes ( void  )
extern

Register all object type callbacks (Mario, Goomba, Koopa) with correct bank bytes.

Each object type has an update callback function pointer stored with its ROM bank byte. Since C cannot express the :label bank-byte operator, this assembly function handles registration for all three entity types.

Variable Documentation

◆ mapmario

u8 mapmario
extern

Map data (tile indices for the scrolling level layout)

◆ nbobjects

u16 nbobjects

Running count of active objects in the level.

Initialized to 1 (Mario is always object 0) and incremented as enemies are loaded from the map's object layer by objLoadObjects().

◆ objmario

u8 objmario
extern

Object layer data (spawn positions and type IDs for entities)

◆ palsprite

u8 palsprite
extern

Shared sprite palette for all entity types (BGR555)

◆ tileset

u8 tileset
extern

BG1 tileset tile data (4bpp) – start label.

◆ tilesetatt

u8 tilesetatt
extern

Tile attribute table (collision flags per tile index)

◆ tilesetdef

u8 tilesetdef
extern

Tile definition table (visual properties per tile index)

◆ tilesetend

u8 tilesetend
extern

BG1 tileset tile data – end label (for size calculation)

◆ tilesetpal

u8 tilesetpal
extern

BG1 tileset palette (BGR555, 16 colors)