pixelsrc 0.2.0

Pixelsrc - GenAI-native pixel art format and compiler
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# Build System Integration

Integrate Pixelsrc into your build pipeline to automatically render sprites during development and deployment.

## Command-Line Interface

The `pxl` CLI is designed for build system integration:

```bash
# Render a sprite
pxl render sprite.pxl -o output.png

# Validate without rendering (fast)
pxl validate sprite.pxl

# Strict mode for CI (warnings become errors)
pxl render sprite.pxl --strict -o output.png

# Full project build
pxl build
```

## Exit Codes

Use exit codes for conditional build logic:

| Code | Meaning |
|------|---------|
| 0 | Success |
| 1 | Error (or warning in strict mode) |
| 2 | Invalid arguments |

## Incremental Builds

The build system tracks file changes and only rebuilds what's necessary:

```bash
# First build - processes all files
pxl build
# Build complete - Files: 24 | Sprites: 18

# Second build (no changes) - skips everything
pxl build
# Build complete - Files: 24 | Sprites: 0 (all skipped)

# After modifying player.pxl - only rebuilds affected files
pxl build
# Build complete - Files: 24 | Sprites: 1
```

The manifest file (`.pxl-manifest.json`) tracks:
- Source file hashes
- Output file paths
- Build timestamps
- Dependency relationships

### Forcing Full Rebuild

To bypass incremental checking:

```bash
# Delete the manifest
rm .pxl-manifest.json && pxl build

# Or use clean build in your tooling
```

## Parallel Builds

The build system automatically parallelizes independent targets:

```
Building 12 targets (4 workers)...
  sprite:player ... ok (150ms)
  sprite:enemy ... ok (120ms)
  sprite:coin ... ok (80ms)
  atlas:characters ... ok (45ms)  # waits for sprites
  export:godot ... ok (12ms)      # waits for atlas
```

Dependencies are respected: atlas builds wait for their source sprites, exports wait for atlases.

## Progress Reporting

### Console Output

Standard progress shows build status:

```
Building...
Build complete - Files: 24 | Sprites: 18
```

Verbose mode (`-v`) shows per-target details:

```bash
pxl build -v
# Using config: /project/pxl.toml
# Building 5 targets...
#   sprite:player ... ok (150ms)
#   sprite:enemy ... ok (120ms)
#   ...
```

### JSON Output (CI Integration)

For programmatic parsing, the build system can output JSON progress events:

```json
{"event":"build_started","total_targets":5}
{"event":"target_completed","target_id":"sprite:player","status":"success","duration_ms":150}
{"event":"target_completed","target_id":"sprite:enemy","status":"success","duration_ms":120}
{"event":"build_completed","success":true,"duration_ms":335,"succeeded":5,"skipped":0,"failed":0}
```

## Game Engine Exports

Configure exports in `pxl.toml` to generate engine-specific files alongside your sprites.

### Godot Export

```toml
[export.godot]
enabled = true
resource_path = "res://assets/sprites"
animation_player = true
sprite_frames = true
```

Generates:
- `.tres` resource files for atlases
- `AnimationPlayer` resources for animations
- `SpriteFrames` resources for animated sprites

### Unity Export

```toml
[export.unity]
enabled = true
pixels_per_unit = 16
filter_mode = "point"  # or "bilinear"
```

Generates:
- `.asset` meta files for sprites
- Sprite slicing data for atlases
- Animation clips for animated sprites

### libGDX Export

```toml
[export.libgdx]
enabled = true
```

Generates:
- `.atlas` files in libGDX TexturePacker format
- Region definitions for sprite lookup

## Make / Justfile

Basic Makefile integration:

```makefile
SPRITES := $(wildcard assets/*.pxl)
PNGS := $(SPRITES:.pxl=.png)

all: $(PNGS)

%.png: %.pxl
	pxl render $< -o $@

clean:
	rm -f $(PNGS)
```

