Rendering

Functions for drawing text, shapes, images, gradients, and other visual elements.

render.create_font

Signature: render.create_font(font_path_or_name, size)

Description: Creates a font from a file path or system font name with the specified size. If a file path (e.g. "verdana.ttf") is provided, it loads directly. If only a name is provided (e.g. "Verdana"), it will try to load from C:/Windows/Fonts/.

Parameters:

  • font_path_or_name (string): Path to a .ttf font file or a system-installed font name.

  • size (number): The font size in points.

Returns:

  • handle: A font handle to be used with render.draw_text().

Example:

-- Create Verdana font from file
local font1 = render.create_font("verdana.ttf", 25)

-- Create Arial font from system fonts
local font2 = render.create_font("Arial", 20)

-- Draw text using created fonts
render.draw_text(font1, "Text from file font", 100, 100, 255, 255, 255, 255)
render.draw_text(font2, "Text from system font", 100, 150, 0, 255, 0, 255)

render.create_font_from_buffer

Signature: render.create_font_from_buffer(label, size, buffer_handle)

Description: Creates a font from a memory buffer. Buffer must be allocated using m.alloc.

Parameters:

  • label (string): Label/name for the font.

  • size (number): The font size in points.

  • buffer_handle (handle): Buffer handle containing font data.

Returns:

  • handle: A font handle to be used with render.draw_text().

Example:

-- Assume buffer is allocated with font data
local font = render.create_font_from_buffer("Verdana", 25, buffer_handle)

-- Draw text
render.draw_text(font, "Buffered font text", 200, 200, 255, 255, 0, 255)

render.get_viewport_size

Signature: render.get_viewport_size()

Description: Retrieves the current screen/viewport dimensions.

Parameters: None

Returns:

  • width (number): The width of the viewport in pixels.

  • height (number): The height of the viewport in pixels.

Example:

-- Get screen dimensions
local screen_w, screen_h = render.get_viewport_size()
engine.log("Viewport size: " .. screen_w .. "x" .. screen_h, 255, 255, 255, 255)

render.measure_text

Signature: render.measure_text(font_handle, text)

Description: Measures the width and height of text using the given font.

Parameters:

  • font_handle (handle): Font handle created with render.create_font() or render.create_font_from_buffer().

  • text (string): The text to measure.

Returns:

  • width (number): Width of the text in pixels.

  • height (number): Height of the text in pixels.

Example:

local font = render.create_font("verdana.ttf", 25)
local w, h = render.measure_text(font, "Measure me!")
render.draw_rectangle(100, 100, w, h, 0, 255, 0, 128, 1, true)
render.draw_text(font, "Measure me!", 100, 100, 255, 255, 255, 255)

render.draw_line

Signature: render.draw_line(x1, y1, x2, y2, r, g, b, a, thickness)

Description: Draws a line between two points with the specified color and thickness.

Parameters:

  • x1 (number): X-coordinate of the starting point.

  • y1 (number): Y-coordinate of the starting point.

  • x2 (number): X-coordinate of the ending point.

  • y2 (number): Y-coordinate of the ending point.

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • thickness (number): Line thickness in pixels.

Returns: None

Example:

-- White diagonal line
render.draw_line(500, 500, 600, 600, 255, 255, 255, 255, 5)

render.draw_rectangle

Signature: render.draw_rectangle(x, y, width, height, r, g, b, a, thickness, filled, rounding)

Description: Draws a rectangle with optional fill and rounding.

Parameters:

  • x (number): X-coordinate of the top-left corner.

  • y (number): Y-coordinate of the top-left corner.

  • width (number): Width of the rectangle in pixels.

  • height (number): Height of the rectangle in pixels.

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • thickness (number): Border thickness in pixels (ignored if filled is true).

  • filled (boolean): Whether to draw a filled rectangle (true) or just the outline (false).

  • rounding (number): Corner rounding radius in pixels.

Returns: None

Example:

-- Filled yellow rectangle
render.draw_rectangle(700, 700, 50, 50, 255, 255, 0, 255, 1, true)

-- Outlined red rectangle with rounded corners
render.draw_rectangle(300, 300, 100, 50, 255, 0, 0, 255, 2, false, 6)

render.draw_circle

Signature: render.draw_circle(x, y, radius, r, g, b, a, thickness, filled)

Description: Draws a circle with fill or outline.

Parameters:

  • x (number): X-coordinate of the center.

  • y (number): Y-coordinate of the center.

  • radius (number): Radius in pixels.

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • thickness (number): Border thickness in pixels (ignored if filled is true).

  • filled (boolean): Whether to draw a filled circle (true) or just the outline (false).

Returns: None

Example:

-- Filled red circle
render.draw_circle(900, 900, 25, 255, 0, 0, 255, 5, true)

render.draw_text

Signature: render.draw_text(font, text, x, y, r, g, b, a, outline_thickness, o_r, o_g, o_b, o_a)

