Compare commits

...

26 Commits

Author SHA1 Message Date
9b441fa35c feat: shared blinking textures for power pellets 2025-07-24 16:23:09 -05:00
61ca537909 fix: continue removing lifetime annotations 2025-07-24 16:09:25 -05:00
0a82aea922 refactor: intentionally unsafe leak to drop annoying lifetimes, use IVec2 in all remaining spots 2025-07-24 16:04:47 -05:00
f41c550bb8 feat: enable basic binary size reduction options 2025-07-24 15:39:16 -05:00
829462d3b6 refactor: move direction & edible into entity submodule 2025-07-24 12:48:39 -05:00
002da46045 refactor: split up and move texture-related code into src/texture submodule 2025-07-24 12:48:39 -05:00
cfa73c58a8 refactor: move entity-related code into src/entity submodule 2025-07-24 12:36:48 -05:00
5728effcc6 chore: bump to v0.2.0 2025-07-24 12:14:26 -05:00
fa1a0175b0 ci: remove save-always, remove old vcpkg cache key, flush vcpkg caches 2025-07-24 03:29:22 -05:00
85edb18380 ci: drop linux dependencies: build-essential gettext zlib1g-dev 2025-07-24 03:26:05 -05:00
3a535ee04f fix: linux build linking arg, working build 2025-07-24 02:59:58 -05:00
9b31b392d2 fix(wasm): increase asyncify stack size, working wasm build 2025-07-24 02:39:57 -05:00
999fa14059 chore: remove unused config.toml comments 2025-07-24 02:37:49 -05:00
e925376b7a feat: setup emscripten module for api layer 2025-07-24 02:37:41 -05:00
2596034365 feat: use smallrng for emscripten compat 2025-07-24 02:37:27 -05:00
163855b6e7 fix(wasm): increase audio chunksize to 256 minimum for emscripten audio ctx 2025-07-24 01:14:03 -05:00
645d48aeae ci: use updated setup-emsdk from 'pyodide' v15, fixes emsdk caching, always save vcpkg cache 2025-07-24 01:12:45 -05:00
ec800a88fc fix: use sdl2 internal methods for loading resources for emscripten asset handling 2025-07-24 01:07:22 -05:00
abc37dee4e ci: fix vcpkg cache keys to use target for platforms with multiple targets, allow restore oldkey 2025-07-24 01:04:21 -05:00
1ae7839275 ci: install zlib for linux builds, correct deps/.data filepath 2025-07-24 01:04:21 -05:00
d976d1bc59 ci: specify vcpkg triplets for macos targets 2025-07-24 00:49:22 -05:00
531a5b5d05 ci: inline build script contents, shorten assembly process, separate build/assemble steps 2025-07-24 00:48:49 -05:00
67713fab06 ci: add aarch64-apple-drawin target 2025-07-24 00:46:06 -05:00
b572729e9d feat: reorganize assets/ folder into web/ and game/ 2025-07-24 00:46:06 -05:00
cdc6979458 ci: add cache vcpkg step, remove ineffective apt pkgs 2025-07-24 00:09:43 -05:00
564f88fee5 ci: use proper vcpkg triplet for linux, macos 2025-07-24 00:09:43 -05:00
50 changed files with 650 additions and 436 deletions

View File

@@ -1,12 +1,16 @@
[target.'cfg(target_os = "emscripten")']
# TODO: Document what the fuck this is.
rustflags = [
# "-O", "-C", "link-args=-O2 --profiling",
#"-C", "link-args=-O3 --closure 1",
# "-C", "link-args=-g -gsource-map",
"-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1",
# "-C", "link-args=-sALLOW_MEMORY_GROWTH=1",
# Stack size is required for this project, it will crash otherwise.
"-C", "link-args=-sASYNCIFY=1 -sASYNCIFY_STACK_SIZE=8192 -sALLOW_MEMORY_GROWTH=1",
"-C", "link-args=-sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_MIXER=2 -sUSE_OGG=1 -sUSE_SDL_GFX=2 -sUSE_SDL_TTF=2 -sSDL2_IMAGE_FORMATS=['png']",
# USE_OGG, USE_VORBIS for OGG/VORBIS usage
"-C", "link-args=--preload-file assets/",
"-C", "link-args=--preload-file assets/game/",
]
[target.'cfg(target_os = "linux")']
rustflags = [
# Manually link zlib.
# The `sdl2` crate's build script uses `libpng`, which requires `zlib`.
# By adding `-lz` here, we ensure it's passed to the linker after `libpng`,
# which is required for the linker to correctly resolve symbols.
"-C", "link-arg=-lz",
]

View File

@@ -11,8 +11,6 @@ env:
jobs:
build:
name: Build (${{ matrix.target }})
env:
VCPKG_SYSTEM_LIBRARIES: "OFF"
strategy:
fail-fast: false
matrix:
@@ -23,6 +21,9 @@ jobs:
- os: macos-13
target: x86_64-apple-darwin
artifact_name: pacman
- os: macos-latest
target: aarch64-apple-darwin
artifact_name: pacman
- os: windows-latest
target: x86_64-pc-windows-gnu
artifact_name: pacman.exe
@@ -40,11 +41,19 @@ jobs:
- name: Rust Cache
uses: Swatinem/rust-cache@v2
- name: Cache vcpkg
uses: actions/cache@v4
with:
path: target/vcpkg
key: A-vcpkg-${{ runner.os }}-${{ matrix.target }}-${{ hashFiles('Cargo.toml', 'Cargo.lock') }}
restore-keys: |
A-vcpkg-${{ runner.os }}-${{ matrix.target }}-
- name: Vcpkg Linux Dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y build-essential autoconf automake libtool pkg-config gettext m4 libtool libltdl-dev
sudo apt-get install -y libltdl-dev
- name: Vcpkg
run: |
@@ -80,7 +89,7 @@ jobs:
uses: actions/checkout@v4
- name: Setup Emscripten SDK
uses: mymindstorm/setup-emsdk@v14
uses: pyodide/setup-emsdk@v15
with:
version: 3.1.43
actions-cache-folder: "emsdk-cache"
@@ -100,8 +109,21 @@ jobs:
version: 8
run_install: true
- name: Build
run: ./build.sh -er # release mode, skip emsdk
- name: Build with Emscripten
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
uses: actions/upload-pages-artifact@v3

4
.gitignore vendored
View File

@@ -3,5 +3,5 @@
.idea
*.dll
rust-sdl2-emscripten/
assets/build.css
emsdk/
assets/site/build.css
emsdk/

42
Cargo.lock generated
View File

