use crate::camera::{Camera2d, Camera3d};
use crate::color::Color;
use crate::post_processing::PostProcessingEffect;
use crate::renderer::Renderer3d;
use crate::scene::{SceneNode2d, SceneNode3d};
use crate::window::{CanvasSetup, Window};
use glamx::UVec2;
use image::{ImageBuffer, Rgb};
pub struct OffscreenSurface {
window: Window,
}
impl OffscreenSurface {
pub async fn new(width: u32, height: u32) -> OffscreenSurface {
OffscreenSurface {
window: Window::do_new_headless(width, height, None).await,
}
}
pub async fn with_setup(width: u32, height: u32, setup: CanvasSetup) -> OffscreenSurface {
OffscreenSurface {
window: Window::do_new_headless(width, height, Some(setup)).await,
}
}
pub async fn render_3d(&mut self, scene: &mut SceneNode3d, camera: &mut impl Camera3d) {
let _ = self.window.render_3d(scene, camera).await;
}
pub async fn render_2d(&mut self, scene: &mut SceneNode2d, camera: &mut impl Camera2d) {
let _ = self.window.render_2d(scene, camera).await;
}
#[allow(clippy::too_many_arguments)]
pub async fn render(
&mut self,
scene: Option<&mut SceneNode3d>,
scene_2d: Option<&mut SceneNode2d>,
camera: Option<&mut dyn Camera3d>,
camera_2d: Option<&mut dyn Camera2d>,
renderer: Option<&mut dyn Renderer3d>,
post_processing: Option<&mut dyn PostProcessingEffect>,
) {
let _ = self
.window
.render(
scene,
scene_2d,
camera,
camera_2d,
renderer,
post_processing,
)
.await;
}
pub async fn render_image_3d(
&mut self,
scene: &mut SceneNode3d,
camera: &mut impl Camera3d,
) -> ImageBuffer<Rgb<u8>, Vec<u8>> {
self.render_3d(scene, camera).await;
self.snap_image()
}
pub fn snap(&self, out: &mut Vec<u8>) {
self.window.snap(out)
}
pub fn snap_rect(&self, out: &mut Vec<u8>, x: usize, y: usize, width: usize, height: usize) {
self.window.snap_rect(out, x, y, width, height)
}
pub fn snap_image(&self) -> ImageBuffer<Rgb<u8>, Vec<u8>> {
self.window.snap_image()
}
pub fn resize(&mut self, width: u32, height: u32) {
self.window.canvas_mut().resize(width, height);
}
pub fn size(&self) -> UVec2 {
self.window.size()
}
pub fn width(&self) -> u32 {
self.window.width()
}
pub fn height(&self) -> u32 {
self.window.height()
}
pub fn set_background_color(&mut self, color: Color) {
self.window.set_background_color(color);
}
}