mirror of
https://gitlab.com/veloren/veloren.git
synced 2024-08-30 18:12:32 +00:00
Started adding voxygen and windowing code
This commit is contained in:
parent
523db9f054
commit
7a29286053
@ -3,6 +3,7 @@ members = [
|
||||
"common",
|
||||
"client",
|
||||
"server",
|
||||
"voxygen",
|
||||
]
|
||||
|
||||
[profile.dev]
|
||||
|
3
voxygen/.gitignore
vendored
Normal file
3
voxygen/.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/target
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
14
voxygen/Cargo.toml
Normal file
14
voxygen/Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "voxygen"
|
||||
version = "0.1.0"
|
||||
authors = ["Joshua Barretto <joshua.s.barretto@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
common = { path = "../common" }
|
||||
|
||||
# Graphics
|
||||
gfx = "0.17.1"
|
||||
gfx_device_gl = "0.15.0"
|
||||
gfx_window_glutin = "0.25.0"
|
||||
glutin = "0.17.0"
|
50
voxygen/src/main.rs
Normal file
50
voxygen/src/main.rs
Normal file
@ -0,0 +1,50 @@
|
||||
mod menu;
|
||||
mod window;
|
||||
|
||||
// Standard
|
||||
use std::mem;
|
||||
|
||||
// Crate
|
||||
use crate::{
|
||||
menu::title::TitleState,
|
||||
window::Window,
|
||||
};
|
||||
|
||||
// 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 StateResult {
|
||||
Close,
|
||||
Push(Box<dyn PlayState>),
|
||||
Switch(Box<dyn PlayState>),
|
||||
}
|
||||
|
||||
pub trait PlayState {
|
||||
fn play(&mut self, global_state: &mut GlobalState) -> StateResult;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let mut states: Vec<Box<dyn PlayState>> = vec![Box::new(TitleState::new())];
|
||||
|
||||
let mut global_state = GlobalState {
|
||||
window: Window::new(),
|
||||
};
|
||||
|
||||
loop {
|
||||
// Implement state transfer logic
|
||||
match states.last_mut().map(|last| last.play(&mut global_state)) {
|
||||
Some(StateResult::Close) => { states.pop(); },
|
||||
Some(StateResult::Push(new_state)) => { states.push(new_state); },
|
||||
Some(StateResult::Switch(mut new_state)) => if let Some(old_state) = states.last_mut() {
|
||||
mem::swap(old_state, &mut new_state);
|
||||
} else {
|
||||
break;
|
||||
},
|
||||
None => break,
|
||||
}
|
||||
}
|
||||
}
|
1
voxygen/src/menu/mod.rs
Normal file
1
voxygen/src/menu/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod title;
|
30
voxygen/src/menu/title.rs
Normal file
30
voxygen/src/menu/title.rs
Normal file
@ -0,0 +1,30 @@
|
||||
// Crate
|
||||
use crate::{
|
||||
PlayState,
|
||||
StateResult,
|
||||
GlobalState,
|
||||
window::Event,
|
||||
};
|
||||
|
||||
pub struct TitleState;
|
||||
|
||||
impl TitleState {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
}
|
||||
|
||||
impl PlayState for TitleState {
|
||||
fn play(&mut self, global_state: &mut GlobalState) -> StateResult {
|
||||
let mut running = true;
|
||||
while running {
|
||||
global_state.window.poll_events(|event| match event {
|
||||
Event::Close => running = false,
|
||||
});
|
||||
|
||||
global_state.window.swap_buffers();
|
||||
}
|
||||
|
||||
StateResult::Close
|
||||
}
|
||||
}
|
87
voxygen/src/window.rs
Normal file
87
voxygen/src/window.rs
Normal file
@ -0,0 +1,87 @@
|
||||
// External
|
||||
use gfx::handle::{RenderTargetView, DepthStencilView};
|
||||
use gfx_device_gl::{Device, Resources, Factory};
|
||||
use gfx_window_glutin;
|
||||
use glutin::{
|
||||
self,
|
||||
Api::OpenGl,
|
||||
dpi::LogicalSize,
|
||||
ContextBuilder,
|
||||
EventsLoop,
|
||||
GlContext,
|
||||
GlRequest,
|
||||
GlWindow,
|
||||
WindowBuilder,
|
||||
};
|
||||
|
||||
type TgtColorView = RenderTargetView<Resources, gfx::format::Srgba8>;
|
||||
type TgtDepthView = DepthStencilView<Resources, gfx::format::DepthStencil>;
|
||||
|
||||
pub struct RenderCtx {
|
||||
device: Device,
|
||||
factory: Factory,
|
||||
tgt_color_view: TgtColorView,
|
||||
tgt_depth_view: TgtDepthView,
|
||||
}
|
||||
|
||||
pub struct Window {
|
||||
events_loop: EventsLoop,
|
||||
gl_window: GlWindow,
|
||||
render_ctx: RenderCtx,
|
||||
}
|
||||
|
||||
|
||||
impl Window {
|
||||
pub fn new() -> Window {
|
||||
let events_loop = EventsLoop::new();
|
||||
|
||||
let (
|
||||
gl_window,
|
||||
device,
|
||||
factory,
|
||||
tgt_color_view,
|
||||
tgt_depth_view,
|
||||
) = gfx_window_glutin::init(
|
||||
WindowBuilder::new()
|
||||
.with_title("Veloren (Voxygen)")
|
||||
.with_dimensions(LogicalSize::new(800.0, 500.0))
|
||||
.with_maximized(false),
|
||||
ContextBuilder::new()
|
||||
.with_gl(GlRequest::Specific(OpenGl, (3, 2)))
|
||||
.with_multisampling(2)
|
||||
.with_vsync(true),
|
||||
&events_loop,
|
||||
);
|
||||
|
||||
Self {
|
||||
events_loop,
|
||||
gl_window,
|
||||
render_ctx: RenderCtx {
|
||||
device,
|
||||
factory,
|
||||
tgt_color_view,
|
||||
tgt_depth_view,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn poll_events<F: FnMut(Event)>(&mut self, mut f: F) {
|
||||
self.events_loop.poll_events(|event| match event {
|
||||
glutin::Event::WindowEvent { event, .. } => match event {
|
||||
glutin::WindowEvent::CloseRequested => f(Event::Close),
|
||||
_ => {},
|
||||
},
|
||||
_ => {},
|
||||
});
|
||||
}
|
||||
|
||||
pub fn swap_buffers(&self) {
|
||||
self.gl_window
|
||||
.swap_buffers()
|
||||
.expect("Failed to swap window buffers");
|
||||
}
|
||||
}
|
||||
|
||||
pub enum Event {
|
||||
Close,
|
||||
}
|
Loading…
Reference in New Issue
Block a user