Skip to main content

One post tagged with "SNES"

Super Nintendo development notes.

View All Tags

The Power of Fast Polymorphic Object Pools

· 14 min read
R65 author

Strike Force Raiders: the player's helicopter flies over a column of infantry and tanks marching on the enemy

If you've made a game in Unity, Godot or your own engine, you've probably written an object pool: a fixed array of enemies or bullets that you reuse instead of allocating new ones. On the SNES a pool isn't an optimization, it's the only option. There's no heap, and at 60 frames a second the CPU gets about 60,000 cycles a frame. How you lay a pool out in memory decides how many objects you can update before the game starts to slow down.

I wanted a game that would push the R65 compiler as hard as possible, so I made Strike Force Raiders, a helicopter tug-of-war for the SNES written in R65. Each side buys tanks, vans, missile launchers, infantry and engineers, and they march across the desert toward the other side's base while the helicopters fight overhead. On a busy frame the game updates 32 ground units, two helicopters, the buildings, every bullet and missile in the air and the paratroopers, and then draws all of it, inside that same budget.

This post covers the two R65 features that make that possible. #[soa] stores a pool one field at a time instead of one object at a time, the same data-oriented layout that ECS engines use. Tagged enums let tanks, vans and soldiers share one pool while each kind keeps its own data. Together they compile most field accesses in the update loop to a single CPU instruction. You don't need to read 65816 assembly to follow along. The listings are there so you can see how much shorter the code gets.