mireforge_render_wgpu/
gfx_impl.rs

1use crate::gfx::Gfx;
2use crate::{
3    FixedAtlas, FontAndMaterial, FrameLookup, MaterialRef, NineSliceAndMaterial, QuadParams,
4    Render, RenderItem, Renderable, SpriteParams, Text, TileMap, to_wgpu_color,
5};
6use int_math::{URect, UVec2, Vec2, Vec3};
7use mireforge_render::{AspectRatio, Color, ViewportStrategy, VirtualScale};
8use monotonic_time_rs::Millis;
9
10impl Gfx for Render {
11    fn sprite_atlas_frame(&mut self, position: Vec3, frame: u16, atlas: &impl FrameLookup) {
12        self.sprite_atlas_frame(position, frame, atlas);
13    }
14
15    fn sprite_atlas(&mut self, position: Vec3, atlas_rect: URect, material_ref: &MaterialRef) {
16        self.sprite_atlas(position, atlas_rect, material_ref);
17    }
18
19    fn draw_sprite(&mut self, position: Vec3, material_ref: &MaterialRef) {
20        self.draw_sprite(position, material_ref);
21    }
22
23    fn draw_sprite_ex(
24        &mut self,
25        position: Vec3,
26        material_ref: &MaterialRef,
27        params: &SpriteParams,
28    ) {
29        self.draw_sprite_ex(position, material_ref, *params);
30    }
31
32    fn quad(&mut self, position: Vec3, size: UVec2, color: Color) {
33        self.draw_quad(position, size, color);
34    }
35
36    fn quad_ex(&mut self, position: Vec3, size: UVec2, color: Color, params: QuadParams) {
37        self.draw_quad_ex(position, size, color, params);
38    }
39
40    fn draw_with_mask(
41        &mut self,
42        position: Vec3,
43        size: UVec2,
44        color: Color,
45        alpha_masked: &MaterialRef,
46    ) {
47        self.push_mask(position, size, color, alpha_masked);
48    }
49
50    fn nine_slice(
51        &mut self,
52        position: Vec3,
53        size: UVec2,
54        color: Color,
55        nine_slice: &NineSliceAndMaterial,
56    ) {
57        self.nine_slice(position, size, color, nine_slice);
58    }
59
60    fn set_origin(&mut self, position: Vec2) {
61        self.origin = position;
62    }
63
64    fn set_clear_color(&mut self, color: Color) {
65        self.clear_color = to_wgpu_color(color);
66    }
67
68    fn tilemap_params(
69        &mut self,
70        position: Vec3,
71        tiles: &[u16],
72        width: u16,
73        atlas_ref: &FixedAtlas,
74        scale: u8,
75    ) {
76        self.items.push(RenderItem {
77            position,
78            material_ref: atlas_ref.material.clone(),
79            renderable: Renderable::TileMap(TileMap {
80                tiles_data_grid_size: UVec2::new(width, tiles.len() as u16 / width),
81                cell_count_size: atlas_ref.cell_count_size,
82                one_cell_size: atlas_ref.one_cell_size,
83                tiles: Vec::from(tiles),
84                scale,
85            }),
86        });
87    }
88
89    fn text_draw(
90        &mut self,
91        position: Vec3,
92        text: &str,
93        font_and_mat: &FontAndMaterial,
94        color: &Color,
95    ) {
96        self.items.push(RenderItem {
97            position,
98            material_ref: font_and_mat.material_ref.clone(),
99            renderable: Renderable::Text(Text {
100                text: text.to_string(),
101                font_ref: (&font_and_mat.font_ref).into(),
102                color: *color,
103            }),
104        });
105    }
106
107    fn now(&self) -> Millis {
108        self.last_render_at
109    }
110
111    fn physical_aspect_ratio(&self) -> AspectRatio {
112        self.physical_surface_size.into()
113    }
114
115    fn physical_size(&self) -> UVec2 {
116        self.physical_surface_size
117    }
118
119    fn set_virtual_size(&mut self, virtual_size: UVec2) {
120        self.resize_virtual(virtual_size);
121    }
122
123    fn set_viewport(&mut self, viewport_strategy: ViewportStrategy) {
124        self.viewport_strategy = viewport_strategy;
125    }
126
127    fn viewport(&self) -> &ViewportStrategy {
128        &self.viewport_strategy
129    }
130
131    fn set_scale(&mut self, scale_factor: VirtualScale) {
132        match scale_factor {
133            VirtualScale::IntScale(scale) => self.scale = f32::from(scale),
134            VirtualScale::FloatScale(scale) => self.scale = scale,
135        }
136    }
137}