Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::render::Starfield Class Reference

Stars scrolling across the screen in layers, each at its own speed: a parallax background for a side-scrolling shooter. More...

#include <aegis2d/render/starfield.hpp>

Public Member Functions

 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 update (double seconds) noexcept
 Moves time forward.
 
void update (core::Duration step) noexcept
 Moves time forward by a step, such as one tick of core::MainLoop.
 
void draw (IRenderer &renderer, std::int32_t layer=-100) const
 Draws every star, from the farthest layer to the nearest.
 
std::vector< Vec2 > positions (std::size_t layer) const
 Where the stars of a layer are now.
 
double elapsed () const noexcept
 Time spent since the stars were spread.
 
std::size_t layers () const noexcept
 Number of layers.
 
std::size_t size () const noexcept
 Number of stars, every layer counted.
 
Vec2 area () const noexcept
 Area the stars cover.
 

Detailed Description

Stars scrolling across the screen in layers, each at its own speed: a parallax background for a side-scrolling shooter.

The stars are spread from a seed, so the same seed gives the same sky. Their positions are a function of the time spent only (elapsed()): update() adds the time of a frame or a tick, and a star that leaves the screen on one side comes back on the other at the same height. The scroll speed is therefore the same at any frame rate, 30 or 144 frames per second, and whether the time comes in many small steps or few large ones.

render::Starfield stars{{1280.0F, 720.0F}}; // render::default_star_layers
...
stars.update(frame_seconds); // or one fixed step per tick
stars.draw(renderer); // behind everything else
Stars scrolling across the screen in layers, each at its own speed: a parallax background for a side-...
Definition starfield.hpp:54
void update(double seconds) noexcept
Moves time forward.

Constructor & Destructor Documentation

◆ Starfield()

aegis2d::render::Starfield::Starfield ( Vec2  area,
std::span< const StarLayer >  layers = default_star_layers,
std::uint64_t  seed = 1 
)
explicit

Spreads the stars of each layer over an area.

Parameters
areaWidth and height of the screen, in logical pixels.
layersLayers, from the farthest (drawn first) to the nearest.
seedSeed of the stars' positions.
Exceptions
std::invalid_argumentif a dimension of area is not above 0 and finite, or a layer's size is not above 0 and finite or its speed not finite.

Member Function Documentation

◆ area()

Vec2 aegis2d::render::Starfield::area ( ) const
noexcept

Area the stars cover.

Returns
The area given to the constructor.

◆ draw()

void aegis2d::render::Starfield::draw ( IRenderer &  renderer,
std::int32_t  layer = -100 
) const

Draws every star, from the farthest layer to the nearest.

Parameters
rendererRenderer in a frame.
layerDrawing layer of the stars, below the game's sprites.

◆ elapsed()

double aegis2d::render::Starfield::elapsed ( ) const
noexcept

Time spent since the stars were spread.

Returns
The sum of the times given to update(), in seconds.

◆ layers()

std::size_t aegis2d::render::Starfield::layers ( ) const
noexcept

Number of layers.

Returns
The layers given to the constructor.

◆ positions()

std::vector< Vec2 > aegis2d::render::Starfield::positions ( std::size_t  layer) const

Where the stars of a layer are now.

Parameters
layerIndex of the layer, from the farthest.
Returns
The top-left corner of each star, with x from minus its size to the area's width (a star leaving on the left is still partly visible).
Exceptions
std::out_of_rangeif layer is not below layers().

◆ size()

std::size_t aegis2d::render::Starfield::size ( ) const
noexcept

Number of stars, every layer counted.

Returns
The sum of the layers' counts.

◆ update() [1/2]

void aegis2d::render::Starfield::update ( core::Duration  step)
noexcept

Moves time forward by a step, such as one tick of core::MainLoop.

Parameters
stepTime spent.

◆ update() [2/2]

void aegis2d::render::Starfield::update ( double  seconds)
noexcept

Moves time forward.

Parameters
secondsTime spent, such as the last frame's duration; negative values and NaN count as 0.

The documentation for this class was generated from the following file: