little: random improvements

wip/little-gl
Katharina Fey 3 years ago
parent bb9a3f493a
commit 74d2e1793c
Signed by: kookie
GPG Key ID: F972AEEA2887D547
  1. 60
      development/libs/little/little-events/src/reactor.rs
  2. 5
      development/libs/little/little/src/error.rs
  3. 14
      development/libs/little/little/src/game.rs

@ -1,15 +1,42 @@
use crate::EventHandle; use crate::EventHandle;
use crossbeam_channel::{unbounded, Receiver, Sender}; use crossbeam_channel::{unbounded, Receiver, Sender};
use little_core::event::{Event, Key}; use little_core::event::{Event, Key};
use std::collections::{HashMap, HashSet}; use std::{
collections::{HashMap, HashSet},
sync::{
atomic::{AtomicBool, Ordering},
Arc,
},
thread,
};
/// An asynchronous event reactor
pub struct Reactor { pub struct Reactor {
rx: Receiver<Event>, rx: Receiver<Event>,
handlers: HashMap<usize, Box<dyn EventHandle + 'static>>, run: Arc<AtomicBool>,
handlers: HashMap<usize, Box<dyn EventHandle + Send + 'static>>,
mouse: MouseState, mouse: MouseState,
key: KeyboardState, key: KeyboardState,
} }
/// A runtime handle to the event reactor
pub struct ReactorHandle {
run: Arc<AtomicBool>,
tx: Sender<Event>,
}
impl ReactorHandle {
/// Stop the event reactor thread
pub fn stop(&self) {
self.run.store(false, Ordering::Relaxed);
}
/// Send an event to the reactor
pub fn send(&self, e: Event) {
self.tx.send(e).unwrap();
}
}
/// Encode the state the mouse can be in /// Encode the state the mouse can be in
#[derive(Default)] #[derive(Default)]
struct MouseState { struct MouseState {
@ -28,21 +55,28 @@ struct KeyboardState {
} }
impl Reactor { impl Reactor {
pub fn new() -> (Sender<Event>, Self) { pub fn new() -> ReactorHandle {
let (tx, rx) = unbounded(); let (tx, rx) = unbounded();
( let run = Arc::new(true.into());
tx,
Self { // Create and start the event reactor
rx, let this = Self {
handlers: HashMap::new(), rx,
mouse: MouseState::default(), run: Arc::clone(&run),
key: KeyboardState::default(), handlers: HashMap::new(),
}, mouse: MouseState::default(),
) key: KeyboardState::default(),
};
thread::spawn(move || this.run());
// Then return a handle
ReactorHandle { run, tx }
} }
/// Run a detached event reactor /// Run a detached event reactor
pub fn run(self) { pub fn run(self) {
// while self.run.load(Ordering::Relaxed) {
// Do things
}
} }
} }

@ -1,4 +1,5 @@
use little_core::CoreError; use little_core::CoreError;
use little_gl::GlError;
pub type Result<T> = std::result::Result<T, LittleError>; pub type Result<T> = std::result::Result<T, LittleError>;
@ -7,4 +8,8 @@ pub type Result<T> = std::result::Result<T, LittleError>;
pub enum LittleError { pub enum LittleError {
#[error("little-core error: {0}")] #[error("little-core error: {0}")]
Core(CoreError), Core(CoreError),
#[error("little-gl error: {0}")]
Gl(GlError),
#[error("little: no more layer left")]
NoLayer,
} }

@ -1,4 +1,4 @@
use crate::Result; use crate::{LittleError, Result};
use little_core::{ use little_core::{
event::{Event, Events}, event::{Event, Events},
settings::GameSettings, settings::GameSettings,
@ -19,14 +19,18 @@ impl Game {
} }
/// Push a new layer on top of the layer stack /// Push a new layer on top of the layer stack
pub fn push_layer<L: GameLayer + 'static>(&mut self, mut layer: L) { pub fn push_layer<L: GameLayer + 'static>(&mut self, mut layer: L) -> Result<()> {
layer.initialise(); layer.initialise()?;
self.layers.push_front(Box::new(layer)); self.layers.push_front(Box::new(layer));
Ok(())
} }
/// Pop the last created layer from the stack /// Pop the last created layer from the stack
pub fn pop_layer(&mut self) { pub fn pop_layer(&mut self) -> Result<()> {
self.layers.pop_front(); match self.layers.pop_front() {
Some(_) => Ok(()),
None => Err(LittleError::NoLayer)
}
} }
/// Run this game /// Run this game

Loading…
Cancel
Save