First scene refactor working
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m13s

This commit is contained in:
Florian RICHER 2025-05-26 19:55:34 +02:00
parent 5b74eef561
commit 7401a9b5f3
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
6 changed files with 251 additions and 110 deletions

View file

@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::sync::Arc;
use crate::core::input::InputState;
use crate::core::scene::SceneManager;
@ -13,17 +14,19 @@ use vulkano::render_pass::{AttachmentLoadOp, AttachmentStoreOp};
use vulkano::swapchain::PresentMode;
use vulkano::sync::GpuFuture;
use vulkano_util::context::VulkanoContext;
use vulkano_util::renderer::VulkanoWindowRenderer;
use vulkano_util::window::{VulkanoWindows, WindowDescriptor};
use winit::application::ApplicationHandler;
use winit::event::{ElementState, WindowEvent};
use winit::event_loop::ActiveEventLoop;
use winit::window::WindowId;
use super::render_context::RenderContext;
use super::vulkan_context::VulkanContext;
pub struct App {
vulkan_context: VulkanContext,
vulkano_windows: VulkanoWindows,
vulkan_context: Arc<VulkanContext>,
vulkano_windows: Arc<VulkanoWindows>,
gui: HashMap<WindowId, Gui>,
clear_color: [f32; 3],
input_state: InputState,
@ -31,11 +34,29 @@ pub struct App {
timer: Timer,
}
impl From<(&VulkanContext, &VulkanoWindowRenderer)> for RenderContext {
fn from((vulkan_context, renderer): (&VulkanContext, &VulkanoWindowRenderer)) -> Self {
RenderContext {
window_size: renderer.resolution(),
aspect_ratio: renderer.aspect_ratio(),
instance: vulkan_context.vulkano_context().instance().clone(),
device: vulkan_context.vulkano_context().device().clone(),
graphics_queue: vulkan_context.vulkano_context().graphics_queue().clone(),
compute_queue: vulkan_context.vulkano_context().compute_queue().clone(),
transfer_queue: vulkan_context.vulkano_context().transfer_queue().cloned(),
memory_allocator: vulkan_context.vulkano_context().memory_allocator().clone(),
command_buffer_allocator: vulkan_context.command_buffer_allocator().clone(),
descriptor_set_allocator: vulkan_context.descriptor_set_allocator().clone(),
swapchain_format: renderer.swapchain_format(),
}
}
}
impl From<VulkanoContext> for App {
fn from(vulkano_context: VulkanoContext) -> Self {
Self {
vulkan_context: VulkanContext::new(vulkano_context),
vulkano_windows: VulkanoWindows::default(),
vulkan_context: Arc::new(VulkanContext::new(vulkano_context)),
vulkano_windows: Arc::new(VulkanoWindows::default()),
gui: HashMap::new(),
clear_color: [0.0, 0.0, 0.0],
input_state: InputState::default(),
@ -47,41 +68,47 @@ impl From<VulkanoContext> for App {
impl ApplicationHandler for App {
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
let window_id = self.vulkano_windows.create_window(
event_loop,
self.vulkan_context.vulkano_context(),
&WindowDescriptor {
title: "Rust ASH Test".to_string(),
width: 800.0,
height: 600.0,
present_mode: PresentMode::Fifo,
..Default::default()
},
|_| {},
);
let gui = {
let renderer = self.vulkano_windows.get_renderer_mut(window_id).unwrap();
Gui::new(
if let Some(vulkano_windows) = Arc::get_mut(&mut self.vulkano_windows) {
let window_id = vulkano_windows.create_window(
event_loop,
renderer.surface(),
renderer.graphics_queue(),
renderer.swapchain_format(),
GuiConfig {
is_overlay: true,
self.vulkan_context.vulkano_context(),
&WindowDescriptor {
title: "Rust ASH Test".to_string(),
width: 800.0,
height: 600.0,
present_mode: PresentMode::Fifo,
..Default::default()
},
)
};
self.gui.insert(window_id, gui);
|_| {},
);
let gui = {
let renderer = vulkano_windows.get_renderer_mut(window_id).unwrap();
Gui::new(
event_loop,
renderer.surface(),
renderer.graphics_queue(),
renderer.swapchain_format(),
GuiConfig {
is_overlay: true,
..Default::default()
},
)
};
self.gui.insert(window_id, gui);
}
self.scene_manager
.load_scene(Box::new(MainScene::default()));
}
fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
let renderer = self.vulkano_windows.get_renderer_mut(id).unwrap();
let renderer = Arc::get_mut(&mut self.vulkano_windows)
.unwrap()
.get_renderer_mut(id)
.unwrap();
let gui = self.gui.get_mut(&id).unwrap();
let render_context = RenderContext::from((self.vulkan_context.as_ref(), &*renderer));
if !gui.update(&event) {
self.input_state.process_event(&event);
@ -100,6 +127,11 @@ impl ApplicationHandler for App {
}
WindowEvent::RedrawRequested => {
self.input_state.update();
self.timer.update();
self.scene_manager.load_scene_if_not_loaded(&render_context);
if let Some(scene) = self.scene_manager.current_scene_mut() {
scene.update(&render_context, &self.input_state, &self.timer);
}
let acquire_future = renderer.acquire(None, |_| {}).unwrap();
@ -137,10 +169,8 @@ impl ApplicationHandler for App {
.unwrap();
}
if let Some(scene) = self.scene.as_ref() {
scene
.render(&self.vulkan_context, renderer, &mut builder)
.unwrap();
if let Some(scene) = self.scene_manager.current_scene() {
scene.render(&render_context, &mut builder);
}
builder.end_rendering().unwrap();
@ -174,6 +204,8 @@ impl ApplicationHandler for App {
self.input_state.get_mouse_state().delta
));
ui.label(format!("Delta time: {:?}", self.timer.delta_time()));
for (key, state) in self
.input_state
.key_states

View file

@ -1,4 +1,5 @@
pub mod app;
pub mod pipelines;
pub mod render_context;
pub mod vertex;
pub mod vulkan_context;

View file

@ -0,0 +1,70 @@
use std::sync::Arc;
use vulkano::{
command_buffer::allocator::StandardCommandBufferAllocator,
descriptor_set::allocator::StandardDescriptorSetAllocator,
device::{Device, Queue},
format::Format,
instance::Instance,
memory::allocator::StandardMemoryAllocator,
};
pub struct RenderContext {
pub(super) instance: Arc<Instance>,
pub(super) device: Arc<Device>,
pub(super) graphics_queue: Arc<Queue>,
pub(super) compute_queue: Arc<Queue>,
pub(super) transfer_queue: Option<Arc<Queue>>,
pub(super) memory_allocator: Arc<StandardMemoryAllocator>,
pub(super) command_buffer_allocator: Arc<StandardCommandBufferAllocator>,
pub(super) descriptor_set_allocator: Arc<StandardDescriptorSetAllocator>,
pub(super) window_size: [f32; 2],
pub(super) aspect_ratio: f32,
pub(super) swapchain_format: Format,
}
impl RenderContext {
pub fn instance(&self) -> &Arc<Instance> {
&self.instance
}
pub fn device(&self) -> &Arc<Device> {
&self.device
}
pub fn graphics_queue(&self) -> &Arc<Queue> {
&self.graphics_queue
}
pub fn compute_queue(&self) -> &Arc<Queue> {
&self.compute_queue
}
pub fn transfer_queue(&self) -> Option<&Arc<Queue>> {
self.transfer_queue.as_ref()
}
pub fn memory_allocator(&self) -> &Arc<StandardMemoryAllocator> {
&self.memory_allocator
}
pub fn command_buffer_allocator(&self) -> &Arc<StandardCommandBufferAllocator> {
&self.command_buffer_allocator
}
pub fn descriptor_set_allocator(&self) -> &Arc<StandardDescriptorSetAllocator> {
&self.descriptor_set_allocator
}
pub fn window_size(&self) -> &[f32; 2] {
&self.window_size
}
pub fn aspect_ratio(&self) -> f32 {
self.aspect_ratio
}
pub fn swapchain_format(&self) -> Format {
self.swapchain_format
}
}

View file

@ -1,11 +1,16 @@
use vulkano_util::renderer::VulkanoWindowRenderer;
use vulkano::command_buffer::{AutoCommandBufferBuilder, PrimaryAutoCommandBuffer};
use super::{input::InputState, render::vulkan_context::VulkanContext, timer::Timer};
use super::{input::InputState, render::render_context::RenderContext, timer::Timer};
pub trait Scene {
fn load(&mut self, app: &mut App);
fn update(&mut self, app: &mut App);
fn render(&self);
fn loaded(&self) -> bool;
fn load(&mut self, render_context: &RenderContext);
fn update(&mut self, render_context: &RenderContext, input_state: &InputState, timer: &Timer);
fn render(
&self,
render_context: &RenderContext,
builder: &mut AutoCommandBufferBuilder<PrimaryAutoCommandBuffer>,
);
fn unload(&mut self);
}
@ -20,10 +25,36 @@ impl SceneManager {
}
}
pub fn load_scene_if_not_loaded(&mut self, render_context: &RenderContext) {
if let Some(current_scene) = self.current_scene.as_mut() {
if !current_scene.loaded() {
current_scene.load(render_context);
}
}
}
pub fn load_scene(&mut self, scene: Box<dyn Scene>) {
if let Some(current_scene) = self.current_scene.as_mut() {
current_scene.unload();
}
self.current_scene = Some(scene);
}
pub fn current_scene(&self) -> Option<&Box<dyn Scene>> {
if let Some(current_scene) = self.current_scene.as_ref() {
if current_scene.loaded() {
return Some(current_scene);
}
}
None
}
pub fn current_scene_mut(&mut self) -> Option<&mut Box<dyn Scene>> {
if let Some(current_scene) = self.current_scene.as_mut() {
if current_scene.loaded() {
return Some(current_scene);
}
}
None
}
}

View file

@ -24,7 +24,11 @@ impl Timer {
self.last_time = self.current_time;
}
pub fn get_delta_time(&self) -> f32 {
pub fn delta_time(&self) -> f32 {
self.delta_time
}
pub fn start_time(&self) -> std::time::Instant {
self.start_time
}
}