Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
starfield.hpp
Go to the documentation of this file.
1
3#pragma once
4
8#include <aegis2d/render/export.hpp>
10
11#include <array>
12#include <cstddef>
13#include <cstdint>
14#include <span>
15#include <vector>
16
17namespace aegis2d::render {
18
20struct StarLayer {
22 std::size_t count = 100;
25 float speed = 60.0F;
27 float size = 1.0F;
29 Color color{.r = 0.5F, .g = 0.5F, .b = 0.63F, .a = 1.0F};
30};
31
34inline constexpr std::array<StarLayer, 3> default_star_layers{{
35 {.count = 160, .speed = 25.0F, .size = 1.0F, .color = rgb(0x50506A)},
36 {.count = 90, .speed = 70.0F, .size = 2.0F, .color = rgb(0x8080A0)},
37 {.count = 40, .speed = 160.0F, .size = 3.0F, .color = rgb(0xC8C8E6)},
38}};
39
54class AEGIS2D_RENDER_EXPORT Starfield {
55public:
62 explicit Starfield(Vec2 area, std::span<const StarLayer> layers = default_star_layers,
63 std::uint64_t seed = 1);
64
68 void update(double seconds) noexcept;
69
72 void update(core::Duration step) noexcept;
73
77 void draw(IRenderer& renderer, std::int32_t layer = -100) const;
78
84 [[nodiscard]] std::vector<Vec2> positions(std::size_t layer) const;
85
88 [[nodiscard]] double elapsed() const noexcept;
89
92 [[nodiscard]] std::size_t layers() const noexcept;
93
96 [[nodiscard]] std::size_t size() const noexcept;
97
100 [[nodiscard]] Vec2 area() const noexcept;
101
102private:
103 struct Layer {
104 StarLayer style;
105 std::vector<Vec2> starts;
106 };
107
108 [[nodiscard]] float x_of(const Layer& layer, float start) const noexcept;
109
110 Vec2 area_;
111 std::vector<Layer> layers_;
112 double elapsed_ = 0.0;
113};
114
115} // namespace aegis2d::render
What the game draws with, whatever the graphics API behind it.
Definition renderer.hpp:81
Stars scrolling across the screen in layers, each at its own speed: a parallax background for a side-...
Definition starfield.hpp:54
std::vector< Vec2 > positions(std::size_t layer) const
Where the stars of a layer are now.
void update(core::Duration step) noexcept
Moves time forward by a step, such as one tick of core::MainLoop.
void update(double seconds) noexcept
Moves time forward.
double elapsed() const noexcept
Time spent since the stars were spread.
Starfield(Vec2 area, std::span< const StarLayer > layers=default_star_layers, std::uint64_t seed=1)
Spreads the stars of each layer over an area.
void draw(IRenderer &renderer, std::int32_t layer=-100) const
Draws every star, from the farthest layer to the nearest.
Fixed-timestep accumulator and monotonic stopwatch.
RGBA color used by the renderer.
Vector and matrix types of the engine (GLM behind engine names).
std::chrono::nanoseconds Duration
Duration type used for simulation time (nanoseconds).
Definition clock.hpp:14
OpenGL renderer: sprites, text and camera (client only).
Definition bitmap_font.hpp:16
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
constexpr std::array< StarLayer, 3 > default_star_layers
Three layers, from the farthest: many small dim stars scrolling slowly, then fewer,...
Definition starfield.hpp:34
constexpr Color rgb(std::uint32_t hex, float alpha=1.0F) noexcept
Color from its hexadecimal sRGB code, as design tools and palettes give it.
Definition color.hpp:25
Definition endpoint.hpp:85
Backend-independent renderer interface and the sprite it draws.
RGBA color with components in [0, 1].
Definition color.hpp:10
float r
Red component.
Definition color.hpp:12
One layer of a starfield: stars at one depth.
Definition starfield.hpp:20
float size
Side of a star, in logical pixels.
Definition starfield.hpp:27
Color color
Color of the stars.
Definition starfield.hpp:29
float speed
Scroll speed, in logical pixels per second, to the left (negative: to the right). Far layers scroll s...
Definition starfield.hpp:25
std::size_t count
Number of stars.
Definition starfield.hpp:22