Module ark_api::render

source Ā·
Expand description

šŸ–¼ļø Render API

Immediate mode rendering API for drawing 2D and 3D meshes on screen.

Unlike the World API, which has persistent entities, in the Render API you list everything that should be drawn every frame, hence immediate mode.

The Render API supports the creation of textures through a TextureBuilder. Textures are created from image data and can be ā€œwrapped aroundā€ surfaces to add another level of artistic control to your game.

Examples

Given a valid texture named texture created with the TextureBuilder, you can draw a textured 2D square in screen space like so:

use ark_api::render::{Rectangle};
use ark_api::{Vec2, ColorRgba8};

// We draw two triangles, reusing some of the vertices:
let indices = [
    [0, 1, 2], // first triangle
    [1, 2, 3], // second triangle
];

// We can specify a region to which to clip our triangles.
// In this case, we don't want to clip.
let clip_rect = Rectangle {
    min_x: 0.0,
    min_y: 0.0,
    max_x: 1_000_000.0,
    max_y: 1_000_000.0,
};

// Render API expects positions in physical pixel coordinates,
// so we multiply our local coordinates with dpi_factor:
let dpi_factor = applet().window_state().dpi_factor;

let positions = [
    dpi_factor * Vec2::new(50.0, 50.0),   // 0
    dpi_factor * Vec2::new(50.0, 300.0),  // 1
    dpi_factor * Vec2::new(300.0, 50.0),  // 2
    dpi_factor * Vec2::new(300.0, 300.0), // 3
];
let colors = [
    ColorRgba8([255, 0, 0, 255]),     // 0
    ColorRgba8([0, 255, 0, 255]),     // 1
    ColorRgba8([0, 0, 255, 255]),     // 2
    ColorRgba8([255, 255, 255, 255]), // 3
];

let uvs = [
    Vec2::new(0.0, 0.0), // 0
    Vec2::new(0.0, 1.0), // 1
    Vec2::new(1.0, 0.0), // 2
    Vec2::new(1.0, 1.0), // 3
];

render().draw_textured_triangles(&clip_rect, &texture, &indices, &positions, &colors, &uvs)?;

Structs

Enums

Functions

Type Definitions