MXVK Vulkan Framework 0.24.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)
Rainoperator= (const Rain &)=delete
Rainoperator= (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_Spritesprite () 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 166 of file rain.cpp.

167 : Rain(std::move(rain_config), SurfaceMode::window_driven) {
168 resize(window);
169 }
void resize(mxvk::VK_Window &window)
Definition rain.cpp:243
Rain(mxvk::VK_Window &window, RainConfig config)
Definition rain.cpp:166

◆ Rain() [2/6]

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

Definition at line 171 of file rain.cpp.

172 : 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 220 of file rain.cpp.

221 : 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:157

◆ Rain() [4/6]

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

Definition at line 223 of file rain.cpp.

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

◆ ~Rain()

matrix::Rain::~Rain ( )

Definition at line 226 of file rain.cpp.

226 {
227 glyphs.clear();
228 font.reset();
229 background.reset();
230 canvas.reset();
231 if (ttf_acquired) {
232 release_ttf();
233 }
234 }

◆ 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 153 of file rain.cpp.

153 {
154 return {U'0', U'1'};
155 }

◆ event()

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

Definition at line 236 of file rain.cpp.

236 {
237 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_SPACE) {
238 randomize_streams();
239 clear_canvas();
240 }
241 }

◆ full_symbol_set()

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

Definition at line 142 of file rain.cpp.

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

◆ head_color()

SDL_Color matrix::Rain::head_color ( )
static

Definition at line 138 of file rain.cpp.

138 {
139 return SDL_Color{208, 255, 200, 255};
140 }

◆ height()

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

Definition at line 343 of file rain.cpp.

343 {
344 return canvas != nullptr ? canvas->h : 0;
345 }

◆ matrix_color()

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

Definition at line 128 of file rain.cpp.

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

◆ on_swapchain_recreated()

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

Definition at line 323 of file rain.cpp.

323 {
324 resize(window);
325 }

◆ 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 347 of file rain.cpp.

347 {
348 return canvas != nullptr ? canvas->pitch : 0;
349 }

◆ pixels()

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

Definition at line 335 of file rain.cpp.

335 {
336 return canvas != nullptr ? canvas->pixels : nullptr;
337 }

◆ render() [1/2]

void matrix::Rain::render ( )

Definition at line 279 of file rain.cpp.

279 {
280 if (canvas == nullptr) {
281 return;
282 }
283 render(canvas->w, canvas->h);
284 }
void render()
Definition rain.cpp:279

◆ render() [2/2]

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

Definition at line 286 of file rain.cpp.

286 {
287 sync_texture();
288 if (rain_sprite == nullptr || canvas == nullptr) {
289 return;
290 }
291 rain_sprite->drawSpriteRect(0, 0, width, height);
292 }
int height() const
Definition rain.cpp:343
void sync_texture()
Definition rain.cpp:272
int width() const
Definition rain.cpp:339

◆ reset()

void matrix::Rain::reset ( )

Definition at line 298 of file rain.cpp.

298 {
299 clear_canvas();
300 randomize_streams();
301 opacity = 1.0f;
302 last_frame = std::chrono::steady_clock::now();
303 }

◆ resize() [1/2]

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

Definition at line 247 of file rain.cpp.

247 {
248 if (width <= 0 || height <= 0 || (canvas != nullptr && canvas->w == width && canvas->h == height)) {
249 return;
250 }
251
252 canvas.reset(SDL_CreateSurface(width, height, SDL_PIXELFORMAT_RGBA32));
253 if (canvas == nullptr) {
254 throw mxvk::Exception("Failed to create matrix canvas: " + std::string(SDL_GetError()));
255 }
256 clear_canvas();
257
258 columns = std::max(1, (width + cell_w - 1) / cell_w);
259 rows = std::max(1, (height + cell_h - 1) / cell_h);
260 streams.assign(columns, {});
261 randomize_streams();
262 }

◆ resize() [2/2]

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

Definition at line 243 of file rain.cpp.

243 {
244 rebuild_for_extent(window);
245 }

◆ set_opacity()

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

Definition at line 294 of file rain.cpp.

294 {
295 opacity = std::clamp(value, 0.0f, 1.0f);
296 }

◆ sprite()

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

Definition at line 327 of file rain.cpp.

327 {
328 return rain_sprite;
329 }

◆ surface()

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

Definition at line 331 of file rain.cpp.

331 {
332 return canvas.get();
333 }

◆ sync_texture()

void matrix::Rain::sync_texture ( )

Definition at line 272 of file rain.cpp.

272 {
273 if (rain_sprite == nullptr || canvas == nullptr) {
274 return;
275 }
276 rain_sprite->updateTexture(canvas->pixels, canvas->w, canvas->h, canvas->pitch);
277 }

◆ update()

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

Definition at line 264 of file rain.cpp.

264 {
265 dt = std::clamp(dt, 0.0f, 1.0f / 15.0f);
266 if (canvas == nullptr || glyphs.empty()) {
267 return;
268 }
269 update_rain(dt);
270 }

◆ update_and_render() [1/2]

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

Definition at line 305 of file rain.cpp.

305 {
306 const auto now = std::chrono::steady_clock::now();
307 float dt = std::chrono::duration<float>(now - last_frame).count();
308 last_frame = now;
309 resize(window);
310 update(dt);
311 render();
312 }
void update(float dt)
Definition rain.cpp:264

◆ update_and_render() [2/2]

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

Definition at line 314 of file rain.cpp.

314 {
315 const auto now = std::chrono::steady_clock::now();
316 float dt = std::chrono::duration<float>(now - last_frame).count();
317 last_frame = now;
318 resize(window);
319 update(dt);
320 render(render_width, render_height);
321 }

◆ width()

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

Definition at line 339 of file rain.cpp.

339 {
340 return canvas != nullptr ? canvas->w : 0;
341 }

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