Skip to Content

render

2D and world-space drawing: text, fonts, lines, rectangles, circles, polygons, clipping, textures and screen queries, and when drawing is available.


Drawing is available while the frame's draw list is being recorded: in render callbacks and the frame's resize_window, key and timer processing. Draw calls from createmove, frame_stage, game events and override_view are silently discarded.

Queries (screen_size, frame_count, frame_time, calc_text_size, world_to_screen), texture setup functions and load_font may be called outside this drawing phase.

Coordinates are window pixels measured from the top-left. Screen positions are 2D vectors; world positions are 3D vectors. Non-finite drawing coordinates produce no output; point lists skip the invalid pieces. A non-finite thickness or radius raises thickness must be a finite number / radius must be a finite number. Thickness defaults to 1 and is truncated to a whole number for 2D shapes, where below 1 produces no output (bezier_cubic draws at least 1; circle_3d keeps fractions). Rounding defaults to 0 and is clamped to half the smaller rectangle side. A points table whose elements are not all 2D vectors raises expected a table of vector2d.

Queries

FunctionReturnsDescription
render.screen_size()vector2dWindow dimensions in pixels.
render.frame_count()numberRendered-frame count.
render.frame_time()numberSeconds since the previous frame.
render.world_to_screen(world)vector2d or nilProjects a 3D point with the current frame's camera, in the same window pixels render.screen_size() measures (also in Fullscreen Windowed at a custom resolution). Returns nil behind or immediately in front of the camera; off-screen points still return coordinates. Outside drawing it uses the last drawn frame's camera.
render.calc_text_size(text, size?, font?)vector2dDimensions of text rendered by render.text with the same size and font. size defaults to the font's own size; font (a font_t from render.load_font) defaults to the menu font. A font that is neither nil nor a font_t raises expected font_t or nil.

Each call returns a new copy; modifying it does not affect anything else.

render.text

render.text(text, pos, color?, size?, font?)

Draws text. The default colour is white; the default size is the font's own size (the menu font's size without font). font is a font_t from render.load_font; nil or absent draws in the menu font, and any other value raises expected font_t or nil. A color or size of another type reads as its default.

Returns: no values.

render.load_font

render.load_font(name_or_path, size)

Loads a font for render.text and render.calc_text_size.

  • name_or_path ending in .ttf, .otf or .ttc (any case) is a file in the script's data directory, resolved exactly as fs resolves a path (same path restrictions; write the file there with fs.write or copy it in). A .ttc collection loads its first face.
  • Anything else is the family name of an installed font, for example 'Arial' or 'Segoe UI', in its regular style. The name must match the installed family (case-insensitive): where Windows would substitute another family, the load is refused. A family that lives only inside a .ttc collection cannot be loaded by name.
  • size is the font's size in pixels, from 1 to 256, and the default size of text drawn with it; another value raises load_font: size must be a number from 1 to 256.
  • Glyphs are taken from the file for Latin, Latin-1, Cyrillic, Vietnamese and general punctuation (the menu font's ranges, without CJK) only.
  • A newly loaded font draws from the next frame on: until then render.text draws nothing with it and render.calc_text_size measures 0, 0.

Returns: font_t, or nil, reason. Reasons: a script loads 16 fonts at most, no data directory, path, not found, file too large (over 64 MiB), read failed, not a TrueType or OpenType font, no installed font family by that name, the family is inside a font collection (.ttc); load the .ttc file by path, the font could not be created.

A script holds at most 16 fonts. Fonts belong to the script and are freed on unload; there is no way to free one earlier.

Lines and rectangles

render.line(from, to, color, thickness?)
render.rect(pos, size, color, rounding?)
render.rect_outline(pos, size, color, thickness?, rounding?)
render.rect_filled(from, to, color, rounding?)
render.rect_filled_fade(from, to, top_left, top_right, bottom_right, bottom_left)
  • line: draws a line; thickness defaults to 1.
  • rect: fills the rectangle from pos to pos + size.
  • rect_outline: draws an outline using position and size.
  • rect_filled: fills a corner-to-corner rectangle.
  • rect_filled_fade: fills a rectangle with four corner colours in the order shown.

Returns: no values.

Circles and arcs

render.circle(center, color, radius, start_degrees?, fraction?)
render.circle(center, radius, segments, color, thickness?)
render.circle_filled(center, radius, segments, color)
render.circle_fade(center, radius, color_inner, color_outer)
render.arc(center, radius, from_radians, to_radians, segments, color, thickness?)

The first circle form draws a filled disc, or a filled sector when fraction is below 1. start_degrees defaults to 0; fraction defaults to 1 and is clamped to 0..1, where 0 draws nothing. Angle 0 points right and increases clockwise. The second form, selected by a numeric second argument, draws an outline.

circle_filled draws a filled disc. circle_fade draws a radial gradient. arc takes angles in radians, unlike the filled-sector form of circle; its sweep is clamped to one turn either way, and equal angles draw nothing.

The segments argument is required where shown but ignored.

Returns: no values.

Polygons and curves

render.polygon(points, color)
render.filled_polygon(points, color)
render.concave_polygon(points, color)
render.poly_line(points, color, thickness?, closed?)
render.bezier_cubic(p1, p2, p3, p4, color, thickness?)

The three polygon functions fill a table of at least three 2D points. Concave polygons must not self-intersect.

poly_line joins the supplied points; closed defaults to true, joining the last point to the first. Pass false for an open line. bezier_cubic draws a screen-space cubic Bézier curve.

Returns: no values.

World-space circles

render.circle_3d(center, radius, color, thickness?, normal?)
render.circle_filled_3d(center, radius, color, normal?)
render.circle_fade_3d(center, radius, color_inner, color_outer, normal?)

Draw an outline, filled circle or radial gradient in world space. Without normal, the circle lies flat on the ground. A circle is not drawn if any part is behind the camera, or if radius is below 0.5.

Returns: no values.

Clipping

render.push_clip_rect(from, to, intersect?)
render.pop_clip_rect()

Restricts subsequent drawing to the corner-to-corner rectangle; the corners are not reordered, so a reversed rectangle clips everything. intersect = true intersects it with the current clip; otherwise (the default) it replaces the clip. pop_clip_rect with nothing pushed does nothing. Unmatched pushes are popped at the end of the frame.

Returns: no values.

Textures

render.setup_texture(filename)
render.setup_texture_rgba(pixels, size)
render.setup_texture_from_memory(bytes)
render.texture(texture, from, to, tint?, rounding?)
  • setup_texture: loads PNG, JPEG or the first frame of a GIF. Relative paths use the game's working directory. An unreadable or empty file returns nil.
  • setup_texture_rgba: accepts a table of exactly w * h packed pixels, row by row. Each integer is r + g*256 + b*65536 + a*16777216. size must be whole numbers from 1 to 16384 (setup_texture_rgba: size must be whole numbers from 1 to 16384), and a table of another length raises setup_texture_rgba: the table must hold exactly w * h pixels.
  • setup_texture_from_memory: accepts a table of encoded PNG/JPEG/GIF bytes, not a Lua string. An empty table returns nil.
  • texture: draws corner to corner; tint defaults to white. A newly created texture becomes drawable on the next frame at the earliest. Data that does not decode still returns a texture_t, which never draws.

Returns: setup functions return texture_t, or nil without a reason for an unreadable file or an empty setup_texture_from_memory table; texture returns no values.

Textures belong to the script and are freed on unload.

On this page