Compare commits

...

9 Commits

10 changed files with 98 additions and 70 deletions

View File

@@ -11,8 +11,6 @@ env:
jobs: jobs:
build: build:
name: Build (${{ matrix.target }}) name: Build (${{ matrix.target }})
env:
VCPKG_SYSTEM_LIBRARIES: "OFF"
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
@@ -46,16 +44,18 @@ jobs:
- name: Cache vcpkg - name: Cache vcpkg
uses: actions/cache@v4 uses: actions/cache@v4
with: with:
save-always: true # deprecated
path: target/vcpkg path: target/vcpkg
key: ${{ runner.os }}-vcpkg-${{ hashFiles('Cargo.toml', 'Cargo.lock') }} key: vcpkg-${{ runner.os }}-${{ matrix.target }}-${{ hashFiles('Cargo.toml', 'Cargo.lock') }}
restore-keys: | restore-keys: |
${{ runner.os }}-vcpkg- ${{ runner.os }}-vcpkg
vcpkg-${{ runner.os }}-${{ matrix.target }}-
- name: Vcpkg Linux Dependencies - name: Vcpkg Linux Dependencies
if: runner.os == 'Linux' if: runner.os == 'Linux'
run: | run: |
sudo apt-get update sudo apt-get update
sudo apt-get install -y build-essential gettext libltdl-dev sudo apt-get install -y build-essential gettext libltdl-dev zlib1g-dev
- name: Vcpkg - name: Vcpkg
run: | run: |
@@ -91,7 +91,7 @@ jobs:
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Setup Emscripten SDK - name: Setup Emscripten SDK
uses: mymindstorm/setup-emsdk@v14 uses: pyodide/setup-emsdk@v15
with: with:
version: 3.1.43 version: 3.1.43
actions-cache-folder: "emsdk-cache" actions-cache-folder: "emsdk-cache"
@@ -111,8 +111,21 @@ jobs:
version: 8 version: 8
run_install: true run_install: true
- name: Build - name: Build with Emscripten
run: ./build.sh -er # release mode, skip emsdk run: |
cargo build --target=wasm32-unknown-emscripten --release
- name: Assemble
run: |
echo "Generating CSS"
pnpx postcss-cli ./assets/site/styles.scss -o ./assets/site/build.css
echo "Copying WASM files"
mkdir -p dist
cp assets/site/{build.css,favicon.ico,index.html} dist
output_folder="target/wasm32-unknown-emscripten/release"
cp $output_folder/pacman.{wasm,js} $output_folder/deps/pacman.data dist
- name: Upload Artifact - name: Upload Artifact
uses: actions/upload-pages-artifact@v3 uses: actions/upload-pages-artifact@v3

40
Cargo.lock generated
View File

@@ -212,15 +212,6 @@ version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58"
[[package]]
name = "ppv-lite86"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9"
dependencies = [
"zerocopy",
]
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.95" version = "1.0.95"
@@ -251,17 +242,6 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
dependencies = [ dependencies = [
"rand_chacha",
"rand_core",
]
[[package]]
name = "rand_chacha"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb"
dependencies = [
"ppv-lite86",
"rand_core", "rand_core",
] ]
@@ -647,23 +627,3 @@ checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [ dependencies = [
"bitflags 2.9.1", "bitflags 2.9.1",
] ]
[[package]]
name = "zerocopy"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1039dd0d3c310cf05de012d8a39ff557cb0d23087fd44cad61df08fc31907a2f"
dependencies = [
"zerocopy-derive",
]
[[package]]
name = "zerocopy-derive"
version = "0.8.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9ecf5b4cc5364572d7f4c329661bcc82724222973f2cab6f050a4e5c22f75181"
dependencies = [
"proc-macro2",
"quote",
"syn",
]

View File

