Add sounds

This commit is contained in:
cool-mist 2025-06-04 16:05:11 +05:30
parent c551715a52
commit ab0c333935
11 changed files with 147 additions and 10 deletions

98
Cargo.lock generated
View File

@ -52,6 +52,24 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "audir-sles"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea47348666a8edb7ad80cbee3940eb2bccf70df0e6ce09009abe1a836cb779f5"
[[package]]
name = "audrey"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58b92a84e89497e3cd25d3672cd5d1c288abaac02c18ff21283f17d118b889b8"
dependencies = [
"dasp_frame",
"dasp_sample",
"hound",
"lewton",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.4.0" version = "1.4.0"
@ -103,6 +121,21 @@ dependencies = [
"cfg-if", "cfg-if",
] ]
[[package]]
name = "dasp_frame"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2a3937f5fe2135702897535c8d4a5553f8b116f76c1529088797f2eee7c5cd6"
dependencies = [
"dasp_sample",
]
[[package]]
name = "dasp_sample"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"
[[package]] [[package]]
name = "fdeflate" name = "fdeflate"
version = "0.3.7" version = "0.3.7"
@ -159,6 +192,12 @@ dependencies = [
"ahash", "ahash",
] ]
[[package]]
name = "hound"
version = "3.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62adaabb884c94955b19907d60019f4e145d091c75345379e70d1ee696f7854f"
[[package]] [[package]]
name = "image" name = "image"
version = "0.24.9" version = "0.24.9"
@ -172,6 +211,17 @@ dependencies = [
"png", "png",
] ]
[[package]]
name = "lewton"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d542c1a317036c45c2aa1cf10cc9d403ca91eb2d333ef1a4917e5cb10628bd0"
dependencies = [
"byteorder",
"ogg",
"smallvec",
]
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.169" version = "0.2.169"
@ -190,6 +240,7 @@ dependencies = [
"macroquad_macro", "macroquad_macro",
"miniquad", "miniquad",
"quad-rand", "quad-rand",
"quad-snd",
"slotmap", "slotmap",
] ]
@ -208,6 +259,12 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "maybe-uninit"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
[[package]] [[package]]
name = "miniquad" name = "miniquad"
version = "0.4.6" version = "0.4.6"
@ -254,6 +311,15 @@ dependencies = [
"malloc_buf", "malloc_buf",
] ]
[[package]]
name = "ogg"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e571c3517af9e1729d4c63571a27edd660ade0667973bfc74a67c660c2b651"
dependencies = [
"byteorder",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.20.2" version = "1.20.2"
@ -291,12 +357,34 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "quad-alsa-sys"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c66c2f04a6946293477973d85adc251d502da51c57b08cd9c997f0cfd8dcd4b5"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "quad-rand" name = "quad-rand"
version = "0.2.3" version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a651516ddc9168ebd67b24afd085a718be02f8858fe406591b013d101ce2f40" checksum = "5a651516ddc9168ebd67b24afd085a718be02f8858fe406591b013d101ce2f40"
[[package]]
name = "quad-snd"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cba0c4943fc67147fbe9d1eb731fb9e678bfc9d926507eebbbfe0103e154e5b0"
dependencies = [
"audir-sles",
"audrey",
"libc",
"quad-alsa-sys",
"winapi",
]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.38" version = "1.0.38"
@ -382,12 +470,22 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "smallvec"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b97fcaeba89edba30f044a10c6a3cc39df9c3f17d7cd829dd1446cab35f890e0"
dependencies = [
"maybe-uninit",
]
[[package]] [[package]]
name = "sol_chess" name = "sol_chess"
version = "0.1.1" version = "0.1.1"
dependencies = [ dependencies = [
"argh", "argh",
"macroquad", "macroquad",
"quad-snd",
"rand", "rand",
] ]

View File

@ -6,7 +6,8 @@ default-run = "sol_chess"
[dependencies] [dependencies]
argh = "0.1.13" argh = "0.1.13"
macroquad = "0.4.13" macroquad = { version = "0.4.13", features = ["audio"] }
quad-snd = "0.2.8"
[dev-dependencies] [dev-dependencies]
rand = "0.9.1" rand = "0.9.1"

BIN
assets/button.wav Normal file

Binary file not shown.

BIN
assets/click.wav Normal file

Binary file not shown.

BIN
assets/loss.wav Normal file

Binary file not shown.

BIN
assets/mode.wav Normal file

Binary file not shown.

BIN
assets/win.wav Normal file

Binary file not shown.

View File

@ -5,17 +5,19 @@ use std::{
use button::Button; use button::Button;
use color::UiColor; use color::UiColor;
use macroquad::{math, prelude::*, rand}; use macroquad::{audio, math, prelude::*, rand};
use shadow::draw_shadow; use shadow::draw_shadow;
use sol_chess::{ use sol_chess::{
board::{Board, BoardState}, board::{Board, BoardState},
generator::{self, RandomRange}, generator::{self, RandomRange},
}; };
use sound::Sounds;
use texture::PieceTexture; use texture::PieceTexture;
pub mod button; pub mod button;
pub mod color; pub mod color;
pub mod shadow; pub mod shadow;
pub mod sound;
pub mod texture; pub mod texture;
pub struct MacroquadRandAdapter; pub struct MacroquadRandAdapter;
@ -34,6 +36,7 @@ pub struct Game {
// Constants througout the game // Constants througout the game
texture_res: Texture2D, texture_res: Texture2D,
sounds: Sounds,
num_squares: usize, num_squares: usize,
heading_text: String, heading_text: String,
@ -88,7 +91,7 @@ enum GameState {
} }
impl Game { impl Game {
pub fn new(texture_res: Texture2D) -> Self { pub fn new(texture_res: Texture2D, sounds: Sounds) -> Self {
let num_squares: usize = 4; let num_squares: usize = 4;
let game_mode = GameMode::Medium; let game_mode = GameMode::Medium;
let board = Game::generate_puzzle(game_mode); let board = Game::generate_puzzle(game_mode);
@ -103,6 +106,7 @@ impl Game {
heading_font_size: 0., heading_font_size: 0.,
num_squares, num_squares,
texture_res, texture_res,
sounds,
state: GameState::SelectSource(None), state: GameState::SelectSource(None),
game_mode, game_mode,
debug: false, debug: false,
@ -432,6 +436,7 @@ impl Game {
"Reset", "Reset",
Rect::new(board_x + btn_x_offset, btn_y, btn_w, btn_h), Rect::new(board_x + btn_x_offset, btn_y, btn_w, btn_h),
UiColor::Yellow, UiColor::Yellow,
self.sounds.button.clone(),
); );
let mut next_btn = Button::new( let mut next_btn = Button::new(
"Next", "Next",
@ -442,6 +447,7 @@ impl Game {
btn_h, btn_h,
), ),
UiColor::Green, UiColor::Green,
self.sounds.button.clone(),
); );
next_btn.is_active = false; next_btn.is_active = false;
@ -454,6 +460,7 @@ impl Game {
btn_h, btn_h,
), ),
UiColor::Brown, UiColor::Brown,
self.sounds.button.clone(),
); );
self.rules_btn = vec![rules_button]; self.rules_btn = vec![rules_button];
@ -470,6 +477,7 @@ impl Game {
btn_h, btn_h,
), ),
UiColor::Yellow, UiColor::Yellow,
self.sounds.mode.clone(),
); );
let medium_btn = Button::new( let medium_btn = Button::new(
@ -481,6 +489,7 @@ impl Game {
btn_h, btn_h,
), ),
UiColor::Yellow, UiColor::Yellow,
self.sounds.mode.clone(),
); );
let hard_button = Button::new( let hard_button = Button::new(
@ -492,6 +501,7 @@ impl Game {
btn_h, btn_h,
), ),
UiColor::Yellow, UiColor::Yellow,
self.sounds.mode.clone(),
); );
self.mode_btns = HashMap::new(); self.mode_btns = HashMap::new();
@ -586,7 +596,6 @@ impl Game {
}); });
let m = m.expect("legal move should be found"); let m = m.expect("legal move should be found");
self.board.make_move(m.clone()); self.board.make_move(m.clone());
if self.board.game_state == BoardState::Won || self.board.game_state == BoardState::Lost if self.board.game_state == BoardState::Won || self.board.game_state == BoardState::Lost
@ -598,6 +607,9 @@ impl Game {
.get_mut(&ButtonAction::Next) .get_mut(&ButtonAction::Next)
.expect("Cannot find next button"); .expect("Cannot find next button");
next_btn.is_active = true; next_btn.is_active = true;
audio::play_sound_once(&self.sounds.win);
} else {
audio::play_sound_once(&self.sounds.loss);
} }
return GameState::GameOver((x, y)); return GameState::GameOver((x, y));
@ -605,6 +617,7 @@ impl Game {
self.reset_squares(); self.reset_squares();
self.get(x, y).is_target = true; self.get(x, y).is_target = true;
audio::play_sound_once(&self.sounds.click);
return GameState::SelectSource(Some((x, y))); return GameState::SelectSource(Some((x, y)));
} }

