use astrelis::prelude::*;
use astrelis::render::{RenderTarget, RenderableWindow, WindowContextDescriptor};
use astrelis::winit::window::WindowBackend;
use std::sync::Arc;
struct WindowApp {
#[allow(dead_code)]
engine: Engine,
renderable: Option<RenderableWindow>,
}
impl App for WindowApp {
fn render(&mut self, _ctx: &mut AppCtx, window_id: WindowId, events: &mut EventBatch) {
let renderable = match &mut self.renderable {
Some(r) if r.id() == window_id => r,
_ => return,
};
events.dispatch(|event| {
match event {
Event::WindowResized(new_size) => {
renderable.resized(*new_size);
HandleStatus::consumed()
}
Event::CloseRequested => {
HandleStatus::ignored() }
_ => HandleStatus::ignored(),
}
});
let mut frame = renderable.begin_drawing();
frame.clear_and_render(
RenderTarget::Surface,
Color::rgb(0.1, 0.1, 0.15), |_pass| {
},
);
}
}
fn main() {
println!("Window App Example");
println!("==================");
println!();
println!("This example demonstrates:");
println!(" - Creating an Engine with RenderPlugin");
println!(" - Using RenderableWindow for window rendering");
println!(" - Handling window events (resize, close)");
println!(" - Rendering a simple clear pass");
println!();
println!("Press Ctrl+C or close the window to exit.");
println!();
run_app(|ctx| {
let window = ctx
.create_window(WindowDescriptor {
title: "Astrelis Window App".to_string(),
..Default::default()
})
.expect("Failed to create window");
let engine = Engine::builder().add_plugin(RenderPlugin).build();
let graphics = engine.get::<Arc<GraphicsContext>>().unwrap();
let renderable = RenderableWindow::new_with_descriptor(
window,
graphics.clone(),
WindowContextDescriptor::default(),
)
.expect("Failed to create renderable window");
Box::new(WindowApp {
engine,
renderable: Some(renderable),
})
});
}