MXVK Vulkan Framework 0.35.0
C++20 Vulkan rendering framework for practical 2D and 3D application development with SDL3.
Loading...
Searching...
No Matches
app.py
Go to the documentation of this file.
1## @file app.py
2## @brief Application and window helpers.
3
4from pathlib import Path
5from typing import Any
6
7from ._native import mxvk
8
9Color = mxvk.Color
10"""Alias for MXVK's RGBA color class."""
11
12
13class App(mxvk.Window):
14 ## @brief A simple callback-driven MXVK application.
15 ## @details Subclass this class and override @c draw and optionally
16 ## @c on_event. Managed sprites and models are released before the window.
17
18 def __init__(self, title: str = "MXVK", width: int = 1280, height: int = 720, *, fullscreen: bool = False, vsync: bool = True, validation: bool = False, shader_directory: str | Path | None = None) -> None:
19 ## @brief Create a Vulkan window with practical Python defaults.
20 ## @param title Text displayed in the window title bar.
21 ## @param width Initial window width in pixels.
22 ## @param height Initial window height in pixels.
23 ## @param fullscreen Start in fullscreen mode when true.
24 ## @param vsync Synchronize presentation with the display when true.
25 ## @param validation Enable Vulkan validation layers when true.
26 ## @param shader_directory Optional directory containing shared shaders.
27 if shader_directory is not None:
28 mxvk.set_default_shader_directory(str(shader_directory))
29 present_mode = mxvk.PresentModePreference.vsync if vsync else mxvk.PresentModePreference.low_latency
30 super().__init__(title, width, height, fullscreen, validation, present_mode, mxvk.RuntimeMode.windowed)
31 self._managed: list[Any] = []
32 self._closed = False
33
34 def draw(self) -> None:
35 ## @brief Draw one frame.
36 ## @details Override this method; queue sprite drawing here.
37 pass
38
39 def on_event(self, event: mxvk.Event) -> None:
40 ## @brief Receive one SDL input event.
41 ## @param event Read-only MXVK event data.
42 pass
43
44 def proc(self) -> None:
45 ## @brief Native render-loop callback that forwards to @c draw.
46 self.draw()
47
48 def event(self, event: mxvk.Event) -> None:
49 ## @brief Native event-loop callback that forwards to @c on_event.
50 self.on_event(event)
51
52 def add(self, resource: Any) -> Any:
53 ## @brief Keep a wrapper resource alive for the application's lifetime.
54 ## @param resource A wrapper created for this application.
55 ## @return The supplied resource, for convenient inline use.
56 self._managed.append(resource)
57 return resource
58
59 def draw_text(self, text: str, x: int, y: int, font: Any, color: Color = Color(255, 255, 255, 255)) -> None:
60 ## @brief Queue text using a supplied font without changing the default font.
61 ## @param text Text to draw.
62 ## @param x Left pixel coordinate.
63 ## @param y Top pixel coordinate.
64 ## @param font A wrapper @c Font created from a font file and size.
65 ## @param color RGBA text color.
66 ## @details This calls MXVK's per-draw font overload and does not call
67 ## @c set_font, so existing default-font text remains unchanged.
68 self.print_text(text, x, y, color, font.native)
69
70 def text_size(self, text: str, font: Any) -> tuple[int, int] | None:
71 ## @brief Measure text using a supplied font without changing the default font.
72 ## @param text Text to measure.
73 ## @param font A wrapper @c Font created from a font file and size.
74 ## @return Width and height in pixels, or @c None when measurement fails.
75 return self.get_text_dimensions(text, font.native)
76
77 def run(self) -> None:
78 ## @brief Run until @c quit is called or the window is closed.
79 try:
80 self.loop()
81 finally:
82 self.close()
83
84 def quit(self) -> None:
85 ## @brief Request that the render loop exit after the current frame.
86 self.request_exit()
87
88 def close(self) -> None:
89 ## @brief Safely release managed objects and Vulkan window resources.
90 if self._closed:
91 return
92 self._closed = True
93 try:
94 self.wait_idle()
95 for resource in reversed(self._managed):
96 close = getattr(resource, "close", None)
97 if close is not None:
98 close()
99 finally:
100 self._managed.clear()
101 self.release()
102
103 def __enter__(self) -> "App":
104 ## @brief Enter a context manager owning this application.
105 return self
106
107 def __exit__(self, *_: object) -> None:
108 ## @brief Close the application when its context exits.
109 self.close()
None quit(self)
Definition app.py:84
bool _closed
Safely release managed objects and Vulkan window resources.
Definition app.py:32
None run(self)
Definition app.py:77
None __init__(self, str title="MXVK", int width=1280, int height=720, *, bool fullscreen=False, bool vsync=True, bool validation=False, str|Path|None shader_directory=None)
A simple callback-driven MXVK application.
Definition app.py:18
None __exit__(self, *object _)
Definition app.py:107
Any add(self, Any resource)
Definition app.py:52
None on_event(self, mxvk.Event event)
Definition app.py:39
None draw(self)
Definition app.py:34
None close(self)
Definition app.py:88
list _managed
Safely release managed objects and Vulkan window resources.
Definition app.py:31
None event(self, mxvk.Event event)
Definition app.py:48
None draw_text(self, str text, int x, int y, Any font, Color color=Color(255, 255, 255, 255))
Definition app.py:59
"App" __enter__(self)
Definition app.py:103
None proc(self)
Definition app.py:44
tuple[int, int]|None text_size(self, str text, Any font)
Definition app.py:70