mod menu; mod render; mod window; // Standard use std::mem; // Library use winit; use failure; // Crate use crate::{ menu::title::TitleState, window::Window, }; #[derive(Debug)] pub enum VoxygenErr { WinitCreationErr(winit::CreationError), Other(failure::Error), } // A type used to store state that is shared between all play states pub struct GlobalState { window: Window, } // States can either close (and revert to a previous state), push a new state on top of themselves, // or switch to a totally different state pub enum PlayStateResult { /// Pop all play states in reverse order and shut down the program Shutdown, /// Close the current play state Close, /// Push a new play state onto the play state stack Push(Box), /// Switch the current play state with a new play state Switch(Box), } pub trait PlayState { fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult; } fn main() { let mut states: Vec> = vec![Box::new(TitleState::new())]; let mut global_state = GlobalState { window: Window::new() .expect("Failed to create window"), }; while let Some(state_result) = states.last_mut().map(|last| last.play(&mut global_state)){ // Implement state transfer logic match state_result { PlayStateResult::Shutdown => while states.last().is_some() { states.pop(); }, PlayStateResult::Close => { states.pop(); }, PlayStateResult::Push(new_state) => { states.push(new_state); }, PlayStateResult::Switch(mut new_state) => { states.last_mut().map(|old_state| mem::swap(old_state, &mut new_state)); }, } } }