rust_pixel/lib.rs
1// RustPixel
2// copyright zipxing@hotmail.com 2022~2026
3
4//! **RustPixel** is a lightweight, tile-based 2D game engine and rapid-prototyping toolkit.
5//!
6//! It provides a unified abstraction for both **terminal** and **graphic** rendering modes
7//! (including WebAssembly), allowing developers to build pixel-style games, tools, and
8//! simulation prototypes with minimal boilerplate.
9//!
10//! RustPixel supports **TUI both with and without a real terminal environment**, thanks to its
11//! built-in glyph atlas and software-rendered text engine. This enables rich UI overlays on
12//! top of game layers even in pure graphic mode.
13//!
14//! Designed for clarity, portability, and fast iteration, RustPixel is ideal for:
15//! - Tiles / PETSCII / ASCII / emoji-based pixel games
16//! - Terminal applications & hybrid TUI-over-graphics UIs
17//! - Rapid prototyping of gameplay ideas
18//! - Cross-platform rendering (Desktop, Web, Mobile, Mini-Game platforms)
19//!
20//! RustPixel's architecture emphasizes simplicity and composability, making it a practical
21//! foundation for building both experimental and production-ready pixel-driven experiences.
22//!
23//! Rendering modes:
24//! - Text mode: Runs in a terminal via `crossterm`, drawing with ASCII and Unicode/Emoji.
25//! - Graphics mode (native): Uses `wgpu`, rendering PETSCII and custom symbol sets.
26//! - Graphics mode (web): Same core logic compiled to WASM, rendered via wgpu + JavaScript
27//!
28//! Core concepts:
29//! - Cell: Smallest renderable unit (a character in text mode, or a fixed‑size glyph in graphics mode).
30//! - Buffer: A collection of cells representing the screen state, with diff‑friendly updates.
31//! - Panel/Sprite/Style: Higher‑level drawing abstractions that work uniformly across backends.
32//!
33//! Modules overview:
34//! - `algorithm`, `event`, `util`: Always available; form the minimal runtime.
35//! - `asset`, `audio`, `context`, `game`, `log`, `render`, `ui`: Enabled when not in `base` mode.
36//!
37//! Minimal build (base mode): Only `algorithm`, `event`, and `util` are compiled, reducing
38//! dependencies for shipping as FFI or WASM libraries. This is ideal when you only need the
39//! engine’s core data structures and event system.
40
41// ============================================================================
42// Asset Initialization Module
43// ============================================================================
44
45#[cfg(not(feature = "base"))]
46pub mod init;
47
48// Re-export commonly used items from init module
49#[cfg(not(feature = "base"))]
50pub use init::{
51 get_game_config, init_game_config, GameConfig,
52 GAME_CONFIG,
53 get_pixel_layer_data, PixelLayerData, PIXEL_LAYER_DATA,
54};
55
56#[cfg(all(graphics_mode, not(target_arch = "wasm32")))]
57pub use init::init_layered_pixel_assets;
58
59#[cfg(target_arch = "wasm32")]
60pub use init::wasm_init_pixel_assets;
61
62#[cfg(not(feature = "base"))]
63pub use init::{get_wasm_app_data, set_wasm_app_data};
64
65/// Target frames per second for the main game loop. Keep this moderate to conserve CPU.
66pub const GAME_FRAME: u32 = 60;
67
68#[cfg(not(graphics_mode))]
69pub const LOGO_FRAME: u32 = GAME_FRAME / 4 * 2;
70
71#[cfg(graphics_mode)]
72pub const LOGO_FRAME: u32 = GAME_FRAME / 4 * 3;
73
74/// Re‑export the `paste` crate so downstream crates can use it in macros generated by this crate.
75#[cfg(not(feature = "base"))]
76pub use paste;
77
78// ============================================================================
79// Application Macro Module
80// ============================================================================
81
82/// Macros for scaffolding RustPixel applications.
83///
84/// Provides `app!` macro for generating
85/// cross-platform game entry points.
86#[cfg(not(feature = "base"))]
87mod macros;
88
89/// Algorithms and data structures used by demos and utilities (e.g., disjoint‑set/union‑find,
90/// A* pathfinding).
91pub mod algorithm;
92
93/// Resource/asset manager with optional asynchronous loading for better compatibility with WASM.
94#[cfg(not(feature = "base"))]
95pub mod asset;
96
97/// Event system for input, timers, and custom user events.
98pub mod event;
99
100// /// Alternative event implementation used for benchmarking and mutex‑based comparisons.
101// pub mod event_mutex;
102
103/// Common utilities and data structures such as object pools, RNG, matrices, circles, and dots.
104pub mod util;
105
106/// Audio playback utilities and abstractions.
107#[cfg(not(feature = "base"))]
108pub mod audio;
109
110/// Runtime context, including the active rendering adapter and other shared state.
111#[cfg(not(feature = "base"))]
112pub mod context;
113
114/// Game orchestration: integrates model and renderer, encapsulating the main loop.
115#[cfg(not(feature = "base"))]
116pub mod game;
117
118/// Logging facilities tailored for demos and examples.
119pub mod log;
120
121/// Rendering subsystem supporting both text and graphics modes.
122///
123/// Components:
124/// - Adapter: Rendering adapter interface (crossterm; winit + wgpu; wgpu-web).
125/// - Cell: Base drawing unit (character in text mode; glyph/small bitmap in graphics mode).
126/// - Buffer: Screen buffer built from cells, with efficient updates.
127/// - Sprite: Higher‑level drawing primitive built on top of buffers.
128/// - Layer: Collection of sprites managed as a group.
129/// - Style: Foreground/background colors and other attributes.
130/// - Scene: Unified drawing surface that works in both modes.
131///
132/// In text mode a cell is a Unicode character. In graphics mode a cell can be a fixed‑size dot
133/// matrix image, a PETSCII character, or a custom texture. Graphics mode also supports per‑sprite
134/// pixel offsets to improve expressiveness.
135#[cfg(not(feature = "base"))]
136pub mod render;
137
138/// UI framework for building character‑based interfaces, including widgets, layouts, events,
139/// and themes for rapid development.
140#[cfg(not(feature = "base"))]
141pub mod ui;
142