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

Full-screen flashes and camera shake triggered by gameplay (explosions, hits). More...

#include <aegis2d/render/screen_effects.hpp>

Public Member Functions

 ScreenEffects (EffectSettings settings={}, std::uint64_t seed=0) noexcept
 Creates the effects, with nothing showing.
 
void set_settings (EffectSettings settings) noexcept
 Changes the player's choice; turning an effect off stops it at once.
 
EffectSettings settings () const noexcept
 The player's choice.
 
bool flash (Color color, float seconds) noexcept
 Starts a full-screen flash that fades out, replacing the current one.
 
bool shake (float strength, float seconds) noexcept
 Starts a shake that calms down, unless a stronger one is running.
 
void update (float seconds) noexcept
 Moves the effects forward in time.
 
Color flash_color () const noexcept
 Color drawn over the screen this frame.
 
Color tint (Color background) const noexcept
 Blends the flash over a color, for games that flash their background.
 
std::optional< Sprite > overlay (Vec2 screen) const noexcept
 Full-screen sprite of the flash, to draw over the scene.
 
Vec2 offset () const noexcept
 Offset to add to the camera this frame.
 

Static Public Attributes

static constexpr std::size_t max_flashes_per_second = 3
 Most flashes started in any one-second window (WCAG 2.3.1).
 

Detailed Description

Full-screen flashes and camera shake triggered by gameplay (explosions, hits).

Gameplay asks for a flash or a shake; each frame the game calls update(), then draws overlay() over the scene, or blends the flash into its clear color with tint(), and moves its camera by offset() (Camera::set_shake()). With EffectSettings::flashes or EffectSettings::shake off, the requests are ignored and nothing is drawn or moved. Even when flashes are on, at most max_flashes_per_second start in any second, the limit of WCAG 2.3.1 for content that flashes.

effects.flash(render::rgb(0xFFFFFF, 0.6F), 0.2F); // on an explosion
effects.shake(8.0F, 0.3F);
...
effects.update(frame_seconds);
renderer.begin_frame(effects.tint(background));
camera.set_shake(effects.offset());
renderer.set_camera(camera);
... draw the scene ...
renderer.set_camera(std::nullopt);
if (const auto flash = effects.overlay(screen_size)) { renderer.draw(*flash); }
Full-screen flashes and camera shake triggered by gameplay (explosions, hits).
Definition screen_effects.hpp:65
bool flash(Color color, float seconds) noexcept
Starts a full-screen flash that fades out, replacing the current one.
EffectSettings settings() const noexcept
The player's choice.
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
bool shake
Whether the screen shakes.
Definition screen_effects.hpp:28

Constructor & Destructor Documentation

◆ ScreenEffects()

aegis2d::render::ScreenEffects::ScreenEffects ( EffectSettings  settings = {},
std::uint64_t  seed = 0 
)
explicitnoexcept

Creates the effects, with nothing showing.

Parameters
settingsThe player's choice.
seedSeed of the shake directions, so a replay shakes the same way.

Member Function Documentation

◆ flash()

bool aegis2d::render::ScreenEffects::flash ( Color  color,
float  seconds 
)
noexcept

Starts a full-screen flash that fades out, replacing the current one.

Parameters
colorColor of the flash; its alpha is the opacity at the start, clamped to [0, 1].
secondsDuration of the fade, more than 0.
Returns
false if the flash was refused: flashes are off, seconds is not positive, or max_flashes_per_second flashes already started in the last second.

◆ flash_color()

Color aegis2d::render::ScreenEffects::flash_color ( ) const
noexcept

Color drawn over the screen this frame.

Returns
The flash color with its faded alpha; alpha 0 when no flash shows.

◆ offset()

Vec2 aegis2d::render::ScreenEffects::offset ( ) const
noexcept

Offset to add to the camera this frame.

Returns
A vector no longer than the shake's current strength; zero when the screen does not shake.

◆ overlay()

std::optional< Sprite > aegis2d::render::ScreenEffects::overlay ( Vec2  screen) const
noexcept

Full-screen sprite of the flash, to draw over the scene.

Parameters
screenSize of the screen, in logical pixels.
Returns
A plain sprite covering the screen on the top layer, or nothing when no flash shows.

◆ set_settings()

void aegis2d::render::ScreenEffects::set_settings ( EffectSettings  settings)
noexcept

Changes the player's choice; turning an effect off stops it at once.

Parameters
settingsThe new choice.

◆ settings()

EffectSettings aegis2d::render::ScreenEffects::settings ( ) const
noexcept

The player's choice.

Returns
The settings given to the constructor or to set_settings().

◆ shake()

bool aegis2d::render::ScreenEffects::shake ( float  strength,
float  seconds 
)
noexcept

Starts a shake that calms down, unless a stronger one is running.

Parameters
strengthLargest offset of the camera, in logical pixels.
secondsDuration of the shake, more than 0.
Returns
false if the shake was refused: shake is off, or a parameter is not positive.

◆ tint()

Color aegis2d::render::ScreenEffects::tint ( Color  background) const
noexcept

Blends the flash over a color, for games that flash their background.

Parameters
backgroundColor under the flash, such as the clear color.
Returns
background with flash_color() blended over it; background when no flash shows.

◆ update()

void aegis2d::render::ScreenEffects::update ( float  seconds)
noexcept

Moves the effects forward in time.

Parameters
secondsTime since the last update, such as one frame.

Member Data Documentation

◆ max_flashes_per_second

constexpr std::size_t aegis2d::render::ScreenEffects::max_flashes_per_second = 3
staticconstexpr

Most flashes started in any one-second window (WCAG 2.3.1).


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