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 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<Event>,
handlers: HashMap<usize, Box<dyn EventHandle + 'static>>,
run: Arc<AtomicBool>,
handlers: HashMap<usize, Box<dyn EventHandle + Send + 'static>>,
mouse: MouseState,
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
#[derive(Default)]
struct MouseState {
@ -28,21 +55,28 @@ struct KeyboardState {
}
impl Reactor {
pub fn new() -> (Sender<Event>, 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
}
}
}

@ -1,4 +1,5 @@
use little_core::CoreError;
use little_gl::GlError;
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 {
#[error("little-core error: {0}")]
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::{
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<L: GameLayer + 'static>(&mut self, mut layer: L) {
layer.initialise();
pub fn push_layer<L: GameLayer + 'static>(&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

Loading…
Cancel
Save