Triangle

Play Demo

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 Node2D

  • draw() override – Render shapes every frame via the renderer parameter

  • draw_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:

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

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