mirror of
https://github.com/Xevion/Pac-Man.git
synced 2025-12-06 09:15:46 -06:00
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| e925376b7a |
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 debug;
|
||||||
mod direction;
|
mod direction;
|
||||||
mod edible;
|
mod edible;
|
||||||
|
#[cfg(target_os = "emscripten")]
|
||||||
|
mod emscripten;
|
||||||
mod entity;
|
mod entity;
|
||||||
mod game;
|
mod game;
|
||||||
mod ghost;
|
mod ghost;
|
||||||
@@ -68,6 +70,26 @@ mod map;
|
|||||||
mod modulation;
|
mod modulation;
|
||||||
mod pacman;
|
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.
|
/// The main entry point of the application.
|
||||||
///
|
///
|
||||||
/// This function initializes SDL, the window, the game state, and then enters
|
/// This function initializes SDL, the window, the game state, and then enters
|
||||||
@@ -179,14 +201,7 @@ pub fn main() {
|
|||||||
if start.elapsed() < loop_time {
|
if start.elapsed() < loop_time {
|
||||||
let time = loop_time.saturating_sub(start.elapsed());
|
let time = loop_time.saturating_sub(start.elapsed());
|
||||||
if time != Duration::ZERO {
|
if time != Duration::ZERO {
|
||||||
#[cfg(not(target_os = "emscripten"))]
|
sleep(time);
|
||||||
{
|
|
||||||
spin_sleep::sleep(time);
|
|
||||||
}
|
|
||||||
#[cfg(target_os = "emscripten")]
|
|
||||||
{
|
|
||||||
std::thread::sleep(time);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
event!(
|
event!(
|
||||||
|
|||||||
Reference in New Issue
Block a user