外观
Graphics 速查
cici::run / run_state 的闭包无参数,绘制、输入、纹理、字体和音频都通过全局函数访问;只有需要原始 &mut Graphics 时才用 run_graphics / run_state_graphics(以及带 _with 的配置版)。
窗口与时间
| 方法 | 返回 | 说明 |
|---|---|---|
screen_width() | f32 | 逻辑宽度 |
screen_height() | f32 | 逻辑高度 |
screen_size() | Vec2 | 逻辑尺寸 |
dpi_scale() | f32 | DPI 缩放 |
window() | Arc<winit::window::Window> | 原始窗口 |
set_title(&str) | () | 设置标题 |
set_window_size(w, h) | () | 逻辑尺寸 |
set_window_position(x, y) | () | 物理坐标 |
set_cursor_visible(bool) | () | 光标显示 |
set_cursor_grab(bool) | () | 锁定/释放鼠标 |
set_window_icon(&Image) | Result<()> | 设置图标 |
set_min_inner_size(w, h) / set_max_inner_size(w, h) | () | 尺寸限制 |
set_resizable(bool) / set_decorations(bool) / set_always_on_top(bool) | () | 窗口样式 |
set_pixel_perfect(bool) / pixel_perfect() | () / bool | 像素完美 |
set_cursor_icon(CursorIcon) | () | 光标图标 |
current_monitor() / available_monitors() | Option / Vec | 显示器 |
dropped_files() / hovered_file() | Vec<PathBuf> / Option<PathBuf> | 文件拖放 |
request_quit() / quit_requested() | () / bool | 请求退出 |
set_pc_assets_folder(path) | () | 资源根目录 |
dt() | f32 | 本帧时间步长(秒) |
elapsed() / time() | f32 | 启动至今(秒) |
frame_count() | u64 | 帧计数 |
fps() | f32 | 平滑 FPS |
backend_name() | &'static str | 实际后端名称 |
adapter_name() | String | 适配器名称 |
清除与相机
| 方法 | 说明 |
|---|---|
clear(Color) / clear_background(Color) | 设置本帧清屏色 |
set_camera(Camera2D) | 使用 2D 相机 |
set_default_camera() | 恢复屏幕坐标 |
camera() | Option<&Camera2D> |
输入
| 方法 | 说明 |
|---|---|
is_key_down(Key) / is_key_pressed(Key) / is_key_released(Key) | 键盘 |
is_mouse_button_down(MouseButton) / is_mouse_button_pressed(...) / is_mouse_button_released(...) | 鼠标键 |
mouse_position() / mouse_x() / mouse_y() | 鼠标位置 |
mouse_wheel() | 本帧滚轮 |
typed_text() | 本帧可打印字符 |
shift_down() / ctrl_down() / alt_down() / super_down() | 修饰键 |
touches() | 本帧 Vec<Touch> |
touch_position(id) | Option<Vec2> |
keys_down() / keys_pressed() | 当前/本帧按键列表 |
last_key_pressed() | 最近按下的键 |
with_input(f) | 访问完整 InputState |
draw_calls() / batch_count() / instance_count() | 上一帧统计 |
形状
| 方法 | 说明 |
|---|---|
draw_rectangle(x, y, w, h, color) | 矩形 |
draw_rect(rect: Rect, color) | 矩形 |
draw_rectangle_lines(x, y, w, h, thickness, color) | 矩形描边 |
draw_rounded_rectangle(x, y, w, h, radius, color) | 圆角矩形 |
draw_rounded_rectangle_lines(x, y, w, h, radius, thickness, color) | 圆角矩形描边 |
draw_rectangle_rotated(x, y, w, h, rotation, color) | 旋转矩形 |
draw_rounded_rectangle_rotated(x, y, w, h, radius, rotation, color) | 旋转圆角矩形 |
draw_circle(x, y, radius, color) | 圆 |
draw_circle_lines(x, y, radius, thickness, color) | 圆环 |
draw_ellipse(x, y, rx, ry, color) | 椭圆 |
draw_ellipse_lines(x, y, rx, ry, thickness, color) | 椭圆描边 |
draw_arc(center, radius, start, end, segments, thickness, color) | 圆弧 |
draw_ring(center, radius, thickness, color) | 圆环 |
draw_line(x1, y1, x2, y2, thickness, color) | 圆头线段 |
draw_polyline(points: &[Vec2], thickness, color) | 折线 |
draw_triangle(a: Vec2, b: Vec2, c: Vec2, color) | 三角形 |
draw_triangle_lines(a, b, c, thickness, color) | 三角形描边 |
draw_polygon(points: &[Vec2], color) | 凸多边形(扇形三角化) |
draw_polygon_lines(points: &[Vec2], thickness, color) | 多边形描边 |
裁剪与视口
| 方法 | 说明 |
|---|---|
set_scissor(x, y, w, h) | 裁剪后续绘制,可与普通绘制共存 |
clear_scissor() | 取消裁剪 |
set_viewport(x, y, w, h) | 把整个画面缩放到视口 |
clear_viewport() | 恢复全屏投影 |
纹理
| 方法 | 说明 |
|---|---|
load_texture(path) | 从文件加载(桌面) |
load_texture_from_bytes(&[u8]) | 从内存加载 PNG / JPEG |
create_texture(w, h, &[u8]) | 从 RGBA8 创建,长度需为 w*h*4 |
texture_from_image(&Image) | Image 上传为纹理 |
texture_from_wgpu(wgpu::Texture) | 包装自定义 wgpu 纹理 |
generate_texture(w, h, f) | 程序生成,f(x, y) -> Color |
draw_texture(&Texture2D, x, y, color) | 按原始尺寸绘制 |
draw_texture_ex(&Texture2D, x, y, w, h, rotation, color) | 指定尺寸与旋转 |
draw_texture_rect(&Texture2D, dest: Rect, source: Rect, color) | 源矩形(像素) |
draw_texture_params(&Texture2D, x, y, color, DrawTextureParams) | 翻转、轴心、源矩形、旋转 |
render_target(w, h) | 创建 RenderTarget |
set_render_target(&RenderTarget) | 后续绘制进入目标 |
clear_render_target() | 恢复窗口输出 |
read_texture(&Texture2D) | 同步读回 Image |
get_screen_data() | 当前帧截图 Image |
文本
| 方法 | 说明 |
|---|---|
draw_text(text, x, y, size, color) | 使用默认字体 |
draw_text_ex(text, x, y, TextParams) | 高级参数 |
measure_text(text, size) | TextMeasure { width, height } |
measure_text_ex(text, size, FontId) | 指定字体测量 |
load_font(path) | 从文件加载字体(桌面) |
load_font_from_bytes(Vec<u8>) | 从内存加载字体 |
set_default_font(FontId) | 设置默认字体 |
default_font() | 当前默认字体 |
TextParams:font_size、color、font、font_scale、font_scale_aspect、rotation、letter_spacing。
音频
| 方法 | 说明 |
|---|---|
with_audio(f) | Option<R>,首次调用时初始化音频 |
load_sound(path) | 从文件加载音效(桌面) |
load_sound_from_bytes(&[u8]) | 从内存加载音效 |
load_music(path) | 从文件加载音乐(桌面) |
load_music_with(path, MusicParams) | 指定参数 |
load_music_from_bytes(&[u8]) | 从内存加载音乐 |
load_music_from_bytes_with(&[u8], MusicParams) | 指定参数 |
set_master_volume(f32) / master_volume() | 总音量 |
3D
| 方法 | 说明 |
|---|---|
set_camera_3d(Camera3D) | 启用 3D 相机 |
camera_3d() | 当前 3D 相机 |
project_3d(Vec3) | 投影到屏幕坐标 |
draw_line_3d(a, b, color) | 线段 |
draw_triangle_3d(a, b, c, color) | 三角形 |
draw_cube(center, size, color) / draw_cube_wires(...) | 立方体 |
draw_sphere(center, radius, color) | 球体 |
draw_plane(center, size, color) | XZ 平面 |
load_model(path) / load_model_from_bytes(&[u8]) | OBJ 模型 |
draw_model(&model, position, scale, color) | 绘制模型 |
draw_model_textured(&model, position, scale, &texture, color) | UV 贴图模型 |
Material
| 方法 | 说明 |
|---|---|
Material::new(fragment, &[u8]) | 创建自定义 fragment + uniform |
Material::set_uniforms(&[u8]) | 更新 uniform(≤256 字节) |
set_material(&Material) / clear_material() | 后续绘制使用/退出材质 |
material() | 当前材质 |
wgpu 互操作
| 方法 | 返回 |
|---|---|
wgpu_device() | wgpu::Device(克隆句柄) |
wgpu_queue() | wgpu::Queue(克隆句柄) |
surface_format() | wgpu::TextureFormat |
texture_from_wgpu(wgpu::Texture) | 包装成 Texture2D |
on_render_pass(f) | 当前帧额外的 wgpu pass |
详见 wgpu 互操作。