mirror of
https://github.com/Xevion/rust-sdl2-emscripten.git
synced 2025-12-06 03:16:23 -06:00
Add localStorage accessor for storing/retrieving volume between sessions, script execution
This commit is contained in:
@@ -6,7 +6,6 @@ pub mod emscripten {
|
||||
extern "C" {
|
||||
pub fn emscripten_get_now() -> f64;
|
||||
pub fn emscripten_sleep(ms: c_uint);
|
||||
pub fn emscripten_run_script(script: *const u8);
|
||||
pub fn emscripten_get_element_css_size(
|
||||
target: *const u8,
|
||||
width: *mut f64,
|
||||
@@ -25,12 +24,6 @@ pub mod emscripten {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exec(script: &str) {
|
||||
unsafe {
|
||||
emscripten_run_script(script.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_canvas_size() -> (u32, u32) {
|
||||
let mut width = 0.0;
|
||||
let mut height = 0.0;
|
||||
|
||||
30
src/main.rs
30
src/main.rs
@@ -17,6 +17,8 @@ static BLACK: Color = Color::RGB(0, 0, 0);
|
||||
#[cfg(target_os = "emscripten")]
|
||||
mod emscripten;
|
||||
|
||||
mod store;
|
||||
|
||||
#[cfg(not(target_os = "emscripten"))]
|
||||
fn sleep(ms: u32) {
|
||||
std::thread::sleep(Duration::from_millis(ms as u64));
|
||||
@@ -59,6 +61,8 @@ fn main() {
|
||||
let window = match video_ctx
|
||||
.window("Hello, Rust / SDL2 / WASM!", 640, 480)
|
||||
.position_centered()
|
||||
.resizable()
|
||||
.allow_highdpi()
|
||||
.opengl()
|
||||
.build()
|
||||
{
|
||||
@@ -71,6 +75,8 @@ fn main() {
|
||||
Err(err) => panic!("failed to create canvas: {}", err),
|
||||
};
|
||||
|
||||
// canvas.set_logical_size(1000, 1000);
|
||||
|
||||
let texture_creator = canvas.texture_creator();
|
||||
let mut point = Point::new(0, 0);
|
||||
|
||||
@@ -84,8 +90,14 @@ fn main() {
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut volume = 1;
|
||||
mixer::Music::set_volume(volume);
|
||||
let storage = store::Store {};
|
||||
|
||||
let mut volume = storage.volume().map_or_else(|| {
|
||||
println!("No volume stored, using default");
|
||||
1
|
||||
}, |v| v);
|
||||
print!("Volume: {}", volume);
|
||||
mixer::Music::set_volume(volume as i32);
|
||||
|
||||
let music_data = RWops::from_bytes(MUSIC_DATA).unwrap();
|
||||
let music = mixer::LoaderRWops::load_music(&music_data).unwrap();
|
||||
@@ -96,6 +108,7 @@ fn main() {
|
||||
// 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();
|
||||
@@ -116,6 +129,13 @@ fn main() {
|
||||
let mut moved = false;
|
||||
for event in ctx.event_pump().unwrap().poll_iter() {
|
||||
match event {
|
||||
Event::Window { win_event, .. } => match win_event {
|
||||
sdl2::event::WindowEvent::Resized(w, h) => {
|
||||
println!("Resized to {}x{}", w, h);
|
||||
canvas.window_mut().set_size(w as u32, h as u32).unwrap();
|
||||
}
|
||||
_ => {}
|
||||
},
|
||||
Event::Quit { .. }
|
||||
| Event::KeyDown {
|
||||
keycode: Some(Keycode::Escape),
|
||||
@@ -148,7 +168,8 @@ fn main() {
|
||||
{
|
||||
if volume < 128 {
|
||||
volume += 1;
|
||||
mixer::Music::set_volume(volume);
|
||||
mixer::Music::set_volume(volume as i32);
|
||||
storage.set_volume(volume);
|
||||
}
|
||||
} else {
|
||||
point.y -= 32;
|
||||
@@ -164,7 +185,8 @@ fn main() {
|
||||
{
|
||||
if volume > 0 {
|
||||
volume -= 1;
|
||||
mixer::Music::set_volume(volume);
|
||||
mixer::Music::set_volume(volume as i32);
|
||||
storage.set_volume(volume);
|
||||
}
|
||||
} else {
|
||||
point.y += 32;
|
||||
|
||||
54
src/store.rs
Normal file
54
src/store.rs
Normal file
@@ -0,0 +1,54 @@
|
||||
pub struct Store;
|
||||
|
||||
#[cfg(not(target_os = "emscripten"))]
|
||||
impl Store {
|
||||
pub fn volume(&self) -> Option<u32> {
|
||||
None
|
||||
}
|
||||
|
||||
pub fn set_volume(&self, volume: u32) {
|
||||
// Do nothing
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(target_os = "emscripten")]
|
||||
extern "C" {
|
||||
pub fn emscripten_run_script(script: *const libc::c_char);
|
||||
pub fn emscripten_run_script_string(script: *const libc::c_char) -> *mut libc::c_char;
|
||||
}
|
||||
|
||||
#[cfg(target_os = "emscripten")]
|
||||
impl Store {
|
||||
fn run_script(script: &str) {
|
||||
use std::ffi::CString;
|
||||
|
||||
let script = CString::new(script).unwrap();
|
||||
unsafe {
|
||||
emscripten_run_script(script.as_ptr());
|
||||
}
|
||||
}
|
||||
|
||||
fn run_script_string(script: &str) -> String {
|
||||
use std::ffi::{CStr, CString};
|
||||
|
||||
let script = CString::new(script).unwrap();
|
||||
unsafe {
|
||||
let ptr = emscripten_run_script_string(script.as_ptr());
|
||||
let c_str = CStr::from_ptr(ptr);
|
||||
String::from(c_str.to_str().unwrap())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn volume(&self) -> Option<u32> {
|
||||
// Use local storage to try and read volume
|
||||
let script = "localStorage.getItem('volume')";
|
||||
let response = Store::run_script_string(&script);
|
||||
response.parse::<u32>().ok()
|
||||
}
|
||||
|
||||
pub fn set_volume(&self, volume: u32) {
|
||||
// Use local storage to set volume
|
||||
let script = format!("localStorage.setItem('volume', '{}')", volume);
|
||||
Store::run_script_string(&script);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user