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.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 } // namespace
22
24 public:
25 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)
26 : mxvk::VK_Window(title, width, height, fullscreen, MXVK_VALIDATION, enable_vsync), rain(std::make_unique<matrix::Rain>(*this, [path, binary, font_size, font_path, color]() {
27 matrix::RainConfig config = matrix::make_matrix_rain_config(path.empty() ? std::string(matrix_ASSET_DIR) : path, binary);
28 config.surface_width = MATRIX_SURFACE_WIDTH;
29 config.surface_height = MATRIX_SURFACE_HEIGHT;
30 config.font_size = std::max(1, font_size);
31 if (!font_path.empty()) {
32 config.font_path = font_path;
33 }
34 config.color = color;
35 return config;
36 }())) {
37 setClearColor(0.0f, 0.0f, 0.0f, 1.0f);
38 }
39
40 ~MatrixWindow() override {}
41
42 void event(SDL_Event &e) override {
43 if (e.type == SDL_EVENT_KEY_DOWN) {
44 if (e.key.key == SDLK_ESCAPE) {
45 exit();
46 }
47 }
48
49 if (rain) {
50 rain->event(e);
51 }
52 }
53
54 void proc() override {
55 if (rain) {
56 const VkExtent2D extent = getSwapchainExtent();
57 rain->update_and_render(*this, static_cast<int>(extent.width), static_cast<int>(extent.height));
58 }
59 }
60
61 void onSwapchainRecreated() override {
62 if (rain) {
63 rain->resize(*this);
64 rain->on_swapchain_recreated(*this);
65 }
66 }
67
68 private:
69 std::unique_ptr<matrix::Rain> rain;
70 };
71} // namespace example
72
73int main(int argc, char **argv) {
74 try {
75 Arguments args = proc_args(argc, argv);
76 example::MatrixWindow window(args.path, "-[ MXVK Matrix Digital Rain ]-", args.width, args.height, args.fullscreen, args.enable_vsync, args.binary, args.font_size, args.font_path, args.color);
77 window.loop();
78 } catch (mxvk::Exception &e) {
79 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
80 return EXIT_FAILURE;
81 } catch (ArgException<std::string> &e) {
82 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
83 return EXIT_FAILURE;
84 }
85
86 return EXIT_SUCCESS;
87}
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:854
Exception thrown by Argz::proc() on unrecognised or malformed options.
Definition argz.hpp:169
void proc() override
Execute one processing/update step.
Definition matrix.cpp:54
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:40
void onSwapchainRecreated() override
Called after swapchain and render resources are recreated.
Definition matrix.cpp:61
void event(SDL_Event &e) override
Handle one SDL event.
Definition matrix.cpp:42
std::string text() const
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:39
VkExtent2D getSwapchainExtent() const noexcept
Get the current swapchain extent.
Definition mxvk.hpp:222
void loop()
Run the main event/render loop.
Definition mxvk.cpp:651
std::string font_path
Definition mxvk.hpp:714
void setClearColor(float r, float g, float b, float a=1.0f)
Set the per-frame color attachment clear color.
Definition mxvk.cpp:637
void exit()
Request loop termination.
Definition mxvk.cpp:1378
VK_Window()=default
Construct an empty window object.
#define MXVK_VALIDATION
Definition mxvk.hpp:28
int main()
Definition main.py:165
RainConfig make_matrix_rain_config(const std::string &asset_root, bool binary_glyph_mode)
Definition rain.cpp:142
Utilities for loading and saving PNG images.
Definition mxvk.hpp:31
Plain data structure returned by proc_args() with all common libmx2 CLI options.
Definition argz.hpp:718
std::string font_path
Optional font file path (--font-path).
Definition argz.hpp:750
bool fullscreen
Whether fullscreen mode was requested.
Definition argz.hpp:724
bool enable_vsync
Enable FIFO present mode / v-sync (--enable-vsync).
Definition argz.hpp:738
int height
Viewport height in pixels (default: 720).
Definition argz.hpp:721
std::string color
Optional rain RGB tint (--color).
Definition argz.hpp:751
std::string path
Asset root; proc_args() defaults it to the executable directory.
Definition argz.hpp:723
int width
Viewport width in pixels (default: 1280).
Definition argz.hpp:720
int font_size
Matrix rain font size in pixels (--font-size).
Definition argz.hpp:749
bool binary
Use binary glyphs only (--binary).
Definition argz.hpp:736
std::string color
Definition rain.hpp:19
std::string font_path
Definition rain.hpp:18