From 74d2e1793c303b84ecf8578fb568ad415735fdd2 Mon Sep 17 00:00:00 2001 From: Katharina Fey Date: Mon, 14 Jun 2021 00:29:13 +0200 Subject: [PATCH] little: random improvements --- .../libs/little/little-events/src/reactor.rs | 60 +++++++++++++++---- development/libs/little/little/src/error.rs | 5 ++ development/libs/little/little/src/game.rs | 14 +++-- 3 files changed, 61 insertions(+), 18 deletions(-) diff --git a/development/libs/little/little-events/src/reactor.rs b/development/libs/little/little-events/src/reactor.rs index add4c360d00..e19623bf63a 100644 --- a/development/libs/little/little-events/src/reactor.rs +++ b/development/libs/little/little-events/src/reactor.rs @@ -1,15 +1,42 @@ use crate::EventHandle; use crossbeam_channel::{unbounded, Receiver, Sender}; 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 { rx: Receiver, - handlers: HashMap>, + run: Arc, + handlers: HashMap>, mouse: MouseState, key: KeyboardState, } +/// A runtime handle to the event reactor +pub struct ReactorHandle { + run: Arc, + tx: Sender, +} + +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 #[derive(Default)] struct MouseState { @@ -28,21 +55,28 @@ struct KeyboardState { } impl Reactor { - pub fn new() -> (Sender, Self) { + pub fn new() -> ReactorHandle { let (tx, rx) = unbounded(); - ( - tx, - Self { - rx, - handlers: HashMap::new(), - mouse: MouseState::default(), - key: KeyboardState::default(), - }, - ) + let run = Arc::new(true.into()); + + // Create and start the event reactor + let this = Self { + rx, + run: Arc::clone(&run), + 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 pub fn run(self) { - // + while self.run.load(Ordering::Relaxed) { + // Do things + } } } diff --git a/development/libs/little/little/src/error.rs b/development/libs/little/little/src/error.rs index dc3f8880966..e6088c3eea2 100644 --- a/development/libs/little/little/src/error.rs +++ b/development/libs/little/little/src/error.rs @@ -1,4 +1,5 @@ use little_core::CoreError; +use little_gl::GlError; pub type Result = std::result::Result; @@ -7,4 +8,8 @@ pub type Result = std::result::Result; pub enum LittleError { #[error("little-core error: {0}")] Core(CoreError), + #[error("little-gl error: {0}")] + Gl(GlError), + #[error("little: no more layer left")] + NoLayer, } diff --git a/development/libs/little/little/src/game.rs b/development/libs/little/little/src/game.rs index ae1aa993a47..dbe437f2a6b 100644 --- a/development/libs/little/little/src/game.rs +++ b/development/libs/little/little/src/game.rs @@ -1,4 +1,4 @@ -use crate::Result; +use crate::{LittleError, Result}; use little_core::{ event::{Event, Events}, settings::GameSettings, @@ -19,14 +19,18 @@ impl Game { } /// Push a new layer on top of the layer stack - pub fn push_layer(&mut self, mut layer: L) { - layer.initialise(); + pub fn push_layer(&mut self, mut layer: L) -> Result<()> { + layer.initialise()?; self.layers.push_front(Box::new(layer)); + Ok(()) } /// Pop the last created layer from the stack - pub fn pop_layer(&mut self) { - self.layers.pop_front(); + pub fn pop_layer(&mut self) -> Result<()> { + match self.layers.pop_front() { + Some(_) => Ok(()), + None => Err(LittleError::NoLayer) + } } /// Run this game