Deluge Firmware 1.3.0
Build date: 2026.08.23
Loading...
Searching...
No Matches
deluge::hid::display::Starfield Class Reference

Perspective-projection starfield. More...

#include <starfield.h>

Classes

struct  Projected
 A star projected onto the screen. More...
struct  Star

Public Member Functions

 Starfield ()
 Construct a field that is already fully populated, so the first rendered frame is not empty.
void scatter ()
 Re-scatter every star across the full depth range.
void advance ()
 Advance every star one frame toward the viewer, respawning any that pass it.
Projected project (size_t i) const
 Project a star onto the screen.
float depthOf (size_t i) const
 Depth of a star. Exposed for tests.

Static Public Attributes

Field geometry
static constexpr size_t kNumStars = 60
 Number of stars in the field.
static constexpr float kMaxDepth = 32.0f
 Stars respawn at this depth once they pass the viewer.
static constexpr float kZStep = 0.4f
 Depth consumed per frame.
static constexpr float kFov = 3.14159265358979323846f
 Field of view, radians.
static constexpr float kCentreX = 64.0f
 Projected screen centre, X, in pixels.
static constexpr float kCentreY = 26.0f
 Projected screen centre, Y, in pixels: (OLED_MAIN_TOPMOST_PIXEL + OLED_MAIN_HEIGHT_PIXELS) / 2.
static constexpr float kNearThreshold = kMaxDepth * 0.4f
static constexpr float kMinDepth = 0.01f
 Floor applied to z when projecting.

Private Member Functions

uint32_t nextRandom ()
 Step the linear congruential generator.
float nextRandomFloat ()
 Step the linear congruential generator, mapped to [0, 1).
void respawn (Star &star, float z)
 Give a star a fresh random x/y at the given depth.

Private Attributes

std::array< Star, kNumStarsstars_ {}
uint32_t rngState_ {0xCAFE5678}

Detailed Description

Perspective-projection starfield.

Stars hold a 3D position and are projected with factor = kFov / z, so they appear to fly toward the viewer as z decrements.

Note
Deliberately has no display dependency, so the projection maths is unit-testable on the host.
See also
The deluge-sdk demo firmware, firmwares/demo-firmware/src/tasks/oled.rs – the reference implementation this is ported from, and the source of the LCG constants and depth ranges.

Member Function Documentation

◆ depthOf()

float deluge::hid::display::Starfield::depthOf ( size_t i) const
inlinenodiscard

Depth of a star. Exposed for tests.

Parameters
iStar index, less than kNumStars.
Returns
The star's z, always strictly positive.

◆ nextRandom()

uint32_t deluge::hid::display::Starfield::nextRandom ( )
private

Step the linear congruential generator.

Returns
The next raw LCG output.

◆ nextRandomFloat()

float deluge::hid::display::Starfield::nextRandomFloat ( )
private

Step the linear congruential generator, mapped to [0, 1).

Returns
The next LCG output, mapped to [0, 1).

◆ project()

Starfield::Projected deluge::hid::display::Starfield::project ( size_t i) const
nodiscard

Project a star onto the screen.

Parameters
iStar index, less than kNumStars.
Returns
The star's unclipped screen position and pixel size.

◆ respawn()

void deluge::hid::display::Starfield::respawn ( Star & star,
float z )
private

Give a star a fresh random x/y at the given depth.

Parameters
starThe star to overwrite.
zDepth to place it at.

◆ scatter()

void deluge::hid::display::Starfield::scatter ( )

Re-scatter every star across the full depth range.

Note
Does not reseed the LCG, so successive calls produce different fields.

Member Data Documentation

◆ kMinDepth

float deluge::hid::display::Starfield::kMinDepth = 0.01f
staticconstexpr

Floor applied to z when projecting.

The reference implementation relies on Rust's saturating f32 as i32. A C++ static_cast of an out-of-range float is undefined behaviour, and z can land arbitrarily close to zero, so the projection factor is bounded instead.

Note
A star this close is off-screen and respawns next frame, so nothing visible changes.

◆ kNearThreshold

float deluge::hid::display::Starfield::kNearThreshold = kMaxDepth * 0.4f
staticconstexpr

Depth threshold: stars nearer than this (same depth units as kMaxDepth) draw as 2x2 blocks rather than single pixels.


The documentation for this class was generated from the following files:
  • src/deluge/hid/display/starfield.h
  • src/deluge/hid/display/starfield.cpp