Like2D Logo Like2D
← Back to Docs

Getting Started

Overview

Like2D is a lightweight 2D game framework built with SDL3 and Luau scripting. It provides a simple API for creating 2D applications, games, and interactive experiences with the power of Luau scripting.

System Requirements

Quick Start

1. Running Your First Application

There are several ways to run a Like2D application:

Method 1: Drag and Drop

  1. Create a folder with your project files
  2. Add a main.luau file
  3. Drag the folder onto Like2D.exe

Method 2: Command Line

# Run with specific script folder
.\Like2D.exe "path\to\your\project"

# Run with specific script file
.\Like2D.exe "path\to\main.luau"

Method 3: Debug Mode

# Run debug version with console output
.\LikeC.exe "path\to\your\project"

Your First Like2D Application

Create a new folder called MyFirstGame and add a main.luau file. Like2D has 4 main callback functions: onInit() (once at start), onUpdate(dt) (game logic each frame), onDraw() (rendering each frame), and onCleanup() (once at exit).

-- main.luau - Your first Like2D application

-- This function is called once when the application starts
function onInit()
    -- Set window title and size
    setWindowTitle("My First Game")
    setWindowSize(800, 600)
    
    -- Load assets
    loadImage("assets/player.png")
    loadImage("assets/background.jpg")
    
    -- Load font for text rendering
    loadFont("fonts/arial.ttf", 24)
    
    print("Game initialized successfully!")
end

-- Called every frame BEFORE onDraw() — put game logic here
function onUpdate(dt)
    -- dt = delta time in seconds (~0.016 at 60 FPS)
    -- Use dt for smooth, frame-rate-independent movement
    if isKeyDown("Right") then
        playerX = playerX + 200 * dt
    end
    if isKeyDown("Left") then
        playerX = playerX - 200 * dt
    end
end

-- This function is called every frame for rendering
function onDraw()
    -- Clear screen with blue background
    clearScreen(100, 149, 237)
    
    -- Draw background image
    renderImage("assets/background.jpg", 0, 0, 800, 600)
    
    -- Draw player at center
    local playerX = 400 - 32  -- Center X - half width
    local playerY = 300 - 32  -- Center Y - half height
    renderImage("assets/player.png", playerX, playerY, 64, 64)
    
    -- Draw text
    drawTextDirect("Hello Like2D!", 10, 10, 24, 255, 255, 255, 255)
    
    -- Display current time
    local currentTime = getTime()
    drawTextDirect("Time: " .. currentTime, 10, 40, 18, 255, 255, 255, 255)
end

-- This function is called when the application is closing
function onCleanup()
    print("Game is shutting down...")
end

Project Structure

A typical Like2D project looks like this:

MyGame/
├── main.luau          # Main script file
├── assets/            # Images and resources
│   ├── player.png
│   ├── background.jpg
│   └── items/
│       ├── coin.png
│       └── powerup.png
├── fonts/             # Font files
│   ├── arial.ttf
│   └── game_font.otf
└── scripts/           # Additional Lua modules
    ├── player.lua
    ├── enemies.lua
    └── utils.lua

Code Organization (Multi-file Projects)

As your game grows, you should split your code into multiple files (like anim.luau, player.luau, etc.) using the dofile() function.

How to Include Files

At the top of your main.luau, you can load other scripts like this:

-- main.luau
dofile("anim.luau")
dofile("scripts/player.luau")

function onInit()
    -- Initialize things from other files
    anim_setup()
end

Note: Files are searched in your project folder first, then in the engine directory.

Essential API Functions

Window Management

-- Set window title
setWindowTitle("My Game Title")

-- Set window size
setWindowSize(1024, 768)

-- Get current window size
local width, height = getWindowSize()

-- Toggle fullscreen
setFullscreen(true)   -- Enable fullscreen
setFullscreen(false)  -- Disable fullscreen

Rendering

-- Clear screen with RGB color
clearScreen(red, green, blue)

-- Load an image (returns true/false)
local success = loadImage("assets/image.png")

-- Render a previously loaded image at position with size
renderImage("assets/image.png", x, y, width, height)

-- Load a font
loadFont("fonts/font.ttf", size)

-- Draw text (using preloaded font)
drawText("Hello World", "fonts/font.ttf", x, y, r, g, b)

-- Draw text directly (no preloading needed — easiest method)
drawTextDirect("Hello World", x, y, size, r, g, b, a)

Input & Time

-- Check if a key is currently held down
if isKeyDown("Space") then
    -- Space bar is held
end

-- Check if a key was just pressed this frame
if isKeyJustPressed("Escape") then
    -- Escape was just pressed
end

-- Get delta time (seconds since last frame) for smooth movement
local dt = getDeltaTime()
player.x = player.x + speed * dt

-- Get total time since start (milliseconds)
local time = getTime()

-- Print to console (debug mode only)
print("Debug message")

Audio

-- Load a sound with a unique key
loadSound("bgm", "assets/music.mp3")
loadSound("jump", "assets/jump.wav")

-- Play: true = loop (music), false = one-shot (SFX)
playSound("bgm", true)
playSound("jump", false)

-- Volume control (0.0 to 1.0)
setSoundVolume("bgm", 0.5)
setMasterVolume(0.8)

-- Cleanup when done
unloadSound("bgm")
unloadSound("jump")

Compiler & Build System

Like2D includes a standalone compiler tool (LikeC.exe) for packaging games into distributable executables. It bundles your Luau scripts into a game.like archive, encrypts assets, and produces a single-file executable that players can run without any dependencies.

Building a Standalone Executable

The compiler packages your project folder into a single .exe for distribution.

# Basic build: package the "MyGame" folder
.\LikeC.exe "C:\path\to\MyGame"

# The output is a MyGame.exe in the same folder as Like2D.exe

Build with Game Archive (game.like)

You can also build your game into a game.like archive, which packs all scripts into a single file while keeping assets separate:

# Build game.like archive + standalone exe
.\LikeC.exe "C:\path\to\MyGame" --build-like

# Run the game.like directly (Like2D auto-detects it)
.\Like2D.exe "C:\path\to\MyGame"

Modules System

Like2D supports a module system for reusable code. When building, you can include modules by placing them in a modules/ folder in your project, or by specifying module paths to the compiler. Modules are automatically packaged into the final build.

MyGame/
├── main.luau
├── modules/          # ← Module folder
│   └── mylib.luau
└── assets/

How Building Works

  1. The compiler scans your project folder for main.luau and all referenced scripts
  2. Scripts are compiled to Luau bytecode for faster loading and obfuscation
  3. Assets (images, audio, etc.) are optionally encrypted
  4. Everything is packaged with the Like2D runtime into a standalone executable
  5. The output .exe requires no external dependencies — just double-click to run

Building From Source

If you're building Like2D from source, run the build.bat script (requires Visual Studio 2022 with C++ desktop development tools). See the BUILD_SYSTEM.md file for full build instructions.

# Build from source
build.bat

Next Steps