fix: minor comments, disable accelerated, use std sleep on web builds

This commit is contained in:
2023-09-14 15:42:49 -05:00
parent 00c4c76299
commit 229d2242ef
2 changed files with 25 additions and 6 deletions

View File

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

View File

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