Description: Draws text at position with optional outline.

Parameters:

  • font (handle): Font handle created with render.create_font() or render.create_font_from_buffer().

  • text (string): The text to draw.

  • x (number): X-coordinate of the text position.

  • y (number): Y-coordinate of the text position.

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • outline_thickness (number, optional): Thickness of the text outline (0 for no outline).

  • o_r (number, optional): Red color component for the outline (0-255).

  • o_g (number, optional): Green color component for the outline (0-255).

  • o_b (number, optional): Blue color component for the outline (0-255).

  • o_a (number, optional): Alpha component for the outline (0-255).

Returns: None

Example:

local font = render.create_font("verdana.ttf", 25)

-- Magenta with white outline
render.draw_text(font, "Test", 1000, 1000, 255, 0, 255, 255, 5, 255, 255, 255, 255)

render.draw_triangle

Signature: render.draw_triangle(x1, y1, x2, y2, x3, y3, r, g, b, a, thickness, filled)

Description: Draws a triangle connecting three points.

Parameters:

  • x1, y1 (number): Coordinates of the first point.

  • x2, y2 (number): Coordinates of the second point.

  • x3, y3 (number): Coordinates of the third point.

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • thickness (number): Border thickness in pixels (ignored if filled is true).

  • filled (boolean): Whether to draw a filled triangle (true) or just the outline (false).

Returns: None

Example:

-- Red triangle outline
render.draw_triangle(775, 825, 825, 825, 800, 775, 255, 0, 0, 255, 1, false)

render.get_fps

Signature: render.get_fps()

Description: Gets the current overlay FPS.

Parameters: None

Returns:

  • fps (number): The current frames per second.

Example:

local fps = render.get_fps()
engine.log("FPS: " .. fps, 255, 255, 255, 255)

render.create_bitmap_from_url

Signature: render.create_bitmap_from_url(url)

Description: Creates a bitmap from a URL.

Parameters:

  • url (string): The URL of the image to load.

Returns:

  • bitmap_handle: A handle to the created bitmap for use with rendering functions.

Example:

-- Create a bitmap from a URL
local bmp = render.create_bitmap_from_url("https://example.com/image.png")

render.create_bitmap_from_buffer

Signature: render.create_bitmap_from_buffer(buffer_handle)

Description: Creates a bitmap from a memory buffer handle.

Parameters:

  • buffer_handle (handle): Buffer handle containing image data.

Returns:

  • bitmap_handle: A handle to the created bitmap for use with rendering functions.

Example:

-- Given a buffer handle with image bytes
local bmp = render.create_bitmap_from_buffer(buffer_handle)

render.create_bitmap_from_file

Signature: render.create_bitmap_from_file(file_name)

Description: Creates a bitmap from a local file.

Parameters:

  • file_name (string): Path to the image file.

Returns:

  • bitmap_handle: A handle to the created bitmap for use with rendering functions.

Example:

local bmp = render.create_bitmap_from_file("images/logo.png")

render.clip_start

Signature: render.clip_start(x, y, width, height)

Description: Begins a clipping region. All subsequent rendering will be restricted to this rectangle until render.clip_end() is called.

Parameters:

  • x (number): X-coordinate of the clipping region's top-left corner.

  • y (number): Y-coordinate of the clipping region's top-left corner.

  • width (number): Width of the clipping region in pixels.

  • height (number): Height of the clipping region in pixels.

Returns: None

Example:

render.clip_start(100, 100, 300, 200)
-- draw clipped content here
render.clip_end()

render.clip_end

Signature: render.clip_end()

Description: Ends the active clipping region started by render.clip_start. Restores rendering output to the entire screen or parent context.

Parameters: None

Returns: None

Example:

-- Typically paired with clip_start; see that example.
render.clip_end()

render.draw_four_corner_gradient

Signature: render.draw_four_corner_gradient(x, y, width, height, r1, g1, b1, r2, g2, b2, r3, g3, b3, r4, g4, b4)

Description: Draws a rectangle filled with a four-corner gradient. Each corner has its own RGB color value (top-left, top-right, bottom-left, bottom-right), blended smoothly across the area.

Parameters:

  • x (number): X-coordinate of the top-left corner.

  • y (number): Y-coordinate of the top-left corner.

  • width (number): Width of the rectangle in pixels.

  • height (number): Height of the rectangle in pixels.

  • r1, g1, b1 (number): RGB color components for the top-left corner (0-255).

  • r2, g2, b2 (number): RGB color components for the top-right corner (0-255).

  • r3, g3, b3 (number): RGB color components for the bottom-left corner (0-255).

  • r4, g4, b4 (number): RGB color components for the bottom-right corner (0-255).

Returns: None

Example:

