Deluge Firmware 1.3.0
Build date: 2026.08.23
Loading...
Searching...
No Matches
starfield.h
1/*
2 * Copyright © 2026 Synthstrom Audible Limited
3 *
4 * This file is part of The Synthstrom Audible Deluge Firmware.
5 *
6 * The Synthstrom Audible Deluge Firmware is free software: you can redistribute it and/or modify it under the
7 * terms of the GNU General Public License as published by the Free Software Foundation,
8 * either version 3 of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
11 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * See the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along with this program.
15 * If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#pragma once
19
20#include <array>
21#include <cstddef>
22#include <cstdint>
23
24namespace deluge::hid::display {
25
35class Starfield {
36public:
39
41 static constexpr size_t kNumStars = 60;
43 static constexpr float kMaxDepth = 32.0f;
45 static constexpr float kZStep = 0.4f;
47 static constexpr float kFov = 3.14159265358979323846f;
49 static constexpr float kCentreX = 64.0f;
51 static constexpr float kCentreY = 26.0f;
54 static constexpr float kNearThreshold = kMaxDepth * 0.4f;
62 static constexpr float kMinDepth = 0.01f;
63
65
69 struct Projected {
70 int32_t x;
71 int32_t y;
72 int32_t size;
73 };
74
78
82 void scatter();
83
85 void advance();
86
91 [[nodiscard]] Projected project(size_t i) const;
92
97 [[nodiscard]] float depthOf(size_t i) const { return stars_[i].z; }
98
99private:
100 struct Star {
101 float x;
102 float y;
103 float z;
104 };
105
109 uint32_t nextRandom();
110
114 float nextRandomFloat();
115
120 void respawn(Star& star, float z);
121
122 std::array<Star, kNumStars> stars_{};
123 uint32_t rngState_{0xCAFE5678};
124};
125
126} // namespace deluge::hid::display
static constexpr float kMaxDepth
Stars respawn at this depth once they pass the viewer.
Definition starfield.h:43
uint32_t nextRandom()
Step the linear congruential generator.
Definition starfield.cpp:23
static constexpr float kCentreY
Projected screen centre, Y, in pixels: (OLED_MAIN_TOPMOST_PIXEL + OLED_MAIN_HEIGHT_PIXELS) / 2.
Definition starfield.h:51
Projected project(size_t i) const
Project a star onto the screen.
Definition starfield.cpp:57
void advance()
Advance every star one frame toward the viewer, respawning any that pass it.
Definition starfield.cpp:48
float nextRandomFloat()
Step the linear congruential generator, mapped to [0, 1).
Definition starfield.cpp:28
static constexpr float kMinDepth
Floor applied to z when projecting.
Definition starfield.h:62
static constexpr float kZStep
Depth consumed per frame.
Definition starfield.h:45
Starfield()
Construct a field that is already fully populated, so the first rendered frame is not empty.
Definition starfield.h:77
static constexpr float kFov
Field of view, radians.
Definition starfield.h:47
static constexpr float kCentreX
Projected screen centre, X, in pixels.
Definition starfield.h:49
static constexpr size_t kNumStars
Number of stars in the field.
Definition starfield.h:41
float depthOf(size_t i) const
Depth of a star. Exposed for tests.
Definition starfield.h:97
void scatter()
Re-scatter every star across the full depth range.
Definition starfield.cpp:40
static constexpr float kNearThreshold
Definition starfield.h:54
void respawn(Star &star, float z)
Give a star a fresh random x/y at the given depth.
Definition starfield.cpp:32
A star projected onto the screen.
Definition starfield.h:69
int32_t x
screen X, pixels
Definition starfield.h:70
int32_t y
screen Y, pixels
Definition starfield.h:71
int32_t size
1 or 2, in pixels square
Definition starfield.h:72
Definition starfield.h:100
float y
3D position, roughly -100 to +100
Definition starfield.h:102
float x
3D position, roughly -256 to +256
Definition starfield.h:101
float z
depth, positive, roughly up to kMaxDepth
Definition starfield.h:103