With [Just](https://just.systems), the project includes a ready-to-use justfile:

```bash
# Render an example
just render coin

# Run all checks
just check

# List available commands
just --list
```

## npm Scripts

Add to `package.json`:

```json
{
  "scripts": {
    "sprites:build": "pxl build",
    "sprites:watch": "pxl build --watch",
    "sprites:validate": "pxl validate assets/sprites/*.pxl --strict"
  }
}
```

## Watch Mode

The CLI supports watch mode for development:

```bash
pxl build --watch
```

Watch mode:
- Monitors source files for changes
- Re-renders only changed files (incremental)
- Shows errors inline without stopping
- Recovers automatically when errors are fixed
- Configurable debounce delay

Configure watch behavior in `pxl.toml`:

```toml
[watch]
debounce_ms = 100    # Wait before rebuilding
clear_screen = true  # Clear terminal between builds
```

### Error Recovery

Watch mode continues running after errors. When you fix the error, the build automatically recovers:

```
Watching for changes (Ctrl+C to stop)...
[15:23:01] player.pxl changed
  ERROR: Invalid color 'gren' at line 5
[15:23:15] player.pxl changed
  ok - player.png
```

## CI/CD Integration

### GitHub Actions

```yaml
name: Build Sprites

on: [push, pull_request]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install Pixelsrc
        run: cargo install pixelsrc

      - name: Validate sprites
        run: pxl validate assets/*.pxl --strict

      - name: Build sprites
        run: pxl build

      - name: Upload artifacts
        uses: actions/upload-artifact@v4
        with:
          name: sprites
          path: build/
```

### GitLab CI

```yaml
build-sprites:
  image: rust:latest
  script:
    - cargo install pixelsrc
    - pxl validate assets/*.pxl --strict
    - pxl build
  artifacts:
    paths:
      - build/
```

### Caching

Cache the Cargo build to speed up CI:

```yaml
# GitHub Actions
- uses: actions/cache@v4
  with:
    path: ~/.cargo
    key: ${{ runner.os }}-cargo-pixelsrc

# GitLab CI
cache:
  paths:
    - ~/.cargo
```

## Configuration File

Create `pxl.toml` in your project root for persistent settings:

```toml
[project]
name = "my-game"
src = "assets/sprites"
out = "dist/sprites"

[defaults]
scale = 4
padding = 1

[atlases.characters]
sources = ["player/**", "enemies/**"]
max_size = [2048, 2048]
power_of_two = true

[animations]
sources = ["animations/**"]
preview = true
sheet_layout = "horizontal"

[export.godot]
enabled = true
resource_path = "res://sprites"

[validate]
strict = true
unused_palettes = "warn"
missing_refs = "error"

[watch]
debounce_ms = 100
clear_screen = true
```

## Project Structure

Recommended layout for projects with sprites:

```
project/
├── assets/
│   └── sprites/
│       ├── characters/
│       │   ├── hero.pxl
│       │   └── enemy.pxl
│       ├── items/
│       │   ├── sword.pxl
│       │   └── potion.pxl
│       └── palettes/
│           └── shared.pxl
├── build/
│   ├── sprites/
│   │   └── ... (generated PNGs)
│   ├── godot/
│   │   └── ... (generated .tres files)
│   └── .pxl-manifest.json
├── pxl.toml
└── package.json
```

## Shared Palettes

Use the `--include` flag to share palettes across sprites:

```bash
pxl render sprite.pxl --include palettes/shared.pxl -o output.png
```

Or reference palettes by path in your source files using the `includes` field.

## Batch Processing

Render multiple sprites with glob patterns:

```bash
# Render all sprites in a directory
pxl render assets/sprites/**/*.pxl -o dist/

# Render with specific output names
pxl render character.pxl enemy.pxl -o dist/{name}.png
```

The `{name}` placeholder uses the sprite name for the output filename.

## Format Conversion

Convert between output formats:

```bash
# PNG (default)
pxl render sprite.pxl -o output.png

# GIF animation
pxl render animation.pxl -o output.gif

# Spritesheet
pxl render sprites.pxl -o sheet.png --spritesheet

# Terminal preview
pxl render sprite.pxl --terminal
```

## Error Handling in Scripts

Example bash script with proper error handling:

```bash
#!/bin/bash
set -e

echo "Validating sprites..."
if ! pxl validate assets/*.pxl --strict; then
    echo "Validation failed!"
    exit 1
fi

echo "Building sprites..."
pxl build

echo "Done! Rendered $(ls build/*.png 2>/dev/null | wc -l) sprites."
```

## Performance Tips

1. **Use incremental builds** - Let the manifest track changes
2. **Validate before building** - Faster than full renders
3. **Batch similar sprites** - Reduces CLI startup overhead
4. **Cache in CI** - Cache Cargo builds and manifest files
5. **Watch mode for development** - Avoids manual re-running
6. **Parallel builds** - Enabled by default for multi-core systems

## Troubleshooting

### Build is slow

- Check if incremental builds are working (look for "skipped" in output)
- Ensure manifest file isn't being deleted between builds
- Use `--verbose` to identify slow targets

### Watch mode missing changes

- Check `debounce_ms` setting (increase if changes are batched)
- Verify source directory path in `pxl.toml`
- Some editors use atomic saves that may need different handling

### Exports not generating

- Verify export is enabled in `pxl.toml` (e.g., `[export.godot] enabled = true`)
- Check that atlas sources are correctly specified
- Run with `--verbose` to see export target status

## Related

- [CLI Reference]../cli/overview.md - Complete command documentation
- [build command]../cli/build.md - Full build command reference
- [Configuration]../reference/config.md - pxl.toml reference
- [Exit Codes]../reference/exit-codes.md - All exit code meanings