Cleaning
This commit is contained in:
parent
313b188873
commit
bc728f8908
@ -1,7 +1,7 @@
|
||||
//! This module helps us deal with controls and events.
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::collections::{HashMap, VecDeque};
|
||||
use std::rc::Rc;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
@ -29,6 +29,13 @@ impl Keyboard {
|
||||
Event::KeyPressed(x) => *b.keys.entry(x).or_insert(true) = true,
|
||||
Event::KeyReleased(x) => *b.keys.entry(x).or_insert(false) = false,
|
||||
}
|
||||
|
||||
b.events.push_back(event);
|
||||
}
|
||||
|
||||
/// Gets an event from the event stack.
|
||||
pub fn pop(&self) -> Option<Event> {
|
||||
self.inner.borrow_mut().events.pop_front()
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,7 +80,7 @@ pub struct InnerKeyboard {
|
||||
keys: HashMap<Key, bool>,
|
||||
|
||||
/// The list of events to be processed.
|
||||
events: Vec<Event>,
|
||||
events: VecDeque<Event>,
|
||||
}
|
||||
|
||||
impl InnerKeyboard {
|
||||
@ -81,7 +88,7 @@ impl InnerKeyboard {
|
||||
pub fn new() -> InnerKeyboard {
|
||||
InnerKeyboard {
|
||||
keys: HashMap::new(),
|
||||
events: vec![],
|
||||
events: VecDeque::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,6 @@
|
||||
//! A module that makes it easier to deal with images.
|
||||
|
||||
use std::borrow::BorrowMut;
|
||||
use std::cell::{Ref, RefCell};
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
|
@ -11,7 +11,16 @@ use wasm_bindgen::JsCast;
|
||||
|
||||
use crate::engine::event::{Key, Keyboard};
|
||||
use crate::engine::image::Image;
|
||||
use crate::Result;
|
||||
use crate::{error_js, Result};
|
||||
|
||||
macro_rules! unwrap {
|
||||
($t: expr) => {{
|
||||
match $t {
|
||||
None => return Err(JsValue::from_str("cannot unwrap on none")),
|
||||
Some(x) => x,
|
||||
}
|
||||
}};
|
||||
}
|
||||
|
||||
/// Our game engine.
|
||||
pub struct Engine {
|
||||
@ -32,8 +41,9 @@ pub struct Engine {
|
||||
impl Engine {
|
||||
/// Creates a new engine.
|
||||
pub fn new() -> Result<Engine> {
|
||||
let document = web_sys::window().unwrap().document().unwrap();
|
||||
let canvas = document.get_element_by_id("canvas").unwrap();
|
||||
let window = unwrap!(web_sys::window());
|
||||
let document = unwrap!(window.document());
|
||||
let canvas = unwrap!(document.get_element_by_id("canvas"));
|
||||
let canvas: web_sys::HtmlCanvasElement = canvas.dyn_into::<web_sys::HtmlCanvasElement>()?;
|
||||
|
||||
canvas.set_width(1920);
|
||||
@ -70,8 +80,10 @@ impl Engine {
|
||||
/// Starts the engine.
|
||||
pub fn start(&self) -> Result<()> {
|
||||
let clone = self.clone();
|
||||
let cb = Closure::<dyn FnMut(_)>::new(move |event: web_sys::Event| {
|
||||
clone.run();
|
||||
let cb = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::Event| {
|
||||
if let Err(e) = clone.run() {
|
||||
error_js!(e);
|
||||
}
|
||||
});
|
||||
|
||||
web_sys::window()
|
||||
@ -94,8 +106,10 @@ impl Engine {
|
||||
|
||||
// Schedule next render
|
||||
let clone = self.clone();
|
||||
let cb = Closure::<dyn FnMut(_)>::new(move |event: web_sys::Event| {
|
||||
clone.run();
|
||||
let cb = Closure::<dyn FnMut(_)>::new(move |_event: web_sys::Event| {
|
||||
if let Err(e) = clone.run() {
|
||||
error_js!(e);
|
||||
}
|
||||
});
|
||||
|
||||
web_sys::window()
|
||||
@ -112,6 +126,11 @@ impl Engine {
|
||||
pub fn update(&self) -> Result<()> {
|
||||
let mut inner = self.inner.borrow_mut();
|
||||
|
||||
// Manage events
|
||||
while let Some(_event) = inner.keyboard.pop() {
|
||||
// Nothing to do here yet
|
||||
}
|
||||
|
||||
// Perform update
|
||||
let mut dx = 0.0;
|
||||
let mut dy = 0.0;
|
||||
|
44
src/lib.rs
44
src/lib.rs
@ -1,8 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
use std::result::Result as StdResult;
|
||||
|
||||
use wasm_bindgen::prelude::*;
|
||||
use wasm_bindgen::JsCast;
|
||||
|
||||
pub mod engine;
|
||||
|
||||
@ -16,12 +14,20 @@ extern "C" {
|
||||
|
||||
#[wasm_bindgen(js_namespace = console, js_name = log)]
|
||||
pub fn console_log_js(s: JsValue);
|
||||
|
||||
#[wasm_bindgen(js_name = console, js_name = error)]
|
||||
pub fn console_error(s: &str);
|
||||
|
||||
#[wasm_bindgen(js_name = console, js_name = error)]
|
||||
pub fn console_error_js(s: JsValue);
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
macro_rules! log {
|
||||
($($t:tt)*) => (crate::console_log(&format_args!($($t)*).to_string()))
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
macro_rules! log_js {
|
||||
($t:tt) => {{
|
||||
let e: JsValue = $t.into();
|
||||
@ -29,12 +35,40 @@ macro_rules! log_js {
|
||||
}};
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
macro_rules! error {
|
||||
($($t:tt)*) => (crate::console_error(&format_args!($($t)*).to_string()))
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
macro_rules! error_js {
|
||||
($t:tt) => {{
|
||||
let e: JsValue = $t.into();
|
||||
crate::console_error_js(e)
|
||||
}};
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) use error;
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) use error_js;
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) use log;
|
||||
|
||||
#[allow(unused)]
|
||||
pub(crate) use log_js;
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn start() -> Result<()> {
|
||||
let mut engine = engine::Engine::new()?;
|
||||
pub fn run() -> Result<()> {
|
||||
let engine = engine::Engine::new()?;
|
||||
engine.start()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[wasm_bindgen(start)]
|
||||
pub fn start() {
|
||||
if let Err(e) = run() {
|
||||
error_js!(e);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user