veloren/voxygen/src/main.rs

72 lines
1.8 KiB
Rust
Raw Normal View History

mod menu;
2019-01-07 21:10:31 +00:00
mod render;
mod window;
// Standard
use std::mem;
2019-01-07 21:10:31 +00:00
// Library
use winit;
use failure;
// Crate
use crate::{
menu::title::TitleState,
window::Window,
};
2019-01-07 21:10:31 +00:00
#[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
2019-01-07 21:10:31 +00:00
pub enum PlayStateResult {
/// Pop all play states in reverse order and shut down the program
Shutdown,
/// Close the current play state
Close,
2019-01-07 21:10:31 +00:00
/// Push a new play state onto the play state stack
Push(Box<dyn PlayState>),
2019-01-07 21:10:31 +00:00
/// Switch the current play state with a new play state
Switch(Box<dyn PlayState>),
}
pub trait PlayState {
2019-01-07 21:10:31 +00:00
fn play(&mut self, global_state: &mut GlobalState) -> PlayStateResult;
}
fn main() {
let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(TitleState::new())];
let mut global_state = GlobalState {
2019-01-07 21:10:31 +00:00
window: Window::new()
.expect("Failed to create window"),
};
2019-01-02 22:08:13 +00:00
while let Some(state_result) = states.last_mut().map(|last| last.play(&mut global_state)){
// Implement state transfer logic
2019-01-02 22:08:13 +00:00
match state_result {
2019-01-07 21:10:31 +00:00
PlayStateResult::Shutdown => while states.last().is_some() {
states.pop();
},
PlayStateResult::Close => {
2019-01-02 22:08:13 +00:00
states.pop();
},
2019-01-07 21:10:31 +00:00
PlayStateResult::Push(new_state) => {
2019-01-02 22:08:13 +00:00
states.push(new_state);
},
2019-01-07 21:10:31 +00:00
PlayStateResult::Switch(mut new_state) => {
2019-01-02 22:08:13 +00:00
states.last_mut().map(|old_state| mem::swap(old_state, &mut new_state));
},
}
}
}