Skip to content

快速开始 ​

环境要求 ​

  • Rust 1.80+(2021 edition)
  • Linux 需要安装窗口与音频运行库:
bash
sudo apt install pkg-config libxkbcommon-dev libwayland-dev libasound2-dev

Windows、macOS 无需额外系统库。

添加依赖 ​

在 Cargo.toml 中:

toml
[dependencies]
cici = "0.1"

最小示例 ​

rust
use cici::*;

fn main() {
    cici::run(|| {
        clear_background(hex(0x101820));

        let t = elapsed();
        let x = screen_width() * 0.5 + t.cos() * 180.0;
        let y = screen_height() * 0.5 + t.sin() * 120.0;

        draw_circle(x, y, 48.0, ORANGE);
        draw_text("Hello, cici!", 24.0, 40.0, 40.0, WHITE);
    });
}

run 使用默认窗口配置,每帧调用一次闭包。绘制、输入、时间都是全局函数;闭包是 FnMut,可以捕获可变状态。需要自定义标题、尺寸或后端时改用 run_with:

rust
cici::run_with(
    Config::default().with_title("hello cici").with_size(960, 640),
    || {
        // ...
    },
);

保存游戏状态 ​

状态较多时使用 run_state:

rust
use cici::*;

struct Game {
    pos: Vec2,
    speed: f32,
}

fn main() {
    let game = Game {
        pos: Vec2::new(480.0, 320.0),
        speed: 320.0,
    };

    cici::run_state(game, |game| {
        let mut dir = Vec2::ZERO;
        if is_key_down(Key::A) { dir.x -= 1.0; }
        if is_key_down(Key::D) { dir.x += 1.0; }
        if is_key_down(Key::W) { dir.y -= 1.0; }
        if is_key_down(Key::S) { dir.y += 1.0; }

        game.pos += dir.normalize() * game.speed * dt();

        clear(hex(0x0a0f18));
        draw_circle(game.pos.x, game.pos.y, 24.0, SKYBLUE);
    });
}

状态版同样有 run_state_with(config, state, ...),在需要自定义窗口时使用。绘制、输入、纹理、字体、音频都提供了全局函数;只有需要原始 &mut Graphics 时才用 run_graphics / run_graphics_with / run_state_graphics / run_state_graphics_with。

常用配置 ​

rust
let config = Config::default()
    .with_title("My Game")
    .with_size(1280, 720)
    .with_resizable(true)
    .with_vsync(true)
    .with_backend(Backend::Vulkan)
    .with_clear_color(BLACK)
    .with_antialiasing(AntiAliasing::Sdf)
    .with_max_instances(131_072);

cici::run_with(config, || {
    // ...
});
方法说明
with_title窗口标题
with_size逻辑像素尺寸
with_resizable是否允许缩放窗口
with_fullscreen启动时全屏
with_vsync垂直同步
with_backend固定后端;默认 Backend::Auto
with_clear_color每帧初始清屏色
with_antialiasingAntiAliasing::None / Sdf / Msaa4
with_pixel_perfect使用物理像素坐标
with_culling自动跳过屏幕外图元(默认开启)
with_max_instances单帧绘制上限,超出会自动扩容

原生平台还可以通过环境变量覆盖后端:

powershell
$env:CICI_BACKEND = "vulkan"   # auto | vulkan | opengl | dx12 | metal
cargo run

启动后会打印实际后端与适配器,例如:

text
[cici] backend=Vulkan adapter="..." driver="..."

运行示例 ​

bash
cargo run --example shapes
cargo run --example text
cargo run --example input
cargo run --example camera
cargo run --example bunnies
cargo run --example audio
示例内容
shapes矩形、圆角矩形、圆、线、三角形、旋转与文字
text多行文本、字号、测量、输入回显
input键鼠、滚轮、修饰键
camera2D 相机平移/缩放/旋转、世界层与 HUD 层
bunnies15000 个精灵的批处理性能演示
audio音频:程序生成的 WAV、音效、音乐、延迟显示

接下来 ​

简单、强力、跨平台的 2D 游戏库