mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 03:15:48 -06:00
feat: setup emscripten module for api layer
This commit is contained in:
31
src/emscripten.rs
Normal file
31
src/emscripten.rs
Normal file
@@ -0,0 +1,31 @@
|
||||
#[allow(dead_code)]
|
||||
#[cfg(target_os = "emscripten")]
|
||||
pub mod emscripten {
|
||||
use std::os::raw::c_uint;
|
||||
|
||||
extern "C" {
|
||||
pub fn emscripten_get_now() -> f64;
|
||||
pub fn emscripten_sleep(ms: c_uint);
|
||||
pub fn emscripten_get_element_css_size(target: *const u8, width: *mut f64, height: *mut f64) -> i32;
|
||||
}
|
||||
|
||||
// milliseconds since start of program
|
||||
pub fn now() -> f64 {
|
||||
unsafe { emscripten_get_now() }
|
||||
}
|
||||
|
||||
pub fn sleep(ms: u32) {
|
||||
unsafe {
|
||||
emscripten_sleep(ms);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_canvas_size() -> (u32, u32) {
|
||||
let mut width = 0.0;
|
||||
let mut height = 0.0;
|
||||
unsafe {
|
||||
emscripten_get_element_css_size("canvas\0".as_ptr(), &mut width, &mut height);
|
||||
}
|
||||
(width as u32, height as u32)
|
||||
}
|
||||
}
|
||||
31
src/main.rs
31
src/main.rs
@@ -59,6 +59,8 @@ mod constants;
|
||||
mod debug;
|
||||
mod direction;
|
||||
mod edible;
|
||||
#[cfg(target_os = "emscripten")]
|
||||
mod emscripten;
|
||||
mod entity;
|
||||
mod game;
|
||||
mod ghost;
|
||||
@@ -68,6 +70,26 @@ mod map;
|
||||
mod modulation;
|
||||
mod pacman;
|
||||
|
||||
#[cfg(not(target_os = "emscripten"))]
|
||||
fn sleep(value: Duration) {
|
||||
spin_sleep::sleep(value);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "emscripten")]
|
||||
fn sleep(value: Duration) {
|
||||
emscripten::emscripten::sleep(value.as_millis() as u32);
|
||||
}
|
||||
|
||||
#[cfg(target_os = "emscripten")]
|
||||
fn now() -> std::time::Instant {
|
||||
std::time::Instant::now() + std::time::Duration::from_millis(emscripten::emscripten::now() as u64)
|
||||
}
|
||||
|
||||
#[cfg(not(target_os = "emscripten"))]
|
||||
fn now() -> std::time::Instant {
|
||||
std::time::Instant::now()
|
||||
}
|
||||
|
||||
/// The main entry point of the application.
|
||||
///
|
||||
/// This function initializes SDL, the window, the game state, and then enters
|
||||
@@ -179,14 +201,7 @@ pub fn main() {
|
||||
if start.elapsed() < loop_time {
|
||||
let time = loop_time.saturating_sub(start.elapsed());
|
||||
if time != Duration::ZERO {
|
||||
#[cfg(not(target_os = "emscripten"))]
|
||||
{
|
||||
spin_sleep::sleep(time);
|
||||
}
|
||||
#[cfg(target_os = "emscripten")]
|
||||
{
|
||||
std::thread::sleep(time);
|
||||
}
|
||||
sleep(time);
|
||||
}
|
||||
} else {
|
||||
event!(
|
||||
|
||||
Reference in New Issue
Block a user