Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Feb 2, 2024
1 parent 473334c commit 0679f06
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 92 deletions.
2 changes: 1 addition & 1 deletion dunge/src/bind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl<'a, V> VisitMember<'a> for &'a Uniform<V> {

impl<'a> VisitMember<'a> for BoundTexture<'a> {
fn visit_member(self, visitor: &mut Visitor<'a>) {
visitor.push(BindingResource::TextureView(self.get().view()));
visitor.push(BindingResource::TextureView(self.0.view()));
}
}

Expand Down
2 changes: 1 addition & 1 deletion dunge/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Context {
Layer::new(&self.0, shader, &opts)
}

pub fn make_mesh<V>(&self, data: &mesh::Data<V>) -> Mesh<V>
pub fn make_mesh<V>(&self, data: &mesh::MeshData<V>) -> Mesh<V>
where
V: Vertex,
{
Expand Down
14 changes: 7 additions & 7 deletions dunge/src/draw.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
use crate::state::Frame;

pub trait Draw {
fn draw(&mut self, frame: Frame);
fn draw(&self, frame: Frame);
}

impl<D> Draw for &mut D
impl<D> Draw for &D
where
D: Draw + ?Sized,
{
fn draw(&mut self, frame: Frame) {
fn draw(&self, frame: Frame) {
(**self).draw(frame);
}
}

pub fn from_fn<D>(draw: D) -> impl Draw
pub fn draw<D>(draw: D) -> impl Draw
where
D: FnMut(Frame),
D: Fn(Frame),
{
struct Func<D>(D);

impl<D> Draw for Func<D>
where
D: FnMut(Frame),
D: Fn(Frame),
{
fn draw(&mut self, frame: Frame) {
fn draw(&self, frame: Frame) {
(self.0)(frame);
}
}
Expand Down
6 changes: 3 additions & 3 deletions dunge/src/el.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ where
match ctrl.view.output() {
Ok(output) => {
let rv = output.render_view();
cx.state().draw(rv, &mut upd);
cx.state().draw(rv, &upd);
output.present();
}
Err(SurfaceError::Timeout) => log::info!("suface error: timeout"),
Expand Down Expand Up @@ -338,7 +338,7 @@ macro_rules! then_try {
match $e {
::std::result::Result::Ok(v) => _ = v,
::std::result::Result::Err(e) => {
return $crate::el::Then::Fail(::std::boxed::Box::from(e));
return $crate::Then::Fail(::std::boxed::Box::from(e));
}
}
};
Expand All @@ -347,7 +347,7 @@ macro_rules! then_try {
match $e {
::std::result::Result::Ok(v) => v,
::std::result::Result::Err(e) => {
return $crate::el::Then::Fail(::std::boxed::Box::from(e));
return $crate::Then::Fail(::std::boxed::Box::from(e));
}
}
};
Expand Down
6 changes: 1 addition & 5 deletions dunge/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
pub use dunge_shader::group::Projection;

#[derive(Clone, Copy)]
pub struct BoundTexture<'a>(&'a Texture);
pub struct BoundTexture<'a>(pub(crate) &'a Texture);

impl<'a> BoundTexture<'a> {
pub fn new<T>(texture: &'a T) -> Self
Expand All @@ -17,10 +17,6 @@ impl<'a> BoundTexture<'a> {
{
Self(texture.bind_texture())
}

pub(crate) fn get(self) -> &'a Texture {
self.0
}
}

/// Describes a group member type projection.
Expand Down
20 changes: 13 additions & 7 deletions dunge/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,39 @@
pub mod bind;
pub mod color;
pub mod context;
pub mod draw;
mod context;
mod draw;
mod format;
pub mod group;
mod init;
pub mod instance;
pub mod layer;
pub mod mesh;
pub mod shader;
mod shader;
mod state;
pub mod texture;
pub mod uniform;
pub mod vertex;

#[cfg(feature = "winit")]
pub mod el;
mod el;
#[cfg(feature = "winit")]
mod time;
#[cfg(feature = "winit")]
pub mod update;
mod update;
#[cfg(feature = "winit")]
pub mod window;

pub mod prelude {
pub use crate::{context::Context, draw, sl, types, update, Frame, Group, Instance, Vertex};
pub use crate::{context::Context, shader::Shader, sl, types, Frame, Group, Instance, Vertex};

#[cfg(feature = "winit")]
pub use crate::el::{Control, KeyCode, Then};
}

pub use {
crate::{
context::{Context, Error},
draw::{draw, Draw},
format::Format,
init::context,
state::{Frame, Options},
Expand All @@ -42,4 +44,8 @@ pub use {
};

#[cfg(feature = "winit")]
pub use crate::init::window;
pub use crate::{
el::{Control, Flow, Key, KeyCode, LoopError, SmolStr, Then},
init::window,
update::{update, update_with_state, Update},
};
16 changes: 8 additions & 8 deletions dunge/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ use {
type Face = [u16; 3];

#[derive(Clone)]
pub struct Data<'a, V> {
pub struct MeshData<'a, V> {
verts: &'a [V],
indxs: Option<Cow<'a, [Face]>>,
}

impl<'a, V> Data<'a, V> {
/// Creates a [mesh data](crate::mesh::Data) from given vertices.
impl<'a, V> MeshData<'a, V> {
/// Creates a [mesh data](crate::mesh::MeshData) from given vertices.
pub fn from_verts(verts: &'a [V]) -> Self {
Self { verts, indxs: None }
}

/// Creates a [mesh data](crate::mesh::Data) from given vertices and indices.
/// Creates a [mesh data](crate::mesh::MeshData) from given vertices and indices.
///
/// # Errors
/// Returns an [error](crate::mesh::Error) if the passed data is incorrect.
Expand All @@ -32,7 +32,7 @@ impl<'a, V> Data<'a, V> {
Ok(Self { verts, indxs })
}

/// Creates a [mesh data](crate::mesh::Data) from given quadrilaterals.
/// Creates a [mesh data](crate::mesh::MeshData) from given quadrilaterals.
///
/// # Errors
/// Returns an [error](crate::mesh::TooManyVertices) if too many vertices are passed.
Expand All @@ -58,7 +58,7 @@ impl<'a, V> Data<'a, V> {
}
}

/// An error returned from the [mesh data](crate::mesh::Data) constructors.
/// An error returned from the [mesh data](crate::mesh::MeshData) constructors.
#[derive(Debug)]
pub enum Error {
/// Vertices length doesn't fit in [`u16`](std::u16) integer.
Expand Down Expand Up @@ -98,7 +98,7 @@ pub struct Mesh<V> {
}

impl<V> Mesh<V> {
pub(crate) fn new(state: &State, data: &Data<V>) -> Self
pub(crate) fn new(state: &State, data: &MeshData<V>) -> Self
where
V: Vertex,
{
Expand Down Expand Up @@ -164,7 +164,7 @@ mod tests {
#[test]
fn from_quads() {
let verts = [[0, 1, 2, 3], [4, 5, 6, 7]];
let data = Data::from_quads(&verts).expect("mesh data");
let data = MeshData::from_quads(&verts).expect("mesh data");
let indxs = data.indxs.expect("indices");
assert_eq!(data.verts.len(), 8);
assert_eq!(indxs.len(), 4);
Expand Down
2 changes: 1 addition & 1 deletion dunge/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl State {
self.shader_ids.fetch_add(1, atomic::Ordering::Relaxed)
}

pub fn draw<D>(&self, view: RenderView, mut draw: D)
pub fn draw<D>(&self, view: RenderView, draw: D)
where
D: Draw,
{
Expand Down
14 changes: 7 additions & 7 deletions dunge/src/texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ use {
};

#[derive(Clone, Copy)]
pub struct Data<'a> {
pub struct TextureData<'a> {
data: &'a [u8],
size: (u32, u32),
format: Format,
}

impl<'a> Data<'a> {
impl<'a> TextureData<'a> {
pub const fn empty(size: (u32, u32), format: Format) -> Result<Self, ZeroSized> {
let (width, height) = size;
if width == 0 || height == 0 {
Expand Down Expand Up @@ -64,7 +64,7 @@ impl<'a> Data<'a> {
}
}

/// The [texture data](crate::texture::Data) error.
/// The [texture data](crate::texture::TextureData) error.
#[derive(Debug)]
pub enum Error {
/// The texture data is zero sized.
Expand All @@ -85,7 +85,7 @@ impl fmt::Display for Error {

impl error::Error for Error {}

/// The [texture data](crate::texture::Data) is zero sized.
/// The [texture data](crate::texture::TextureData) is zero sized.
#[derive(Debug)]
pub struct ZeroSized;

Expand All @@ -103,7 +103,7 @@ pub struct Texture {
}

impl Texture {
fn new(state: &State, mut usage: TextureUsages, data: Data) -> Self {
fn new(state: &State, mut usage: TextureUsages, data: TextureData) -> Self {
use wgpu::*;

let (width, height) = data.size;
Expand Down Expand Up @@ -446,9 +446,9 @@ pub trait Make: private::Sealed {
fn make(self, maker: Maker) -> Self::Out;
}

impl private::Sealed for Data<'_> {}
impl private::Sealed for TextureData<'_> {}

impl Make for Data<'_> {
impl Make for TextureData<'_> {
type Out = Texture;

fn make(self, Maker { state, usage }: Maker) -> Self::Out {
Expand Down
16 changes: 8 additions & 8 deletions dunge/src/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ pub trait Update: Draw {
fn update(&mut self, ctrl: &Control) -> Self::Flow;
}

pub fn from_fn<U, F, D>(mut upd: U, mut draw: D) -> impl Update
pub fn update<U, F, D>(mut upd: U, draw: D) -> impl Update
where
U: FnMut(&Control) -> F,
F: Flow,
D: FnMut(Frame),
D: Fn(Frame),
{
with_state((), move |(), ctrl| upd(ctrl), move |(), frame| draw(frame))
update_with_state((), move |(), ctrl| upd(ctrl), move |(), frame| draw(frame))
}

pub fn with_state<S, U, F, D>(state: S, upd: U, draw: D) -> impl Update
pub fn update_with_state<S, U, F, D>(state: S, upd: U, draw: D) -> impl Update
where
U: FnMut(&mut S, &Control) -> F,
F: Flow,
D: FnMut(&S, Frame),
D: Fn(&S, Frame),
{
struct Func<S, U, D>(S, U, D);

impl<S, U, D> Draw for Func<S, U, D>
where
D: FnMut(&S, Frame),
D: Fn(&S, Frame),
{
fn draw(&mut self, frame: Frame) {
fn draw(&self, frame: Frame) {
(self.2)(&self.0, frame);
}
}
Expand All @@ -39,7 +39,7 @@ where
where
U: FnMut(&mut S, &Control) -> F,
F: Flow,
D: FnMut(&S, Frame),
D: Fn(&S, Frame),
{
type Flow = F;

Expand Down
22 changes: 9 additions & 13 deletions dunge/tests/triangle_group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ fn render() -> Result<(), Error> {
use {
dunge::{
color::Rgba,
draw,
group::BoundTexture,
mesh,
mesh::MeshData,
sl::{self, Groups, InVertex, Out},
texture::{self, Filter, Sampler},
texture::{Filter, Sampler, TextureData},
Format, Group, Vertex,
},
glam::Vec2,
Expand Down Expand Up @@ -45,10 +44,9 @@ fn render() -> Result<(), Error> {

let map = {
let texture = {
use texture::Data;

let gradient = Image::decode(include_bytes!("gradient.png"));
let data = Data::new(&gradient.data, gradient.size, Format::RgbAlpha)?.with_bind();
let data =
TextureData::new(&gradient.data, gradient.size, Format::RgbAlpha)?.with_bind();
cx.make_texture(data)
};

Expand All @@ -65,15 +63,13 @@ fn render() -> Result<(), Error> {

let layer = cx.make_layer(&shader, Format::RgbAlpha);
let view = {
use texture::Data;

let data = Data::empty(SIZE, Format::RgbAlpha)?.with_draw().with_copy();
let data = TextureData::empty(SIZE, Format::RgbAlpha)?
.with_draw()
.with_copy();
cx.make_texture(data)
};

let mesh = {
use mesh::Data;

const VERTS: [Vert; 3] = [
Vert {
pos: [0., -0.75],
Expand All @@ -89,13 +85,13 @@ fn render() -> Result<(), Error> {
},
];

let data = Data::from_verts(&VERTS);
let data = MeshData::from_verts(&VERTS);
cx.make_mesh(&data)
};

let buffer = cx.make_copy_buffer(SIZE);
let opts = Rgba::from_standard([0., 0., 0., 1.]);
let draw = draw::from_fn(|mut frame| {
let draw = dunge::draw(|mut frame| {
frame.layer(&layer, opts).bind(&map).draw(&mesh);
frame.copy_texture(&buffer, &view);
});
Expand Down
Loading

0 comments on commit 0679f06

Please sign in to comment.