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.cpp
Go to the documentation of this file.
1#include "mxvk/argz.hpp"
2#include "mxvk/mxvk.hpp"
4
5#include "rain.hpp"
6
7#include <SDL3/SDL.h>
8
9#include <algorithm>
10#include <chrono>
11#include <cstdlib>
12#include <format>
13#include <iostream>
14#include <memory>
15#include <string>
16
17namespace example {
18 namespace {
19 constexpr int MATRIX_SURFACE_WIDTH = 1280;
20 constexpr int MATRIX_SURFACE_HEIGHT = 720;
21 }
22
24 public:
25 MatrixWindow(const std::string &path,
26 const std::string &title,
27 const int width,
28 const int height,
29 const bool fullscreen,
30 const bool enable_vsync,
31 const bool binary,
32 const int font_size,
33 const std::string &font_path,
34 const std::string &color)
35 : mxvk::VK_Window(title, width, height, fullscreen, MXVK_VALIDATION, enable_vsync),
36 rain(std::make_unique<matrix::Rain>(
37 *this, [path, binary, font_size, font_path, color]() {
38 matrix::RainConfig config = matrix::make_matrix_rain_config(path.empty() ? std::string(matrix_ASSET_DIR) : path, binary);
39 config.surface_width = MATRIX_SURFACE_WIDTH;
40 config.surface_height = MATRIX_SURFACE_HEIGHT;
41 config.font_size = std::max(1, font_size);
42 if (!font_path.empty()) {
43 config.font_path = font_path;
44 }
45 config.color = color;
46 return config;
47 }())) {
48 setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
49 }
50
51 ~MatrixWindow() override {
52 }
53
54 void event(SDL_Event &e) override {
55 if (e.type == SDL_EVENT_KEY_DOWN) {
56 if (e.key.key == SDLK_ESCAPE) {
57 exit();
58 }
59 }
60
61 if (rain) {
62 rain->event(e);
63 }
64 }
65
66 void proc() override {
67 if (rain) {
68 const VkExtent2D extent = getSwapchainExtent();
69 rain->update_and_render(*this, static_cast<int>(extent.width), static_cast<int>(extent.height));
70 }
71 }
72
73 void onSwapchainRecreated() override {
74 if (rain) {
75 rain->resize(*this);
76 rain->on_swapchain_recreated(*this);
77 }
78 }
79
80 private:
81 std::unique_ptr<matrix::Rain> rain;
82 };
83} // namespace example
84
85int main(int argc, char **argv) {
86 try {
87 Arguments args = proc_args(argc, argv);
89 args.path, "-[ MXVK Matrix Digital Rain ]-", args.width, args.height, args.fullscreen, args.enable_vsync, args.binary,
90 args.font_size, args.font_path, args.color);
91 window.loop();
92 } catch (mxvk::Exception &e) {
93 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
94 return EXIT_FAILURE;
95 } catch (ArgException<std::string> &e) {
96 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
97 return EXIT_FAILURE;
98 }
99
100 return EXIT_SUCCESS;
101}
Lightweight, header-only, template command-line argument parser.
Arguments proc_args(int &argc, char **argv)
Parse standard libmx2 command-line options from main()'s argv.
Definition argz.hpp:872
Exception thrown by Argz::proc() on unrecognised or malformed options.
Definition argz.hpp:178
void proc() override
Execute one processing/update step.
Definition matrix.cpp:66
MatrixWindow(const std::string &path, const std::string &title, const int width, const int height, const bool fullscreen, const bool enable_vsync, const bool binary, const int font_size, const std::string &font_path, const std::string &color)
Definition matrix.cpp:25
~MatrixWindow() override
Definition matrix.cpp:51
void onSwapchainRecreated() override
Called after swapchain and render resources are recreated.
Definition matrix.cpp:73
void event(SDL_Event &e) override
Handle one SDL event.
Definition matrix.cpp:54
std::string text() const
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:37
VkExtent2D getSwapchainExtent() const noexcept
Get the current swapchain extent.
Definition mxvk.hpp:186
void loop()
Run the main event/render loop.
Definition mxvk.cpp:600
std::string font_path
Definition mxvk.hpp:568
void setClearColor(float r, float g, float b, float a=1.0f)
Set the per-frame color attachment clear color.
Definition mxvk.cpp:593
void exit()
Request loop termination.
Definition mxvk.cpp:1126
VK_Window()=default
Construct an empty window object.
int main(void)
Definition main.cpp:7
#define MXVK_VALIDATION
Definition mxvk.hpp:27
RainConfig make_matrix_rain_config(const std::string &asset_root, bool binary_glyph_mode)
Definition rain.cpp:157
Utilities for loading and saving PNG images.
Definition mxvk.hpp:30
Plain data structure returned by proc_args() with all common libmx2 CLI options.
Definition argz.hpp:730
std::string font_path
Optional font file path (--font-path).
Definition argz.hpp:762
bool fullscreen
Whether fullscreen mode was requested.
Definition argz.hpp:736
bool enable_vsync
Enable FIFO present mode / v-sync (--enable-vsync).
Definition argz.hpp:750
int height
Viewport height in pixels (default: 720).
Definition argz.hpp:733
std::string color
Optional rain RGB tint (--color).
Definition argz.hpp:763
std::string path
Asset root; proc_args() defaults it to the executable directory.
Definition argz.hpp:735
int width
Viewport width in pixels (default: 1280).
Definition argz.hpp:732
int font_size
Matrix rain font size in pixels (--font-size).
Definition argz.hpp:761
bool binary
Use binary glyphs only (--binary).
Definition argz.hpp:748
std::string color
Definition rain.hpp:19
std::string font_path
Definition rain.hpp:18