@@ -12,7 +12,7 @@ tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
lazy_static = "1.5.0" 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 = "0.9.2" rand = { version = "0.9.2", default-features = false, features = ["small_rng", "os_rng"] }
pathfinding = "4.14" pathfinding = "4.14"
once_cell = "1.21.3" once_cell = "1.21.3"
thiserror = "1.0" thiserror = "1.0"
@@ -42,7 +42,8 @@ rev = "2024.05.24" # release 2024.05.24 # to check for a new one, check https://
[package.metadata.vcpkg.target] [package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" } x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }
x86_64-unknown-linux-gnu = { triplet = "x64-linux" } x86_64-unknown-linux-gnu = { triplet = "x64-linux" }
# x86_64-apple-darwin = { triplet = "x64-osx-release" } x86_64-apple-darwin = { triplet = "x64-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.16"

View File

@@ -78,15 +78,17 @@ mod imp {
#[cfg(target_os = "emscripten")] #[cfg(target_os = "emscripten")]
mod imp { mod imp {
use super::*; use super::*;
use std::fs; use sdl2::rwops::RWops;
use std::path::Path; use std::io::Read;
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> { pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
let path = Path::new("assets/game").join(asset.path()); let path = format!("assets/game/{}", asset.path());
if !path.exists() { let mut rwops = RWops::from_file(&path, "rb").map_err(|_| AssetError::NotFound(asset.path().to_string()))?;
return Err(AssetError::NotFound(asset.path().to_string())); let len = rwops.len().ok_or_else(|| AssetError::NotFound(asset.path().to_string()))?;
} let mut buf = vec![0u8; len];
let bytes = fs::read(&path)?; rwops
Ok(Cow::Owned(bytes)) .read_exact(&mut buf)
.map_err(|e| AssetError::Io(std::io::Error::new(std::io::ErrorKind::Other, e)))?;
Ok(Cow::Owned(buf))
} }
} }

View File

@@ -24,7 +24,7 @@ impl Audio {
let frequency = 44100; let frequency = 44100;
let format = DEFAULT_FORMAT; let format = DEFAULT_FORMAT;
let channels = 4; let channels = 4;
let chunk_size = 128; let chunk_size = 256; // 256 is minimum for emscripten
mixer::open_audio(frequency, format, 1, chunk_size).expect("Failed to open audio"); mixer::open_audio(frequency, format, 1, chunk_size).expect("Failed to open audio");
mixer::allocate_channels(channels); mixer::allocate_channels(channels);

31
src/emscripten.rs Normal file
View File

@@ -0,0 +1,31 @@
#[allow(dead_code)]
#[cfg(target_os = "emscripten")]
pub mod emscripten {
use std::os::raw::c_uint;
extern "C" {
pub fn emscripten_get_now() -> f64;
pub fn emscripten_sleep(ms: c_uint);
pub fn emscripten_get_element_css_size(target: *const u8, width: *mut f64, height: *mut f64) -> i32;
}
// milliseconds since start of program
pub fn now() -> f64 {
unsafe { emscripten_get_now() }
}
pub fn sleep(ms: u32) {
unsafe {
emscripten_sleep(ms);
}
}
pub fn get_canvas_size() -> (u32, u32) {
let mut width = 0.0;
let mut height = 0.0;
unsafe {
emscripten_get_element_css_size("canvas\0".as_ptr(), &mut width, &mut height);
}
(width as u32, height as u32)
}
}

View File

@@ -4,7 +4,9 @@ use std::ops::Not;
use std::rc::Rc; use std::rc::Rc;
use glam::UVec2; use glam::UVec2;
use rand::rngs::SmallRng;
use rand::seq::IteratorRandom; use rand::seq::IteratorRandom;
use rand::SeedableRng;
use sdl2::image::LoadTexture; use sdl2::image::LoadTexture;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::render::{Texture, TextureCreator}; use sdl2::render::{Texture, TextureCreator};
@@ -211,7 +213,7 @@ impl<'a> Game<'a> {
{ {
let mut map = self.map.borrow_mut(); let mut map = self.map.borrow_mut();
let valid_positions = map.get_valid_playable_positions(); let valid_positions = map.get_valid_playable_positions();
let mut rng = rand::rng(); let mut rng = SmallRng::from_os_rng();
// Randomize Pac-Man position // Randomize Pac-Man position
if let Some(pos) = valid_positions.iter().choose(&mut rng) { if let Some(pos) = valid_positions.iter().choose(&mut rng) {

View File

@@ -1,4 +1,6 @@
use rand::rngs::SmallRng;
use rand::Rng; use rand::Rng;
use rand::SeedableRng;
use crate::animation::{AnimatedAtlasTexture, FrameDrawn}; use crate::animation::{AnimatedAtlasTexture, FrameDrawn};
use crate::constants::{MapTile, BOARD_WIDTH}; use crate::constants::{MapTile, BOARD_WIDTH};
@@ -117,7 +119,7 @@ impl Ghost<'_> {
/// Gets a random adjacent tile for frightened mode /// Gets a random adjacent tile for frightened mode
fn get_random_target(&self) -> IVec2 { fn get_random_target(&self) -> IVec2 {
let mut rng = rand::rng(); let mut rng = SmallRng::from_os_rng();
let mut possible_moves = Vec::new(); let mut possible_moves = Vec::new();
// Check all four directions // Check all four directions

View File

@@ -59,6 +59,8 @@ mod constants;
mod debug; mod debug;
mod direction; mod direction;
mod edible; mod edible;
#[cfg(target_os = "emscripten")]
mod emscripten;
mod entity; mod entity;
mod game; mod game;
mod ghost; mod ghost;
@@ -68,6 +70,26 @@ mod map;
mod modulation; mod modulation;
mod pacman; mod pacman;
#[cfg(not(target_os = "emscripten"))]
fn sleep(value: Duration) {
spin_sleep::sleep(value);
}
#[cfg(target_os = "emscripten")]
fn sleep(value: Duration) {
emscripten::emscripten::sleep(value.as_millis() as u32);
}
#[cfg(target_os = "emscripten")]
fn now() -> std::time::Instant {
std::time::Instant::now() + std::time::Duration::from_millis(emscripten::emscripten::now() as u64)
}
#[cfg(not(target_os = "emscripten"))]
fn now() -> std::time::Instant {
std::time::Instant::now()
}
/// The main entry point of the application. /// The main entry point of the application.
/// ///
/// This function initializes SDL, the window, the game state, and then enters /// This function initializes SDL, the window, the game state, and then enters
@@ -179,14 +201,7 @@ pub fn main() {
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 {
#[cfg(not(target_os = "emscripten"))] sleep(time);
{
spin_sleep::sleep(time);
}
#[cfg(target_os = "emscripten")]
{
std::thread::sleep(time);
}
} }
} else { } else {
event!( event!(

View File

@@ -1,5 +1,7 @@
//! This module defines the game map and provides functions for interacting with it. //! This module defines the game map and provides functions for interacting with it.
use rand::rngs::SmallRng;
use rand::seq::IteratorRandom; use rand::seq::IteratorRandom;
use rand::SeedableRng;
use crate::constants::{MapTile, BOARD_OFFSET, CELL_SIZE}; use crate::constants::{MapTile, BOARD_OFFSET, CELL_SIZE};
use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH}; use crate::constants::{BOARD_HEIGHT, BOARD_WIDTH};
@@ -120,7 +122,7 @@ impl Map {
} }
} }
} }
let mut rng = rand::rng(); let mut rng = SmallRng::from_os_rng();
let &start = pellet_positions let &start = pellet_positions
.iter() .iter()
.choose(&mut rng) .choose(&mut rng)