rust_pixel 2.4.0

2d pixel-art game engine & rapid prototype tools support terminal, wgpu and web...
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

<div align="center">

![logo](./screen-shot/logo.png)

![License] [![Latest Version]][crates.io] ![Downloads] [![API Documentation]][docs.rs] ![MSRV]

[License]: https://img.shields.io/badge/license-Apache2.0-blue.svg
[Latest Version]: https://img.shields.io/crates/v/rust_pixel.svg
[crates.io]: https://crates.io/crates/rust_pixel
[Downloads]: https://img.shields.io/crates/d/rust_pixel.svg
[API Documentation]: https://docs.rs/rust_pixel/badge.svg
[docs.rs]: https://docs.rs/rust_pixel
[MSRV]: https://img.shields.io/badge/rust-1.71+-brightgreen.svg?&logo=rust

**Tile-first. Retro-ready. Write Once, Run Anywhere—2D Engine!**

[Change Log] | [Architecture] | [FAQ] | [Roadmap] | [Online Demo]

[Change Log]: doc/change.md
[Architecture]: doc/architecture.md
[FAQ]: doc/faq.md
[Roadmap]: doc/roadmap_2026.md
[Online Demo]: https://zipxing.github.io/rust_pixel

</div>

---

## Core Philosophy

<table>
<tr>
<td width="33%" align="center">

### Everything is Tiles

Scene > Layer > Sprite > Buffer > Cell

Unified rendering abstraction

High performance - **Texture2DArray, one draw call** 

</td>
<td width="33%" align="center">

### Write Once, Run Anywhere

Terminal | Desktop | Web

**TUI in GPU windows — no terminal emulator**

One codebase, multiple targets

</td>
<td width="33%" align="center">

### Quick Start

`app!` macro scaffolding

**Built-in BASIC interpreter**

Model-Render-Game pattern with Event driven

</td>
</tr>
</table>

---

## ⭐ Killer App: MDPT

**A Markdown-first presentation toolkit with a self-rendered TUI(no terminal emulator needed).**

MDPT demonstrates RustPixel's unique capability: rendering a full-featured terminal UI in a native GPU window, completely independent of any terminal emulator.

![MDPT](./screen-shot/mdpt.gif)

```bash
cargo pixel r mdpt g -r # WGPU mode (native)
cargo pixel r mdpt t -r # Term mode (native)
cargo pixel r mdpt w -r # Web mode (browser)
```

### Key Features

| Feature | Description |
|---------|-------------|
| **GPU-Accelerated Transitions** | 6 transition effects (dissolve, circle, wipe, etc.) powered by shaders |
| **Code Highlighting** | 100+ languages with dynamic line-by-line reveal `{1-4\|6-10\|all}` |
| **Text Animations** | Spotlight, Wave, FadeIn, Typewriter effects |
| **Charts** | Line charts, bar charts, pie charts, Mermaid diagrams |
| **Column Layouts** | Flexible multi-column content arrangement |
| **PETSCII/SSF Images** | Native pixel-art and animation support |
| **Full CJK Support** | Chinese, Japanese, Korean text rendering |
| **Mipmap Rendering** | Crisp text at any window size or fullscreen, single draw call performance |
| **Incremental Display** | Step-by-step content reveal with pause markers |

### Why MDPT?

Unlike terminal-based presenters (presenterm, slides), MDPT:
- **No terminal emulator** — Runs in a native window with GPU rendering
- **Consistent rendering** — Same look across all platforms
- **Rich transitions** — GPU shader effects impossible in terminals
- **True graphics** — Not limited by terminal cell constraints

### AI-Powered Slide Generation

The `gen-mdpt` Claude Code skill can automatically generate complete MDPT presentations from any topic or document — including code blocks, charts, tables, column layouts, and animations. Below is a Rust tutorial generated by `gen-mdpt`:

![gen-mdpt](./screen-shot/rust.gif)

---

## ⭐ Tetris — Play Against AI

Two-player Tetris with AI opponent, attack flight animations, and sweep clear effects.

![Tetris](./screen-shot/tetris_sdl.gif)

```bash
cargo pixel r tetris g -r  # WGPU mode (native)
cargo pixel r tetris t -r   # Terminal mode
cargo pixel r tetris w -r   # Web mode (browser)
```

---