@@ -174,7 +174,7 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "pacman"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"anyhow",
"glam",
@@ -212,15 +212,6 @@ version = "0.2.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "proc-macro2"
version = "1.0.95"
@@ -251,17 +242,6 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1"
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",
]
@@ -647,23 +627,3 @@ checksum = "6f42320e61fe2cfd34354ecb597f86f413484a798ba44a8ca1165c58d42da6c1"
dependencies = [
"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

@@ -1,6 +1,6 @@
[package]
name = "pacman"
version = "0.1.0"
version = "0.2.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -12,13 +12,19 @@ tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
lazy_static = "1.5.0"
sdl2 = { version = "0.38.0", features = ["image", "ttf"] }
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"
once_cell = "1.21.3"
thiserror = "1.0"
anyhow = "1.0"
glam = "0.30.4"
[profile.release]
lto = true
panic = "abort"
panic-strategy = "abort"
opt-level = "z"
[target.'cfg(target_os = "windows")'.dependencies.winapi]
version = "0.3"
features = ["consoleapi", "fileapi", "handleapi", "processenv", "winbase", "wincon", "winnt", "winuser", "windef", "minwindef"]
@@ -41,7 +47,9 @@ rev = "2024.05.24" # release 2024.05.24 # to check for a new one, check https://
[package.metadata.vcpkg.target]
x86_64-pc-windows-msvc = { triplet = "x64-windows-static-md" }
x86_64-unknown-linux-gnu = { triplet = "x86_64-unknown-linux-gnu" }
x86_64-unknown-linux-gnu = { triplet = "x64-linux" }
x86_64-apple-darwin = { triplet = "x64-osx" }
aarch64-apple-darwin = { triplet = "arm64-osx" }
[target.'cfg(target_os = "emscripten")'.dependencies]
libc = "0.2.16"

View File

Before

Width:  |  Height:  |  Size: 174 B

After

Width:  |  Height:  |  Size: 174 B

View File

Before

Width:  |  Height:  |  Size: 158 B

After

Width:  |  Height:  |  Size: 158 B

View File

Before

Width:  |  Height:  |  Size: 1.5 KiB

After

Width:  |  Height:  |  Size: 1.5 KiB

View File

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.2 KiB

View File

Before

Width:  |  Height:  |  Size: 528 B

After

Width:  |  Height:  |  Size: 528 B

View File

Before

Width:  |  Height:  |  Size: 394 B

After

Width:  |  Height:  |  Size: 394 B

View File

Before

Width:  |  Height:  |  Size: 228 B

After

Width:  |  Height:  |  Size: 228 B

View File

Before

Width:  |  Height:  |  Size: 370 B

After

Width:  |  Height:  |  Size: 370 B

View File

Before

Width:  |  Height:  |  Size: 90 B

After

Width:  |  Height:  |  Size: 90 B

View File

Before

Width:  |  Height:  |  Size: 28 KiB

After

Width:  |  Height:  |  Size: 28 KiB

View File

View File

View File

View File

23
assets/site/build.css Normal file
View File

@@ -0,0 +1,23 @@
@tailwind base;
@tailwind components;
@tailwind utilities;
@font-face {
font-family: "Liberation Mono";
src:
url("LiberationMono.woff2") format("woff2"),
url("LiberationMono.woff") format("woff");
font-weight: normal;
font-style: normal;
font-display: swap;
}
canvas {
@apply w-full h-[65vh] min-h-[200px] block mx-auto bg-black;
}
.code {
@apply px-1 rounded font-mono bg-zinc-900 border border-zinc-700 lowercase;
}
/*# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInN0eWxlcy5zY3NzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBIiwiZmlsZSI6ImJ1aWxkLmNzcyIsInNvdXJjZXNDb250ZW50IjpbIkB0YWlsd2luZCBiYXNlO1xuQHRhaWx3aW5kIGNvbXBvbmVudHM7XG5AdGFpbHdpbmQgdXRpbGl0aWVzO1xuXG5AZm9udC1mYWNlIHtcbiAgICBmb250LWZhbWlseTogXCJMaWJlcmF0aW9uIE1vbm9cIjtcbiAgICBzcmM6XG4gICAgICAgIHVybChcIkxpYmVyYXRpb25Nb25vLndvZmYyXCIpIGZvcm1hdChcIndvZmYyXCIpLFxuICAgICAgICB1cmwoXCJMaWJlcmF0aW9uTW9uby53b2ZmXCIpIGZvcm1hdChcIndvZmZcIik7XG4gICAgZm9udC13ZWlnaHQ6IG5vcm1hbDtcbiAgICBmb250LXN0eWxlOiBub3JtYWw7XG4gICAgZm9udC1kaXNwbGF5OiBzd2FwO1xufVxuXG5jYW52YXMge1xuICAgIEBhcHBseSB3LWZ1bGwgaC1bNjV2aF0gbWluLWgtWzIwMHB4XSBibG9jayBteC1hdXRvIGJnLWJsYWNrO1xufVxuXG4uY29kZSB7XG4gICAgQGFwcGx5IHB4LTEgcm91bmRlZCBmb250LW1vbm8gYmctemluYy05MDAgYm9yZGVyIGJvcmRlci16aW5jLTcwMCBsb3dlcmNhc2U7XG59XG4iXX0= */

View File

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -52,20 +52,18 @@ else
fi
echo "Generating CSS"
pnpx postcss-cli ./assets/styles.scss -o ./assets/build.css
pnpx postcss-cli ./assets/site/styles.scss -o ./assets/site/build.css
echo "Copying WASM files"
mkdir -p dist
output_folder="target/wasm32-unknown-emscripten/$build_type"
cp assets/index.html dist
# cp assets/*.woff* dist
cp assets/build.css dist
cp assets/favicon.ico dist
cp $output_folder/pacman.wasm dist
cp $output_folder/pacman.js dist
# only if .data file exists
cp $output_folder/deps/pacman.data dist
cp assets/site/{build.css,favicon.ico,index.html} dist
cp $output_folder/pacman.{wasm,js} dist
if [ -f $output_folder/deps/pacman.data ]; then
cp $output_folder/deps/pacman.data dist
fi
if [ -f $output_folder/pacman.wasm.map ]; then
cp $output_folder/pacman.wasm.map dist
fi

View File

@@ -56,40 +56,47 @@ async function setupEmscripten() {
async function buildWeb(release: boolean) {
console.log("Building WASM with Emscripten...");
const rustcFlags = [
"-C",
"link-arg=--preload-file",
"-C",
"link-arg=assets",
].join(" ");
if (release) {
await $`cargo build --target=wasm32-unknown-emscripten --release`;
await $`env RUSTFLAGS=${rustcFlags} cargo build --target=wasm32-unknown-emscripten --release`;
} else {
await $`cargo build --target=wasm32-unknown-emscripten`;
await $`env RUSTFLAGS=${rustcFlags} cargo build --target=wasm32-unknown-emscripten`;
}
console.log("Generating CSS...");
await $`pnpx postcss-cli ./assets/styles.scss -o ./assets/build.css`;
await $`pnpx postcss-cli ./assets/site/styles.scss -o ./assets/site/build.css`;
console.log("Copying WASM files...");
const buildType = release ? "release" : "debug";
const outputFolder = `target/wasm32-unknown-emscripten/${buildType}`;
await $`mkdir -p dist`;
await $`cp assets/index.html dist`;
await $`cp assets/*.woff* dist`;
await $`cp assets/build.css dist`;
await $`cp assets/favicon.ico dist`;
await $`cp ${outputFolder}/spiritus.wasm dist`;
await $`cp ${outputFolder}/spiritus.js dist`;
await $`cp assets/site/index.html dist`;
await $`cp assets/site/*.woff* dist`;
await $`cp assets/site/build.css dist`;
await $`cp assets/site/favicon.ico dist`;
await $`cp ${outputFolder}/pacman.wasm dist`;
await $`cp ${outputFolder}/pacman.js dist`;
// Check if .data file exists before copying
try {
await fs.access(`${outputFolder}/deps/spiritus.data`);
await $`cp ${outputFolder}/deps/spiritus.data dist`;
await fs.access(`${outputFolder}/pacman.data`);
await $`cp ${outputFolder}/pacman.data dist`;
} catch (e) {
console.log("No spiritus.data file found, skipping copy.");
console.log("No pacman.data file found, skipping copy.");
}
// Check if .map file exists before copying
try {
await fs.access(`${outputFolder}/spiritus.wasm.map`);
await $`cp ${outputFolder}/spiritus.wasm.map dist`;
await fs.access(`${outputFolder}/pacman.wasm.map`);
await $`cp ${outputFolder}/pacman.wasm.map dist`;
} catch (e) {
console.log("No spiritus.wasm.map file found, skipping copy.");
console.log("No pacman.wasm.map file found, skipping copy.");
}
console.log("WASM files copied.");

View File

@@ -1,136 +0,0 @@
//! This module provides a simple animation and atlas system for textures.
use sdl2::{
rect::Rect,
render::{Canvas, Texture},
video::Window,
};
use crate::direction::Direction;
/// Trait for drawable atlas-based textures
pub trait FrameDrawn {
fn render(&self, canvas: &mut Canvas<Window>, position: (i32, i32), direction: Direction, frame: Option<u32>);
}
/// A texture atlas abstraction for static (non-animated) rendering.
pub struct AtlasTexture<'a> {
pub raw_texture: Texture<'a>,
pub offset: (i32, i32),
pub frame_count: u32,
pub frame_width: u32,
pub frame_height: u32,
}
impl<'a> AtlasTexture<'a> {
pub fn new(texture: Texture<'a>, frame_count: u32, frame_width: u32, frame_height: u32, offset: Option<(i32, i32)>) -> Self {
AtlasTexture {
raw_texture: texture,
frame_count,
frame_width,
frame_height,
offset: offset.unwrap_or((0, 0)),
}
}
pub fn get_frame_rect(&self, frame: u32) -> Option<Rect> {
if frame >= self.frame_count {
return None;
}
Some(Rect::new(
frame as i32 * self.frame_width as i32,
0,
self.frame_width,
self.frame_height,
))
}
pub fn set_color_modulation(&mut self, r: u8, g: u8, b: u8) {
self.raw_texture.set_color_mod(r, g, b);
}
}
impl<'a> FrameDrawn for AtlasTexture<'a> {
fn render(&self, canvas: &mut Canvas<Window>, position: (i32, i32), direction: Direction, frame: Option<u32>) {
let texture_source_frame_rect = self.get_frame_rect(frame.unwrap_or(0));
let canvas_destination_rect = Rect::new(
position.0 + self.offset.0,
position.1 + self.offset.1,
self.frame_width,
self.frame_height,
);
canvas
.copy_ex(
&self.raw_texture,
texture_source_frame_rect,
Some(canvas_destination_rect),
direction.angle(),
None,
false,
false,
)
.expect("Could not render texture on canvas");
}
}
/// An animated texture using a texture atlas.
pub struct AnimatedAtlasTexture<'a> {
pub atlas: AtlasTexture<'a>,
pub ticks_per_frame: u32,
pub ticker: u32,
pub reversed: bool,
pub paused: bool,
}
impl<'a> AnimatedAtlasTexture<'a> {
pub fn new(
texture: Texture<'a>,
ticks_per_frame: u32,
frame_count: u32,
width: u32,
height: u32,
offset: Option<(i32, i32)>,
) -> Self {
AnimatedAtlasTexture {
atlas: AtlasTexture::new(texture, frame_count, width, height, offset),
ticks_per_frame,
ticker: 0,
reversed: false,
paused: false,
}
}
fn current_frame(&self) -> u32 {
self.ticker / self.ticks_per_frame
}
/// Advances the animation by one tick, unless paused.
pub fn tick(&mut self) {
if self.paused {
return;
}
if self.reversed {
if self.ticker > 0 {
self.ticker -= 1;
}
if self.ticker == 0 {
self.reversed = !self.reversed;
}
} else {
self.ticker += 1;
if self.ticker + 1 == self.ticks_per_frame * self.atlas.frame_count {
self.reversed = !self.reversed;
}
}
}
pub fn set_color_modulation(&mut self, r: u8, g: u8, b: u8) {
self.atlas.set_color_modulation(r, g, b);
}
}
impl<'a> FrameDrawn for AnimatedAtlasTexture<'a> {
fn render(&self, canvas: &mut Canvas<Window>, position: (i32, i32), direction: Direction, frame: Option<u32>) {
let frame = frame.unwrap_or_else(|| self.current_frame());
self.atlas.render(canvas, position, direction, Some(frame));
}
}

View File

@@ -56,17 +56,17 @@ mod imp {
macro_rules! asset_bytes_enum {
( $asset:expr ) => {
match $asset {
Asset::Wav1 => Cow::Borrowed(include_bytes!("../assets/wav/1.ogg")),
Asset::Wav2 => Cow::Borrowed(include_bytes!("../assets/wav/2.ogg")),
Asset::Wav3 => Cow::Borrowed(include_bytes!("../assets/wav/3.ogg")),
Asset::Wav4 => Cow::Borrowed(include_bytes!("../assets/wav/4.ogg")),
Asset::Pacman => Cow::Borrowed(include_bytes!("../assets/32/pacman.png")),
Asset::Pellet => Cow::Borrowed(include_bytes!("../assets/24/pellet.png")),
Asset::Energizer => Cow::Borrowed(include_bytes!("../assets/24/energizer.png")),
Asset::Map => Cow::Borrowed(include_bytes!("../assets/map.png")),
Asset::FontKonami => Cow::Borrowed(include_bytes!("../assets/font/konami.ttf")),
Asset::GhostBody => Cow::Borrowed(include_bytes!("../assets/32/ghost_body.png")),
Asset::GhostEyes => Cow::Borrowed(include_bytes!("../assets/32/ghost_eyes.png")),
Asset::Wav1 => Cow::Borrowed(include_bytes!("../assets/game/wav/1.ogg")),
Asset::Wav2 => Cow::Borrowed(include_bytes!("../assets/game/wav/2.ogg")),
Asset::Wav3 => Cow::Borrowed(include_bytes!("../assets/game/wav/3.ogg")),
Asset::Wav4 => Cow::Borrowed(include_bytes!("../assets/game/wav/4.ogg")),
Asset::Pacman => Cow::Borrowed(include_bytes!("../assets/game/32/pacman.png")),
Asset::Pellet => Cow::Borrowed(include_bytes!("../assets/game/24/pellet.png")),
Asset::Energizer => Cow::Borrowed(include_bytes!("../assets/game/24/energizer.png")),
Asset::Map => Cow::Borrowed(include_bytes!("../assets/game/map.png")),
Asset::FontKonami => Cow::Borrowed(include_bytes!("../assets/game/font/konami.ttf")),
Asset::GhostBody => Cow::Borrowed(include_bytes!("../assets/game/32/ghost_body.png")),
Asset::GhostEyes => Cow::Borrowed(include_bytes!("../assets/game/32/ghost_eyes.png")),
}
};
}
@@ -78,15 +78,17 @@ mod imp {
#[cfg(target_os = "emscripten")]
mod imp {
use super::*;
use std::fs;
use std::path::Path;
use sdl2::rwops::RWops;
use std::io::Read;
pub fn get_asset_bytes(asset: Asset) -> Result<Cow<'static, [u8]>, AssetError> {
let path = Path::new("assets").join(asset.path());
if !path.exists() {
return Err(AssetError::NotFound(asset.path().to_string()));
}
let bytes = fs::read(&path)?;
Ok(Cow::Owned(bytes))
let path = format!("assets/game/{}", asset.path());
let mut rwops = RWops::from_file(&path, "rb").map_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];
rwops
.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 format = DEFAULT_FORMAT;
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::allocate_channels(channels);

View File

@@ -1,7 +1,7 @@
//! Debug rendering utilities for Pac-Man.
use crate::{
constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH},
ghosts::blinky::Blinky,
entity::blinky::Blinky,
map::Map,
};
use glam::{IVec2, UVec2};

View File

@@ -1,82 +0,0 @@
//! Edible entity for Pac-Man: pellets, power pellets, and fruits.
use crate::animation::{AtlasTexture, FrameDrawn};
use crate::constants::{FruitType, MapTile, BOARD_HEIGHT, BOARD_WIDTH};
use crate::direction::Direction;
use crate::entity::{Entity, Renderable, StaticEntity};
use crate::map::Map;
use glam::{IVec2, UVec2};
use sdl2::{render::Canvas, video::Window};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EdibleKind {
Pellet,
PowerPellet,
Fruit(FruitType),
}
pub struct Edible<'a> {
pub base: StaticEntity,
pub kind: EdibleKind,
pub sprite: Rc<AtlasTexture<'a>>,
}
impl<'a> Edible<'a> {
pub fn new(kind: EdibleKind, cell_position: UVec2, sprite: Rc<AtlasTexture<'a>>) -> Self {
let pixel_position = Map::cell_to_pixel(cell_position);
Edible {
base: StaticEntity::new(pixel_position, cell_position),
kind,
sprite,
}
}
/// Checks collision with Pac-Man (or any entity)
pub fn collide(&self, pacman: &dyn Entity) -> bool {
self.base.is_colliding(pacman)
}
}
impl<'a> Entity for Edible<'a> {
fn base(&self) -> &StaticEntity {
&self.base
}
}
impl<'a> Renderable for Edible<'a> {
fn render(&self, canvas: &mut Canvas<Window>) {
let pos = self.base.pixel_position;
self.sprite.render(canvas, (pos.x, pos.y), Direction::Right, Some(0));
}
}
/// Reconstruct all edibles from the original map layout
pub fn reconstruct_edibles<'a>(
map: Rc<RefCell<Map>>,
pellet_sprite: Rc<AtlasTexture<'a>>,
power_pellet_sprite: Rc<AtlasTexture<'a>>,
_fruit_sprite: Rc<AtlasTexture<'a>>,
) -> Vec<Edible<'a>> {
let mut edibles = Vec::new();
for x in 0..BOARD_WIDTH {
for y in 0..BOARD_HEIGHT {
let tile = map.borrow().get_tile(IVec2::new(x as i32, y as i32));
match tile {
Some(MapTile::Pellet) => {
edibles.push(Edible::new(EdibleKind::Pellet, UVec2::new(x, y), Rc::clone(&pellet_sprite)));
}
Some(MapTile::PowerPellet) => {
edibles.push(Edible::new(
EdibleKind::PowerPellet,
UVec2::new(x, y),
Rc::clone(&power_pellet_sprite),
));
}
// Fruits can be added here if you have fruit positions
_ => {}
}
}
}
edibles
}

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,32 +4,32 @@ use std::rc::Rc;
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use crate::direction::Direction;
use crate::entity::direction::Direction;
use crate::entity::ghost::{Ghost, GhostMode, GhostType};
use crate::entity::pacman::Pacman;
use crate::entity::{Entity, Moving, Renderable, StaticEntity};
use crate::ghost::{Ghost, GhostMode, GhostType};
use crate::map::Map;
use crate::pacman::Pacman;
use glam::{IVec2, UVec2};
pub struct Blinky<'a> {
ghost: Ghost<'a>,
pub struct Blinky {
ghost: Ghost,
}
impl<'a> Blinky<'a> {
impl Blinky {
pub fn new(
starting_position: UVec2,
body_texture: Texture<'a>,
eyes_texture: Texture<'a>,
body_texture: Texture<'_>,
eyes_texture: Texture<'_>,
map: Rc<RefCell<Map>>,
pacman: Rc<RefCell<Pacman<'a>>>,
) -> Blinky<'a> {
pacman: Rc<RefCell<Pacman>>,
) -> Blinky {
Blinky {
ghost: Ghost::new(GhostType::Blinky, starting_position, body_texture, eyes_texture, map, pacman),
}
}
/// Gets Blinky's chase target - directly targets Pac-Man's current position
fn get_chase_target(&self) -> IVec2 {
pub fn get_chase_target(&self) -> IVec2 {
let pacman = self.ghost.pacman.borrow();
let cell = pacman.base().cell_position;
IVec2::new(cell.x as i32, cell.y as i32)
@@ -44,19 +44,19 @@ impl<'a> Blinky<'a> {
}
}
impl<'a> Entity for Blinky<'a> {
impl Entity for Blinky {
fn base(&self) -> &StaticEntity {
self.ghost.base.base()
}
}
impl<'a> Renderable for Blinky<'a> {
impl Renderable for Blinky {
fn render(&self, canvas: &mut Canvas<Window>) {
self.ghost.render(canvas);
}
}
impl<'a> Moving for Blinky<'a> {
impl Moving for Blinky {
fn move_forward(&mut self) {
self.ghost.move_forward();
}
@@ -81,15 +81,15 @@ impl<'a> Moving for Blinky<'a> {
}
// Allow direct access to ghost fields
impl<'a> std::ops::Deref for Blinky<'a> {
type Target = Ghost<'a>;
impl std::ops::Deref for Blinky {
type Target = Ghost;
fn deref(&self) -> &Self::Target {
&self.ghost
}
}
impl<'a> std::ops::DerefMut for Blinky<'a> {
impl std::ops::DerefMut for Blinky {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.ghost
}

View File

@@ -1,5 +1,6 @@
//! This module defines the `Direction` enum, which is used to represent the
//! direction of an entity.
use glam::IVec2;
use sdl2::keyboard::Keycode;
/// An enum representing the direction of an entity.
@@ -23,12 +24,12 @@ impl Direction {
}
/// Returns the offset of the direction as a tuple of (x, y).
pub fn offset(&self) -> (i32, i32) {
pub fn offset(&self) -> IVec2 {
match self {
Direction::Right => (1, 0),
Direction::Down => (0, 1),
Direction::Left => (-1, 0),
Direction::Up => (0, -1),
Direction::Right => IVec2::new(1, 0),
Direction::Down => IVec2::new(0, 1),
Direction::Left => IVec2::new(-1, 0),
Direction::Up => IVec2::new(0, -1),
}
}

96
src/entity/edible.rs Normal file
View File

@@ -0,0 +1,96 @@
//! Edible entity for Pac-Man: pellets, power pellets, and fruits.
use crate::constants::{FruitType, MapTile, BOARD_HEIGHT, BOARD_WIDTH};
use crate::entity::direction::Direction;
use crate::entity::{Entity, Renderable, StaticEntity};
use crate::map::Map;
use crate::texture::atlas::AtlasTexture;
use crate::texture::blinking::BlinkingTexture;
use crate::texture::FrameDrawn;
use glam::{IVec2, UVec2};
use sdl2::{render::Canvas, video::Window};
use std::cell::RefCell;
use std::rc::Rc;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EdibleKind {
Pellet,
PowerPellet,
Fruit(FruitType),
}
pub enum EdibleSprite {
Pellet(Rc<Box<dyn FrameDrawn>>),
PowerPellet(Rc<RefCell<BlinkingTexture>>),
}
pub struct Edible {
pub base: StaticEntity,
pub kind: EdibleKind,
pub sprite: EdibleSprite,
}
impl Edible {
pub fn new_pellet(cell_position: UVec2, sprite: Rc<Box<dyn FrameDrawn>>) -> Self {
let pixel_position = Map::cell_to_pixel(cell_position);
Edible {
base: StaticEntity::new(pixel_position, cell_position),
kind: EdibleKind::Pellet,
sprite: EdibleSprite::Pellet(sprite),
}
}
pub fn new_power_pellet(cell_position: UVec2, sprite: Rc<RefCell<BlinkingTexture>>) -> Self {
let pixel_position = Map::cell_to_pixel(cell_position);
Edible {
base: StaticEntity::new(pixel_position, cell_position),
kind: EdibleKind::PowerPellet,
sprite: EdibleSprite::PowerPellet(sprite),
}
}
/// Checks collision with Pac-Man (or any entity)
pub fn collide(&self, pacman: &dyn Entity) -> bool {
self.base.is_colliding(pacman)
}
}
impl Entity for Edible {
fn base(&self) -> &StaticEntity {
&self.base
}
}
impl Renderable for Edible {
fn render(&self, canvas: &mut Canvas<Window>) {
let pos = self.base.pixel_position;
match &self.sprite {
EdibleSprite::Pellet(sprite) => sprite.render(canvas, pos, Direction::Right, Some(0)),
EdibleSprite::PowerPellet(sprite) => sprite.borrow().render(canvas, pos, Direction::Right, Some(0)),
}
}
}
/// Reconstruct all edibles from the original map layout
pub fn reconstruct_edibles(
map: Rc<RefCell<Map>>,
pellet_sprite: Rc<Box<dyn FrameDrawn>>,
power_pellet_sprite: Rc<RefCell<BlinkingTexture>>,
_fruit_sprite: Rc<Box<dyn FrameDrawn>>,
) -> Vec<Edible> {
let mut edibles = Vec::new();
for x in 0..BOARD_WIDTH {
for y in 0..BOARD_HEIGHT {
let tile = map.borrow().get_tile(IVec2::new(x as i32, y as i32));
match tile {
Some(MapTile::Pellet) => {
edibles.push(Edible::new_pellet(UVec2::new(x, y), Rc::clone(&pellet_sprite)));
}
Some(MapTile::PowerPellet) => {
edibles.push(Edible::new_power_pellet(UVec2::new(x, y), Rc::clone(&power_pellet_sprite)));
}
// Fruits can be added here if you have fruit positions
_ => {}
}
}
}
edibles
}

View File

@@ -1,12 +1,16 @@
use rand::rngs::SmallRng;
use rand::Rng;
use rand::SeedableRng;
use crate::animation::{AnimatedAtlasTexture, FrameDrawn};
use crate::constants::{MapTile, BOARD_WIDTH};
use crate::direction::Direction;
use crate::entity::direction::Direction;
use crate::entity::pacman::Pacman;
use crate::entity::{Entity, MovableEntity, Moving, Renderable};
use crate::map::Map;
use crate::modulation::{SimpleTickModulator, TickModulator};
use crate::pacman::Pacman;
use crate::texture::animated::AnimatedAtlasTexture;
use crate::texture::atlas::{texture_to_static, AtlasTexture};
use crate::texture::FrameDrawn;
use glam::{IVec2, UVec2};
use sdl2::pixels::Color;
use sdl2::render::Texture;
@@ -50,7 +54,7 @@ impl GhostType {
}
/// Base ghost struct that contains common functionality
pub struct Ghost<'a> {
pub struct Ghost {
/// Shared movement and position fields.
pub base: MovableEntity,
/// The current mode of the ghost
@@ -58,23 +62,30 @@ pub struct Ghost<'a> {
/// The type/personality of this ghost
pub ghost_type: GhostType,
/// Reference to Pac-Man for targeting
pub pacman: Rc<RefCell<Pacman<'a>>>,
pub body_sprite: AnimatedAtlasTexture<'a>,
pub eyes_sprite: AnimatedAtlasTexture<'a>,
pub pacman: Rc<RefCell<Pacman>>,
pub body_sprite: AnimatedAtlasTexture,
pub eyes_sprite: AnimatedAtlasTexture,
}
impl Ghost<'_> {
impl Ghost {
/// Creates a new ghost instance
pub fn new<'a>(
pub fn new(
ghost_type: GhostType,
starting_position: UVec2,
body_texture: Texture<'a>,
eyes_texture: Texture<'a>,
body_texture: Texture<'_>,
eyes_texture: Texture<'_>,
map: Rc<RefCell<Map>>,
pacman: Rc<RefCell<Pacman<'a>>>,
) -> Ghost<'a> {
pacman: Rc<RefCell<Pacman>>,
) -> Ghost {
let color = ghost_type.color();
let mut body_sprite = AnimatedAtlasTexture::new(body_texture, 8, 2, 32, 32, Some((-4, -4)));
let mut body_sprite = AnimatedAtlasTexture::new(
unsafe { texture_to_static(body_texture) },
8,
2,
32,
32,
Some(IVec2::new(-4, -4)),
);
body_sprite.set_color_modulation(color.r, color.g, color.b);
let pixel_position = Map::cell_to_pixel(starting_position);
Ghost {
@@ -90,7 +101,14 @@ impl Ghost<'_> {
ghost_type,
pacman,
body_sprite,
eyes_sprite: AnimatedAtlasTexture::new(eyes_texture, 1, 4, 32, 32, Some((-4, -4))),
eyes_sprite: AnimatedAtlasTexture::new(
unsafe { texture_to_static(eyes_texture) },
1,
4,
32,
32,
Some((-4, -4).into()),
),
}
}
@@ -117,7 +135,7 @@ impl Ghost<'_> {
/// Gets a random adjacent tile for frightened mode
fn get_random_target(&self) -> IVec2 {
let mut rng = rand::rng();
let mut rng = SmallRng::from_os_rng();
let mut possible_moves = Vec::new();
// Check all four directions
@@ -178,8 +196,8 @@ impl Ghost<'_> {
}
}
for dir in &[Direction::Up, Direction::Down, Direction::Left, Direction::Right] {
let (dx, dy) = dir.offset();
let next_p = IVec2::new(p.x as i32 + dx, p.y as i32 + dy);
let offset = dir.offset();
let next_p = IVec2::new(p.x as i32 + offset.x, p.y as i32 + offset.y);
if let Some(tile) = map.get_tile(next_p) {
if tile == MapTile::Wall {
continue;
@@ -264,7 +282,7 @@ impl Ghost<'_> {
}
}
impl<'a> Moving for Ghost<'a> {
impl Moving for Ghost {
fn move_forward(&mut self) {
self.base.move_forward();
}
@@ -288,10 +306,10 @@ impl<'a> Moving for Ghost<'a> {
}
}
impl<'a> Renderable for Ghost<'a> {
impl Renderable for Ghost {
fn render(&self, canvas: &mut sdl2::render::Canvas<sdl2::video::Window>) {
let pos = self.base.base.pixel_position;
self.body_sprite.render(canvas, (pos.x, pos.y), Direction::Right, None);
self.body_sprite.render(canvas, pos, Direction::Right, None);
// Inline the eye_frame logic here
let eye_frame = if self.mode == GhostMode::Frightened {
4 // Frightened frame
@@ -303,7 +321,6 @@ impl<'a> Renderable for Ghost<'a> {
Direction::Down => 3,
}
};
self.eyes_sprite
.render(canvas, (pos.x, pos.y), Direction::Right, Some(eye_frame));
self.eyes_sprite.render(canvas, pos, Direction::Right, Some(eye_frame));
}
}

View File

@@ -1,6 +1,12 @@
pub mod blinky;
pub mod direction;
pub mod edible;
pub mod ghost;
pub mod pacman;
use crate::{
constants::{MapTile, BOARD_OFFSET, BOARD_WIDTH, CELL_SIZE},
direction::Direction,
entity::direction::Direction,
map::Map,
modulation::SimpleTickModulator,
};
@@ -108,7 +114,7 @@ impl Moving for MovableEntity {
);
}
fn next_cell(&self, direction: Option<Direction>) -> IVec2 {
let (x, y) = direction.unwrap_or(self.direction).offset();
let IVec2 { x, y } = direction.unwrap_or(self.direction).offset();
IVec2::new(self.base.cell_position.x as i32 + x, self.base.cell_position.y as i32 + y)
}
fn is_wall_ahead(&self, direction: Option<Direction>) -> bool {

View File

@@ -8,33 +8,33 @@ use sdl2::{
};
use crate::{
animation::{AnimatedAtlasTexture, FrameDrawn},
direction::Direction,
entity::{Entity, MovableEntity, Moving, Renderable, StaticEntity},
entity::{direction::Direction, Entity, MovableEntity, Moving, Renderable, StaticEntity},
map::Map,
modulation::{SimpleTickModulator, TickModulator},
texture::animated::AnimatedAtlasTexture,
texture::FrameDrawn,
};
use glam::{IVec2, UVec2};
/// The Pac-Man entity.
pub struct Pacman<'a> {
pub struct Pacman {
/// Shared movement and position fields.
pub base: MovableEntity,
/// The next direction of Pac-Man, which will be applied when Pac-Man is next aligned with the grid.
pub next_direction: Option<Direction>,
/// Whether Pac-Man is currently stopped.
pub stopped: bool,
pub sprite: AnimatedAtlasTexture<'a>,
pub sprite: AnimatedAtlasTexture,
}
impl<'a> Entity for Pacman<'a> {
impl Entity for Pacman {
fn base(&self) -> &StaticEntity {
&self.base.base
}
}
impl<'a> Moving for Pacman<'a> {
impl Moving for Pacman {
fn move_forward(&mut self) {
self.base.move_forward();
}
@@ -58,9 +58,9 @@ impl<'a> Moving for Pacman<'a> {
}
}
impl Pacman<'_> {
impl Pacman {
/// Creates a new `Pacman` instance.
pub fn new<'a>(starting_position: UVec2, atlas: Texture<'a>, map: Rc<RefCell<Map>>) -> Pacman<'a> {
pub fn new(starting_position: UVec2, atlas: Texture<'_>, map: Rc<RefCell<Map>>) -> Pacman {
let pixel_position = Map::cell_to_pixel(starting_position);
Pacman {
base: MovableEntity::new(
@@ -73,7 +73,14 @@ impl Pacman<'_> {
),
next_direction: None,
stopped: false,
sprite: AnimatedAtlasTexture::new(atlas, 2, 3, 32, 32, Some((-4, -4))),
sprite: AnimatedAtlasTexture::new(
unsafe { crate::texture::atlas::texture_to_static(atlas) },
2,
3,
32,
32,
Some(IVec2::new(-4, -4)),
),
}
}
@@ -119,14 +126,14 @@ impl Pacman<'_> {
}
}
impl Renderable for Pacman<'_> {
impl Renderable for Pacman {
fn render(&self, canvas: &mut Canvas<Window>) {
let pos = self.base.base.pixel_position;
let dir = self.base.direction;
if self.stopped {
self.sprite.render(canvas, (pos.x, pos.y), dir, Some(2));
self.sprite.render(canvas, pos, dir, Some(2));
} else {
self.sprite.render(canvas, (pos.x, pos.y), dir, None);
self.sprite.render(canvas, pos, dir, None);
}
}
}

View File

@@ -3,8 +3,10 @@ use std::cell::RefCell;
use std::ops::Not;
use std::rc::Rc;
use glam::UVec2;
use glam::{IVec2, UVec2};
use rand::rngs::SmallRng;
use rand::seq::IteratorRandom;
use rand::SeedableRng;
use sdl2::image::LoadTexture;
use sdl2::keyboard::Keycode;
use sdl2::render::{Texture, TextureCreator};
@@ -13,38 +15,40 @@ use sdl2::ttf::Font;
use sdl2::video::WindowContext;
use sdl2::{pixels::Color, render::Canvas, video::Window};
use crate::animation::AtlasTexture;
use crate::asset::{get_asset_bytes, Asset};
use crate::audio::Audio;
use crate::constants::RAW_BOARD;
use crate::debug::{DebugMode, DebugRenderer};
use crate::direction::Direction;
use crate::edible::{reconstruct_edibles, Edible, EdibleKind};
use crate::entity::blinky::Blinky;
use crate::entity::direction::Direction;
use crate::entity::edible::{reconstruct_edibles, Edible, EdibleKind};
use crate::entity::pacman::Pacman;
use crate::entity::Renderable;
use crate::ghosts::blinky::Blinky;
use crate::map::Map;
use crate::pacman::Pacman;
use crate::texture::atlas::{texture_to_static, AtlasTexture};
use crate::texture::blinking::BlinkingTexture;
use crate::texture::FrameDrawn;
/// The main game state.
///
/// This struct contains all the information necessary to run the game, including
/// the canvas, textures, fonts, game objects, and the current score.
pub struct Game<'a> {
canvas: &'a mut Canvas<Window>,
map_texture: Texture<'a>,
pellet_texture: Rc<AtlasTexture<'a>>,
power_pellet_texture: Rc<AtlasTexture<'a>>,
font: Font<'a, 'static>,
pacman: Rc<RefCell<Pacman<'a>>>,
pub struct Game {
canvas: &'static mut Canvas<Window>,
map_texture: Texture<'static>,
pellet_texture: Rc<Box<dyn FrameDrawn>>,
power_pellet_texture: Rc<RefCell<BlinkingTexture>>,
font: Font<'static, 'static>,
pacman: Rc<RefCell<Pacman>>,
map: Rc<RefCell<Map>>,
debug_mode: DebugMode,
score: u32,
pub audio: Audio,
blinky: Blinky<'a>,
edibles: Vec<Edible<'a>>,
blinky: Blinky,
edibles: Vec<Edible>,
}
impl<'a> Game<'a> {
impl Game {
/// Creates a new `Game` instance.
///
/// # Arguments
@@ -54,11 +58,11 @@ impl<'a> Game<'a> {
/// * `ttf_context` - The SDL TTF context.
/// * `_audio_subsystem` - The SDL audio subsystem (currently unused).
pub fn new(
canvas: &'a mut Canvas<Window>,
texture_creator: &'a TextureCreator<WindowContext>,
ttf_context: &'a sdl2::ttf::Sdl2TtfContext,
_audio_subsystem: &'a sdl2::AudioSubsystem,
) -> Game<'a> {
canvas: &'static mut Canvas<Window>,
texture_creator: &TextureCreator<WindowContext>,
ttf_context: &sdl2::ttf::Sdl2TtfContext,
_audio_subsystem: &sdl2::AudioSubsystem,
) -> Game {
let map = Rc::new(RefCell::new(Map::new(RAW_BOARD)));
// Load Pacman texture from asset API
@@ -89,17 +93,21 @@ impl<'a> Game<'a> {
// Load pellet texture from asset API
let pellet_bytes = get_asset_bytes(Asset::Pellet).expect("Failed to load asset");
let pellet_texture = Rc::new(AtlasTexture::new(
texture_creator
.load_texture_bytes(&pellet_bytes)
.expect("Could not load pellet texture from asset API"),
let power_pellet_bytes = get_asset_bytes(Asset::Energizer).expect("Failed to load asset");
let pellet_texture: Rc<Box<dyn FrameDrawn>> = Rc::new(Box::new(AtlasTexture::new(
unsafe {
texture_to_static(
texture_creator
.load_texture_bytes(&pellet_bytes)
.expect("Could not load pellet texture from asset API"),
)
},
1,
24,
24,
None,
));
let power_pellet_bytes = get_asset_bytes(Asset::Energizer).expect("Failed to load asset");
let power_pellet_texture = Rc::new(AtlasTexture::new(
)));
let power_pellet_texture = Rc::new(RefCell::new(BlinkingTexture::new(
texture_creator
.load_texture_bytes(&power_pellet_bytes)
.expect("Could not load power pellet texture from asset API"),
@@ -107,7 +115,9 @@ impl<'a> Game<'a> {
24,
24,
None,
));
30, // on_ticks
9, // off_ticks
)));
// Load map texture from asset API
let map_bytes = get_asset_bytes(Asset::Map).expect("Failed to load asset");
@@ -115,6 +125,7 @@ impl<'a> Game<'a> {
.load_texture_bytes(&map_bytes)
.expect("Could not load map texture from asset API");
map_texture.set_color_mod(0, 0, 255);
let map_texture = unsafe { texture_to_static(map_texture) };
let edibles = reconstruct_edibles(
Rc::clone(&map),
@@ -128,7 +139,9 @@ impl<'a> Game<'a> {
let font_bytes = get_asset_bytes(Asset::FontKonami).expect("Failed to load asset").into_owned();
let font_bytes_static: &'static [u8] = Box::leak(font_bytes.into_boxed_slice());
let font_rwops = RWops::from_bytes(font_bytes_static).expect("Failed to create RWops for font");
ttf_context
// Leak the ttf_context to get a 'static lifetime
let ttf_context_static: &'static sdl2::ttf::Sdl2TtfContext = unsafe { std::mem::transmute(ttf_context) };
ttf_context_static
.load_font_from_rwops(font_rwops, 24)
.expect("Could not load font from asset API")
};
@@ -211,7 +224,7 @@ impl<'a> Game<'a> {
{
let mut map = self.map.borrow_mut();
let valid_positions = map.get_valid_playable_positions();
let mut rng = rand::rng();
let mut rng = SmallRng::from_os_rng();
// Randomize Pac-Man position
if let Some(pos) = valid_positions.iter().choose(&mut rng) {
@@ -230,7 +243,7 @@ impl<'a> Game<'a> {
self.blinky.base.base.cell_position = *pos;
self.blinky.base.in_tunnel = false;
self.blinky.base.direction = Direction::Left;
self.blinky.mode = crate::ghost::GhostMode::Chase;
self.blinky.mode = crate::entity::ghost::GhostMode::Chase;
}
}
@@ -249,6 +262,9 @@ impl<'a> Game<'a> {
self.blinky.body_sprite.tick();
self.blinky.eyes_sprite.tick();
// Advance blinking for power pellets
self.power_pellet_texture.borrow_mut().tick();
let pacman = self.pacman.borrow();
let mut eaten_indices = vec![];
for (i, edible) in self.edibles.iter().enumerate() {
@@ -338,18 +354,18 @@ impl<'a> Game<'a> {
// Render the score and high score
self.render_text(
&format!("{lives}UP HIGH SCORE "),
(24 * lives_offset + x_offset, y_offset),
IVec2::new(24 * lives_offset + x_offset, y_offset),
Color::WHITE,
);
self.render_text(
&score_text,
(24 * score_offset + x_offset, 24 + y_offset + gap_offset),
IVec2::new(24 * score_offset + x_offset, 24 + y_offset + gap_offset),
Color::WHITE,
);
}
/// Renders text to the screen at the given position.
fn render_text(&mut self, text: &str, position: (i32, i32), color: Color) {
fn render_text(&mut self, text: &str, position: IVec2, color: Color) {
let surface = self.font.render(text).blended(color).expect("Could not render text surface");
let texture_creator = self.canvas.texture_creator();
@@ -358,7 +374,7 @@ impl<'a> Game<'a> {
.expect("Could not create texture from surface");
let query = texture.query();
let dst_rect = sdl2::rect::Rect::new(position.0, position.1, query.width, query.height);
let dst_rect = sdl2::rect::Rect::new(position.x, position.y, query.width, query.height);
self.canvas
.copy(&texture, None, Some(dst_rect))

View File

@@ -1 +0,0 @@
pub mod blinky;

View File

@@ -52,21 +52,38 @@ unsafe fn attach_console() {
// Do NOT call AllocConsole here - we don't want a console when launched from Explorer
}
mod animation;
mod asset;
mod audio;
mod constants;
mod debug;
mod direction;
mod edible;
#[cfg(target_os = "emscripten")]
mod emscripten;
mod entity;
mod game;
mod ghost;
mod ghosts;
mod helper;
mod map;
mod modulation;
mod pacman;
mod texture;
#[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.
///
@@ -106,7 +123,8 @@ pub fn main() {
.expect("Could not set logical size");
let texture_creator = canvas.texture_creator();
let mut game = Game::new(&mut canvas, &texture_creator, &ttf_context, &audio_subsystem);
let canvas_static: &'static mut sdl2::render::Canvas<sdl2::video::Window> = Box::leak(Box::new(canvas));
let mut game = Game::new(canvas_static, &texture_creator, &ttf_context, &audio_subsystem);
game.audio.set_mute(cfg!(debug_assertions));
let mut event_pump = sdl_context.event_pump().expect("Could not get SDL EventPump");
@@ -179,14 +197,7 @@ pub fn main() {
if start.elapsed() < loop_time {
let time = loop_time.saturating_sub(start.elapsed());
if time != Duration::ZERO {
#[cfg(not(target_os = "emscripten"))]
{
spin_sleep::sleep(time);
}
#[cfg(target_os = "emscripten")]
{
std::thread::sleep(time);
}
sleep(time);
}
} else {
event!(

View File

@@ -1,5 +1,7 @@
//! This module defines the game map and provides functions for interacting with it.
use rand::rngs::SmallRng;
use rand::seq::IteratorRandom;
use rand::SeedableRng;
use crate::constants::{MapTile, BOARD_OFFSET, CELL_SIZE};
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
.iter()
.choose(&mut rng)

73
src/texture/animated.rs Normal file
View File

@@ -0,0 +1,73 @@
//! This module provides a simple animation and atlas system for textures.
use glam::IVec2;
use sdl2::{
render::{Canvas, Texture},
video::Window,
};
use crate::entity::direction::Direction;
use crate::texture::atlas::AtlasTexture;
use crate::texture::FrameDrawn;
/// An animated texture using a texture atlas.
pub struct AnimatedAtlasTexture {
pub atlas: AtlasTexture,
pub ticks_per_frame: u32,
pub ticker: u32,
pub reversed: bool,
pub paused: bool,
}
impl AnimatedAtlasTexture {
pub fn new(
texture: Texture<'static>,
ticks_per_frame: u32,
frame_count: u32,
width: u32,
height: u32,
offset: Option<IVec2>,
) -> Self {
AnimatedAtlasTexture {
atlas: AtlasTexture::new(texture, frame_count, width, height, offset),
ticks_per_frame,
ticker: 0,
reversed: false,
paused: false,
}
}
fn current_frame(&self) -> u32 {
self.ticker / self.ticks_per_frame
}
/// Advances the animation by one tick, unless paused.
pub fn tick(&mut self) {
if self.paused {
return;
}
if self.reversed {
if self.ticker > 0 {
self.ticker -= 1;
}
if self.ticker == 0 {
self.reversed = !self.reversed;
}
} else {
self.ticker += 1;
if self.ticker + 1 == self.ticks_per_frame * self.atlas.frame_count {
self.reversed = !self.reversed;
}
}
}
pub fn set_color_modulation(&mut self, r: u8, g: u8, b: u8) {
self.atlas.set_color_modulation(r, g, b);
}
}
impl FrameDrawn for AnimatedAtlasTexture {
fn render(&self, canvas: &mut Canvas<Window>, position: IVec2, direction: Direction, frame: Option<u32>) {
let frame = frame.unwrap_or_else(|| self.current_frame());
self.atlas.render(canvas, position, direction, Some(frame));
}
}

74
src/texture/atlas.rs Normal file
View File

@@ -0,0 +1,74 @@
use glam::IVec2;
use sdl2::{
rect::Rect,
render::{Canvas, Texture},
video::Window,
};
use crate::{entity::direction::Direction, texture::FrameDrawn};
/// Unsafely converts a Texture with any lifetime to a 'static lifetime.
/// Only use this if you guarantee the renderer/context will never be dropped!
pub unsafe fn texture_to_static<'a>(texture: Texture<'a>) -> Texture<'static> {
std::mem::transmute::<Texture<'a>, Texture<'static>>(texture)
}
/// A texture atlas abstraction for static (non-animated) rendering.
pub struct AtlasTexture {
pub raw_texture: Texture<'static>,
pub offset: IVec2,
pub frame_count: u32,
pub frame_width: u32,
pub frame_height: u32,
}
impl AtlasTexture {
pub fn new(texture: Texture<'static>, frame_count: u32, frame_width: u32, frame_height: u32, offset: Option<IVec2>) -> Self {
AtlasTexture {
raw_texture: texture,
frame_count,
frame_width,
frame_height,
offset: offset.unwrap_or(IVec2::new(0, 0)).into(),
}
}
pub fn get_frame_rect(&self, frame: u32) -> Option<Rect> {
if frame >= self.frame_count {
return None;
}
Some(Rect::new(
frame as i32 * self.frame_width as i32,
0,
self.frame_width,
self.frame_height,
))
}
pub fn set_color_modulation(&mut self, r: u8, g: u8, b: u8) {
self.raw_texture.set_color_mod(r, g, b);
}
}
impl FrameDrawn for AtlasTexture {
fn render(&self, canvas: &mut Canvas<Window>, position: IVec2, direction: Direction, frame: Option<u32>) {
let texture_source_frame_rect = self.get_frame_rect(frame.unwrap_or(0));
let canvas_destination_rect = Rect::new(
position.x + self.offset.x,
position.y + self.offset.y,
self.frame_width,
self.frame_height,
);
canvas
.copy_ex(
&self.raw_texture,
texture_source_frame_rect,
Some(canvas_destination_rect),
direction.angle(),
None,
false,
false,
)
.expect("Could not render texture on canvas");
}
}

62
src/texture/blinking.rs Normal file
View File

@@ -0,0 +1,62 @@
//! A texture that blinks on/off for a specified number of ticks.
use glam::IVec2;
use sdl2::{
render::{Canvas, Texture},
video::Window,
};
use crate::texture::atlas::AtlasTexture;
use crate::texture::FrameDrawn;
use crate::{entity::direction::Direction, texture::atlas::texture_to_static};
pub struct BlinkingTexture {
pub atlas: AtlasTexture,
pub on_ticks: u32,
pub off_ticks: u32,
pub ticker: u32,
pub visible: bool,
}
impl BlinkingTexture {
pub fn new(
texture: Texture<'_>,
frame_count: u32,
width: u32,
height: u32,
offset: Option<IVec2>,
on_ticks: u32,
off_ticks: u32,
) -> Self {
BlinkingTexture {
atlas: AtlasTexture::new(unsafe { texture_to_static(texture) }, frame_count, width, height, offset),
on_ticks,
off_ticks,
ticker: 0,
visible: true,
}
}
/// Advances the blinking state by one tick.
pub fn tick(&mut self) {
self.ticker += 1;
if self.visible && self.ticker >= self.on_ticks {
self.visible = false;
self.ticker = 0;
} else if !self.visible && self.ticker >= self.off_ticks {
self.visible = true;
self.ticker = 0;
}
}
pub fn set_color_modulation(&mut self, r: u8, g: u8, b: u8) {
self.atlas.set_color_modulation(r, g, b);
}
}
impl FrameDrawn for BlinkingTexture {
fn render(&self, canvas: &mut Canvas<Window>, position: IVec2, direction: Direction, frame: Option<u32>) {
if self.visible {
self.atlas.render(canvas, position, direction, frame);
}
}
}

13
src/texture/mod.rs Normal file
View File

@@ -0,0 +1,13 @@
use glam::IVec2;
use sdl2::{render::Canvas, video::Window};
use crate::entity::direction::Direction;
/// Trait for drawable atlas-based textures
pub trait FrameDrawn {
fn render(&self, canvas: &mut Canvas<Window>, position: IVec2, direction: Direction, frame: Option<u32>);
}
pub mod animated;
pub mod atlas;
pub mod blinking;