Triangle¶
The simplest possible SimVX example. Override draw() on a Node2D to render
a coloured triangle using immediate-mode draw commands.
What You Will Learn¶
Node2D subclass – Create a custom node by subclassing
Node2Ddraw() override – Render shapes every frame via the
rendererparameterdraw_polygon() – Fill an arbitrary polygon with a solid colour
draw_text() – Render text at a given position
App – Launch a window and run a scene with
App.run()
How It Works¶
Triangle extends Node2D and overrides draw(renderer). Each frame the
renderer calls draw() and the node issues draw commands:
draw_polygon()takes a list of(x, y)vertices and a colour tupledraw_text()renders a string at a screen position with optional scale
The App creates a Vulkan-backed window and runs the scene tree containing
our single Triangle node.
Source Code¶
1#!/usr/bin/env python3
2"""Triangle -- Minimal Custom Draw Example
3
4[Play Demo](/demos/start_triangle.html)
5
6The simplest possible SimVX example. Override `draw()` on a Node2D to render
7a coloured triangle using immediate-mode draw commands.
8
9## What You Will Learn
10
11- **Node2D subclass** -- Create a custom node by subclassing `Node2D`
12- **draw() override** -- Render shapes every frame via the `renderer` parameter
13- **draw_polygon()** -- Fill an arbitrary polygon with a solid colour
14- **draw_text()** -- Render text at a given position
15- **App** -- Launch a window and run a scene with `App.run()`
16
17## How It Works
18
19`Triangle` extends `Node2D` and overrides `draw(renderer)`. Each frame the
20renderer calls `draw()` and the node issues draw commands:
21
221. `draw_polygon()` takes a list of `(x, y)` vertices and a colour tuple
232. `draw_text()` renders a string at a screen position with optional scale
24
25The `App` creates a Vulkan-backed window and runs the scene tree containing
26our single `Triangle` node.
27"""
28
29from simvx.core import Node2D
30from simvx.graphics import App
31
32WIDTH, HEIGHT = 800, 600
33
34
35class Triangle(Node2D):
36 def draw(self, renderer):
37 # Centred equilateral-ish triangle
38 cx, cy = WIDTH / 2, HEIGHT / 2
39 size = 200
40 vertices = [
41 (cx, cy - size), # top
42 (cx - size, cy + size), # bottom-left
43 (cx + size, cy + size), # bottom-right
44 ]
45 renderer.draw_polygon(vertices, colour=(0.2, 0.86, 0.47, 1.0))
46
47 renderer.draw_text("Hello, SimVX!", (cx - 90, cy + size + 30), scale=2, colour=(1.0, 1.0, 1.0))
48
49
50if __name__ == "__main__":
51 App(title="Triangle", width=WIDTH, height=HEIGHT).run(Triangle())