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"
4#include <cstdlib>
5#include <format>
6#include <iostream>
7#include <string>
8
9namespace example {
10 class ExampleWindow : public mxvk::VK_Window {
11 std::string current_path = ".";
12 mxvk::VK_Sprite *sprite = nullptr;
13 int fallback_width = 1280;
14 int fallback_height = 720;
15
16 public:
17 ExampleWindow(const std::string path, const std::string &text, int width, int height, bool fullscreen, bool enable_vsync) : mxvk::VK_Window(text, width, height, fullscreen, MXVK_VALIDATION, enable_vsync) {
18 current_path = path.empty() ? std::string(sprite_example_ASSET_DIR) : path;
19 if (current_path == ".") {
20 current_path = sprite_example_ASSET_DIR;
21 }
22 setFont(current_path + "/data/font.ttf", 24);
23 fallback_width = width;
24 fallback_height = height;
25 const std::string image_path = current_path + "/data/intro.png";
26 const std::string vertex_shader = current_path + "/data/sprite.vert.spv";
27 const std::string fragment_shader = current_path + "/data/fragment.frag.spv";
28 sprite = createSprite(image_path, vertex_shader, fragment_shader);
29 }
30
31 void event(SDL_Event &e) override {
32 if (e.type == SDL_EVENT_KEY_DOWN && e.key.key == SDLK_ESCAPE) {
33 exit();
34 }
35 }
36
37 void proc() override {
38 if (sprite == nullptr) {
39 return;
40 }
41 int target_w = fallback_width;
42 int target_h = fallback_height;
43 if (swapchain_extent.width > 0U && swapchain_extent.height > 0U) {
44 target_w = static_cast<int>(swapchain_extent.width);
45 target_h = static_cast<int>(swapchain_extent.height);
46 }
47 sprite->drawSpriteRect(0, 0, target_w, target_h);
48 printText("Hello, World!", 15, 15, {255, 255, 255, 255});
49 }
50 };
51} // namespace example
52
53int main(int argc, char **argv) {
54 try {
55 Arguments args = proc_args(argc, argv);
56 example::ExampleWindow ex_window(args.path, "VK_Example", args.width, args.height, args.fullscreen, args.enable_vsync);
57 ex_window.loop();
58 } catch (mxvk::Exception &e) {
59 std::cerr << std::format("mxvk: Exception: {}\n", e.text());
60 return EXIT_FAILURE;
61 } catch (ArgException<std::string> &e) {
62 std::cerr << std::format("mxvk: Argument Exception: {}\n", e.text());
63 }
64 return EXIT_SUCCESS;
65}
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:31
void proc() override
Execute one processing/update step.
Definition main.cpp:37
ExampleWindow(const std::string path, const std::string &text, int width, int height, bool fullscreen, bool enable_vsync)
Definition main.cpp:17
std::string text() const
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
VkExtent2D swapchain_extent
Definition mxvk.hpp:495
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
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