render.draw_four_corner_gradient(
    100, 100, 300, 200,
    255, 0, 0,   -- top-left red
    0, 255, 0,   -- top-right green
    0, 0, 255,   -- bottom-left blue
    255, 255, 0  -- bottom-right yellow
)

render.draw_polygon

Signature: render.draw_polygon(points_table, r, g, b, a, thickness, filled)

Description: Draws a polygon from a list of {x, y} points.

Parameters:

  • points_table (table): Array of points, each as {x, y} (minimum 3 points).

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • thickness (number): Border thickness in pixels (ignored if filled is true).

  • filled (boolean): Whether to draw a filled polygon (true) or just the outline (false).

Returns: None

Example:

local points = { {100,100}, {200,150}, {150,250}, {50,200} }
render.draw_polygon(points, 0, 255, 255, 255, 2, true)

render.draw_ellipse

Signature: render.draw_ellipse(x, y, rx, ry, r, g, b, a, thickness, filled)

Description: Draws an ellipse.

Parameters:

  • x (number): X-coordinate of the center.

  • y (number): Y-coordinate of the center.

  • rx (number): Radius along the X axis in pixels.

  • ry (number): Radius along the Y axis in pixels.

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • thickness (number): Border thickness in pixels (ignored if filled is true).

  • filled (boolean): Whether to draw a filled ellipse (true) or just the outline (false).

Returns: None

Example:

-- Cyan outlined ellipse
render.draw_ellipse(400, 300, 120, 80, 0, 255, 255, 255, 2, false)

render.draw_arc

Signature: render.draw_arc(x, y, rx, ry, start_angle, sweep_angle, r, g, b, a, thickness, filled)

Description: Draws an arc or pie slice.

Parameters:

  • x (number): X-coordinate of the center.

  • y (number): Y-coordinate of the center.

  • rx (number): Radius along the X axis in pixels.

  • ry (number): Radius along the Y axis in pixels.

  • start_angle (number): Starting angle in degrees.

  • sweep_angle (number): Angle span in degrees (positive = clockwise or implementation-defined).

  • r (number): Red color component (0-255).

  • g (number): Green color component (0-255).

  • b (number): Blue color component (0-255).

  • a (number): Alpha (transparency) component (0-255).

  • thickness (number): Border thickness in pixels (ignored if filled is true).

  • filled (boolean): Whether to draw a filled pie slice (true) or just the arc outline (false).

Returns: None

Example:

-- 90-degree pie slice
render.draw_arc(500, 400, 60, 60, 0, 90, 255, 128, 0, 255, 1, true)

render.draw_gradient_line

Signature: render.draw_gradient_line(x1, y1, x2, y2, color_table, thickness)

Description: Draws a line with gradient colors from start to end.

Parameters:

  • x1 (number): X-coordinate of the starting point.

  • y1 (number): Y-coordinate of the starting point.

  • x2 (number): X-coordinate of the ending point.

  • y2 (number): Y-coordinate of the ending point.

  • color_table (table): List of colors, each as {r, g, b, a}. Provide at least two entries (start and end).

  • thickness (number): Line thickness in pixels.

Returns: None

Example:

-- Gradient from red to blue
render.draw_gradient_line(100, 200, 400, 200, { {255,0,0,255}, {0,0,255,255} }, 3)

render.draw_gradient_rectangle

Signature: render.draw_gradient_rectangle(x, y, width, height, color_table, rounding)

Description: Draws a rectangle with gradient fill.

Parameters:

  • x (number): X-coordinate of the top-left corner.

  • y (number): Y-coordinate of the top-left corner.

  • width (number): Width of the rectangle in pixels.

  • height (number): Height of the rectangle in pixels.

  • color_table (table): List of colors, each as {r, g, b, a}. Typically two entries for a linear gradient.

  • rounding (number, optional): Corner rounding radius in pixels.

Returns: None

Example:

-- Vertical gradient yellow to magenta with rounded corners
render.draw_gradient_rectangle(200, 250, 180, 100, { {255,255,0,255}, {255,0,255,255} }, 8)

Example Usage

-- Create a font for rendering text
local font_handle = render.create_font("verdana.ttf", 25)

-- Callback function for rendering
function enginetick_callback()
    -- Draw a white diagonal line
    render.draw_line(500, 500, 600, 600, 255, 255, 255, 255, 5)

    -- Draw a filled yellow rectangle
    render.draw_rectangle(700, 700, 50, 50, 255, 255, 0, 255, 1, true)

    -- Draw a filled red circle
    render.draw_circle(900, 900, 25, 255, 0, 0, 255, 5, true)

    -- Draw text in magenta
    render.draw_text(font_handle, "Test", 1000, 1000, 255, 0, 255, 255, 5, 255, 0, 0, 0)

    -- Draw a red triangle
    render.draw_triangle(775, 825, 825, 825, 800, 775, 255, 0, 0, 255, 1, false)
end

engine.register_on_engine_tick(enginetick_callback)

Last updated

Was this helpful?