Skip to main content

wgame_gfx_shapes/
lib.rs

1//! GPU-accelerated shapes and textured triangle meshes in 2D and 3D.
2//!
3//! Provides geometry primitives with fill, stroke, and texture support.
4
5#![forbid(unsafe_code)]
6
7mod circle;
8pub mod geometry;
9pub mod material;
10mod pipeline;
11mod polygon;
12mod polyline;
13mod render;
14pub mod shader;
15mod shape;
16
17use core::ops::Deref;
18use wgame_gfx::{
19    Graphics,
20    types::{Color, color},
21};
22use wgame_gfx_texture::{Texture, TexturingLibrary, TexturingState};
23use wgame_image::Image;
24
25use crate::{circle::CircleLibrary, polygon::PolygonLibrary};
26
27pub use self::{
28    circle::{Circle, CircleFill, CircleStroke},
29    geometry::Mesh,
30    polygon::{Polygon, PolygonFill},
31    polyline::{Polyline, PolylineFill, PolylinePoint},
32    shape::{Shape, Textured},
33};
34
35/// Commonly used shape traits.
36pub mod prelude {
37    pub use crate::shape::{Shape, ShapeFill, ShapeStroke, Textured};
38}
39
40/// Shared state for shape rendering.
41#[derive(Clone, PartialEq, Eq, Hash, Debug)]
42pub struct ShapesState {
43    texture: TexturingState,
44}
45
46impl ShapesState {
47    /// Returns a reference to the texture state.
48    pub fn texture(&self) -> &TexturingState {
49        &self.texture
50    }
51}
52
53impl Deref for ShapesState {
54    type Target = Graphics;
55    fn deref(&self) -> &Self::Target {
56        &self.texture
57    }
58}
59
60/// Shared graphics library for rendering shapes and meshes.
61#[derive(Clone)]
62pub struct ShapesLibrary {
63    state: ShapesState,
64    polygon: PolygonLibrary,
65    circle: CircleLibrary,
66    white_texture: Texture,
67}
68
69impl ShapesLibrary {
70    /// Creates a new shapes library.
71    pub fn new(state: &Graphics, texture: &TexturingLibrary) -> Self {
72        assert_eq!(state, &**texture.state());
73        let state = ShapesState {
74            texture: texture.state().clone(),
75        };
76        Self {
77            polygon: PolygonLibrary::new(&state),
78            circle: CircleLibrary::new(&state),
79            white_texture: texture.texture(
80                &Image::with_color((1, 1), color::WHITE.to_rgba_f16()),
81                Default::default(),
82            ),
83            state,
84        }
85    }
86
87    /// Returns a reference to the shapes state.
88    pub fn state(&self) -> &ShapesState {
89        &self.state
90    }
91}