MXVK Vulkan Framework 0.24.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
main.cpp
Go to the documentation of this file.
1#include "mxvk/argz.hpp"
2#include "mxvk/mxvk.hpp"
5
6#include <chrono>
7#include <cstdlib>
8#include <format>
9#include <iostream>
10#include <string>
11
12namespace example {
14 public:
15 ConsoleDemoWindow(const std::string &path,
16 const std::string &title,
17 const int width,
18 const int height,
19 const bool fullscreen, bool enable_vsync)
20 : mxvk::VK_Window(title, width, height, fullscreen, MXVK_VALIDATION, enable_vsync) {
21 const std::string base_path = path.empty() ? std::string(console_demo_ASSET_DIR) : path;
22 const std::string sprite_vert_path = base_path + "/data/sprite.vert.spv";
23 const std::string shader_path = base_path + "/data/background_pulse.frag.spv";
24
25 background = createSprite(base_path + "/data/background.png", sprite_vert_path, shader_path);
26 setFont(base_path + "/data/font.ttf", 20);
27 console.attach(*this, base_path + "/data/font.ttf", 20);
28 console.setSpriteYOriginTopLeft(true);
29 console.setPrompt("mxvk> ");
30 console.printLine("Press F3 to open/close the console.");
31 console.printLine("Type 'help' for built-in commands.");
32
33 console.setCommandCallback([this](mxvk::VK_Window &, const std::vector<std::string> &args, std::ostream &out) {
34 if (args.empty()) {
35 return true;
36 }
37
38 if (args[0] == "echo") {
39 for (std::size_t i = 1; i < args.size(); ++i) {
40 if (i > 1) {
41 out << ' ';
42 }
43 out << args[i];
44 }
45 return true;
46 }
47
48 if (args[0] == "quit" || args[0] == "exit") {
49 out << "Closing window...";
50 exit();
51 return true;
52 }
53
54 if (args[0] == "about") {
55 out << "console_demo: MXVK Vulkan console sample.\n(C) 2026 LostSideDead Software\n";
56 return true;
57 }
58
59 return false;
60 });
61 }
62
63 void event(SDL_Event &e) override {
64 console.handleEvent(e);
65
66 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE && !console.isVisible()) {
67 exit();
68 }
69 }
70
71 void proc() override {
72 if (background != nullptr) {
73 const VkExtent2D extent = getSwapchainExtent();
74 const int target_w = static_cast<int>(extent.width);
75 const int target_h = static_cast<int>(extent.height);
76 if (target_w > 0 && target_h > 0) {
77 const float elapsed = std::chrono::duration<float>(std::chrono::steady_clock::now() - start).count();
78 background->setShaderParams(elapsed, 0.0f, 0.0f, 0.0f);
79 background->drawSpriteRect(0, 0, target_w, target_h);
80 }
81 }
82
83 if (!console.isVisible()) {
84 printText("MXVK Console Demo", 14, 12, SDL_Color{255, 255, 255, 255});
85 printText("Press F3 to toggle console. Press ESC to quit when console is hidden.",
86 14,
87 38,
88 SDL_Color{180, 180, 220, 255});
89 }
90 console.draw();
91 }
92
93 private:
94 std::chrono::steady_clock::time_point start{std::chrono::steady_clock::now()};
95 mxvk::VK_Sprite *background = nullptr;
97 };
98} // namespace example
99
100int main(int argc, char **argv) {
101 try {
102 Arguments args = proc_args(argc, argv);
104 "MXVK_Console_Demo",
105 args.width,
106 args.height,
107 args.fullscreen, args.enable_vsync);
108 window.loop();
109 } catch (mxvk::Exception &e) {
110 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
111 return EXIT_FAILURE;
112 } catch (ArgException<std::string> &e) {
113 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
114 return EXIT_FAILURE;
115 }
116
117 return EXIT_SUCCESS;
118}
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 event(SDL_Event &e) override
Handle one SDL event.
Definition main.cpp:63
ConsoleDemoWindow(const std::string &path, const std::string &title, const int width, const int height, const bool fullscreen, bool enable_vsync)
Definition main.cpp:15
void proc() override
Execute one processing/update step.
Definition main.cpp:71
std::string text() const
Lightweight command console renderer for mxvk::VK_Window.
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
VK_Sprite * createSprite(const std::string &pngPath, const std::string &vertexShaderPath="", const std::string &fragmentShaderPath="")
Create a sprite from a PNG file and register it with this window.
Definition mxvk.cpp:3477
void exit()
Request loop termination.
Definition mxvk.cpp:1126
VK_Window()=default
Construct an empty window object.
void setFont(const std::string &fontPath, int fontSize=24)
Set the active text-render font.
Definition mxvk.cpp:2991
void printText(const std::string &text, int x, int y, const SDL_Color &col)
Queue a text string for rendering during the current frame.
Definition mxvk.cpp:3018
int main(void)
Definition main.cpp:7
#define MXVK_VALIDATION
Definition mxvk.hpp:27
In-game command console for MXVK / Vulkan windows.
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
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 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