View File

@ -1,4 +1,4 @@
use macroquad::prelude::*; use macroquad::{audio::{self, Sound}, prelude::*};
use super::{color::UiColor, shadow::draw_shadow}; use super::{color::UiColor, shadow::draw_shadow};
@ -10,10 +10,11 @@ pub struct Button {
rect: Rect, rect: Rect,
shadow_width: f32, shadow_width: f32,
pub color: UiColor, pub color: UiColor,
sound: Sound,
} }
impl Button { impl Button {
pub fn new(text: &str, rect: Rect, color: UiColor) -> Self { pub fn new(text: &str, rect: Rect, color: UiColor, sound: Sound) -> Self {
Self { Self {
text: text.to_string(), text: text.to_string(),
is_down: false, is_down: false,
@ -22,6 +23,7 @@ impl Button {
rect, rect,
shadow_width: 5.0, shadow_width: 5.0,
color, color,
sound,
} }
} }
@ -117,6 +119,7 @@ impl Button {
if is_mouse_button_released(MouseButton::Left) { if is_mouse_button_released(MouseButton::Left) {
if c.overlaps_rect(&self.rect) { if c.overlaps_rect(&self.rect) {
self.is_clicked = true; self.is_clicked = true;
audio::play_sound_once(&self.sound);
self.is_down = false; self.is_down = false;
return; return;
} }

9
src/game/sound.rs Normal file
View File

@ -0,0 +1,9 @@
use macroquad::audio::Sound;
pub struct Sounds {
pub click: Sound,
pub win: Sound,
pub loss: Sound,
pub button: Sound,
pub mode: Sound,
}

View File

@ -1,5 +1,5 @@
use game::Game; use game::{sound::Sounds, Game};
use macroquad::prelude::*; use macroquad::{audio, prelude::*};
use miniquad::date; use miniquad::date;
mod game; mod game;
@ -30,12 +30,25 @@ async fn main() {
} }
} }
macro_rules! load_sound {
($file_name:expr) => {
audio::load_sound_from_bytes(include_bytes!($file_name))
.await
.unwrap()
};
}
async fn init() -> Game { async fn init() -> Game {
let texture_bytes = include_bytes!("../assets/pieces.png"); let texture_bytes = include_bytes!("../assets/pieces.png");
let texture_res = Texture2D::from_file_with_format(&texture_bytes[..], None); let texture_res = Texture2D::from_file_with_format(&texture_bytes[..], None);
texture_res.set_filter(FilterMode::Nearest); texture_res.set_filter(FilterMode::Nearest);
build_textures_atlas(); build_textures_atlas();
let game = Game::new(texture_res); let click = load_sound!("../assets/click.wav");
let win = load_sound!("../assets/win.wav");
let loss = load_sound!("../assets/loss.wav");
let button = load_sound!("../assets/button.wav");
let mode = load_sound!("../assets/mode.wav");
let sounds = Sounds { click, win, loss, button, mode };
let game = Game::new(texture_res, sounds);
game game
} }