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=-g -gsource-map",
"-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1", "-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1",
# "-C", "link-args=-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 # USE_OGG, USE_VORBIS for OGG/VORBIS usage
"-C", "link-args=--preload-file assets/", "-C", "link-args=--preload-file assets/",
] ]

View File

@@ -9,7 +9,7 @@ edition = "2021"
colors-transform = "0.2.11" colors-transform = "0.2.11"
lazy_static = "1.4.0" lazy_static = "1.4.0"
rand = "0.8.5" rand = "0.8.5"
sdl2 = { version = "0.35", features = ["image", "ttf"] } sdl2 = { version = "0.35", features = ["image", "ttf", "mixer"] }
spin_sleep = "1.1.1" spin_sleep = "1.1.1"
tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]} tracing = { version = "0.1.37", features = ["max_level_debug", "release_max_level_debug"]}
tracing-error = "0.2.0" 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. - 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 - [ ] SDL2 Extensions
- [X] Image - [X] Image
- [ ] Mixer - [X] Mixer
- [X] TTF - [X] TTF
- [ ] GFX - [ ] GFX
- All of these libraries are common and necessary for a lot of projects. Ensuring they work is important. - 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::process;
use std::time::Duration; use std::time::Duration;
use sdl2::rwops::RWops;
use sdl2::ttf;
use sdl2::event::Event; use sdl2::event::Event;
use sdl2::image::LoadTexture; use sdl2::image::LoadTexture;
use sdl2::keyboard::Keycode; use sdl2::keyboard::Keycode;
use sdl2::mixer::{self, LoaderRWops, Music};
use sdl2::pixels::Color; use sdl2::pixels::Color;
use sdl2::rect::{Point, Rect}; 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 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); static BLACK: Color = Color::RGB(0, 0, 0);
#[cfg(target_os = "emscripten")] #[cfg(target_os = "emscripten")]
@@ -22,7 +24,7 @@ fn sleep(ms: u32) {
} }
#[cfg(target_os = "emscripten")] #[cfg(target_os = "emscripten")]
fn now() -> f64 { fn now() -> f64 {
emscripten::emscripten::now() / 1000f64 emscripten::emscripten::now() / 1000f64
} }
@@ -75,12 +77,29 @@ fn main() {
let ttf_ctx = ttf_context(); 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 mut prev = now();
// let font_data = RWops::from_bytes(FONT_DATA).unwrap(); let font_data = RWops::from_bytes(FONT_DATA).unwrap();
// let font_size = 12; let font_size = 12;
// let font = ttf_ctx.load_font_from_rwops(font_data, font_size).unwrap(); 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 = ttf_ctx
.load_font("./assets/TerminalVector.ttf", 12)
.unwrap();
let fruit_atlas = texture_creator let fruit_atlas = texture_creator
.load_texture("./assets/fruit.png") .load_texture("./assets/fruit.png")
@@ -123,12 +142,35 @@ fn main() {
moved = true; moved = true;
} }
Keycode::Up => { Keycode::Up => {
point.y -= 32; if ctx
moved = true; .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 => { Keycode::Down => {
point.y += 32; // Shift means modify volume
moved = true; 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) .render(&text)
.blended(Color::RGBA(255, 255, 255, 50)) .blended(Color::RGBA(255, 255, 255, 50))
.unwrap(); .unwrap();
let texture = texture_creator.create_texture_from_surface(&surface).unwrap(); let texture = texture_creator
let _ = canvas.copy(&texture, None, Rect::new(640i32 - (25i32 * text.len() as i32), 0, 25 * text.len() as u32, 40)); .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(); canvas.present();
@@ -191,7 +243,7 @@ fn main() {
let duration = Duration::from_secs_f64(now() - prev); let duration = Duration::from_secs_f64(now() - prev);
let fps = 1f64 / (duration.as_secs_f64()); let fps = 1f64 / (duration.as_secs_f64());
prev = now(); prev = now();
n += 1; n += 1;
let a = 1f64 / n as f64; let a = 1f64 / n as f64;
let b = 1f64 - a; let b = 1f64 - a;