Compare commits
140 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
39a5df1ffd | ||
|
|
6637691157 | ||
|
|
c79ba0d824 | ||
|
|
b1b03b0e9c | ||
|
|
a62ae8dfe7 | ||
|
|
a21459f337 | ||
|
|
b53db3788d | ||
|
|
e1a2e6ab62 | ||
|
|
2bdb039aa9 | ||
|
|
6dd0152938 | ||
|
|
4881e33c6f | ||
|
|
0cbd6f1aac | ||
|
|
1206cf9ad1 | ||
|
|
bed913d016 | ||
|
|
98196f3e07 | ||
|
|
8f504d6c77 | ||
|
|
66499b6285 | ||
|
|
a8e62aec56 | ||
|
|
cde1ea5394 | ||
|
|
d0628ef70b | ||
|
|
9bfe4a9ce7 | ||
|
|
2da8a312f3 | ||
|
|
2bdd4f0d04 | ||
|
|
5cc9b1a6ee | ||
|
|
5d4adb7743 | ||
|
|
633d467f2c | ||
|
|
d3e83262db | ||
|
|
f31b4952e4 | ||
|
|
ad3f896f82 | ||
|
|
80ebf08dd3 | ||
|
|
f14b3d38a4 | ||
|
|
bf65c34b28 | ||
|
|
89b0790f19 | ||
|
|
9624bcf359 | ||
|
|
67a5c4a1ed | ||
|
|
8b5e66f514 | ||
|
|
5109457fcd | ||
|
|
5497e4b0b9 | ||
| d72b6eec06 | |||
| ae42f6ead0 | |||
| 471b118efd | |||
| 13a9c165f7 | |||
| da3c8e8284 | |||
| 9c0711a54c | |||
| 4598dc07e2 | |||
| 9c9dc5f423 | |||
| 12ee16faab | |||
| 398d041d96 | |||
| 7a02d6b0b5 | |||
| d47d70ff5b | |||
| 313ca4f3e6 | |||
| f940f01d9b | |||
| 90adaf9e84 | |||
| 2140fbec1b | |||
| 78300bdf9c | |||
| 514a447162 | |||
| 3d0bc66e40 | |||
| e0a15c1ca8 | |||
| fa12611c69 | |||
| 342f378860 | |||
| e8944598cc | |||
| 6af25af5f3 | |||
| f1935ad016 | |||
| 4d397bba5f | |||
| 80930ddd35 | |||
| 0133dd5329 | |||
| 635418a4da | |||
| 31193160a9 | |||
| 3086453c7b | |||
| a8b83b8e2b | |||
| 8ce2af89c8 | |||
| 5f0ee87dd9 | |||
| b88895e82f | |||
| 2f0c734d13 | |||
| e96b3159d7 | |||
| 8c95ecc547 | |||
| 02a98c9f32 | |||
| 7f95c0233e | |||
| a531228b95 | |||
| de86f383bf | |||
| bd811ee783 | |||
| 57d7f75940 | |||
| c5d6ea28e1 | |||
| 730daed20a | |||
| b9bae99a4c | |||
| 2c65048fb0 | |||
| 3388d77ec5 | |||
| 242da2e263 | |||
| 70fb2b9503 | |||
| 0aa056a0ae | |||
| b270318640 | |||
| bc759f1ed4 | |||
| 2f1ff85d8f | |||
| b7429cd9ec | |||
| 12a63374a8 | |||
| d80d7061e7 | |||
| abdefe0af0 | |||
| 4f76de7c9f | |||
| db8cd6220a | |||
| ced4e87d41 | |||
| 09e3d85821 | |||
| c1e421bbbb | |||
| 3a9381a56c | |||
| 90bdfbd2ae | |||
| a230d15ffc | |||
| 60bbd1f5d6 | |||
| 43ce8a4e01 | |||
| 1529a64588 | |||
| be5eec64c9 | |||
| 780a33f657 | |||
| c1c5dae6f2 | |||
| c489f32908 | |||
| b91f70cf2f | |||
| 24a207be01 | |||
| 44e31d9b21 | |||
|
|
b67234765a | ||
|
|
d07498c30e | ||
| 183a432116 | |||
| ead1466b2d | |||
| 8ef09a4e3e | |||
| 33672d8d5a | |||
| 1dc8aca373 | |||
| 02089a78da | |||
| 1f8e7c6d71 | |||
| 27079e127d | |||
| 5e9bb3535e | |||
| 250cf2fc89 | |||
| 57975495a9 | |||
| f3e7a780e2 | |||
| ee6cb0a670 | |||
| b3df34b405 | |||
| dbafa17670 | |||
| d9c8f97903 | |||
| ad2ec35bfb | |||
| 6331ba0b2f | |||
| 3d275b8e85 | |||
| bd61db9aae | |||
| ed8bd07518 | |||
| 27705f1ba2 | |||
| e964adc818 |
@@ -1,2 +1,5 @@
|
|||||||
[profile.default]
|
[profile.default]
|
||||||
fail-fast = false
|
fail-fast = false
|
||||||
|
|
||||||
|
[profile.coverage]
|
||||||
|
status-level = "none"
|
||||||
|
|||||||
20
.github/dependabot.yml
vendored
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
|
||||||
|
|
||||||
|
version: 2
|
||||||
|
updates:
|
||||||
|
- package-ecosystem: "cargo"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "monthly"
|
||||||
|
groups:
|
||||||
|
dependencies:
|
||||||
|
patterns:
|
||||||
|
- "*"
|
||||||
|
- package-ecosystem: "github-actions"
|
||||||
|
directory: "/"
|
||||||
|
schedule:
|
||||||
|
interval: "monthly"
|
||||||
|
groups:
|
||||||
|
dependencies:
|
||||||
|
patterns:
|
||||||
|
- "*"
|
||||||
27
.github/workflows/audit.yaml
vendored
@@ -1,27 +0,0 @@
|
|||||||
name: Audit
|
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
|
||||||
|
|
||||||
env:
|
|
||||||
CARGO_TERM_COLOR: always
|
|
||||||
RUST_TOOLCHAIN: 1.88.0
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
audit:
|
|
||||||
name: Audit
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Checkout code
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
|
|
||||||
- name: Install Rust toolchain
|
|
||||||
uses: dtolnay/rust-toolchain@master
|
|
||||||
with:
|
|
||||||
toolchain: ${{ env.RUST_TOOLCHAIN }}
|
|
||||||
|
|
||||||
- name: Install cargo-audit
|
|
||||||
run: cargo install cargo-audit
|
|
||||||
|
|
||||||
- name: Run security audit
|
|
||||||
run: cargo audit
|
|
||||||
27
.github/workflows/build.yaml
vendored
@@ -1,5 +1,4 @@
|
|||||||
name: Builds
|
name: Builds
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
on: ["push", "pull_request"]
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
@@ -15,23 +14,23 @@ jobs:
|
|||||||
- os: ubuntu-latest
|
- os: ubuntu-latest
|
||||||
target: x86_64-unknown-linux-gnu
|
target: x86_64-unknown-linux-gnu
|
||||||
artifact_name: pacman
|
artifact_name: pacman
|
||||||
toolchain: 1.88.0
|
toolchain: 1.86.0
|
||||||
- os: macos-13
|
- os: macos-13
|
||||||
target: x86_64-apple-darwin
|
target: x86_64-apple-darwin
|
||||||
artifact_name: pacman
|
artifact_name: pacman
|
||||||
toolchain: 1.88.0
|
toolchain: 1.86.0
|
||||||
- os: macos-latest
|
- os: macos-latest
|
||||||
target: aarch64-apple-darwin
|
target: aarch64-apple-darwin
|
||||||
artifact_name: pacman
|
artifact_name: pacman
|
||||||
toolchain: 1.88.0
|
toolchain: 1.86.0
|
||||||
- os: windows-latest
|
- os: windows-latest
|
||||||
target: x86_64-pc-windows-gnu
|
target: x86_64-pc-windows-gnu
|
||||||
artifact_name: pacman.exe
|
artifact_name: pacman.exe
|
||||||
toolchain: 1.88.0
|
toolchain: 1.86.0
|
||||||
runs-on: ${{ matrix.os }}
|
runs-on: ${{ matrix.os }}
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Setup Rust Toolchain
|
- name: Setup Rust Toolchain
|
||||||
uses: dtolnay/rust-toolchain@master
|
uses: dtolnay/rust-toolchain@master
|
||||||
@@ -65,15 +64,16 @@ jobs:
|
|||||||
run: cargo build --release
|
run: cargo build --release
|
||||||
|
|
||||||
- name: Acquire Package Version
|
- name: Acquire Package Version
|
||||||
shell: bash
|
id: get_version
|
||||||
|
shell: bash # required to prevent Windows runners from failing
|
||||||
run: |
|
run: |
|
||||||
PACKAGE_VERSION=$(cargo metadata --format-version 1 --no-deps | jq '.packages[0].version' -r)
|
set -euo pipefail # exit on error
|
||||||
echo "PACKAGE_VERSION=${PACKAGE_VERSION}" >> $GITHUB_ENV
|
echo "version=$(cargo metadata --format-version 1 --no-deps | jq '.packages[0].version' -r)" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
- name: Upload Artifact
|
- name: Upload Artifact
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v4
|
||||||
with:
|
with:
|
||||||
name: "pacman-${{ env.PACKAGE_VERSION }}-${{ matrix.target }}"
|
name: "pacman-${{ steps.get_version.outputs.version }}-${{ matrix.target }}"
|
||||||
path: ./target/release/${{ matrix.artifact_name }}
|
path: ./target/release/${{ matrix.artifact_name }}
|
||||||
retention-days: 7
|
retention-days: 7
|
||||||
if-no-files-found: error
|
if-no-files-found: error
|
||||||
@@ -84,10 +84,13 @@ jobs:
|
|||||||
permissions:
|
permissions:
|
||||||
pages: write
|
pages: write
|
||||||
id-token: write
|
id-token: write
|
||||||
|
# concurrency group is used to prevent multiple page deployments from being attempted at the same time
|
||||||
|
concurrency:
|
||||||
|
group: ${{ github.workflow }}-wasm
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Setup Emscripten SDK
|
- name: Setup Emscripten SDK
|
||||||
uses: pyodide/setup-emsdk@v15
|
uses: pyodide/setup-emsdk@v15
|
||||||
@@ -99,7 +102,7 @@ jobs:
|
|||||||
uses: dtolnay/rust-toolchain@master
|
uses: dtolnay/rust-toolchain@master
|
||||||
with:
|
with:
|
||||||
target: wasm32-unknown-emscripten
|
target: wasm32-unknown-emscripten
|
||||||
toolchain: 1.86.0 # we are unfortunately pinned to 1.86.0 for some reason, bulk-memory-opt related issues
|
toolchain: 1.86.0
|
||||||
|
|
||||||
- name: Rust Cache
|
- name: Rust Cache
|
||||||
uses: Swatinem/rust-cache@v2
|
uses: Swatinem/rust-cache@v2
|
||||||
|
|||||||
43
.github/workflows/coverage.yaml
vendored
@@ -1,4 +1,4 @@
|
|||||||
name: Coverage
|
name: Code Coverage
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
on: ["push", "pull_request"]
|
||||||
|
|
||||||
@@ -8,11 +8,10 @@ env:
|
|||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
coverage:
|
coverage:
|
||||||
name: Code Coverage
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Install Rust toolchain
|
- name: Install Rust toolchain
|
||||||
uses: dtolnay/rust-toolchain@master
|
uses: dtolnay/rust-toolchain@master
|
||||||
@@ -43,15 +42,39 @@ jobs:
|
|||||||
|
|
||||||
- uses: taiki-e/install-action@cargo-llvm-cov
|
- uses: taiki-e/install-action@cargo-llvm-cov
|
||||||
- uses: taiki-e/install-action@nextest
|
- uses: taiki-e/install-action@nextest
|
||||||
|
- uses: taiki-e/install-action@just
|
||||||
|
|
||||||
# Note: We manually link zlib. This should be synchronized with the flags set for Linux in .cargo/config.toml.
|
|
||||||
- name: Generate coverage report
|
- name: Generate coverage report
|
||||||
run: |
|
run: |
|
||||||
cargo llvm-cov --no-fail-fast --lcov --output-path lcov.info nextest
|
just coverage
|
||||||
|
|
||||||
|
- name: Download Coveralls CLI
|
||||||
|
run: |
|
||||||
|
# use GitHub Releases URL instead of coveralls.io because they can't maintain their own files; it 404s
|
||||||
|
curl -L https://github.com/coverallsapp/coverage-reporter/releases/download/v0.6.15/coveralls-linux-x86_64.tar.gz | tar -xz -C /usr/local/bin
|
||||||
|
|
||||||
- name: Upload coverage to Coveralls
|
- name: Upload coverage to Coveralls
|
||||||
uses: coverallsapp/github-action@v2
|
env:
|
||||||
with:
|
COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }}
|
||||||
files: ./lcov.info
|
run: |
|
||||||
format: lcov
|
if [ ! -f "lcov.info" ]; then
|
||||||
allow-empty: false
|
echo "Error: lcov.info file not found. Coverage generation may have failed."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
for i in {1..10}; do
|
||||||
|
echo "Attempt $i: Uploading coverage to Coveralls..."
|
||||||
|
if coveralls -n report lcov.info; then
|
||||||
|
echo "Successfully uploaded coverage report."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $i -lt 10 ]; then
|
||||||
|
delay=$((2**i))
|
||||||
|
echo "Attempt $i failed. Retrying in $delay seconds..."
|
||||||
|
sleep $delay
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "Failed to upload coverage report after 10 attempts."
|
||||||
|
exit 1
|
||||||
|
|||||||
@@ -1,19 +1,18 @@
|
|||||||
name: Tests
|
name: Tests & Checks
|
||||||
|
|
||||||
on: ["push", "pull_request"]
|
on: ["push", "pull_request"]
|
||||||
|
|
||||||
env:
|
env:
|
||||||
CARGO_TERM_COLOR: always
|
CARGO_TERM_COLOR: always
|
||||||
RUST_TOOLCHAIN: 1.88.0
|
RUST_TOOLCHAIN: 1.86.0
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test:
|
test:
|
||||||
name: Test
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v5
|
||||||
|
|
||||||
- name: Install Rust toolchain
|
- name: Install Rust toolchain
|
||||||
uses: dtolnay/rust-toolchain@master
|
uses: dtolnay/rust-toolchain@master
|
||||||
@@ -52,3 +51,8 @@ jobs:
|
|||||||
|
|
||||||
- name: Check formatting
|
- name: Check formatting
|
||||||
run: cargo fmt -- --check
|
run: cargo fmt -- --check
|
||||||
|
|
||||||
|
- uses: taiki-e/install-action@cargo-audit
|
||||||
|
|
||||||
|
- name: Run security audit
|
||||||
|
run: cargo audit
|
||||||
16
.gitignore
vendored
@@ -1,7 +1,17 @@
|
|||||||
|
# IDE, Other files
|
||||||
|
.vscode
|
||||||
|
.idea
|
||||||
|
rust-sdl2-emscripten/
|
||||||
|
|
||||||
|
# Build files
|
||||||
target/
|
target/
|
||||||
dist/
|
dist/
|
||||||
emsdk/
|
emsdk/
|
||||||
.idea
|
|
||||||
rust-sdl2-emscripten/
|
# Site build f iles
|
||||||
assets/site/build.css
|
|
||||||
tailwindcss-*
|
tailwindcss-*
|
||||||
|
assets/site/build.css
|
||||||
|
|
||||||
|
# Coverage reports
|
||||||
|
lcov.info
|
||||||
|
coverage.html
|
||||||
|
|||||||
@@ -20,3 +20,15 @@ repos:
|
|||||||
language: system
|
language: system
|
||||||
types: [rust]
|
types: [rust]
|
||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
- id: cargo-check
|
||||||
|
name: cargo check
|
||||||
|
entry: cargo check --all-targets
|
||||||
|
language: system
|
||||||
|
types_or: [rust, cargo, cargo-lock]
|
||||||
|
pass_filenames: false
|
||||||
|
- id: cargo-check-wasm
|
||||||
|
name: cargo check for wasm32-unknown-emscripten
|
||||||
|
entry: cargo check --all-targets --target=wasm32-unknown-emscripten
|
||||||
|
language: system
|
||||||
|
types_or: [rust, cargo, cargo-lock]
|
||||||
|
pass_filenames: false
|
||||||
|
|||||||
993
Cargo.lock
generated
42
Cargo.toml
@@ -6,29 +6,41 @@ edition = "2021"
|
|||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tracing = { version = "0.1.40", features = ["max_level_debug", "release_max_level_debug"]}
|
tracing = { version = "0.1.41", features = ["max_level_debug", "release_max_level_debug"]}
|
||||||
tracing-error = "0.2.0"
|
tracing-error = "0.2.0"
|
||||||
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
|
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
|
||||||
lazy_static = "1.5.0"
|
|
||||||
sdl2 = { version = "0.38.0", features = ["image", "ttf"] }
|
sdl2 = { version = "0.38.0", features = ["image", "ttf"] }
|
||||||
spin_sleep = "1.3.2"
|
spin_sleep = "1.3.2"
|
||||||
rand = { version = "0.9.2", default-features = false, features = ["small_rng", "os_rng"] }
|
rand = { version = "0.9.2", default-features = false, features = ["small_rng", "os_rng"] }
|
||||||
pathfinding = "4.14"
|
pathfinding = "4.14"
|
||||||
once_cell = "1.21.3"
|
thiserror = "2.0.14"
|
||||||
thiserror = "1.0"
|
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
glam = { version = "0.30.4", features = [] }
|
glam = "0.30.5"
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde_json = "1.0.142"
|
||||||
serde_json = "1.0.141"
|
smallvec = "1.15.1"
|
||||||
|
strum = "0.27.2"
|
||||||
|
strum_macros = "0.27.2"
|
||||||
|
phf = { version = "0.12.1", features = ["macros"] }
|
||||||
|
bevy_ecs = "0.16.1"
|
||||||
|
bitflags = "2.9.1"
|
||||||
|
parking_lot = "0.12.3"
|
||||||
|
micromap = "0.1.0"
|
||||||
|
thousands = "0.2.0"
|
||||||
|
pretty_assertions = "1.4.1"
|
||||||
|
num-width = "0.1.0"
|
||||||
|
circular-buffer = "1.1.0"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
lto = true
|
lto = true
|
||||||
panic = "abort"
|
panic = "abort"
|
||||||
opt-level = "z"
|
opt-level = "z"
|
||||||
|
|
||||||
[target.'cfg(target_os = "windows")'.dependencies.winapi]
|
[target.'cfg(target_os = "windows")'.dependencies]
|
||||||
version = "0.3"
|
windows = { version = "0.61.3", features = ["Win32_Security", "Win32_Storage_FileSystem", "Win32_System_Console"] }
|
||||||
features = ["consoleapi", "fileapi", "handleapi", "processenv", "winbase", "wincon", "winnt", "winuser", "windef", "minwindef"]
|
windows-sys = { version = "0.60.2", features = ["Win32_System_Console"] }
|
||||||
|
|
||||||
|
[target.'cfg(not(target_os = "windows"))'.dependencies]
|
||||||
|
libc = "0.2"
|
||||||
|
|
||||||
|
|
||||||
[target.'cfg(target_os = "emscripten")'.dependencies.sdl2]
|
[target.'cfg(target_os = "emscripten")'.dependencies.sdl2]
|
||||||
@@ -53,4 +65,12 @@ x86_64-apple-darwin = { triplet = "x64-osx" }
|
|||||||
aarch64-apple-darwin = { triplet = "arm64-osx" }
|
aarch64-apple-darwin = { triplet = "arm64-osx" }
|
||||||
|
|
||||||
[target.'cfg(target_os = "emscripten")'.dependencies]
|
[target.'cfg(target_os = "emscripten")'.dependencies]
|
||||||
libc = "0.2.16"
|
libc = "0.2.175"
|
||||||
|
|
||||||
|
[build-dependencies]
|
||||||
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
serde_json = "1.0"
|
||||||
|
phf = { version = "0.12.1", features = ["macros"] }
|
||||||
|
|
||||||
|
[package.metadata.cargo-machete]
|
||||||
|
ignored = ["phf"]
|
||||||
|
|||||||
33
Justfile
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
set shell := ["bash", "-c"]
|
||||||
|
set windows-shell := ["powershell.exe", "-NoLogo", "-Command"]
|
||||||
|
|
||||||
|
# Regex to exclude files from coverage report, double escapes for Justfile + CLI
|
||||||
|
# You can use src\\\\..., but the filename alone is acceptable too
|
||||||
|
coverage_exclude_pattern := "src\\\\app.rs|audio.rs|src\\\\error.rs|platform\\\\emscripten.rs"
|
||||||
|
|
||||||
|
# !!! --ignore-filename-regex should be used on both reports & coverage testing
|
||||||
|
# !!! --remap-path-prefix prevents the absolute path from being used in the generated report
|
||||||
|
|
||||||
|
# Generate HTML report (for humans, source line inspection)
|
||||||
|
html: coverage
|
||||||
|
cargo llvm-cov report \
|
||||||
|
--remap-path-prefix \
|
||||||
|
--ignore-filename-regex "{{ coverage_exclude_pattern }}" \
|
||||||
|
--html \
|
||||||
|
--open
|
||||||
|
|
||||||
|
# Display report (for humans)
|
||||||
|
report-coverage: coverage
|
||||||
|
cargo llvm-cov report \
|
||||||
|
--remap-path-prefix \
|
||||||
|
--ignore-filename-regex "{{ coverage_exclude_pattern }}"
|
||||||
|
|
||||||
|
# Run & generate report (for CI)
|
||||||
|
coverage:
|
||||||
|
cargo llvm-cov \
|
||||||
|
--lcov \
|
||||||
|
--remap-path-prefix \
|
||||||
|
--ignore-filename-regex "{{ coverage_exclude_pattern }}" \
|
||||||
|
--output-path lcov.info \
|
||||||
|
--profile coverage \
|
||||||
|
--no-fail-fast nextest
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
# Pac-Man
|
# Pac-Man
|
||||||
|
|
||||||
[![Tests Status][badge-test]][test] [![Build Status][badge-build]][build] [![Code Coverage][badge-coverage]][coverage] [![Online Demo][badge-online-demo]][demo] [![Last Commit][badge-last-commit]][commits]
|
[![Tests Status][badge-test]][test] [![Build Status][badge-build]][build] [![If you're seeing this, Coveralls.io is broken again and it's not my fault.][badge-coverage]][coverage] [![Online Demo][badge-online-demo]][demo] [![Last Commit][badge-last-commit]][commits]
|
||||||
|
|
||||||
[badge-test]: https://github.com/Xevion/Pac-Man/actions/workflows/test.yaml/badge.svg
|
[badge-test]: https://github.com/Xevion/Pac-Man/actions/workflows/tests.yaml/badge.svg
|
||||||
[badge-build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml/badge.svg
|
[badge-build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml/badge.svg
|
||||||
[badge-coverage]: https://coveralls.io/repos/github/Xevion/Pac-Man/badge.svg?branch=master
|
[badge-coverage]: https://coveralls.io/repos/github/Xevion/Pac-Man/badge.svg?branch=master
|
||||||
[badge-demo]: https://img.shields.io/github/deployments/Xevion/Pac-Man/github-pages?label=GitHub%20Pages
|
[badge-demo]: https://img.shields.io/github/deployments/Xevion/Pac-Man/github-pages?label=GitHub%20Pages
|
||||||
[badge-online-demo]: https://img.shields.io/badge/GitHub%20Pages-Demo-brightgreen
|
[badge-online-demo]: https://img.shields.io/badge/GitHub%20Pages-Demo-brightgreen
|
||||||
[badge-last-commit]: https://img.shields.io/github/last-commit/Xevion/Pac-Man
|
[badge-last-commit]: https://img.shields.io/github/last-commit/Xevion/Pac-Man
|
||||||
[build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml
|
[build]: https://github.com/Xevion/Pac-Man/actions/workflows/build.yaml
|
||||||
[test]: https://github.com/Xevion/Pac-Man/actions/workflows/test.yaml
|
[test]: https://github.com/Xevion/Pac-Man/actions/workflows/tests.yaml
|
||||||
[coverage]: https://coveralls.io/github/Xevion/Pac-Man?branch=master
|
[coverage]: https://coveralls.io/github/Xevion/Pac-Man?branch=master
|
||||||
[demo]: https://xevion.github.io/Pac-Man/
|
[demo]: https://xevion.github.io/Pac-Man/
|
||||||
[commits]: https://github.com/Xevion/Pac-Man/commits/master
|
[commits]: https://github.com/Xevion/Pac-Man/commits/master
|
||||||
@@ -72,6 +72,8 @@ I wanted to hit a log of goals and features, making it a 'perfect' project that
|
|||||||
|
|
||||||
Since this project is still in progress, I'm only going to cover non-obvious build details. By reading the code, build scripts, and copying the online build workflows, you should be able to replicate the build process.
|
Since this project is still in progress, I'm only going to cover non-obvious build details. By reading the code, build scripts, and copying the online build workflows, you should be able to replicate the build process.
|
||||||
|
|
||||||
|
- We use rustc 1.86.0 for the build, due to bulk-memory-opt related issues on wasm32-unknown-emscripten.
|
||||||
|
- Technically, we could probably use stable or even nightly on desktop targets, but using different versions for different targets is a pain, mainly because of clippy warnings changing between versions.
|
||||||
- Install `cargo-vcpkg` with `cargo install cargo-vcpkg`, then run `cargo vcpkg build` to build the requisite dependencies via vcpkg.
|
- Install `cargo-vcpkg` with `cargo install cargo-vcpkg`, then run `cargo vcpkg build` to build the requisite dependencies via vcpkg.
|
||||||
- For the WASM build, you need to have the Emscripten SDK cloned; you can do so with `git clone https://github.com/emscripten-core/emsdk.git`
|
- For the WASM build, you need to have the Emscripten SDK cloned; you can do so with `git clone https://github.com/emscripten-core/emsdk.git`
|
||||||
- The first time you clone, you'll need to install the appropriate SDK version with `./emsdk install 3.1.43` and then activate it with `./emsdk activate 3.1.43`. On Windows, use `./emsdk/emsdk.ps1` instead.
|
- The first time you clone, you'll need to install the appropriate SDK version with `./emsdk install 3.1.43` and then activate it with `./emsdk activate 3.1.43`. On Windows, use `./emsdk/emsdk.ps1` instead.
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 23 KiB After Width: | Height: | Size: 16 KiB |
@@ -30,7 +30,7 @@
|
|||||||
width="80"
|
width="80"
|
||||||
height="80"
|
height="80"
|
||||||
viewBox="0 0 250 250"
|
viewBox="0 0 250 250"
|
||||||
class="fill-yellow-400 text-white"
|
class="fill-yellow-400 [&>.octo-arm,.octo-body]:fill-black"
|
||||||
aria-hidden="true"
|
aria-hidden="true"
|
||||||
>
|
>
|
||||||
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
|
<path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
|
||||||
@@ -46,16 +46,12 @@
|
|||||||
</a>
|
</a>
|
||||||
|
|
||||||
<div class="min-h-screen flex flex-col">
|
<div class="min-h-screen flex flex-col">
|
||||||
<header class="pt-10">
|
|
||||||
<h1 class="text-4xl arcade-title scaled-text">Pac-Man in Rust</h1>
|
|
||||||
</header>
|
|
||||||
|
|
||||||
<main class="flex-1 flex items-center justify-center px-4">
|
<main class="flex-1 flex items-center justify-center px-4">
|
||||||
<div class="w-full max-w-5xl">
|
<div class="w-full max-w-5xl">
|
||||||
<canvas
|
<canvas
|
||||||
id="canvas"
|
id="canvas"
|
||||||
oncontextmenu="event.preventDefault()"
|
oncontextmenu="event.preventDefault()"
|
||||||
class="block bg-black w-full max-w-[90vw] h-auto rounded-xl shadow-[inset_0_0_0_2px_rgba(255,255,255,0.12),0_10px_30px_rgba(0,0,0,0.8)]"
|
class="block w-full h-full max-h-[90vh] aspect-square"
|
||||||
></canvas>
|
></canvas>
|
||||||
|
|
||||||
<div
|
<div
|
||||||
|
|||||||
|
Before Width: | Height: | Size: 1.8 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
BIN
assets/unpacked/maze/tiles/0.png
Normal file
|
After Width: | Height: | Size: 102 B |
BIN
assets/unpacked/maze/tiles/1.png
Normal file
|
After Width: | Height: | Size: 79 B |
BIN
assets/unpacked/maze/tiles/10.png
Normal file
|
After Width: | Height: | Size: 84 B |
BIN
assets/unpacked/maze/tiles/11.png
Normal file
|
After Width: | Height: | Size: 77 B |
BIN
assets/unpacked/maze/tiles/12.png
Normal file
|
After Width: | Height: | Size: 80 B |
BIN
assets/unpacked/maze/tiles/13.png
Normal file
|
After Width: | Height: | Size: 87 B |
BIN
assets/unpacked/maze/tiles/14.png
Normal file
|
After Width: | Height: | Size: 79 B |
BIN
assets/unpacked/maze/tiles/15.png
Normal file
|
After Width: | Height: | Size: 89 B |
BIN
assets/unpacked/maze/tiles/16.png
Normal file
|
After Width: | Height: | Size: 91 B |
BIN
assets/unpacked/maze/tiles/17.png
Normal file
|
After Width: | Height: | Size: 87 B |
BIN
assets/unpacked/maze/tiles/18.png
Normal file
|
After Width: | Height: | Size: 107 B |
BIN
assets/unpacked/maze/tiles/19.png
Normal file
|
After Width: | Height: | Size: 77 B |
BIN
assets/unpacked/maze/tiles/2.png
Normal file
|
After Width: | Height: | Size: 93 B |
BIN
assets/unpacked/maze/tiles/20.png
Normal file
|
After Width: | Height: | Size: 91 B |
BIN
assets/unpacked/maze/tiles/21.png
Normal file
|
After Width: | Height: | Size: 97 B |
BIN
assets/unpacked/maze/tiles/22.png
Normal file
|
After Width: | Height: | Size: 107 B |
BIN
assets/unpacked/maze/tiles/23.png
Normal file
|
After Width: | Height: | Size: 88 B |
BIN
assets/unpacked/maze/tiles/24.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/25.png
Normal file
|
After Width: | Height: | Size: 80 B |
BIN
assets/unpacked/maze/tiles/26.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/27.png
Normal file
|
After Width: | Height: | Size: 93 B |
BIN
assets/unpacked/maze/tiles/28.png
Normal file
|
After Width: | Height: | Size: 89 B |
BIN
assets/unpacked/maze/tiles/29.png
Normal file
|
After Width: | Height: | Size: 90 B |
BIN
assets/unpacked/maze/tiles/3.png
Normal file
|
After Width: | Height: | Size: 87 B |
BIN
assets/unpacked/maze/tiles/30.png
Normal file
|
After Width: | Height: | Size: 79 B |
BIN
assets/unpacked/maze/tiles/31.png
Normal file
|
After Width: | Height: | Size: 100 B |
BIN
assets/unpacked/maze/tiles/32.png
Normal file
|
After Width: | Height: | Size: 98 B |
BIN
assets/unpacked/maze/tiles/33.png
Normal file
|
After Width: | Height: | Size: 96 B |
BIN
assets/unpacked/maze/tiles/34.png
Normal file
|
After Width: | Height: | Size: 100 B |
BIN
assets/unpacked/maze/tiles/4.png
Normal file
|
After Width: | Height: | Size: 105 B |
BIN
assets/unpacked/maze/tiles/5.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/6.png
Normal file
|
After Width: | Height: | Size: 71 B |
BIN
assets/unpacked/maze/tiles/7.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/8.png
Normal file
|
After Width: | Height: | Size: 82 B |
BIN
assets/unpacked/maze/tiles/9.png
Normal file
|
After Width: | Height: | Size: 82 B |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.6 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
|
Before Width: | Height: | Size: 2.7 KiB |
86
bacon.toml
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
# This is a configuration file for the bacon tool
|
||||||
|
#
|
||||||
|
# Complete help on configuration: https://dystroy.org/bacon/config/
|
||||||
|
#
|
||||||
|
# You may check the current default at
|
||||||
|
# https://github.com/Canop/bacon/blob/main/defaults/default-bacon.toml
|
||||||
|
|
||||||
|
default_job = "check"
|
||||||
|
env.CARGO_TERM_COLOR = "always"
|
||||||
|
|
||||||
|
[jobs.check]
|
||||||
|
command = ["cargo", "check"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
[jobs.check-all]
|
||||||
|
command = ["cargo", "check", "--all-targets"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
# Run clippy on the default target
|
||||||
|
[jobs.clippy]
|
||||||
|
command = ["cargo", "clippy"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
# Run clippy on all targets
|
||||||
|
[jobs.clippy-all]
|
||||||
|
command = ["cargo", "clippy", "--all-targets"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
[jobs.test]
|
||||||
|
command = [
|
||||||
|
"cargo", "nextest", "run",
|
||||||
|
"--hide-progress-bar", "--failure-output", "final"
|
||||||
|
]
|
||||||
|
need_stdout = true
|
||||||
|
analyzer = "nextest"
|
||||||
|
|
||||||
|
[jobs.coverage]
|
||||||
|
command = [
|
||||||
|
"just", "report-coverage"
|
||||||
|
]
|
||||||
|
need_stdout = true
|
||||||
|
ignored_lines = [
|
||||||
|
"info:",
|
||||||
|
"\\s+Compiling",
|
||||||
|
"test result: ok",
|
||||||
|
"^\\s*$",
|
||||||
|
"running \\d+ test",
|
||||||
|
"Nextest run ID",
|
||||||
|
"[─]+",
|
||||||
|
"test.+ok",
|
||||||
|
"PASS|START",
|
||||||
|
"Starting \\d+ test",
|
||||||
|
"\\s*#",
|
||||||
|
"\\s*Finished.+in \\d+",
|
||||||
|
"\\s*Summary\\s+\\[",
|
||||||
|
"\\s*Blocking",
|
||||||
|
"Finished report saved to"
|
||||||
|
]
|
||||||
|
on_change_strategy = "wait_then_restart"
|
||||||
|
|
||||||
|
[jobs.doc]
|
||||||
|
command = ["cargo", "doc", "--no-deps"]
|
||||||
|
need_stdout = false
|
||||||
|
|
||||||
|
# If the doc compiles, then it opens in your browser and bacon switches to the previous job
|
||||||
|
[jobs.doc-open]
|
||||||
|
command = ["cargo", "doc", "--no-deps", "--open"]
|
||||||
|
need_stdout = false
|
||||||
|
on_success = "back" # so that we don't open the browser at each change
|
||||||
|
|
||||||
|
[jobs.run]
|
||||||
|
command = [
|
||||||
|
"cargo", "run",
|
||||||
|
]
|
||||||
|
need_stdout = true
|
||||||
|
allow_warnings = true
|
||||||
|
background = false
|
||||||
|
on_change_strategy = "kill_then_restart"
|
||||||
|
# kill = ["pkill", "-TERM", "-P"]'
|
||||||
|
|
||||||
|
[keybindings]
|
||||||
|
c = "job:clippy"
|
||||||
|
alt-c = "job:check"
|
||||||
|
ctrl-alt-c = "job:check-all"
|
||||||
|
shift-c = "job:clippy-all"
|
||||||
|
f = "job:coverage"
|
||||||
54
build.rs
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
use std::collections::HashMap;
|
||||||
|
use std::env;
|
||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufWriter, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
struct AtlasMapper {
|
||||||
|
frames: HashMap<String, MapperFrame>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Copy, Clone, Debug, Deserialize)]
|
||||||
|
struct MapperFrame {
|
||||||
|
x: u16,
|
||||||
|
y: u16,
|
||||||
|
width: u16,
|
||||||
|
height: u16,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MapperFrame {
|
||||||
|
fn to_u16vec2_format(self) -> String {
|
||||||
|
format!(
|
||||||
|
"MapperFrame {{ pos: glam::U16Vec2::new({}, {}), size: glam::U16Vec2::new({}, {}) }}",
|
||||||
|
self.x, self.y, self.width, self.height
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let path = Path::new(&env::var("OUT_DIR").unwrap()).join("atlas_data.rs");
|
||||||
|
let mut file = BufWriter::new(File::create(&path).unwrap());
|
||||||
|
|
||||||
|
let atlas_json = include_str!("./assets/game/atlas.json");
|
||||||
|
let atlas_mapper: AtlasMapper = serde_json::from_str(atlas_json).unwrap();
|
||||||
|
|
||||||
|
writeln!(&mut file, "use phf::phf_map;").unwrap();
|
||||||
|
|
||||||
|
writeln!(&mut file, "use crate::texture::sprite::MapperFrame;").unwrap();
|
||||||
|
|
||||||
|
writeln!(
|
||||||
|
&mut file,
|
||||||
|
"pub static ATLAS_FRAMES: phf::Map<&'static str, MapperFrame> = phf_map! {{"
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
for (name, frame) in atlas_mapper.frames {
|
||||||
|
writeln!(&mut file, " \"{}\" => {},", name, frame.to_u16vec2_format()).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
writeln!(&mut file, "}};").unwrap();
|
||||||
|
println!("cargo:rerun-if-changed=assets/game/atlas.json");
|
||||||
|
}
|
||||||
4
rust-toolchain.toml
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
[toolchain]
|
||||||
|
# we are unfortunately pinned to 1.86.0 for some reason, bulk-memory-opt related issues on wasm32-unknown-emscripten
|
||||||
|
channel = "1.86.0"
|
||||||
|
components = ["rustfmt", "llvm-tools-preview", "clippy"]
|
||||||
165
src/app.rs
@@ -1,35 +1,48 @@
|
|||||||
use std::time::{Duration, Instant};
|
use std::time::{Duration, Instant};
|
||||||
|
|
||||||
use anyhow::{anyhow, Result};
|
use sdl2::render::TextureCreator;
|
||||||
use sdl2::event::{Event, WindowEvent};
|
use sdl2::ttf::Sdl2TtfContext;
|
||||||
use sdl2::keyboard::Keycode;
|
use sdl2::video::WindowContext;
|
||||||
use sdl2::render::{Canvas, ScaleMode, Texture, TextureCreator};
|
use sdl2::{AudioSubsystem, EventPump, Sdl, VideoSubsystem};
|
||||||
use sdl2::video::{Window, WindowContext};
|
|
||||||
use sdl2::EventPump;
|
use crate::error::{GameError, GameResult};
|
||||||
use tracing::{error, event};
|
|
||||||
|
|
||||||
use crate::constants::{CANVAS_SIZE, LOOP_TIME, SCALE};
|
use crate::constants::{CANVAS_SIZE, LOOP_TIME, SCALE};
|
||||||
use crate::game::Game;
|
use crate::game::Game;
|
||||||
use crate::platform::get_platform;
|
use crate::platform::get_platform;
|
||||||
|
|
||||||
pub struct App<'a> {
|
/// Main application wrapper that manages SDL initialization, window lifecycle, and the game loop.
|
||||||
game: Game,
|
///
|
||||||
canvas: Canvas<Window>,
|
/// Handles platform-specific setup, maintains consistent frame timing, and delegates
|
||||||
event_pump: EventPump,
|
/// game logic to the contained `Game` instance. The app manages focus state to
|
||||||
backbuffer: Texture<'a>,
|
/// optimize CPU usage when the window loses focus.
|
||||||
paused: bool,
|
pub struct App {
|
||||||
|
pub game: Game,
|
||||||
last_tick: Instant,
|
last_tick: Instant,
|
||||||
|
focused: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App<'_> {
|
impl App {
|
||||||
pub fn new() -> Result<Self> {
|
/// Initializes SDL subsystems, creates the game window, and sets up the game state.
|
||||||
// Initialize platform-specific console
|
///
|
||||||
get_platform().init_console().map_err(|e| anyhow!(e))?;
|
/// Performs comprehensive initialization including video/audio subsystems,
|
||||||
|
/// window creation with proper scaling, and canvas configuration. All SDL
|
||||||
let sdl_context = sdl2::init().map_err(|e| anyhow!(e))?;
|
/// resources are leaked to maintain 'static lifetimes required by the game architecture.
|
||||||
let video_subsystem = sdl_context.video().map_err(|e| anyhow!(e))?;
|
///
|
||||||
let audio_subsystem = sdl_context.audio().map_err(|e| anyhow!(e))?;
|
/// # Errors
|
||||||
let ttf_context = sdl2::ttf::init().map_err(|e| anyhow!(e.to_string()))?;
|
///
|
||||||
|
/// Returns `GameError::Sdl` if any SDL initialization step fails, or propagates
|
||||||
|
/// errors from `Game::new()` during game state setup.
|
||||||
|
pub fn new() -> GameResult<Self> {
|
||||||
|
let sdl_context: &'static Sdl = Box::leak(Box::new(sdl2::init().map_err(|e| GameError::Sdl(e.to_string()))?));
|
||||||
|
let video_subsystem: &'static VideoSubsystem =
|
||||||
|
Box::leak(Box::new(sdl_context.video().map_err(|e| GameError::Sdl(e.to_string()))?));
|
||||||
|
let _audio_subsystem: &'static AudioSubsystem =
|
||||||
|
Box::leak(Box::new(sdl_context.audio().map_err(|e| GameError::Sdl(e.to_string()))?));
|
||||||
|
let _ttf_context: &'static Sdl2TtfContext =
|
||||||
|
Box::leak(Box::new(sdl2::ttf::init().map_err(|e| GameError::Sdl(e.to_string()))?));
|
||||||
|
let event_pump: &'static mut EventPump =
|
||||||
|
Box::leak(Box::new(sdl_context.event_pump().map_err(|e| GameError::Sdl(e.to_string()))?));
|
||||||
|
|
||||||
let window = video_subsystem
|
let window = video_subsystem
|
||||||
.window(
|
.window(
|
||||||
@@ -39,104 +52,62 @@ impl App<'_> {
|
|||||||
)
|
)
|
||||||
.resizable()
|
.resizable()
|
||||||
.position_centered()
|
.position_centered()
|
||||||
.build()?;
|
.build()
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
let mut canvas = window.into_canvas().build()?;
|
let canvas = Box::leak(Box::new(
|
||||||
canvas.set_logical_size(CANVAS_SIZE.x, CANVAS_SIZE.y)?;
|
window
|
||||||
|
.into_canvas()
|
||||||
|
.accelerated()
|
||||||
|
.build()
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?,
|
||||||
|
));
|
||||||
|
|
||||||
let texture_creator_static: &'static TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
canvas
|
||||||
|
.set_logical_size(CANVAS_SIZE.x, CANVAS_SIZE.y)
|
||||||
|
.map_err(|e| GameError::Sdl(e.to_string()))?;
|
||||||
|
|
||||||
let mut game = Game::new(texture_creator_static, &ttf_context, &audio_subsystem);
|
let texture_creator: &'static mut TextureCreator<WindowContext> = Box::leak(Box::new(canvas.texture_creator()));
|
||||||
game.audio.set_mute(cfg!(debug_assertions));
|
|
||||||
|
|
||||||
let mut backbuffer = texture_creator_static.create_texture_target(None, CANVAS_SIZE.x, CANVAS_SIZE.y)?;
|
let game = Game::new(canvas, texture_creator, event_pump)?;
|
||||||
backbuffer.set_scale_mode(ScaleMode::Nearest);
|
// game.audio.set_mute(cfg!(debug_assertions));
|
||||||
|
|
||||||
let event_pump = sdl_context.event_pump().map_err(|e| anyhow!(e))?;
|
Ok(App {
|
||||||
|
|
||||||
// Initial draw
|
|
||||||
game.draw(&mut canvas, &mut backbuffer)?;
|
|
||||||
game.present_backbuffer(&mut canvas, &backbuffer)?;
|
|
||||||
|
|
||||||
Ok(Self {
|
|
||||||
game,
|
game,
|
||||||
canvas,
|
focused: true,
|
||||||
event_pump,
|
|
||||||
backbuffer,
|
|
||||||
paused: false,
|
|
||||||
last_tick: Instant::now(),
|
last_tick: Instant::now(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Executes a single frame of the game loop with consistent timing and optional sleep.
|
||||||
|
///
|
||||||
|
/// Calculates delta time since the last frame, runs game logic via `game.tick()`,
|
||||||
|
/// and implements frame rate limiting by sleeping for remaining time if the frame
|
||||||
|
/// completed faster than the target `LOOP_TIME`. Sleep behavior varies based on
|
||||||
|
/// window focus to conserve CPU when the game is not active.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// `true` if the game should continue running, `false` if the game requested exit.
|
||||||
pub fn run(&mut self) -> bool {
|
pub fn run(&mut self) -> bool {
|
||||||
{
|
{
|
||||||
let start = Instant::now();
|
let start = Instant::now();
|
||||||
|
|
||||||
for event in self.event_pump.poll_iter() {
|
|
||||||
match event {
|
|
||||||
Event::Window { win_event, .. } => match win_event {
|
|
||||||
WindowEvent::Hidden => {
|
|
||||||
event!(tracing::Level::DEBUG, "Window hidden");
|
|
||||||
}
|
|
||||||
WindowEvent::Shown => {
|
|
||||||
event!(tracing::Level::DEBUG, "Window shown");
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
},
|
|
||||||
// It doesn't really make sense to have this available in the browser
|
|
||||||
#[cfg(not(target_os = "emscripten"))]
|
|
||||||
Event::Quit { .. }
|
|
||||||
| Event::KeyDown {
|
|
||||||
keycode: Some(Keycode::Escape) | Some(Keycode::Q),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
event!(tracing::Level::INFO, "Exit requested. Exiting...");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
Event::KeyDown {
|
|
||||||
keycode: Some(Keycode::P),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
self.paused = !self.paused;
|
|
||||||
event!(tracing::Level::INFO, "{}", if self.paused { "Paused" } else { "Unpaused" });
|
|
||||||
}
|
|
||||||
Event::KeyDown {
|
|
||||||
keycode: Some(Keycode::Space),
|
|
||||||
..
|
|
||||||
} => {
|
|
||||||
self.game.debug_mode = !self.game.debug_mode;
|
|
||||||
}
|
|
||||||
Event::KeyDown { keycode, .. } => {
|
|
||||||
self.game.keyboard_event(keycode.unwrap());
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let dt = self.last_tick.elapsed().as_secs_f32();
|
let dt = self.last_tick.elapsed().as_secs_f32();
|
||||||
self.last_tick = Instant::now();
|
self.last_tick = Instant::now();
|
||||||
|
|
||||||
if !self.paused {
|
let exit = self.game.tick(dt);
|
||||||
self.game.tick(dt);
|
|
||||||
if let Err(e) = self.game.draw(&mut self.canvas, &mut self.backbuffer) {
|
if exit {
|
||||||
error!("Failed to draw game: {e}");
|
return false;
|
||||||
}
|
|
||||||
if let Err(e) = self.game.present_backbuffer(&mut self.canvas, &self.backbuffer) {
|
|
||||||
error!("Failed to present backbuffer: {e}");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sleep if we still have time left
|
||||||
if start.elapsed() < LOOP_TIME {
|
if start.elapsed() < LOOP_TIME {
|
||||||
let time = LOOP_TIME.saturating_sub(start.elapsed());
|
let time = LOOP_TIME.saturating_sub(start.elapsed());
|
||||||
if time != Duration::ZERO {
|
if time != Duration::ZERO {
|
||||||
get_platform().sleep(time);
|
get_platform().sleep(time, self.focused);
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
event!(
|
|
||||||
tracing::Level::WARN,
|
|
||||||
"Game loop behind schedule by: {:?}",
|
|
||||||
start.elapsed() - LOOP_TIME
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
true
|
true
|
||||||
|
|||||||