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:
red(integer): Red component (0-255)green(integer): Green component (0-255)blue(integer): Blue component (0-255)
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:
filename(string): Path to the image file
Returns:
success(boolean): true if loaded successfully, false otherwise
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:
key(string): The filename used when loading the imagex(integer): X position in pixelsy(integer): Y position in pixelswidth(integer): Render width in pixelsheight(integer): Render height in pixels
Returns:
success(boolean): true if rendered successfully, false otherwise
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.
- angle: degrees
- flipX, flipY: boolean
- alpha: 0-1 (default 1.0)
- originX, originY: 0-1 (0.5 = center)
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:
filename(string): Path to the font filesize(integer): Font size in points
Returns:
success(boolean): true if loaded successfully, false otherwise
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:
text(string): The text to renderfontKey(string): The filename used when loading the fontx(integer): X position in pixelsy(integer): Y position in pixelsred(integer): Red color component (0-255)green(integer): Green color component (0-255)blue(integer): Blue color component (0-255)
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:
filename(string): Path to a .ttf font file (relative to project folder or absolute path)
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:
text(string): The text to renderx(integer): X position in pixelsy(integer): Y position in pixels (top of text, ascent is added)size(integer): Font size in pointsred(integer): Red color component (0-255)green(integer): Green color component (0-255)blue(integer): Blue color component (0-255)alpha(integer, optional): Alpha component (0-255, default 255)anchor(integer, optional): Anchor point (0-8, default 0). See Anchor System below.
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:
text(string): The text to measuresize(integer): Font size in points
Returns:
width(integer): Width of the rendered text in pixelsheight(integer): Height of the rendered text in pixels
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().