winit: Use user event
Some checks failed
Build legacy Nix package on Ubuntu / build (push) Failing after 8m47s

This commit is contained in:
Florian RICHER 2025-05-29 21:38:07 +02:00
parent 6a6b1821a4
commit f1ae54bc73
Signed by: florian.richer
GPG key ID: C73D37CBED7BFC77
8 changed files with 227 additions and 29 deletions

View file

@ -1,2 +1,2 @@
pub mod assets;
pub mod main_scene;
pub mod scenes;

View file

@ -1,31 +1,26 @@
use std::{error::Error, sync::Arc};
use crate::core::app::user_event::UserEvent;
use crate::core::render::primitives::camera::Camera3D;
use crate::core::render::primitives::transform::Transform;
use crate::core::render::texture::Texture;
use crate::core::scene::Scene;
use crate::core::scene::SceneContext;
use crate::game::assets::square::Square;
use egui_winit_vulkano::{Gui, egui};
use glam::EulerRot;
use glam::Quat;
use glam::Vec3;
use vulkano::format::Format;
use vulkano::image::Image;
use vulkano::image::ImageCreateInfo;
use vulkano::image::ImageUsage;
use vulkano::memory::allocator::AllocationCreateInfo;
use vulkano::{
command_buffer::{
AutoCommandBufferBuilder, CommandBufferUsage, PrimaryCommandBufferAbstract,
RenderingAttachmentInfo, RenderingInfo,
},
image::view::ImageView,
pipeline::graphics::viewport::Viewport,
render_pass::{AttachmentLoadOp, AttachmentStoreOp},
sync::GpuFuture,
};
use super::assets::square::Square;
use winit::window::CursorGrabMode;
pub struct MainSceneState {
square: Square,
@ -119,6 +114,38 @@ impl Scene for MainScene {
scene_context.aspect_ratio,
);
if scene_context
.input_manager
.get_virtual_input_state("mouse_left")
> 0.0
{
let _ = scene_context
.event_loop_proxy
.send_event(UserEvent::CursorVisible(scene_context.window_id, false));
let _ = scene_context
.event_loop_proxy
.send_event(UserEvent::CursorGrabMode(
scene_context.window_id,
CursorGrabMode::Locked,
));
}
if scene_context
.input_manager
.get_virtual_input_state("mouse_right")
> 0.0
{
let _ = scene_context
.event_loop_proxy
.send_event(UserEvent::CursorVisible(scene_context.window_id, true));
let _ = scene_context
.event_loop_proxy
.send_event(UserEvent::CursorGrabMode(
scene_context.window_id,
CursorGrabMode::None,
));
}
Ok(())
}

2
src/game/scenes/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod main_scene;
pub mod test_scene;

View file

@ -0,0 +1,111 @@
use std::error::Error;
use crate::core::app::user_event::UserEvent;
use crate::core::scene::Scene;
use crate::core::scene::SceneContext;
use egui_winit_vulkano::{Gui, egui};
use vulkano::command_buffer::AutoCommandBufferBuilder;
use vulkano::command_buffer::CommandBufferUsage;
use vulkano::command_buffer::RenderingAttachmentInfo;
use vulkano::command_buffer::RenderingInfo;
use vulkano::pipeline::graphics::viewport::Viewport;
use vulkano::render_pass::AttachmentLoadOp;
use vulkano::render_pass::AttachmentStoreOp;
use vulkano::sync::GpuFuture;
use super::main_scene::MainScene;
pub struct MainSceneState {}
#[derive(Default)]
pub struct TestScene {
state: Option<MainSceneState>,
}
impl Scene for TestScene {
fn loaded(&self) -> bool {
self.state.is_some()
}
fn load(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>> {
self.state = Some(MainSceneState {});
Ok(())
}
fn update(&mut self, scene_context: &SceneContext) -> Result<(), Box<dyn Error>> {
Ok(())
}
fn render(
&mut self,
acquire_future: Box<dyn GpuFuture>,
scene_context: &SceneContext,
gui: &mut Gui,
) -> Result<Box<dyn GpuFuture>, Box<dyn Error>> {
let mut builder = AutoCommandBufferBuilder::primary(
scene_context.command_buffer_allocator.clone(),
scene_context.graphics_queue.queue_family_index(),
CommandBufferUsage::OneTimeSubmit,
)?;
{
let viewport = Viewport {
offset: [0.0, 0.0],
extent: scene_context.window_size,
depth_range: 0.0..=1.0,
};
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(
scene_context.swapchain_image_view.clone(),
)
})],
depth_attachment: Some(RenderingAttachmentInfo {
load_op: AttachmentLoadOp::Clear,
store_op: AttachmentStoreOp::DontCare,
clear_value: Some([1.0].into()),
..RenderingAttachmentInfo::image_view(
scene_context.depth_stencil_image_view.clone(),
)
}),
..Default::default()
})?
.set_viewport(0, [viewport].into_iter().collect())?;
}
builder.end_rendering()?;
let command_buffer = builder.build()?;
let render_future =
acquire_future.then_execute(scene_context.graphics_queue.clone(), command_buffer)?;
gui.immediate_ui(|gui| {
let ctx = gui.context();
egui::CentralPanel::default().show(&ctx, |ui| {
if ui.button("Start Game").clicked() {
let _ = scene_context
.event_loop_proxy
.send_event(UserEvent::ChangeScene(
scene_context.window_id,
Box::new(MainScene::default()),
));
}
});
});
let render_future =
gui.draw_on_image(render_future, scene_context.swapchain_image_view.clone());
Ok(render_future)
}
fn unload(&mut self) {}
}