MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
main.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2## @file main.py
3## @brief Minimal textured-sprite MXVK Python example.
4## @details Creates a Python @c VK_Window subclass, loads a sprite and font,
5## stretches the sprite to the swapchain, and exits when Escape is pressed.
6## It is the smallest example of the native window-owned sprite lifetime model.
7##
8## @section sprite_run Running the example
9## @code{.sh}
10## python3 python-examples/sprite/main.py --width 1280 --height 720 --vsync
11## @endcode
12## @section sprite_controls Controls
13## - Escape: exit.
14## @section sprite_lifecycle Lifecycle
15## @c SpriteWindow releases its Python sprite handle before @c release() so the
16## native window/sprite reference relationship cannot form a shutdown cycle.
17
18import argparse
19from pathlib import Path
20import mxvk_ext as mxvk
21
22EXAMPLE_DIR = Path(__file__).resolve().parent
23DATA_DIR = EXAMPLE_DIR / "data"
24
25class SpriteWindow(mxvk.VK_Window):
26 def __init__(self, width: int, height: int, fullscreen: bool, enable_vsync: bool) -> None:
27 super().__init__("VK_Example", width, height, fullscreen, False, enable_vsync)
28 self.fallback_width = width
29 self.fallback_height = height
30 self.set_font(str(DATA_DIR / "font.ttf"), 24)
31 self.sprite = self.create_sprite(str(DATA_DIR / "intro.png"), str(DATA_DIR / "sprite.vert.spv"), str(DATA_DIR / "fragment.frag.spv"))
32
33 def close(self) -> None:
34 # Sprite is owned by the native window and its Python handle keeps this
35 # window alive. Drop the handle first so the two objects cannot form a
36 # shutdown-time reference cycle.
37 self.sprite = None
38 self.release()
39
40 def proc(self) -> None:
41 width, height = self.swapchain_extent
42 if width <= 0 or height <= 0:
43 width = self.fallback_width
44 height = self.fallback_height
45
46 self.sprite.draw_rect(0, 0, width, height)
47 self.print_text("Hello, World!", 15, 15, mxvk.Color(255, 255, 255, 255))
48
49 def event(self, event: mxvk.Event) -> None:
50 if event.type == mxvk.EVENT_KEY_DOWN and event.key == mxvk.KEY_ESCAPE:
51 self.request_exit()
52
53
54def parse_args() -> argparse.Namespace:
55 parser = argparse.ArgumentParser(description="MXVK Python sprite example")
56 parser.add_argument("--width", type=int, default=1280)
57 parser.add_argument("--height", type=int, default=720)
58 parser.add_argument("--fullscreen", action="store_true")
59 parser.add_argument("--vsync", action="store_true")
60 return parser.parse_args()
61
62
63def main() -> None:
64 args = parse_args()
65 window = SpriteWindow(args.width, args.height, args.fullscreen, args.vsync)
66 try:
67 window.loop()
68 finally:
69 window.close()
70
71if __name__ == "__main__":
72 main()
None close(self)
Definition main.py:75
None event(self, mxvk.Event event)
Definition main.py:129
None __init__(self, str filename, int width, int height, bool fullscreen, bool enable_vsync)
Definition main.py:34
Main Vulkan window wrapper for MXVK.
Definition mxvk.hpp:39
VkExtent2D swapchain_extent
Definition mxvk.hpp:616
void release()
Release Vulkan and SDL resources.
Definition mxvk.cpp:247
virtual void proc()
Execute one processing/update step.
Definition mxvk.cpp:1278
Definition main.py:1
int main()
Definition main.py:165
argparse.Namespace parse_args()
Definition main.py:155