diff --git a/src/render/input.rs b/src/core/input.rs similarity index 100% rename from src/render/input.rs rename to src/core/input.rs diff --git a/src/core/mod.rs b/src/core/mod.rs new file mode 100644 index 0000000..ddaf320 --- /dev/null +++ b/src/core/mod.rs @@ -0,0 +1,4 @@ +pub mod input; +pub mod render; +pub mod scene; +pub mod timer; diff --git a/src/render/app.rs b/src/core/render/app.rs similarity index 95% rename from src/render/app.rs rename to src/core/render/app.rs index ee88731..9553a5a 100644 --- a/src/render/app.rs +++ b/src/core/render/app.rs @@ -1,6 +1,9 @@ use std::collections::HashMap; -use crate::render::scene::Scene; +use crate::core::input::InputState; +use crate::core::scene::SceneManager; +use crate::core::timer::Timer; +use crate::game::main_scene::MainScene; use egui_winit_vulkano::{Gui, GuiConfig, egui}; use vulkano::command_buffer::{ AutoCommandBufferBuilder, CommandBufferUsage, RenderingAttachmentInfo, RenderingInfo, @@ -16,16 +19,16 @@ use winit::event::{ElementState, WindowEvent}; use winit::event_loop::ActiveEventLoop; use winit::window::WindowId; -use super::input::InputState; use super::vulkan_context::VulkanContext; pub struct App { vulkan_context: VulkanContext, vulkano_windows: VulkanoWindows, gui: HashMap, - scene: Option, clear_color: [f32; 3], input_state: InputState, + scene_manager: SceneManager, + timer: Timer, } impl From for App { @@ -34,9 +37,10 @@ impl From for App { vulkan_context: VulkanContext::new(vulkano_context), vulkano_windows: VulkanoWindows::default(), gui: HashMap::new(), - scene: None, clear_color: [0.0, 0.0, 0.0], input_state: InputState::default(), + scene_manager: SceneManager::new(), + timer: Timer::new(), } } } @@ -71,13 +75,8 @@ impl ApplicationHandler for App { }; self.gui.insert(window_id, gui); - self.scene = Some( - Scene::load( - &self.vulkan_context, - self.vulkano_windows.get_primary_renderer_mut().unwrap(), - ) - .unwrap(), - ); + self.scene_manager + .load_scene(Box::new(MainScene::default())); } fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) { diff --git a/src/render/mod.rs b/src/core/render/mod.rs similarity index 70% rename from src/render/mod.rs rename to src/core/render/mod.rs index 164a07e..d98c419 100644 --- a/src/render/mod.rs +++ b/src/core/render/mod.rs @@ -1,6 +1,4 @@ pub mod app; -pub mod input; pub mod pipelines; -pub mod scene; pub mod vertex; pub mod vulkan_context; diff --git a/src/render/pipelines/mod.rs b/src/core/render/pipelines/mod.rs similarity index 100% rename from src/render/pipelines/mod.rs rename to src/core/render/pipelines/mod.rs diff --git a/src/render/pipelines/triangle_pipeline.rs b/src/core/render/pipelines/triangle_pipeline.rs similarity index 98% rename from src/render/pipelines/triangle_pipeline.rs rename to src/core/render/pipelines/triangle_pipeline.rs index 9fb3629..62bd8b0 100644 --- a/src/render/pipelines/triangle_pipeline.rs +++ b/src/core/render/pipelines/triangle_pipeline.rs @@ -20,7 +20,7 @@ use vulkano::pipeline::{ }; use vulkano::shader::{EntryPoint, ShaderStages}; -use crate::render::vertex::Vertex2D; +use crate::core::render::vertex::Vertex2D; pub mod shaders { pub mod vs { diff --git a/src/render/vertex.rs b/src/core/render/vertex.rs similarity index 100% rename from src/render/vertex.rs rename to src/core/render/vertex.rs diff --git a/src/render/vulkan_context.rs b/src/core/render/vulkan_context.rs similarity index 100% rename from src/render/vulkan_context.rs rename to src/core/render/vulkan_context.rs diff --git a/src/core/scene.rs b/src/core/scene.rs new file mode 100644 index 0000000..2286820 --- /dev/null +++ b/src/core/scene.rs @@ -0,0 +1,29 @@ +use vulkano_util::renderer::VulkanoWindowRenderer; + +use super::{input::InputState, render::vulkan_context::VulkanContext, timer::Timer}; + +pub trait Scene { + fn load(&mut self, app: &mut App); + fn update(&mut self, app: &mut App); + fn render(&self); + fn unload(&mut self); +} + +pub struct SceneManager { + current_scene: Option>, +} + +impl SceneManager { + pub fn new() -> Self { + Self { + current_scene: None, + } + } + + pub fn load_scene(&mut self, scene: Box) { + if let Some(current_scene) = self.current_scene.as_mut() { + current_scene.unload(); + } + self.current_scene = Some(scene); + } +} diff --git a/src/core/timer.rs b/src/core/timer.rs new file mode 100644 index 0000000..eacf423 --- /dev/null +++ b/src/core/timer.rs @@ -0,0 +1,30 @@ +pub struct Timer { + start_time: std::time::Instant, + last_time: std::time::Instant, + current_time: std::time::Instant, + delta_time: f32, +} + +impl Timer { + pub fn new() -> Self { + Self { + start_time: std::time::Instant::now(), + last_time: std::time::Instant::now(), + current_time: std::time::Instant::now(), + delta_time: 0.0, + } + } + + pub fn update(&mut self) { + self.current_time = std::time::Instant::now(); + self.delta_time = self + .current_time + .duration_since(self.last_time) + .as_secs_f32(); + self.last_time = self.current_time; + } + + pub fn get_delta_time(&self) -> f32 { + self.delta_time + } +} diff --git a/src/render/scene.rs b/src/game/main_scene.rs similarity index 86% rename from src/render/scene.rs rename to src/game/main_scene.rs index 36ba182..4135c38 100644 --- a/src/render/scene.rs +++ b/src/game/main_scene.rs @@ -1,3 +1,4 @@ +use crate::core::scene::Scene; use crate::render::pipelines::triangle_pipeline::shaders::vs; use glam::{Mat3, Mat4, Vec3}; use std::error::Error; @@ -70,14 +71,50 @@ const VERTICES: [Vertex2D; 12] = [ }, ]; -pub struct Scene { +pub struct MainSceneState { pipeline: Arc, vertex_buffer: Subbuffer<[Vertex2D]>, rotation_start: Instant, } -impl Scene { +#[derive(Default)] +pub struct MainScene { + state: Option, +} + +impl Scene for MainScene { + fn load(&mut self, app: &mut App) { + let pipeline = create_triangle_pipeline( + app.vulkan_context.vulkano_context().device(), + app.vulkano_windows.swapchain_format(), + )?; + let vertex_buffer = Vertex2D::create_buffer( + Vec::from_iter(VERTICES), + vulkano_context.vulkano_context().memory_allocator(), + )?; + + self.state = Some(MainSceneState { + pipeline, + vertex_buffer, + rotation_start: Instant::now(), + }) + } + + fn update(&mut self, app: &mut App) { + todo!() + } + + fn render(&self) { + todo!() + } + + fn unload(&mut self) { + todo!() + } +} + +impl MainScene { pub fn load( vulkano_context: &VulkanContext, vulkano_window_renderer: &VulkanoWindowRenderer, diff --git a/src/game/mod.rs b/src/game/mod.rs new file mode 100644 index 0000000..53eb070 --- /dev/null +++ b/src/game/mod.rs @@ -0,0 +1 @@ +pub mod main_scene; diff --git a/src/main.rs b/src/main.rs index 4ef9dfb..52ba1f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,9 @@ use vulkano::device::{DeviceExtensions, DeviceFeatures}; use vulkano_util::context::{VulkanoConfig, VulkanoContext}; use winit::event_loop::{ControlFlow, EventLoop}; -mod render; +mod core; +mod game; + fn main() { env_logger::init(); @@ -28,7 +30,7 @@ fn main() { let event_loop = EventLoop::new().unwrap(); event_loop.set_control_flow(ControlFlow::Poll); - let mut app = render::app::App::from(vulkano_context); + let mut app = core::render::app::App::from(vulkano_context); match event_loop.run_app(&mut app) { Ok(_) => {}