effect_wgpu/app/
effect2d.rs

1use anyhow::Result;
2use effect_core::{camera::camera2d::Camera2D, id::LayerID};
3use effect_events::input::{EffectEvent, EffectEventSystem};
4use winit::{
5    dpi::PhysicalSize,
6    event_loop::{ControlFlow, EventLoop},
7    window::WindowBuilder,
8};
9
10use crate::{
11    engine::engine2d::WebEngine2D, entity::entity2d::WebEntity2D, layer::WebLayer2D,
12    texture::texture2d::WebTexture2D,
13};
14
15pub struct EffectWeb2D {
16    engine: WebEngine2D,
17}
18
19impl EffectWeb2D {
20    pub fn new(
21        screen_dimensions: PhysicalSize<u32>,
22        v_sync: bool,
23        app_name: &'static str,
24        resizable: bool,
25    ) -> (Self, EventLoop<()>) {
26        let event_loop = EventLoop::new().unwrap();
27        event_loop.set_control_flow(ControlFlow::Poll);
28        let window = WindowBuilder::new()
29            .with_title(app_name)
30            .with_inner_size(screen_dimensions)
31            .with_resizable(resizable)
32            .build(&event_loop)
33            .unwrap();
34        let engine = pollster::block_on(WebEngine2D::new(window, v_sync));
35        (Self { engine }, event_loop)
36    }
37
38    /// it is up to the user to sort the layers, they have the tools to do so.
39    pub fn render(
40        &mut self,
41        layers: &Vec<WebLayer2D>,
42        camera: &Camera2D,
43    ) -> Result<(), wgpu::SurfaceError> {
44        self.engine.render(&layers, camera)
45    }
46
47    /// Make sure your texture_size is set to the larger dimension that appears in your textures.
48    /// It would be easier to use textures which all have the same dimensions
49    /// and set that to the texture size, otherwise 2D transformations may not
50    /// behave as you would expect them to.
51    /// The maximum texture size for a layer is 8192px * 8192px
52    /// The optimal stratergy is to keep similar textures on the same layer
53    /// (provided you want the rendered in that order)
54    /// It is advisable to have the texture_size be a square to avoid some textures getting crushed.
55    pub fn init_layer(
56        &self,
57        id: LayerID,
58        textures: Vec<WebTexture2D>,
59        texture_size: PhysicalSize<u32>,
60        pixel_art: bool,
61    ) -> Result<WebLayer2D> {
62        self.engine
63            .init_layer(id, textures, texture_size, pixel_art)
64    }
65
66    pub fn set_entities(&self, layer: &mut WebLayer2D, entities: &[&WebEntity2D]) {
67        self.engine.set_entities(layer, entities);
68    }
69
70    pub fn init_camera(&self, fov: f32) -> Camera2D {
71        self.engine.init_camera(fov)
72    }
73
74    pub fn update_camera(&self, camera: &mut Camera2D) {
75        self.engine.update_camera(camera);
76    }
77
78    pub fn set_background(&mut self, texture: WebTexture2D, pixel_art: bool) -> Result<()> {
79        self.engine.set_background(texture, pixel_art)
80    }
81
82    pub fn update(&mut self, ctx: &mut EffectEvent) {
83        if ctx.window_resized() {
84            self.engine.resize(ctx.window_size());
85        }
86        if ctx.scale_factor_changed() {
87            self.engine.resize(ctx.window_size());
88        }
89        EffectEventSystem::reset_window_changes(ctx)
90    }
91
92    pub fn resize_window(&mut self, width: u32, height: u32) {
93        let size = PhysicalSize::new(width, height);
94        self.engine.window.set_resizable(true);
95        let _ = self.engine.window.request_inner_size(size);
96        self.engine.window.set_resizable(false);
97    }
98
99    pub fn queue(&self) -> &wgpu::Queue {
100        self.engine.queue()
101    }
102
103    pub fn device(&self) -> &wgpu::Device {
104        self.engine.device()
105    }
106
107    pub fn surface(&self) -> &wgpu::Surface {
108        self.engine.surface()
109    }
110}