modif appbuilder
This commit is contained in:
+101
@@ -0,0 +1,101 @@
|
||||
use crate::AppHandler;
|
||||
use crate::core::{Context, Renderer};
|
||||
use crate::pipeline::PipelineCache;
|
||||
use crate::scene::Scene;
|
||||
use crate::utils::WsgError;
|
||||
use crate::utils::conf::{APP_DEFAULT_HEIGHT, APP_DEFAULT_TITLE, APP_DEFAULT_WIDTH};
|
||||
use std::sync::Arc;
|
||||
use winit::event_loop::EventLoop;
|
||||
use winit::window::Window;
|
||||
|
||||
pub struct App {
|
||||
pub context: Context,
|
||||
pub renderer: Renderer,
|
||||
pub event_loop: Option<EventLoop<()>>, // On met en Option pour pouvoir faire .take() facilement
|
||||
pub cache: PipelineCache,
|
||||
pub scene: Scene,
|
||||
pub window: Arc<Window>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn run<H: AppHandler + 'static>(mut self, mut handler: H) -> Result<(), WsgError> {
|
||||
// On extrait l'event_loop de manière sûre grâce au Option
|
||||
let event_loop = self.event_loop.take().ok_or(WsgError::WindowSystem)?; // Erreur si déjà pris
|
||||
event_loop
|
||||
.run(move |event, elwt| {
|
||||
match event {
|
||||
winit::event::Event::AboutToWait => {
|
||||
// update logic
|
||||
handler.update(&mut self);
|
||||
self.window.request_redraw();
|
||||
}
|
||||
winit::event::Event::WindowEvent {
|
||||
event: winit::event::WindowEvent::RedrawRequested,
|
||||
..
|
||||
} => {
|
||||
// render logic
|
||||
handler.render(&mut self);
|
||||
}
|
||||
winit::event::Event::WindowEvent {
|
||||
event: winit::event::WindowEvent::CloseRequested,
|
||||
..
|
||||
} => {
|
||||
elwt.exit();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
})
|
||||
.map_err(|_| WsgError::WindowSystem)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct AppBuilder {
|
||||
title: String,
|
||||
width: u32,
|
||||
height: u32,
|
||||
}
|
||||
|
||||
impl AppBuilder {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
title: APP_DEFAULT_TITLE.to_string(),
|
||||
width: APP_DEFAULT_WIDTH,
|
||||
height: APP_DEFAULT_HEIGHT,
|
||||
}
|
||||
}
|
||||
pub fn title(mut self, title: &str) -> Self {
|
||||
self.title = title.to_string();
|
||||
self
|
||||
}
|
||||
pub fn size(mut self, width: u32, height: u32) -> Self {
|
||||
self.width = width;
|
||||
self.height = height;
|
||||
self
|
||||
}
|
||||
pub async fn build(self) -> Result<App, WsgError> {
|
||||
let event_loop = EventLoop::new().unwrap();
|
||||
let window = Arc::new(
|
||||
winit::window::WindowBuilder::new()
|
||||
.with_title(&self.title)
|
||||
.build(&event_loop)
|
||||
.map_err(|_| WsgError::WindowSystem)?,
|
||||
);
|
||||
let context = Context::new(window.clone()).await?;
|
||||
let device = Arc::new(context.device.clone());
|
||||
let format = context
|
||||
.configure(&context.adapter, self.width, self.height)
|
||||
.map_err(|_| WsgError::SurfaceIncompatible)?;
|
||||
let renderer = Renderer::new(&context, format);
|
||||
let cache = PipelineCache::new(device);
|
||||
let scene = Scene::new();
|
||||
|
||||
Ok(App {
|
||||
context,
|
||||
renderer,
|
||||
cache,
|
||||
scene,
|
||||
event_loop: Some(event_loop),
|
||||
window,
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user