coloX Application Development
coloX applications are written as text files with the .clx extension, containing a specification declaration, Lua scripts, and resource data. Running and debugging on the host automatically generates a binary for deployment to the device.
Contents
1. Core Concepts
| Term | Description |
|---|---|
| BIOS | System component controlling hardware drivers, cartridge switching, and background services |
| Cartridge | A cartridge/function container representing an application |
| Setting | System configuration functionality |
Runtime Model
- BIOS controls all hardware drivers and the cartridge lifecycle
- Only one cartridge is active (in the foreground) at a time; the rest are suspended or released
- Background services can share data and communicate across cartridges
2. File Format
Complete Example
coloX-16
version 1
style:game
name:test001
author:tester
desc:this is a demo
__lua__
function _draw()
cls()
print('hello coloX', 10, 50, red)
end
__gfx__
__gff__
__map__
__sfx__
__music__
File Header
| Line | Required | Description |
|---|---|---|
| First line | Yes | Specification identifier, such as coloX-16 (see platforms.md) |
version N | No | Specification version required by the application; it must not exceed the runtime's supported version (query with stat(5)), otherwise loading is rejected |
style:NAME | No | Application style (such as game) |
name:NAME | No | Application name |
author:NAME | No | Author |
desc:TEXT | No | Description |
Data sections follow the header. Each begins with __name__, and sections are separated by blank lines.
3. Data Sections
__lua__ — Code
Lua script code, with preprocessor directive support (see below).
__svc__ — Background Service Code
Background service script section (same syntax as __lua__, executed by the system in the background service context; see background services in system.md).
__gfx__ — Sprite Images
Sprite pixel data represented as hexadecimal strings. Use #png to import a PNG image directly (other gfx data is ignored after using #png):
__gfx__
#png png/bg.png
__pal__ — Custom Palette
Palette data represented as hexadecimal strings: 256 RGB entries overriding the default palette.
__gff__ — Sprite Flags
Sprite attribute flags represented as hexadecimal strings, with 8 flag bits per sprite.
__map__ — Map Data
Map data represented as hexadecimal strings. Each map element is a 16-bit integer (2 bytes) stored in little-endian order.
__sfx__ — Sound Effects
Hexadecimal strings in a specific format, with one sound effect definition per line and at most 64 lines.
002800001a0551c055180550c05513055000050000500005000050000500005...
__music__ — Music
Hexadecimal strings in a specific format.
__music__
00 00044208
00 00044108
01 00010203
__sfxd__ / __musicd__
An alternative sound effect format: hexadecimal strings storing memory blocks directly.
4. Preprocessor Directives
The following preprocessor directives are available within data sections (#png is meaningful only in __gfx__):
#include — Include External Scripts
#include colox_net.lua
#include my_utils.lua
Inserts external file contents at the current position. Useful for separating code modules or including shared libraries.
#blob — Embed File Data
Converts external file contents into a Lua string variable.
Basic form (raw data):
local tempimg =
#blob temp.png
Compressed form:
local data =
#blob :z:1 temp.bin
Chunked compression (split into fixed-size chunks and compress each separately, producing an array of strings):
local chunks = {
#blob :za:2048 temp.pcm
}
Compression algorithm identifiers:
| Identifier | Algorithm | Usage |
|---|---|---|
z | General-purpose zlib compression | General data |
e | ADPCM audio compression | PCM audio data, used with wave() |
g | G.711A audio compression | PCM audio data, used with wave() |
o | Opus audio compression | PCM audio data, used with wave(), 16kHz/60ms frames |
Audio blob example (ADPCM compression, 16000-sized chunks):
local wavefile = {
#blob :ea:16000: canon.pcm
}
#png — Import Sprite Images
Valid only in __gfx__; imports a PNG image directly as the sprite sheet:
__gfx__
#png png/bg.png
Note: Using
#pngdiscards the other data in the same__gfx__section.
5. Lifecycle Callbacks
While an application runs, coloX calls predefined functions at specific stages. Define these functions in Lua to respond to the corresponding events:
function _init() end -- Run once after loading the application
function _update() end -- Run once per frame while active
function _draw() end -- Run when the canvas needs refreshing while active (normally every frame)
function _idle_update() end -- Run once per frame while idle
function _idle_draw() end -- Run when the canvas needs refreshing while idle
function _cfg_update() end -- Run once per frame while the settings interface is active
function _cfg_draw() end -- Run when the settings canvas needs refreshing
function _svcind(seq, name, path) end -- Listen for service data notifications
function _initsvc() end -- Run once after loading as a background service cartridge
function _updatesvc() end -- Run once per frame as a background service cartridge
function _setstat(stat, statname) end -- Application state change notification
Callback Reference
| Callback | Trigger | Typical use |
|---|---|---|
_init() | Called once when the application first loads | Initialize variables and load resources |
_update() | Called every frame while the application is active | Handle input and update game logic |
_draw() | Called every frame while active (after _update) | Draw the screen |
_idle_update() | Called every frame while the application is idle in the background | Background logic |
_idle_draw() | Refreshes the canvas while the application is idle | Draw the background interface |
_cfg_update() / _cfg_draw() | Called every frame in the configuration interface | Configuration logic and drawing |
_svcind(seq, name, path) | On receiving a service data notification | Respond to background service events |
_initsvc() / _updatesvc() | Background service cartridge load/per-frame callbacks | Implement background services |
_setstat(stat, statname) | When the application's runtime state changes | Respond to foreground/background transitions |
Typical Application Structure
-- Initialize
function _init()
x = 120
y = 120
end
-- Update logic
function _update()
if btn(0) then y = y - 2 end -- Up (upper left)
if btn(1) then y = y + 2 end -- Down (upper right)
if btn(2) then x = x - 2 end -- Left (lower left)
if btn(3) then x = x + 2 end -- Right (lower right)
end
-- Render
function _draw()
cls(black)
circfill(x, y, 5, red)
end
6. Background Services
coloX includes a background service mechanism for communication between cartridges:
- Multiple services are built into the system
- Cartridges can register background services
- Access service data and make requests using keys
- Only background services and the currently active cartridge are allowed to run
For the service API, see system.md.