This commit is contained in:
parent
df99ef3a3f
commit
4f6216635f
17 changed files with 110 additions and 100 deletions
|
@ -1,4 +1,2 @@
|
|||
mod app;
|
||||
pub mod plugin;
|
||||
|
||||
pub use app::App;
|
||||
pub use app::*;
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
use super::app::App;
|
||||
|
||||
pub trait Plugin {
|
||||
fn build(&self, app: &mut App);
|
||||
}
|
|
@ -1,6 +1,4 @@
|
|||
pub mod app;
|
||||
pub mod camera;
|
||||
pub mod old_app;
|
||||
pub mod render;
|
||||
pub mod scene;
|
||||
pub mod window;
|
||||
|
|
|
@ -1,178 +0,0 @@
|
|||
use crate::vulkan::scene::Scene;
|
||||
use crate::vulkan::vulkan_context::VulkanContext;
|
||||
use crate::vulkan::window_render_context::WindowRenderContext;
|
||||
use std::sync::Arc;
|
||||
use vulkano::command_buffer::{RenderingAttachmentInfo, RenderingInfo};
|
||||
use vulkano::render_pass::{AttachmentLoadOp, AttachmentStoreOp};
|
||||
use vulkano::swapchain::{SwapchainPresentInfo, acquire_next_image};
|
||||
use vulkano::sync::GpuFuture;
|
||||
use vulkano::{Validated, VulkanError, sync};
|
||||
use winit::application::ApplicationHandler;
|
||||
use winit::event::WindowEvent;
|
||||
use winit::event_loop::ActiveEventLoop;
|
||||
use winit::window::WindowId;
|
||||
|
||||
pub struct App {
|
||||
vulkan_context: VulkanContext,
|
||||
window_render_context: Option<WindowRenderContext>,
|
||||
scene: Option<Scene>,
|
||||
}
|
||||
|
||||
impl From<VulkanContext> for App {
|
||||
fn from(vulkan_context: VulkanContext) -> Self {
|
||||
Self {
|
||||
vulkan_context,
|
||||
window_render_context: None,
|
||||
scene: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for App {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
let window_attributes = winit::window::Window::default_attributes()
|
||||
.with_title("Rust ASH Test")
|
||||
.with_inner_size(winit::dpi::PhysicalSize::new(
|
||||
f64::from(800),
|
||||
f64::from(600),
|
||||
));
|
||||
|
||||
let window = Arc::new(event_loop.create_window(window_attributes).unwrap());
|
||||
|
||||
let surface = self.vulkan_context.create_surface(window.clone());
|
||||
|
||||
self.window_render_context = Some(WindowRenderContext::new(
|
||||
window,
|
||||
surface,
|
||||
&self.vulkan_context.device,
|
||||
));
|
||||
self.scene = Some(
|
||||
Scene::load(
|
||||
&self.vulkan_context,
|
||||
self.window_render_context.as_ref().unwrap(),
|
||||
)
|
||||
.unwrap(),
|
||||
);
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
WindowEvent::Resized(_) => {
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
rcx.recreate_swapchain = true;
|
||||
}
|
||||
WindowEvent::RedrawRequested => {
|
||||
let (image_index, acquire_future) = {
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
let window_size = rcx.window.inner_size();
|
||||
|
||||
if window_size.width == 0 || window_size.height == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
rcx.previous_frame_end.as_mut().unwrap().cleanup_finished();
|
||||
rcx.update_swapchain().unwrap();
|
||||
|
||||
let (image_index, suboptimal, acquire_future) =
|
||||
match acquire_next_image(rcx.swapchain.clone(), None)
|
||||
.map_err(Validated::unwrap)
|
||||
{
|
||||
Ok(r) => r,
|
||||
Err(VulkanError::OutOfDate) => {
|
||||
rcx.recreate_swapchain = true;
|
||||
return;
|
||||
}
|
||||
Err(e) => panic!("failed to acquire next image: {e}"),
|
||||
};
|
||||
|
||||
if suboptimal {
|
||||
rcx.recreate_swapchain = true;
|
||||
}
|
||||
|
||||
(image_index, acquire_future)
|
||||
};
|
||||
|
||||
let mut builder = self.vulkan_context.create_render_builder();
|
||||
|
||||
{
|
||||
let rcx = self.window_render_context.as_ref().unwrap();
|
||||
builder
|
||||
.begin_rendering(RenderingInfo {
|
||||
color_attachments: vec![Some(RenderingAttachmentInfo {
|
||||
load_op: AttachmentLoadOp::Clear,
|
||||
store_op: AttachmentStoreOp::Store,
|
||||
clear_value: Some([0.0, 0.0, 0.0, 1.0].into()),
|
||||
..RenderingAttachmentInfo::image_view(
|
||||
rcx.attachment_image_views[image_index as usize].clone(),
|
||||
)
|
||||
})],
|
||||
..Default::default()
|
||||
})
|
||||
.unwrap()
|
||||
.set_viewport(0, [rcx.viewport.clone()].into_iter().collect())
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
if let Some(scene) = self.scene.as_ref() {
|
||||
scene
|
||||
.render(
|
||||
&self.vulkan_context,
|
||||
&self.window_render_context.as_ref().unwrap(),
|
||||
&mut builder,
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
builder.end_rendering().unwrap();
|
||||
|
||||
let command_buffer = builder.build().unwrap();
|
||||
|
||||
{
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
|
||||
let future = rcx
|
||||
.previous_frame_end
|
||||
.take()
|
||||
.unwrap()
|
||||
.join(acquire_future)
|
||||
.then_execute(self.vulkan_context.graphics_queue.clone(), command_buffer)
|
||||
.unwrap()
|
||||
.then_swapchain_present(
|
||||
self.vulkan_context.graphics_queue.clone(),
|
||||
SwapchainPresentInfo::swapchain_image_index(
|
||||
rcx.swapchain.clone(),
|
||||
image_index,
|
||||
),
|
||||
)
|
||||
.then_signal_fence_and_flush();
|
||||
|
||||
match future.map_err(Validated::unwrap) {
|
||||
Ok(future) => {
|
||||
rcx.previous_frame_end = Some(future.boxed());
|
||||
}
|
||||
Err(VulkanError::OutOfDate) => {
|
||||
rcx.recreate_swapchain = true;
|
||||
rcx.previous_frame_end =
|
||||
Some(sync::now(self.vulkan_context.device.clone()).boxed());
|
||||
}
|
||||
Err(e) => {
|
||||
println!("failed to flush future: {e}");
|
||||
rcx.previous_frame_end =
|
||||
Some(sync::now(self.vulkan_context.device.clone()).boxed());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) {
|
||||
let rcx = self.window_render_context.as_mut().unwrap();
|
||||
rcx.window.request_redraw();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
17
src/core/window/config.rs
Normal file
17
src/core/window/config.rs
Normal file
|
@ -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<WindowAttributes> 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))
|
||||
}
|
||||
}
|
|
@ -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::<WindowHandler>().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)),
|
||||
}
|
||||
}
|
||||
|
|
45
src/core/window/state.rs
Normal file
45
src/core/window/state.rs
Normal file
|
@ -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<Window>,
|
||||
}
|
||||
|
||||
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::<WindowConfig>().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();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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<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();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue