MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
matrix::Rain Class Reference

#include <examples/rain/rain.hpp>

Public Member Functions

void event (SDL_Event &e)
int height () const
void on_swapchain_recreated (mxvk::VK_Window &window)
Rain & operator= (const Rain &)=delete
Rain & operator= (Rain &&)=delete
int pitch () const
const void * pixels () const
 Rain (const Rain &)=delete
 Rain (const std::string &asset_root, bool binary_glyph_mode)
 Rain (mxvk::VK_Window &window, const std::string &asset_root, bool binary_glyph_mode)
 Rain (mxvk::VK_Window &window, RainConfig config)
 Rain (Rain &&)=delete
 Rain (RainConfig config)
void render ()
void render (int width, int height)
void reset ()
void resize (int width, int height)
void resize (mxvk::VK_Window &window)
void set_opacity (float opacity)
mxvk::VK_Sprite * sprite () const
SDL_Surface * surface () const
void sync_texture ()
void update (float dt)
void update_and_render (mxvk::VK_Window &window)
void update_and_render (mxvk::VK_Window &window, int render_width, int render_height)
int width () const
 ~Rain ()

Static Public Member Functions

static std::vector< Uint32 > binary_symbol_set ()
static std::vector< Uint32 > full_symbol_set ()
static SDL_Color head_color ()
static SDL_Color matrix_color (int level)

Detailed Description

Definition at line 58 of file rain.hpp.

Constructor & Destructor Documentation

◆ Rain() [1/6]

matrix::Rain::Rain ( mxvk::VK_Window & window,
RainConfig config )

Definition at line 151 of file rain.cpp.

151: Rain(std::move(rain_config), SurfaceMode::window_driven) { resize(window); }
void resize(mxvk::VK_Window &window)
Definition rain.cpp:221
Rain(mxvk::VK_Window &window, RainConfig config)
Definition rain.cpp:151

◆ Rain() [2/6]

matrix::Rain::Rain ( RainConfig config)
explicit

Definition at line 153 of file rain.cpp.

153: Rain(std::move(rain_config), SurfaceMode::explicit_default) {}

◆ Rain() [3/6]

matrix::Rain::Rain ( mxvk::VK_Window & window,
const std::string & asset_root,
bool binary_glyph_mode )

Definition at line 200 of file rain.cpp.

200: Rain(window, make_matrix_rain_config(asset_root_path, binary_glyph_mode)) {}
RainConfig make_matrix_rain_config(const std::string &asset_root, bool binary_glyph_mode)
Definition rain.cpp:142

◆ Rain() [4/6]

matrix::Rain::Rain ( const std::string & asset_root,
bool binary_glyph_mode )

Definition at line 202 of file rain.cpp.

202: Rain(make_matrix_rain_config(asset_root_path, binary_glyph_mode), SurfaceMode::explicit_default) {}

◆ ~Rain()

matrix::Rain::~Rain ( )

Definition at line 204 of file rain.cpp.

204 {
205 glyphs.clear();
206 font.reset();
207 background.reset();
208 canvas.reset();
209 if (ttf_acquired) {
210 release_ttf();
211 }
212 }

◆ Rain() [5/6]

matrix::Rain::Rain ( const Rain & )
delete

◆ Rain() [6/6]

matrix::Rain::Rain ( Rain && )
delete

Member Function Documentation

◆ binary_symbol_set()

std::vector< Uint32 > matrix::Rain::binary_symbol_set ( )
static

Definition at line 140 of file rain.cpp.

140{ return {U'0', U'1'}; }

◆ event()

void matrix::Rain::event ( SDL_Event & e)

Definition at line 214 of file rain.cpp.

214 {
215 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_SPACE) {
216 randomize_streams();
217 clear_canvas();
218 }
219 }

◆ full_symbol_set()

std::vector< Uint32 > matrix::Rain::full_symbol_set ( )
static

Definition at line 138 of file rain.cpp.

138{ return {0xFF66, 0xFF67, 0xFF68, 0xFF69, 0xFF6A, 0xFF6B, 0xFF6C, 0xFF6D, 0xFF6E, 0xFF6F, 0xFF71, 0xFF72, 0xFF73, 0xFF74, 0xFF75, 0xFF76, 0xFF77, 0xFF78, 0xFF79, 0xFF7A, 0xFF7B, 0xFF7C, 0xFF7D, 0xFF7E, 0xFF7F, 0xFF80, 0xFF81, 0xFF82, 0xFF83, 0xFF84, 0xFF85, 0xFF86, 0xFF87, 0xFF88, 0xFF89, 0xFF8A, 0xFF8B, 0xFF8C, 0xFF8D, 0xFF8E, 0xFF8F, 0xFF90, 0xFF91, 0xFF92, 0xFF93, 0xFF94, 0xFF95, 0xFF96, 0xFF97, 0xFF98, 0xFF99, 0xFF9A, 0xFF9B, 0xFF9C, 0xFF9D}; }

◆ head_color()

SDL_Color matrix::Rain::head_color ( )
static

Definition at line 136 of file rain.cpp.

136{ return SDL_Color{208, 255, 200, 255}; }

◆ height()

int matrix::Rain::height ( ) const
nodiscard

Definition at line 307 of file rain.cpp.

307{ return canvas != nullptr ? canvas->h : 0; }

◆ matrix_color()

SDL_Color matrix::Rain::matrix_color ( int level)
static

Definition at line 126 of file rain.cpp.

