This commit is contained in:
parent
b361965033
commit
f4694157ab
8 changed files with 143 additions and 3 deletions
54
src/core/app/app.rs
Normal file
54
src/core/app/app.rs
Normal file
|
@ -0,0 +1,54 @@
|
|||
use std::error::Error;
|
||||
|
||||
use bevy_ecs::{schedule::Schedules, world::World};
|
||||
|
||||
pub enum AppExit {
|
||||
Success,
|
||||
Error(Box<dyn Error>),
|
||||
}
|
||||
|
||||
pub type RunnerFn = Box<dyn FnOnce() -> AppExit>;
|
||||
|
||||
pub struct App {
|
||||
world: World,
|
||||
runner: Option<RunnerFn>,
|
||||
}
|
||||
|
||||
impl Default for App {
|
||||
fn default() -> Self {
|
||||
let mut world = World::new();
|
||||
world.init_resource::<Schedules>();
|
||||
|
||||
Self {
|
||||
world,
|
||||
runner: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn world_mut(&mut self) -> &mut World {
|
||||
&mut self.world
|
||||
}
|
||||
|
||||
pub fn world(&self) -> &World {
|
||||
&self.world
|
||||
}
|
||||
|
||||
pub fn run(&mut self) -> Result<(), Box<dyn Error>> {
|
||||
match self.runner.take() {
|
||||
Some(runner) => match runner() {
|
||||
AppExit::Success => Ok(()),
|
||||
AppExit::Error(e) => Err(e),
|
||||
},
|
||||
None => Err(Box::new(std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"runner is not set",
|
||||
))),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_runner(&mut self, runner: RunnerFn) {
|
||||
self.runner = Some(runner);
|
||||
}
|
||||
}
|
4
src/core/app/mod.rs
Normal file
4
src/core/app/mod.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
mod app;
|
||||
pub mod plugin;
|
||||
|
||||
pub use app::App;
|
5
src/core/app/plugin.rs
Normal file
5
src/core/app/plugin.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
use super::app::App;
|
||||
|
||||
pub trait Plugin {
|
||||
fn build(&self, app: &mut App);
|
||||
}
|
|
@ -1,4 +1,6 @@
|
|||
pub mod app;
|
||||
pub mod camera;
|
||||
pub mod old_app;
|
||||
pub mod render;
|
||||
pub mod scene;
|
||||
pub mod window;
|
||||
|
|
24
src/core/window/mod.rs
Normal file
24
src/core/window/mod.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
pub mod window_handler;
|
||||
|
||||
use super::app::{App, plugin::Plugin};
|
||||
use window_handler::WindowHandler;
|
||||
use winit::event_loop::EventLoop;
|
||||
use winit::window::WindowAttributes;
|
||||
|
||||
pub struct WindowPlugin {
|
||||
window_attributes: WindowAttributes,
|
||||
event_loop: EventLoop<()>,
|
||||
}
|
||||
|
||||
impl Plugin for WindowPlugin {
|
||||
fn build(&self, app: &mut App) {
|
||||
let world = app.world_mut();
|
||||
world.insert_resource(WindowHandler::new(self.window_attributes.clone()));
|
||||
|
||||
let window_handler = world.get_resource_mut::<WindowHandler>().unwrap();
|
||||
|
||||
// app.set_runner(Box::new(move || {
|
||||
// self.event_loop.run_app(&mut window_handler);
|
||||
// }));
|
||||
}
|
||||
}
|
40
src/core/window/window_handler.rs
Normal file
40
src/core/window/window_handler.rs
Normal file
|
@ -0,0 +1,40 @@
|
|||
use bevy_ecs::system::Resource;
|
||||
use winit::{
|
||||
application::ApplicationHandler,
|
||||
event::WindowEvent,
|
||||
event_loop::ActiveEventLoop,
|
||||
window::{Window, WindowAttributes, WindowId},
|
||||
};
|
||||
|
||||
#[derive(Resource)]
|
||||
pub struct WindowHandler {
|
||||
window_attributes: WindowAttributes,
|
||||
window: Option<Box<Window>>,
|
||||
}
|
||||
|
||||
impl WindowHandler {
|
||||
pub fn new(window_attributes: WindowAttributes) -> Self {
|
||||
Self {
|
||||
window_attributes,
|
||||
window: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl ApplicationHandler for WindowHandler {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let window = event_loop
|
||||
.create_window(self.window_attributes.clone())
|
||||
.unwrap();
|
||||
self.window = Some(Box::new(window));
|
||||
}
|
||||
|
||||
fn window_event(&mut self, event_loop: &ActiveEventLoop, _id: WindowId, event: WindowEvent) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
log::debug!("The close button was pressed; stopping");
|
||||
event_loop.exit();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
17
src/main.rs
17
src/main.rs
|
@ -5,14 +5,25 @@ pub mod core;
|
|||
pub mod game;
|
||||
pub mod vulkan;
|
||||
|
||||
fn main() -> Result<(), impl Error> {
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
env_logger::init();
|
||||
|
||||
run_old_app()
|
||||
}
|
||||
|
||||
fn run_new_app() -> Result<(), Box<dyn Error>> {
|
||||
let mut app = core::app::App::default();
|
||||
app.run()
|
||||
}
|
||||
|
||||
fn run_old_app() -> Result<(), Box<dyn Error>> {
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
event_loop.set_control_flow(ControlFlow::Poll);
|
||||
|
||||
let vulkan_context = vulkan::vulkan_context::VulkanContext::from(&event_loop);
|
||||
let mut app = core::app::App::from(vulkan_context);
|
||||
let mut app = core::old_app::App::from(vulkan_context);
|
||||
|
||||
event_loop.run_app(&mut app)
|
||||
event_loop.run_app(&mut app).map_err(Box::new)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue