Like2D Logo Like2D
← Back to Docs

Rendering

All functions for drawing images, text, shapes, and managing visual output in Like2D.

Screen & Images

clearScreen(red, green, blue)

Clears the screen with the specified RGB color.

Parameters:

clearScreen(0, 0, 0)        -- Black
clearScreen(255, 255, 255)  -- White
clearScreen(100, 149, 237)  -- Cornflower blue

loadImage(filename)

Loads an image from file; the filename is used as the key. Automatically tries encrypted files first (with L2DE header), then game.like archive, then the project folder.

Parameters:

Returns:

if loadImage("assets/player.png") then
    print("Player image loaded successfully")
else
    print("Failed to load player image")
end

renderImage(key, x, y, width, height)

Renders a previously loaded image at the specified position.

Parameters:

Returns:

renderImage("assets/player.png", 100, 200, 64, 64)

Advanced Image Rendering

renderImageEx(key, x, y, w, h, angle, flipX, flipY, alpha, originX, originY)

Renders image with rotation, flipping, transparency, and scaling.

renderImageEx("player", 400, 300, 64, 64, 45, true, false, 0.5, 0.5, 0.5)

renderImageRegion(key, dx, dy, dw, dh, sx, sy, sw, sh, angle, flipX, flipY, alpha, originX, originY)

Renders a sub-rectangle from a sprite sheet.

renderImageRegion("spritesheet", 400, 300, 64, 64, 0, 0, 32, 32, 0, false, false, 1.0, 0.5, 0.5)

getImageSize(key)

Returns loaded image dimensions (width, height).

local w, h = getImageSize("player")

Text Rendering

loadFont(filename, size)

Loads a font file for text rendering; the filename is used as the key.

Parameters:

Returns:

if loadFont("fonts/arial.ttf", 24) then
    print("Font loaded successfully")
end

drawText(text, fontKey, x, y, red, green, blue)

Draws text at the specified position using a preloaded font.

Parameters:

drawText("Hello World!", "fonts/arial.ttf", 10, 10, 255, 255, 255)

setDefaultFont(filename)

Sets the default font used by drawTextDirect(). If you don't call this, Like2D will automatically try to find a system font (Segoe UI on Windows, DejaVu on Linux).

Parameters:

setDefaultFont("fonts/myfont.ttf")

drawTextDirect(text, x, y, size, red, green, blue, alpha, anchor)

Draws text without preloading a font. Uses the default font set by setDefaultFont(), or automatically detects a system font if none is set. Easiest way to render text.

Parameters:

drawTextDirect("Hello World!", 10, 10, 48, 255, 255, 255, 255)
drawTextDirect("Score: " .. score, 10, 50, 24, 255, 255, 0, 255)

-- Center-aligned text
drawTextDirect("Centered!", 400, 300, 32, 255, 255, 255, 255, 4)

getTextSize(text, size)

Returns the pixel dimensions of a string when rendered with the default font at the given size. Useful for centering text, positioning UI elements, or checking text layout before drawing.

Parameters:

Returns:

local w, h = getTextSize("Hello!", 48)
drawTextDirect("Hello!", 400 - w/2, 300 - h/2, 48, 255, 255, 255, 255)

Anchor System

The anchor parameter controls text alignment relative to the (x, y) position. It uses a 3x3 grid:

0=top-left     1=top-center     2=top-right
3=mid-left     4=mid-center     5=mid-right
6=bot-left     7=bot-center     8=bot-right

Anchor 0 (default top-left) means (x, y) is the top-left corner of the text. Anchor 4 (mid-center) centers the text exactly on (x, y) both horizontally and vertically.

-- Anchor examples
drawTextDirect("Top-left",     0,   0, 20, 255,255,255,255, 0)
drawTextDirect("Top-center", 400,   0, 20, 255,255,255,255, 1)
drawTextDirect("Mid-right",  800, 300, 20, 255,255,255,255, 5)

Primitives

drawRect(x, y, w, h, r, g, b, a, filled, angle)

Draws a rectangle. Set filled=true for solid, false for outline. angle in degrees.

drawRect(100, 100, 64, 64, 255, 0, 0, 255, true, 45)

drawLine(x1, y1, x2, y2, r, g, b, a)

Draws a line between two points.

drawCircle(x, y, radius, r, g, b, a, filled)

Draws a circle at the given position.

See also: Animation for sprite-sheet animation with loadAnimation().