diff --git a/src/core/app/mod.rs b/src/core/app/mod.rs index f7320aa..c6c8a20 100644 --- a/src/core/app/mod.rs +++ b/src/core/app/mod.rs @@ -1,4 +1,2 @@ mod app; -pub mod plugin; - -pub use app::App; +pub use app::*; diff --git a/src/core/app/plugin.rs b/src/core/app/plugin.rs deleted file mode 100644 index 987c540..0000000 --- a/src/core/app/plugin.rs +++ /dev/null @@ -1,5 +0,0 @@ -use super::app::App; - -pub trait Plugin { - fn build(&self, app: &mut App); -} diff --git a/src/core/mod.rs b/src/core/mod.rs index 07427ac..bd7ce34 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -1,6 +1,4 @@ pub mod app; pub mod camera; -pub mod old_app; pub mod render; -pub mod scene; pub mod window; diff --git a/src/core/scene.rs b/src/core/scene.rs deleted file mode 100644 index a424da3..0000000 --- a/src/core/scene.rs +++ /dev/null @@ -1,21 +0,0 @@ -use bevy_ecs::world::World; - -pub struct Scene { - world: World, -} - -impl Scene { - pub fn new() -> Self { - Self { - world: World::new(), - } - } - - pub fn world(&self) -> &World { - &self.world - } - - pub fn world_mut(&mut self) -> &mut World { - &mut self.world - } -} diff --git a/src/core/window/config.rs b/src/core/window/config.rs new file mode 100644 index 0000000..8fbef8c --- /dev/null +++ b/src/core/window/config.rs @@ -0,0 +1,17 @@ +use bevy_ecs::system::Resource; +use winit::{dpi::PhysicalSize, window::WindowAttributes}; + +#[derive(Resource, Clone)] +pub struct WindowConfig { + pub title: String, + pub width: u32, + pub height: u32, +} + +impl Into for &WindowConfig { + fn into(self) -> WindowAttributes { + WindowAttributes::default() + .with_title(self.title.clone()) + .with_inner_size(PhysicalSize::new(self.width as f64, self.height as f64)) + } +} diff --git a/src/core/window/mod.rs b/src/core/window/mod.rs index 55f6a70..e600120 100644 --- a/src/core/window/mod.rs +++ b/src/core/window/mod.rs @@ -1,24 +1,27 @@ -pub mod window_handler; - -use super::app::{App, plugin::Plugin}; -use window_handler::WindowHandler; +use config::WindowConfig; +use state::WindowState; use winit::event_loop::EventLoop; -use winit::window::WindowAttributes; -pub struct WindowPlugin { - window_attributes: WindowAttributes, - event_loop: EventLoop<()>, +use super::app::{App, AppExit}; + +pub mod config; +pub mod state; + +pub fn init(app: &mut App, window_config: WindowConfig) { + let world = app.world_mut(); + world.insert_resource(window_config); + + let mut event_loop_builder = EventLoop::with_user_event(); + let event_loop = event_loop_builder.build().unwrap(); + + app.set_runner(Box::new(move |app| runner(app, event_loop))); } -impl Plugin for WindowPlugin { - fn build(&self, app: &mut App) { - let world = app.world_mut(); - world.insert_resource(WindowHandler::new(self.window_attributes.clone())); +fn runner(app: App, event_loop: EventLoop<()>) -> AppExit { + let mut window_state = WindowState::new(app); - let window_handler = world.get_resource_mut::().unwrap(); - - // app.set_runner(Box::new(move || { - // self.event_loop.run_app(&mut window_handler); - // })); + match event_loop.run_app(&mut window_state) { + Ok(_) => AppExit::Success, + Err(e) => AppExit::Error(Box::new(e)), } } diff --git a/src/core/window/state.rs b/src/core/window/state.rs new file mode 100644 index 0000000..94bfcc7 --- /dev/null +++ b/src/core/window/state.rs @@ -0,0 +1,45 @@ +use bevy_ecs::world::World; +use winit::{ + application::ApplicationHandler, + event::WindowEvent, + event_loop::ActiveEventLoop, + window::{Window, WindowId}, +}; + +use crate::core::app::App; + +use super::config::WindowConfig; + +pub struct WindowState { + app: App, + window: Option, +} + +impl WindowState { + pub fn new(app: App) -> Self { + Self { app, window: None } + } + + fn world(&self) -> &World { + self.app.world() + } +} + +impl ApplicationHandler for WindowState { + fn resumed(&mut self, event_loop: &ActiveEventLoop) { + let window_config = self.world().get_resource::().unwrap(); + + let window = event_loop.create_window(window_config.into()).unwrap(); + self.window = Some(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(); + } + _ => {} + } + } +} diff --git a/src/core/window/window_handler.rs b/src/core/window/window_handler.rs deleted file mode 100644 index 2b87d35..0000000 --- a/src/core/window/window_handler.rs +++ /dev/null @@ -1,40 +0,0 @@ -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>, -} - -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(); - } - _ => {} - } - } -} diff --git a/src/game/mod.rs b/src/game/mod.rs index 8b13789..ffee10f 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -1 +1,14 @@ +use crate::core::{ + app::App, + window::{self, config::WindowConfig}, +}; +pub fn init(app: &mut App) { + let window_config = WindowConfig { + title: "Rust ASH Test".to_string(), + width: 800, + height: 600, + }; + + window::init(app, window_config); +} diff --git a/src/main.rs b/src/main.rs index 9412fad..c981713 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,16 +3,18 @@ use winit::event_loop::{ControlFlow, EventLoop}; pub mod core; pub mod game; -pub mod vulkan; +pub mod old_app; fn main() -> Result<(), impl Error> { env_logger::init(); - run_new_app() + // run_new_app() + run_old_app() } fn run_new_app() -> Result<(), impl Error> { - let app = core::app::App::default(); + let mut app = core::app::App::default(); + game::init(&mut app); app.run() } @@ -20,8 +22,8 @@ fn run_old_app() -> Result<(), impl 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::old_app::App::from(vulkan_context); + let vulkan_context = old_app::vulkan_context::VulkanContext::from(&event_loop); + let mut app = old_app::app::App::from(vulkan_context); event_loop.run_app(&mut app) } diff --git a/src/core/old_app.rs b/src/old_app/app.rs similarity index 97% rename from src/core/old_app.rs rename to src/old_app/app.rs index 93a7d56..cfcf038 100644 --- a/src/core/old_app.rs +++ b/src/old_app/app.rs @@ -1,6 +1,6 @@ -use crate::vulkan::scene::Scene; -use crate::vulkan::vulkan_context::VulkanContext; -use crate::vulkan::window_render_context::WindowRenderContext; +use crate::old_app::scene::Scene; +use crate::old_app::vulkan_context::VulkanContext; +use crate::old_app::window_render_context::WindowRenderContext; use std::sync::Arc; use vulkano::command_buffer::{RenderingAttachmentInfo, RenderingInfo}; use vulkano::render_pass::{AttachmentLoadOp, AttachmentStoreOp}; diff --git a/src/vulkan/mod.rs b/src/old_app/mod.rs similarity index 87% rename from src/vulkan/mod.rs rename to src/old_app/mod.rs index dc3d731..9fce0a9 100644 --- a/src/vulkan/mod.rs +++ b/src/old_app/mod.rs @@ -1,5 +1,5 @@ +pub mod app; pub mod pipelines; +pub mod scene; pub mod vulkan_context; pub mod window_render_context; - -pub mod scene; diff --git a/src/vulkan/pipelines/mod.rs b/src/old_app/pipelines/mod.rs similarity index 100% rename from src/vulkan/pipelines/mod.rs rename to src/old_app/pipelines/mod.rs diff --git a/src/vulkan/pipelines/triangle_pipeline.rs b/src/old_app/pipelines/triangle_pipeline.rs similarity index 100% rename from src/vulkan/pipelines/triangle_pipeline.rs rename to src/old_app/pipelines/triangle_pipeline.rs diff --git a/src/vulkan/scene.rs b/src/old_app/scene.rs similarity index 97% rename from src/vulkan/scene.rs rename to src/old_app/scene.rs index 22986ff..72e3f48 100644 --- a/src/vulkan/scene.rs +++ b/src/old_app/scene.rs @@ -1,4 +1,4 @@ -use crate::vulkan::pipelines::triangle_pipeline::shaders::vs; +use crate::old_app::pipelines::triangle_pipeline::shaders::vs; use glam::{Mat3, Mat4, Vec3}; use std::error::Error; use std::sync::Arc; @@ -10,7 +10,7 @@ use vulkano::memory::allocator::{AllocationCreateInfo, MemoryTypeFilter}; use vulkano::pipeline::{GraphicsPipeline, Pipeline, PipelineBindPoint}; use crate::core::render::vertex::Vertex2D; -use crate::vulkan::pipelines::triangle_pipeline::create_triangle_pipeline; +use crate::old_app::pipelines::triangle_pipeline::create_triangle_pipeline; use super::vulkan_context::VulkanContext; use super::window_render_context::WindowRenderContext; diff --git a/src/vulkan/vulkan_context.rs b/src/old_app/vulkan_context.rs similarity index 100% rename from src/vulkan/vulkan_context.rs rename to src/old_app/vulkan_context.rs diff --git a/src/vulkan/window_render_context.rs b/src/old_app/window_render_context.rs similarity index 100% rename from src/vulkan/window_render_context.rs rename to src/old_app/window_render_context.rs