Compare commits

..

4 Commits

4 changed files with 27 additions and 56 deletions

View File

@@ -5,5 +5,6 @@ rustflags = [
#"-C", "link-args=-O3 --closure 1",
"-C", "link-args=-sASYNCIFY -sALLOW_MEMORY_GROWTH=1",
"-C", "link-args=-sUSE_SDL=2 -sUSE_SDL_IMAGE=2 -sSDL2_IMAGE_FORMATS=['png']",
"-C", "link-args=--preload-file assets/ -lidbfs.js",
# USE_OGG, USE_VORBIS for OGG/VORBIS usage
"-C", "link-args=--preload-file assets/",
]

View File

@@ -1,49 +0,0 @@
// taken from https://github.com/Gigoteur/PX8/blob/master/src/px8/emscripten.rs
#[cfg(target_os = "emscripten")]
pub mod emscripten {
use std::cell::RefCell;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_float, c_int, c_void};
use std::ptr::null_mut;
#[allow(non_camel_case_types)]
type em_callback_func = unsafe extern "C" fn();
extern "C" {
// void emscripten_set_main_loop(em_callback_func func, int fps, int simulate_infinite_loop)
pub fn emscripten_set_main_loop(
func: em_callback_func,
fps: c_int,
simulate_infinite_loop: c_int,
);
pub fn emscripten_cancel_main_loop();
pub fn emscripten_pause_main_loop();
pub fn emscripten_get_now() -> c_float;
}
thread_local!(static MAIN_LOOP_CALLBACK: RefCell<*mut c_void> = RefCell::new(null_mut()));
pub fn set_main_loop_callback<F>(callback: F)
where
F: FnMut(),
{
MAIN_LOOP_CALLBACK.with(|log| {
*log.borrow_mut() = &callback as *const _ as *mut c_void;
});
unsafe {
emscripten_set_main_loop(wrapper::<F>, -1, 1);
}
unsafe extern "C" fn wrapper<F>()
where
F: FnMut(),
{
MAIN_LOOP_CALLBACK.with(|z| {
let closure = *z.borrow_mut() as *mut F;
(*closure)();
});
}
}
}

View File

@@ -62,6 +62,8 @@ impl Game<'_> {
self.canvas.set_draw_color(Color::RGB(0, 0, 0));
self.canvas.clear();
// Render the map
self.canvas
.copy(&self.map_texture, None, None)
.expect("Could not render texture on canvas");
@@ -102,6 +104,7 @@ impl Game<'_> {
self.draw_cell((next_cell.0 as u32, next_cell.1 as u32), Color::YELLOW);
}
// Present the canvas
self.canvas.present();
}

View File

@@ -1,8 +1,7 @@
use crate::constants::{WINDOW_HEIGHT, WINDOW_WIDTH};
use crate::game::Game;
use sdl2::event::Event;
use sdl2::event::{Event, WindowEvent};
use sdl2::keyboard::Keycode;
use spin_sleep::sleep;
use std::time::{Duration, Instant};
use tracing::event;
use tracing_error::ErrorLayer;
@@ -38,7 +37,6 @@ pub fn main() {
let mut canvas = window
.into_canvas()
.accelerated()
.build()
.expect("Could not build canvas");
@@ -64,6 +62,7 @@ pub fn main() {
let mut last_averaging_time = Instant::now();
let mut sleep_time = Duration::ZERO;
let mut paused = false;
let mut shown = false;
event!(
tracing::Level::INFO,
@@ -75,8 +74,20 @@ pub fn main() {
// TODO: Fix key repeat delay issues by using VecDeque for instant key repeat
for event in event_pump.poll_iter() {
match event {
Event::Window { win_event, .. } => {
match win_event {
WindowEvent::Hidden => {
event!(tracing::Level::DEBUG, "Window hidden");
shown = false;
},
WindowEvent::Shown => {
event!(tracing::Level::DEBUG, "Window shown");
shown = true;
},
_ => {}
}
}
// Handle quitting keys or window close
Event::Quit { .. }
| Event::KeyDown {
@@ -111,8 +122,13 @@ pub fn main() {
}
if start.elapsed() < loop_time {
let time = loop_time - start.elapsed();
sleep(time);
let time = loop_time.saturating_sub(start.elapsed());
#[cfg(not(target_os = "emscripten"))] {
spin_sleep::sleep(time);
}
#[cfg(target_os = "emscripten")] {
thread::sleep(time);
}
sleep_time += time;
} else {
event!(