Triangle

Minimal custom-draw example.

▶ Run in browser

Tags: getting_started

The smallest possible SimVX scene. Subclass Node2D, override on_draw(), issue immediate-mode polygon and text commands against the renderer.

How it works

Triangle extends Node2D and overrides on_draw(renderer). Each frame the renderer calls on_draw() and the node issues draw commands:

  1. renderer.draw_polygon() takes a list of (x, y) vertices and a colour tuple.

  2. renderer.draw_text() renders a string at a screen position with optional scale.

App creates a Vulkan-backed window and runs the scene tree.

Source

 1"""Triangle: Minimal custom-draw example.
 2
 3The smallest possible SimVX scene. Subclass `Node2D`, override `on_draw()`,
 4issue immediate-mode polygon and text commands against the renderer.
 5
 6## How it works
 7
 8`Triangle` extends `Node2D` and overrides `on_draw(renderer)`. Each frame the
 9renderer calls `on_draw()` and the node issues draw commands:
10
111. `renderer.draw_polygon()` takes a list of `(x, y)` vertices and a colour tuple.
122. `renderer.draw_text()` renders a string at a screen position with optional scale.
13
14`App` creates a Vulkan-backed window and runs the scene tree.
15"""
16
17from simvx.core import Node2D
18from simvx.graphics import App
19
20WIDTH, HEIGHT = 800, 600
21
22
23class Triangle(Node2D):
24    def on_draw(self, renderer):
25        cx, cy = WIDTH / 2, HEIGHT / 2
26        size = 200
27        vertices = [
28            (cx, cy - size),
29            (cx - size, cy + size),
30            (cx + size, cy + size),
31        ]
32        renderer.draw_polygon(vertices, colour=(0.2, 0.86, 0.47, 1.0))
33        renderer.draw_text("Hello, SimVX!", (cx - 90, cy + size + 30), scale=2, colour=(1.0, 1.0, 1.0))
34
35
36if __name__ == "__main__":
37    App(title="Triangle", width=WIDTH, height=HEIGHT).run(Triangle())