Compare commits

..

3 Commits

12 changed files with 165 additions and 49 deletions

View File

@@ -1,10 +1,12 @@
[target.wasm32-unknown-emscripten] [target.'cfg(target_os = "emscripten")']
# TODO: Document what the fuck this is. # TODO: Document what the fuck this is.
rustflags = [ rustflags = [
"-O", "-C", "link-args=-O2 --profiling", # "-O", "-C", "link-args=-O2 --profiling",
#"-C", "link-args=-O3 --closure 1", #"-C", "link-args=-O3 --closure 1",
# "-C", "link-args=-g -gsource-map",
"-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1", "-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1",
"-C", "link-args=-sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sSDL2_IMAGE_FORMATS=['png']", # "-C", "link-args=-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 # USE_OGG, USE_VORBIS for OGG/VORBIS usage
"-C", "link-args=--preload-file assets/", "-C", "link-args=--preload-file assets/",
] ]

1
Cargo.lock generated
View File

@@ -89,6 +89,7 @@ name = "pacman"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"libc",
"sdl2", "sdl2",
"spin_sleep", "spin_sleep",
"tracing", "tracing",

View File

@@ -13,7 +13,13 @@ tracing-error = "0.2.0"
tracing-subscriber = {version = "0.3.17", features = ["env-filter"]} tracing-subscriber = {version = "0.3.17", features = ["env-filter"]}
winapi = { version = "0.3", features = ["consoleapi", "fileapi", "handleapi", "processenv", "winbase", "wincon", "winnt", "winuser", "windef", "minwindef"] } winapi = { version = "0.3", features = ["consoleapi", "fileapi", "handleapi", "processenv", "winbase", "wincon", "winnt", "winuser", "windef", "minwindef"] }
[dependencies.sdl2]
[target.'cfg(target_os = "emscripten")'.dependencies.sdl2]
version = "0.38"
default-features = false
features = ["ttf","image","gfx","mixer"]
[target.'cfg(not(target_os = "emscripten"))'.dependencies.sdl2]
version = "0.38" version = "0.38"
default-features = false default-features = false
features = ["ttf","image","gfx","mixer","static-link","use-vcpkg"] features = ["ttf","image","gfx","mixer","static-link","use-vcpkg"]
@@ -24,4 +30,8 @@ git = "https://github.com/microsoft/vcpkg"
rev = "2024.05.24" # release 2024.05.24 # to check for a new one, check https://github.com/microsoft/vcpkg/releases rev = "2024.05.24" # release 2024.05.24 # to check for a new one, check https://github.com/microsoft/vcpkg/releases
[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" }
stable-x86_64-unknown-linux-gnu = { triplet = "x86_64-unknown-linux-gnu" }
[target.'cfg(target_os = "emscripten")'.dependencies]
libc = "0.2.16"

BIN
assets/wav/1.ogg Normal file
View File

Binary file not shown.

BIN
assets/wav/2.ogg Normal file
View File

Binary file not shown.

BIN
assets/wav/3.ogg Normal file
View File

Binary file not shown.

BIN
assets/wav/4.ogg Normal file
View File

Binary file not shown.

73
src/audio.rs Normal file
View File

@@ -0,0 +1,73 @@
use sdl2::{
mixer::{self, Chunk, InitFlag, LoaderRWops, DEFAULT_FORMAT},
rwops::RWops,
};
// Embed sound files directly into the executable
const SOUND_1_DATA: &[u8] = include_bytes!("../assets/wav/1.ogg");
const SOUND_2_DATA: &[u8] = include_bytes!("../assets/wav/2.ogg");
const SOUND_3_DATA: &[u8] = include_bytes!("../assets/wav/3.ogg");
const SOUND_4_DATA: &[u8] = include_bytes!("../assets/wav/4.ogg");
const SOUND_DATA: [&[u8]; 4] = [SOUND_1_DATA, SOUND_2_DATA, SOUND_3_DATA, SOUND_4_DATA];
pub struct Audio {
_mixer_context: mixer::Sdl2MixerContext,
sounds: Vec<Chunk>,
next_sound_index: usize,
}
impl Audio {
pub fn new() -> Self {
let frequency = 44100;
let format = DEFAULT_FORMAT;
let channels = 4;
let chunk_size = 128;
mixer::open_audio(frequency, format, 1, chunk_size).expect("Failed to open audio");
mixer::allocate_channels(channels);
// set channel volume
for i in 0..channels {
mixer::Channel(i as i32).set_volume(32);
}
let mixer_context = mixer::init(InitFlag::OGG).expect("Failed to initialize SDL2_mixer");
let sounds: Vec<Chunk> = SOUND_DATA
.iter()
.enumerate()
.map(|(i, data)| {
let rwops = RWops::from_bytes(data)
.expect(&format!("Failed to create RWops for sound {}", i + 1));
rwops.load_wav().expect(&format!(
"Failed to load sound {} from embedded data",
i + 1
))
})
.collect();
Audio {
_mixer_context: mixer_context,
sounds,
next_sound_index: 0,
}
}
pub fn eat(&mut self) {
if let Some(chunk) = self.sounds.get(self.next_sound_index) {
match mixer::Channel(0).play(chunk, 0) {
Ok(channel) => {
tracing::info!(
"Playing sound #{} on channel {:?}",
self.next_sound_index + 1,
channel
);
}
Err(e) => {
tracing::warn!("Could not play sound #{}: {}", self.next_sound_index + 1, e);
}
}
}
self.next_sound_index = (self.next_sound_index + 1) % self.sounds.len();
}
}

View File

View File

@@ -3,17 +3,26 @@ use std::rc::Rc;
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};
use sdl2::ttf::{Font, FontStyle}; use sdl2::rwops::RWops;
use sdl2::ttf::Font;
use sdl2::video::WindowContext; use sdl2::video::WindowContext;
use sdl2::{pixels::Color, render::Canvas, video::Window}; use sdl2::{pixels::Color, render::Canvas, video::Window};
use tracing::event; use tracing::event;
use crate::audio::Audio;
use crate::constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD}; use crate::constants::{MapTile, BOARD_HEIGHT, BOARD_WIDTH, RAW_BOARD};
use crate::direction::Direction; use crate::direction::Direction;
use crate::entity::Entity; use crate::entity::Entity;
use crate::map::Map; use crate::map::Map;
use crate::pacman::Pacman; use crate::pacman::Pacman;
// Embed texture data directly into the executable
static PACMAN_TEXTURE_DATA: &[u8] = include_bytes!("../assets/32/pacman.png");
static PELLET_TEXTURE_DATA: &[u8] = include_bytes!("../assets/24/pellet.png");
static POWER_PELLET_TEXTURE_DATA: &[u8] = include_bytes!("../assets/24/energizer.png");
static MAP_TEXTURE_DATA: &[u8] = include_bytes!("../assets/map.png");
static FONT_DATA: &[u8] = include_bytes!("../assets/font/konami.ttf");
pub struct Game<'a> { pub struct Game<'a> {
canvas: &'a mut Canvas<Window>, canvas: &'a mut Canvas<Window>,
map_texture: Texture<'a>, map_texture: Texture<'a>,
@@ -24,6 +33,7 @@ pub struct Game<'a> {
map: Rc<std::cell::RefCell<Map>>, map: Rc<std::cell::RefCell<Map>>,
debug: bool, debug: bool,
score: u32, score: u32,
audio: Audio,
} }
impl Game<'_> { impl Game<'_> {
@@ -31,36 +41,51 @@ impl Game<'_> {
canvas: &'a mut Canvas<Window>, canvas: &'a mut Canvas<Window>,
texture_creator: &'a TextureCreator<WindowContext>, texture_creator: &'a TextureCreator<WindowContext>,
ttf_context: &'a sdl2::ttf::Sdl2TtfContext, ttf_context: &'a sdl2::ttf::Sdl2TtfContext,
_audio_subsystem: &'a sdl2::AudioSubsystem,
) -> Game<'a> { ) -> Game<'a> {
let map = Rc::new(std::cell::RefCell::new(Map::new(RAW_BOARD))); let map = Rc::new(std::cell::RefCell::new(Map::new(RAW_BOARD)));
// Load Pacman texture from embedded data
let pacman_atlas = texture_creator let pacman_atlas = texture_creator
.load_texture("assets/32/pacman.png") .load_texture_bytes(PACMAN_TEXTURE_DATA)
.expect("Could not load pacman texture"); .expect("Could not load pacman texture from embedded data");
let pacman = Pacman::new((1, 1), pacman_atlas, Rc::clone(&map)); let pacman = Pacman::new((1, 1), pacman_atlas, Rc::clone(&map));
// Load pellet texture from embedded data
let pellet_texture = texture_creator let pellet_texture = texture_creator
.load_texture("assets/24/pellet.png") .load_texture_bytes(PELLET_TEXTURE_DATA)
.expect("Could not load pellet texture"); .expect("Could not load pellet texture from embedded data");
let power_pellet_texture = texture_creator
.load_texture("assets/24/energizer.png")
.expect("Could not load power pellet texture");
// Load power pellet texture from embedded data
let power_pellet_texture = texture_creator
.load_texture_bytes(POWER_PELLET_TEXTURE_DATA)
.expect("Could not load power pellet texture from embedded data");
// Load font from embedded data
let font_rwops = RWops::from_bytes(FONT_DATA).expect("Failed to create RWops for font");
let font = ttf_context let font = ttf_context
.load_font("assets/font/konami.ttf", 24) .load_font_from_rwops(font_rwops, 24)
.expect("Could not load font"); .expect("Could not load font from embedded data");
let audio = Audio::new();
// Load map texture from embedded data
let mut map_texture = texture_creator
.load_texture_bytes(MAP_TEXTURE_DATA)
.expect("Could not load map texture from embedded data");
map_texture.set_color_mod(0, 0, 255);
Game { Game {
canvas, canvas,
pacman: pacman, pacman: pacman,
debug: false, debug: false,
map: map, map: map,
map_texture: texture_creator map_texture,
.load_texture("assets/map.png")
.expect("Could not load map texture"),
pellet_texture, pellet_texture,
power_pellet_texture, power_pellet_texture,
font, font,
score: 0, score: 0,
audio,
} }
} }
@@ -116,36 +141,25 @@ impl Game<'_> {
}; };
if let Some(tile) = tile { if let Some(tile) = tile {
match tile { let pellet_value = match tile {
MapTile::Pellet => { MapTile::Pellet => Some(10),
// Eat the pellet and add score MapTile::PowerPellet => Some(50),
{ _ => None,
let mut map = self.map.borrow_mut(); };
map.set_tile((cell_pos.0 as i32, cell_pos.1 as i32), MapTile::Empty);
} if let Some(value) = pellet_value {
self.add_score(10); {
event!( let mut map = self.map.borrow_mut();
tracing::Level::DEBUG, map.set_tile((cell_pos.0 as i32, cell_pos.1 as i32), MapTile::Empty);
"Pellet eaten at ({}, {})",
cell_pos.0,
cell_pos.1
);
} }
MapTile::PowerPellet => { self.add_score(value);
// Eat the power pellet and add score self.audio.eat();
{ event!(
let mut map = self.map.borrow_mut(); tracing::Level::DEBUG,
map.set_tile((cell_pos.0 as i32, cell_pos.1 as i32), MapTile::Empty); "Pellet eaten at ({}, {})",
} cell_pos.0,
self.add_score(50); cell_pos.1
event!( );
tracing::Level::DEBUG,
"Power pellet eaten at ({}, {})",
cell_pos.0,
cell_pos.1
);
}
_ => {}
} }
} }
} }

