SDL2 Mixer with Volume Adjustment

This commit is contained in:
2024-04-18 03:56:14 -05:00
parent f54922e1e1
commit e4334ece7a
5 changed files with 71 additions and 19 deletions

View File

@@ -6,7 +6,7 @@ rustflags = [
# "-C", "link-args=-g -gsource-map",
"-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1",
# "-C", "link-args=-sALLOW_MEMORY_GROWTH=1",
"-C", "link-args=-sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_TTF=2 -sSDL2_IMAGE_FORMATS=['png']",
"-C", "link-args=-sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sUSE_SDL_MIXER=2 -sUSE_OGG=1 -sUSE_SDL_TTF=2 -sSDL2_IMAGE_FORMATS=['png']",
# USE_OGG, USE_VORBIS for OGG/VORBIS usage
"-C", "link-args=--preload-file assets/",
]

View File

@@ -9,7 +9,7 @@ edition = "2021"
colors-transform = "0.2.11"
lazy_static = "1.4.0"
rand = "0.8.5"
sdl2 = { version = "0.35", features = ["image", "ttf"] }
sdl2 = { version = "0.35", features = ["image", "ttf", "mixer"] }
spin_sleep = "1.1.1"
tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]}
tracing-error = "0.2.0"

View File

@@ -16,7 +16,7 @@ This is an experimental repository while testing a Rust + SDL2 project built wit
- This ensures that the project can iterate safely and be inspected in a safe environment, free from errors. Helps ensure errors are isolated to the machine or build script.
- [ ] SDL2 Extensions
- [X] Image
- [ ] Mixer
- [X] Mixer
- [X] TTF
- [ ] GFX
- All of these libraries are common and necessary for a lot of projects. Ensuring they work is important.

BIN
assets/tetris.ogg Normal file
View File

Binary file not shown.

View File

@@ -1,16 +1,18 @@
use std::process;
use std::time::Duration;
use sdl2::rwops::RWops;
use sdl2::ttf;
use sdl2::event::Event;
use sdl2::image::LoadTexture;
use sdl2::keyboard::Keycode;
use sdl2::mixer::{self, LoaderRWops, Music};
use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect};
use sdl2::rwops::RWops;
use sdl2::sys::AUDIO_S16LSB;
use sdl2::ttf;
static FONT_DATA: &[u8] = include_bytes!("../assets/TerminalVector.ttf");
static MUSIC_DATA: &[u8] = include_bytes!("../assets/tetris.ogg");
static BLACK: Color = Color::RGB(0, 0, 0);
#[cfg(target_os = "emscripten")]
@@ -22,7 +24,7 @@ fn sleep(ms: u32) {
}
#[cfg(target_os = "emscripten")]
fn now() -> f64 {
fn now() -> f64 {
emscripten::emscripten::now() / 1000f64
}
@@ -75,12 +77,29 @@ fn main() {
let ttf_ctx = ttf_context();
mixer::open_audio(
44_100,
sdl2::mixer::AUDIO_S16LSB,
sdl2::mixer::DEFAULT_CHANNELS,
1024,
)
.unwrap();
let mut volume = 3;
mixer::Music::set_volume(volume);
let music_data = RWops::from_bytes(MUSIC_DATA).unwrap();
let music = music_data.load_music().unwrap();
music.play(-1).unwrap();
let mut prev = now();
// let font_data = RWops::from_bytes(FONT_DATA).unwrap();
// let font_size = 12;
// let font = ttf_ctx.load_font_from_rwops(font_data, font_size).unwrap();
let font = ttf_ctx.load_font("./assets/TerminalVector.ttf", 12).unwrap();
let font_data = RWops::from_bytes(FONT_DATA).unwrap();
let font_size = 12;
let font = ttf_ctx.load_font_from_rwops(font_data, font_size).unwrap();
let font = ttf_ctx
.load_font("./assets/TerminalVector.ttf", 12)
.unwrap();
let fruit_atlas = texture_creator
.load_texture("./assets/fruit.png")
@@ -123,12 +142,35 @@ fn main() {
moved = true;
}
Keycode::Up => {
point.y -= 32;
moved = true;
if ctx
.keyboard()
.mod_state()
.contains(sdl2::keyboard::Mod::LSHIFTMOD)
{
if volume < 128 {
volume += 1;
mixer::Music::set_volume(volume);
}
} else {
point.y -= 32;
moved = true;
}
}
Keycode::Down => {
point.y += 32;
moved = true;
// Shift means modify volume
if ctx
.keyboard()
.mod_state()
.contains(sdl2::keyboard::Mod::LSHIFTMOD)
{
if volume > 0 {
volume -= 1;
mixer::Music::set_volume(volume);
}
} else {
point.y += 32;
moved = true;
}
}
_ => {}
},
@@ -172,9 +214,19 @@ fn main() {
.render(&text)
.blended(Color::RGBA(255, 255, 255, 50))
.unwrap();
let texture = texture_creator.create_texture_from_surface(&surface).unwrap();
let _ = canvas.copy(&texture, None, Rect::new(640i32 - (25i32 * text.len() as i32), 0, 25 * text.len() as u32, 40));
let texture = texture_creator
.create_texture_from_surface(&surface)
.unwrap();
let _ = canvas.copy(
&texture,
None,
Rect::new(
640i32 - (25i32 * text.len() as i32),
0,
25 * text.len() as u32,
40,
),
);
canvas.present();