126 {
127 constexpr int max_level = 7;
128 const float t = std::clamp(static_cast<float>(level) / static_cast<float>(max_level), 0.0f, 1.0f);
129 const Uint8 r = static_cast<Uint8>(std::lerp(0.0f, 48.0f, std::pow(t, 2.9f)));
130 const Uint8 g = static_cast<Uint8>(std::lerp(42.0f, 205.0f, std::pow(t, 0.82f)));
131 const Uint8 b = static_cast<Uint8>(std::lerp(0.0f, 24.0f, std::pow(t, 3.1f)));
132 const Uint8 a = static_cast<Uint8>(std::lerp(95.0f, 235.0f, t));
133 return SDL_Color{r, g, b, a};
134 }

◆ on_swapchain_recreated()

void matrix::Rain::on_swapchain_recreated ( mxvk::VK_Window & window)

Definition at line 297 of file rain.cpp.

297{ resize(window); }

◆ operator=() [1/2]

Rain & matrix::Rain::operator= ( const Rain & )
delete

◆ operator=() [2/2]

Rain & matrix::Rain::operator= ( Rain && )
delete

◆ pitch()

int matrix::Rain::pitch ( ) const
nodiscard

Definition at line 309 of file rain.cpp.

309{ return canvas != nullptr ? canvas->pitch : 0; }

◆ pixels()

const void * matrix::Rain::pixels ( ) const
nodiscard

Definition at line 303 of file rain.cpp.

303{ return canvas != nullptr ? canvas->pixels : nullptr; }

◆ render() [1/2]

void matrix::Rain::render ( )

Definition at line 255 of file rain.cpp.

255 {
256 if (canvas == nullptr) {
257 return;
258 }
259 render(canvas->w, canvas->h);
260 }
void render()
Definition rain.cpp:255

◆ render() [2/2]

void matrix::Rain::render ( int width,
int height )

Definition at line 262 of file rain.cpp.

262 {
263 sync_texture();
264 if (rain_sprite == nullptr || canvas == nullptr) {
265 return;
266 }
267 rain_sprite->drawSpriteRect(0, 0, width, height);
268 }
int height() const
Definition rain.cpp:307
void sync_texture()
Definition rain.cpp:248
int width() const
Definition rain.cpp:305

◆ reset()

void matrix::Rain::reset ( )

Definition at line 272 of file rain.cpp.

272 {
273 clear_canvas();
274 randomize_streams();
275 opacity = 1.0f;
276 last_frame = std::chrono::steady_clock::now();
277 }

◆ resize() [1/2]

void matrix::Rain::resize ( int width,
int height )

Definition at line 223 of file rain.cpp.

223 {
224 if (width <= 0 || height <= 0 || (canvas != nullptr && canvas->w == width && canvas->h == height)) {
225 return;
226 }
227
228 canvas.reset(SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGBA32));
229 if (canvas == nullptr) {
230 throw mxvk::Exception("Failed to create matrix canvas: " + std::string(SDL_GetError()));
231 }
232 clear_canvas();
233
234 columns = std::max(1, (width + cell_w - 1) / cell_w);
235 rows = std::max(1, (height + cell_h - 1) / cell_h);
236 streams.assign(columns, {});
237 randomize_streams();
238 }

◆ resize() [2/2]

void matrix::Rain::resize ( mxvk::VK_Window & window)

Definition at line 221 of file rain.cpp.

221{ rebuild_for_extent(window); }

◆ set_opacity()

void matrix::Rain::set_opacity ( float opacity)

Definition at line 270 of file rain.cpp.

270{ opacity = std::clamp(value, 0.0f, 1.0f); }

◆ sprite()

mxvk::VK_Sprite * matrix::Rain::sprite ( ) const
nodiscard

Definition at line 299 of file rain.cpp.

299{ return rain_sprite; }

◆ surface()

SDL_Surface * matrix::Rain::surface ( ) const
nodiscard

Definition at line 301 of file rain.cpp.

301{ return canvas.get(); }

◆ sync_texture()

void matrix::Rain::sync_texture ( )

Definition at line 248 of file rain.cpp.

248 {
249 if (rain_sprite == nullptr || canvas == nullptr) {
250 return;
251 }
252 rain_sprite->updateTexture(canvas->pixels, canvas->w, canvas->h, canvas->pitch);
253 }

◆ update()

void matrix::Rain::update ( float dt)

Definition at line 240 of file rain.cpp.

240 {
241 dt = std::clamp(dt, 0.0f, 1.0f / 15.0f);
242 if (canvas == nullptr || glyphs.empty()) {
243 return;
244 }
245 update_rain(dt);
246 }

◆ update_and_render() [1/2]

void matrix::Rain::update_and_render ( mxvk::VK_Window & window)

Definition at line 279 of file rain.cpp.

279 {
280 const auto now = std::chrono::steady_clock::now();
281 float dt = std::chrono::duration<float>(now - last_frame).count();
282 last_frame = now;
283 resize(window);
284 update(dt);
285 render();
286 }
void update(float dt)
Definition rain.cpp:240

◆ update_and_render() [2/2]

void matrix::Rain::update_and_render ( mxvk::VK_Window & window,
int render_width,
int render_height )

Definition at line 288 of file rain.cpp.

288 {
289 const auto now = std::chrono::steady_clock::now();
290 float dt = std::chrono::duration<float>(now - last_frame).count();
291 last_frame = now;
292 resize(window);
293 update(dt);
294 render(render_width, render_height);
295 }

◆ width()

int matrix::Rain::width ( ) const
nodiscard

Definition at line 305 of file rain.cpp.

305{ return canvas != nullptr ? canvas->w : 0; }

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