5个版本
0.2.1 | 2024年2月21日 |
---|---|
0.2.0 | 2023年7月14日 |
0.1.2 | 2023年7月3日 |
0.1.1 | 2021年4月16日 |
0.1.0 | 2020年12月21日 |
#263 in 游戏开发
84 每月下载量
用于 mquad
1MB
12K SLoC
macroquad
macroquad
是一个简单易用的Rust编程语言游戏库,深受 raylib 启发。
特性
- 所有支持的平台上使用相同的代码,无需平台相关的定义。
- 高效的2D渲染,自动几何批处理。
- 依赖项最少:在 x230 (~6年旧笔记本电脑) 上,运行
cargo clean
需要16秒。 - 包含即时模式UI库。
- 单命令部署WASM和Android 构建说明。
支持的平台
- PC: Windows/Linux/macOS;
- HTML5;
- Android;
- IOS.
构建说明
设置Macroquad项目
Macroquad是一个普通的Rust依赖项,因此可以使用以下方式创建一个空的Macroquad项目:
# Create empty cargo project
cargo init --bin
将macroquad添加到Cargo.toml中作为依赖项
[dependencies]
macroquad = "0.4"
在 src/main.rs
中放入一些macroquad代码
use macroquad::prelude::*;
#[macroquad::main("BasicShapes")]
async fn main() {
loop {
clear_background(RED);
draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
draw_rectangle(screen_width() / 2.0 - 60.0, 100.0, 120.0, 60.0, GREEN);
draw_circle(screen_width() - 30.0, screen_height() - 30.0, 15.0, YELLOW);
draw_text("IT WORKS!", 20.0, 20.0, 30.0, DARKGRAY);
next_frame().await
}
}
然后运行它本地化
cargo run
更多示例请参阅 Macroquad示例文件夹
Linux
# ubuntu system dependencies
apt install pkg-config libx11-dev libxi-dev libgl1-mesa-dev libasound2-dev
# fedora system dependencies
dnf install libX11-devel libXi-devel mesa-libGL-devel alsa-lib-devel
# arch linux system dependencies
pacman -S pkg-config libx11 libxi mesa-libgl alsa-lib
Windows
在Windows上,支持MSVC和GNU目标,无需额外的依赖项。
还支持从Linux交叉编译到Windows
rustup target add x86_64-pc-windows-gnu
cargo run --target x86_64-pc-windows-gnu
WASM
rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown
这将生成 target/debug/wasm32-unknown-unknown/CRATENAME.wasm
或在构建为 --release
的情况下,生成 target/release/wasm32-unknown-unknown/CRATENAME.wasm
然后使用以下.html来加载它
index.html
<html lang="en">
<head>
<meta charset="utf-8">
<title>TITLE</title>
<style>
html,
body,
canvas {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
position: absolute;
background: black;
z-index: 0;
}
</style>
</head>
<body>
<canvas id="glcanvas" tabindex='1'></canvas>
<!-- Minified and statically hosted version of https://github.com/not-fl3/macroquad/blob/master/js/mq_js_bundle.js -->
<script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
<script>load("CRATENAME.wasm");</script> <!-- Your compiled wasm file -->
</body>
</html>
提供静态 .wasm 和 .html 服务器的一种方式
cargo install basic-http-server
basic-http-server .
IOS
在模拟器上运行
mkdir MyGame.app
cargo build --target x86_64-apple-ios --release
cp target/release/mygame MyGame.app
# only if the game have any assets
cp -r assets MyGame.app
cat > MyGame.app/Info.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>mygame</string>
<key>CFBundleIdentifier</key>
<string>com.mygame</string>
<key>CFBundleName</key>
<string>mygame</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
</dict>
</plist>
EOF
xcrun simctl install booted MyGame.app/
xcrun simctl launch booted com.mygame
有关为真实iPhone配置的详细说明和指南,请查看https://macroquad.rs/articles/ios/
提示
将以下片段添加到您的Cargo.toml中可以确保即使在调试模式下,所有依赖项也能编译。在macroquad中,这会使图像加载速度提高几倍,同时保持应用程序的性能,并奇迹般地降低编译时间。[profile.dev.package.'*']
opt-level = 3
async/await
尽管macroquad试图尽可能少地使用Rust特定的概念,但所有示例中的.await
看起来有些可怕。Rust的async/await
用于解决一个问题——跨平台主循环组织。
详细信息
问题:在WASM和Android上,组织主循环并不容易
fn main() {
// do some initialization
// start main loop
loop {
// handle input
// update logic
// draw frame
}
}
在Android上可以通过线程修复此问题,但在Web上无法“暂停”和“恢复”WASM执行,因此WASM代码永远不会阻塞。而那个循环会阻塞整个游戏执行!针对该问题的C++解决方案:https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html
但在Rust中我们有async/await。Rust的futures
基本上是延续——future
的栈可以存储到变量中,以便稍后暂停/恢复future代码的执行。
macroquad中的async/await支持无需任何外部依赖项——没有运行时,没有执行器,futures-rs也不涉及。它只是保留WASM上的main
的栈,并保持代码跨平台,无需任何WASM特定的主循环。
社区
- Quads Discord服务器 - 与库的开发者和其他社区成员聊天的场所。
- Awesome Quads - 一个精选链接列表,包括miniquad/macroquad相关的代码和资源。
金牌赞助商
Macroquad由以下公司支持
依赖项
~18MB
~227K SLoC