### Unified Texture Architecture

Texture2DArray with 3-level mipmaps (4096×4096 layers), containing Sprite, TUI, Emoji, and CJK symbols. Single texture binding, instanced rendering, one draw call.

> Mipmaps = precomputed lower-resolution versions of the same texture, auto-selected based on render size.

Pix resources (`assets/pix/`) are loaded with fallback: app-specific first, then shared root. Workspace apps share root `assets/pix/`; standalone projects need their own copy. See [Architecture](doc/architecture.md#pix-resource-loading-texture2darray) for details.

---

## Showcase and games

PETSCII art gallery with Matrix rain, GPU transitions, and 2000+ artworks. Fullscreen screensaver mode supported.

![PetView](./screen-shot/petview.gif)

```bash
cargo pixel r petview g -r    # WGPU mode (native)
cargo pixel r petview w -r     # Web mode (browser)
```

---

## Quick Start

### Install

```bash
cargo install rust_pixel         # Install cargo-pixel CLI
cargo pixel                      # First run clones workspace to ~/rust_pixel_work
cd ~/rust_pixel_work
```

### Run Demo Games

```bash
cargo pixel r snake t            # Snake - Terminal mode
cargo pixel r snake g           # Snake - WGPU mode (native window)
cargo pixel r tetris w           # Tetris - Web mode (localhost:8080)
cargo pixel r petview g -r      # Petview - WGPU mode (release)
```

### Create Your Own Game

```bash
cargo pixel c mygame             # Create in ./apps/mygame
cargo pixel r mygame t           # Run it!

# Or create standalone project
cargo pixel c myapp ..           # Create in ../myapp
cd ../myapp && cargo pixel r myapp g
```

### Write Games in BASIC

RustPixel includes **pixel_basic** - a built-in BASIC interpreter perfect for beginners or quick prototyping!

```bash
cargo pixel r basic_snake t      # Run BASIC Snake game
```

Write game logic in familiar BASIC syntax (`apps/basic_snake/assets/game.bas`):

```basic
10 REM SNAKE GAME
20 X = 20: Y = 10
30 DIM BX(100): DIM BY(100)
40 YIELD
50 GOTO 40

1000 REM ON_INIT
1010 BOX 0, 0, 60, 24, 1
1020 RETURN

2000 REM ON_TICK
2010 IF KEY("W") THEN DY = -1: DX = 0
2020 X = X + DX: Y = Y + DY
2030 RETURN

3500 REM ON_DRAW
3510 PLOT X, Y, "@", 10, 0
3520 RETURN
```

**pixel_basic** features:
- Classic BASIC syntax with line numbers
- Game hooks: `ON_INIT (1000)`, `ON_TICK (2000)`, `ON_DRAW (3500)`
- Graphics: `PLOT x, y, char, fg, bg` / `BOX` / `CLS`
- Input: `KEY("W")`, `KEY("SPACE")`
- Arrays: `DIM arr(100)`
- Control flow: `GOTO`, `GOSUB/RETURN`, `FOR/NEXT`, `IF/THEN`
- Math: `RND()`, `INT()`, `ABS()`
- Strings: `STR$()`, `LEN()`, `MID$()`

See `pixel_basic/` for the interpreter source code.

---

## Architecture

```
┌─────────────────────────────────────────────────────────┐
│                         Game                            │
│  ┌─────────────────────┐  ┌─────────────────────────┐  │
│  │       Model         │  │        Render           │  │
│  │  ├─ init()          │  │  ├─ init()              │  │
│  │  ├─ handle_input()  │  │  ├─ draw()              │  │
│  │  ├─ handle_auto()   │  │  └─ Panel               │  │
│  │  └─ handle_timer()  │  │      └─ Sprites[]       │  │
│  └─────────────────────┘  │          └─ Buffer      │  │
│                           │              └─ Cells[] │  │
│                           └─────────────────────────┘  │
└─────────────────────────────────────────────────────────┘
```

---

## Demo Games

<table>
<tr>
<td width="50%">

### Snake
PETSCII animations with smooth gameplay

```bash
cargo pixel r snake g -r   # WGPU (native)
cargo pixel r snake t -r    # Terminal
cargo pixel r snake w -r    # Web
```

![Snake](./screen-shot/snake_sdl.gif)

</td>
<td width="50%">

### Tetris
Play against AI

```bash
cargo pixel r tetris g -r  # WGPU (native)
cargo pixel r tetris t -r   # Terminal
cargo pixel r tetris w -r   # Web
```

![Tetris](./screen-shot/tetris_sdl.gif)

</td>
</tr>
<tr>
<td width="50%">

### Tower Defense
Pixel-perfect sprite movement

```bash
cargo pixel r tower g -r   # WGPU (native)
cargo pixel r tower w -r    # Web
```

![Tower](./screen-shot/tower_sdl.gif)

</td>
<td width="50%">

### Poker / Gin Rummy
Card game algorithms + FFI/WASM demos

```bash
cargo pixel r poker t -r
cargo pixel r gin_rummy t -r
```

![Poker](./screen-shot/ginrummy.png)

</td>
</tr>
</table>

---

## Tools

### Palette - Color Tool
Terminal UI for color manipulation

```bash
cargo pixel r palette t -r
```

![Palette](./screen-shot/palette.gif)

### Edit - Character Art Editor

```bash
cargo pixel e t . assets/logo.txt    # Terminal mode
cargo pixel e g . assets/logo.pix   # WGPU mode
```

<table><tr>
<td><img src="./screen-shot/tedit_term.png" alt="Edit Terminal"></td>
<td><img src="./screen-shot/tedit_sdl.png" alt="Edit SDL"></td>
</tr></table>

### Petii - Image to PETSCII Converter

```bash
cargo pixel p assets/lion.png 40 40 > lion.pix
cargo pixel e g . lion.pix
```

<table><tr>
<td><img src="./screen-shot/a.png" alt="Petii Example 1"></td>
<td><img src="./screen-shot/lion.png" alt="Petii Example 2"></td>
</tr></table>

### GIF to PETSCII Animation

```bash
cargo pixel cg input.gif output.ssf 40 25
cargo pixel ssf . output.ssf    # Preview
```

---

## FFI & WASM

RustPixel algorithms can be exported for other languages:

```bash
# C++/Python FFI
cd apps/poker/ffi && make run

# JavaScript WASM
cd apps/poker/wasm && make run
```

---

## Installation Guide

| Platform | Guide |
|----------|-------|
| macOS | [doc/mac.md]doc/mac.md |
| Linux | [doc/linux.md]doc/linux.md |
| Windows (WSL) | [doc/win.md]doc/win.md |
| Windows (Native) | [doc/win-native.md]doc/win-native.md |

**Requirements:**
- [Nerd Font]https://github.com/ryanoasis/nerd-fonts (for terminal mode)
- Rust 1.71+
- wasm-pack (for web mode)

---

## Features

- **`app!` macro** - One-line game scaffolding with cross-platform entry points
- **Model/Render pattern** - Clean separation of logic and presentation
- **Event/Timer system** - Built-in messaging mechanism
- **Unified adapter trait** - Same code for all rendering backends
- **WGPU shaders** - Modern GPU rendering pipeline (native + WebGL2/WebGPU)
- **Game algorithms** - Pathfinding, object pools, utilities
- **Audio support** - Sound effects and music playback

---

## Roadmap

### Three Pillars

| Pillar | Description |
|--------|-------------|
| **Hybrid TUI** | Same code runs in Terminal, Native Window, and Web |
| **Scripting** | BASIC drives game logic, Rust handles engine |
| **AI Art Pipeline** | PETSCII/ASCII art search → generation |

### AI Integration

- **AI-friendly API** — Refine APIs for better AI code generation compatibility
- **AIGC for MDPT** — Auto-generate slide illustrations (converted to pixel art)
- **PETSCII/ASCII Art Generation** — Explore AI-generated low-resolution, high-quality character art

### BASIC Enhancement

- **Richer Game Capabilities** — Expand BASIC to support more complex games and TUI apps
- **Unified Script Template**`init` / `update` / `draw` / `on_key` pattern for all apps

### Toolchain

- **Editor Improvements** — Enhanced character art editor with more features
- **Asset Pipeline** — Charset/Palette/Pack import/export, versioning, hot reload
- **Cross-Platform Consistency** — Unified input and rendering across all backends

See [Roadmap 2026](doc/roadmap_2026.md) for detailed plans.

---

<div align="center">

**Made with Rust**

</div>