View File

@@ -94,7 +94,12 @@ pub fn main() {
.expect("Could not set logical size"); .expect("Could not set logical size");
let texture_creator = canvas.texture_creator(); let texture_creator = canvas.texture_creator();
let mut game = Game::new(&mut canvas, &texture_creator, &ttf_context); let mut game = Game::new(
&mut canvas,
&texture_creator,
&ttf_context,
&audio_subsystem,
);
let mut event_pump = sdl_context let mut event_pump = sdl_context
.event_pump() .event_pump()
@@ -164,6 +169,7 @@ pub fn main() {
// TODO: Proper pausing implementation that does not interfere with statistic gathering // TODO: Proper pausing implementation that does not interfere with statistic gathering
if !paused { if !paused {
// game.audio_demo_tick();
game.tick(); game.tick();
game.draw(); game.draw();
} }

View File

@@ -79,6 +79,16 @@ impl Pacman<'_> {
.get_tile(proposed_next_cell) .get_tile(proposed_next_cell)
.unwrap_or(MapTile::Empty); .unwrap_or(MapTile::Empty);
if proposed_next_tile != MapTile::Wall { if proposed_next_tile != MapTile::Wall {
event!(
tracing::Level::DEBUG,
"Direction change: {:?} -> {:?} at position ({}, {}) internal ({}, {})",
self.direction,
self.next_direction.unwrap(),
self.position.0,
self.position.1,
self.internal_position().0,
self.internal_position().1
);
self.direction = self.next_direction.unwrap(); self.direction = self.next_direction.unwrap();
self.next_direction = None; self.next_direction = None;
} }