Skip to main content

game_toolkit_gfx/
frame.rs

1use crate::graphics::Graphics;
2use crate::painter::Painter;
3
4pub struct Frame {
5    pub(crate) encoder: Option<wgpu::CommandEncoder>,
6    pub(crate) view: wgpu::TextureView,
7    pub(crate) surface_texture: Option<wgpu::SurfaceTexture>,
8    pub clear_color: [f32; 4],
9    pub(crate) flushed: bool,
10}
11
12impl Frame {
13    /// Begin a 2D rendering pass. Returns a [`Painter`] that flushes on drop.
14    pub fn painter<'a>(&'a mut self, gfx: &'a mut Graphics) -> Painter<'a> {
15        Painter::new(self, gfx)
16    }
17
18    pub fn encoder_mut(&mut self) -> Option<&mut wgpu::CommandEncoder> {
19        self.encoder.as_mut()
20    }
21
22    pub fn view(&self) -> &wgpu::TextureView {
23        &self.view
24    }
25
26    /// Split borrow of the view and encoder so overlays (egui, custom passes) can record
27    /// into the same frame without fighting the borrow checker.
28    pub fn view_and_encoder(&mut self) -> (&wgpu::TextureView, &mut wgpu::CommandEncoder) {
29        (
30            &self.view,
31            self.encoder
32                .as_mut()
33                .expect("frame encoder taken/dropped before view_and_encoder"),
34        )
35    }
36}