From 6639f0bb1ef04030af41a6ac990bf62445ea6e92 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Fri, 16 May 2025 14:22:18 +0200 Subject: [PATCH] Init plugins + first system --- src/core/window/mod.rs | 7 ++++++- src/core/window/state.rs | 28 +++++++++++++++++++++++++--- src/game/mod.rs | 5 ++++- src/game/test_plugin.rs | 13 +++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 src/game/test_plugin.rs diff --git a/src/core/window/mod.rs b/src/core/window/mod.rs index d1623b7..4a19fee 100644 --- a/src/core/window/mod.rs +++ b/src/core/window/mod.rs @@ -1,4 +1,4 @@ -use bevy_app::{App, AppExit}; +use bevy_app::{App, AppExit, PluginsState}; use config::WindowConfig; use raw_handle::{DisplayHandleWrapper, EventLoopProxyWrapper}; use state::WindowState; @@ -35,6 +35,11 @@ impl Window { } fn runner(mut app: App, event_loop: EventLoop<()>) -> AppExit { + if app.plugins_state() == PluginsState::Ready { + app.finish(); + app.cleanup(); + } + app.world_mut() .insert_resource(EventLoopProxyWrapper::new(event_loop.create_proxy())); diff --git a/src/core/window/state.rs b/src/core/window/state.rs index 8ffd44b..58d78d2 100644 --- a/src/core/window/state.rs +++ b/src/core/window/state.rs @@ -1,13 +1,16 @@ use std::sync::Arc; -use bevy_app::App; -use bevy_ecs::world::World; +use bevy_app::{App, PluginsState}; +use bevy_ecs::{event, world::World}; use winit::{ application::ApplicationHandler, event::WindowEvent, event_loop::ActiveEventLoop, window::WindowId, }; -use super::{config::WindowConfig, raw_handle::WindowWrapper}; +use super::{ + config::WindowConfig, + raw_handle::{DisplayHandleWrapper, WindowWrapper}, +}; pub struct WindowState { app: App, @@ -33,13 +36,32 @@ impl ApplicationHandler for WindowState { .insert_resource(WindowWrapper(Arc::new(window))); } + fn new_events(&mut self, event_loop: &ActiveEventLoop, cause: winit::event::StartCause) { + if self.app.plugins_state() == PluginsState::Ready { + self.app.finish(); + self.app.cleanup(); + } + } + 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::RedrawRequested => { + log::debug!("The window was requested to be redrawn"); + if self.app.plugins_state() == PluginsState::Cleaned { + self.app.update(); + } + } _ => {} } } + + fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { + let window_wrapper = self.app.world().get_resource::().unwrap(); + + window_wrapper.0.request_redraw(); + } } diff --git a/src/game/mod.rs b/src/game/mod.rs index 6eb9cab..903a179 100644 --- a/src/game/mod.rs +++ b/src/game/mod.rs @@ -5,6 +5,8 @@ use crate::core::{ window::{Window, config::WindowConfig}, }; +pub mod test_plugin; + pub fn init(app: &mut App) { let window_config = WindowConfig { title: "Rust ASH Test".to_string(), @@ -12,6 +14,7 @@ pub fn init(app: &mut App) { height: 600, }; + app.add_plugins(test_plugin::TestPlugin); Window::new(app, window_config).unwrap(); - Vulkan::new(app).unwrap(); + // Vulkan::new(app).unwrap(); } diff --git a/src/game/test_plugin.rs b/src/game/test_plugin.rs new file mode 100644 index 0000000..e82e61b --- /dev/null +++ b/src/game/test_plugin.rs @@ -0,0 +1,13 @@ +use bevy_app::{App, Last, Plugin, Startup}; + +pub struct TestPlugin; + +impl Plugin for TestPlugin { + fn build(&self, app: &mut App) { + app.add_systems(Last, setup_system); + } +} + +fn setup_system() { + log::info!("Hello, world!"); +}