Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
nanoqsh committed Jan 9, 2024
1 parent dbf6ca4 commit 364ad31
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 26 deletions.
31 changes: 12 additions & 19 deletions dunge/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,29 @@ use {
};

#[cfg(feature = "winit")]
use crate::window::{self, Window, WindowBuilder};
use crate::window::WindowBuilder;

fn instance() -> Instance {
pub(crate) async fn make() -> Result<(Context, Instance), context::Error> {
use wgpu::{Backends, InstanceDescriptor};

let desc = InstanceDescriptor {
backends: Backends::PRIMARY,
..Default::default()
let instance = {
let desc = InstanceDescriptor {
backends: Backends::PRIMARY,
..Default::default()
};

Instance::new(desc)
};

Instance::new(desc)
let state = State::new(&instance).await?;
Ok((Context::new(state), instance))
}

pub async fn context() -> Result<Context, context::Error> {
let instance = instance();
let state = State::new(&instance).await?;
Ok(Context::new(state))
make().await.map(|(cx, _)| cx)
}

#[cfg(feature = "winit")]
pub fn window() -> WindowBuilder {
WindowBuilder::new()
}

#[cfg(feature = "winit")]
impl WindowBuilder {
pub async fn make(self) -> Result<Window, window::Error> {
let instance = instance();
let state = State::new(&instance).await?;
let cx = Context::new(state);
self.build(cx, &instance)
}
}
44 changes: 40 additions & 4 deletions dunge/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ use {
context::{self, Context},
el::Loop,
format::Format,
init,
state::{RenderView, State},
update::Update,
},
std::{error, fmt},
std::{
error, fmt,
future::{Future, IntoFuture},
pin::Pin,
task::{self, Poll},
},
wgpu::{
CreateSurfaceError, Instance, Surface, SurfaceConfiguration, SurfaceError, SurfaceTexture,
TextureView,
Expand Down Expand Up @@ -51,12 +57,16 @@ impl WindowBuilder {
self
}

pub(crate) fn build(self, cx: Context, instance: &Instance) -> Result<Window, Error> {
use winit::{dpi::PhysicalSize, window::Fullscreen};
fn build(&mut self, cx: Context, instance: &Instance) -> Result<Window, Error> {
use {
std::mem,
winit::{dpi::PhysicalSize, window::Fullscreen},
};

let el = Loop::new()?;
let inner = {
let builder = window::WindowBuilder::new().with_title(self.title);
let title = mem::take(&mut self.title);
let builder = window::WindowBuilder::new().with_title(title);
let builder = match self.size {
Some((width, height)) => builder.with_inner_size(PhysicalSize::new(width, height)),
None => builder.with_fullscreen(Some(Fullscreen::Borderless(None))),
Expand All @@ -70,6 +80,32 @@ impl WindowBuilder {
}
}

impl IntoFuture for WindowBuilder {
type Output = Result<Window, Error>;
type IntoFuture = Build;

fn into_future(mut self) -> Self::IntoFuture {
let fut = async move {
let (cx, instance) = init::make().await?;
self.build(cx, &instance)
};

Build(Box::pin(fut))
}
}

type BoxFuture<T> = Pin<Box<dyn Future<Output = T>>>;

pub struct Build(BoxFuture<Result<Window, Error>>);

impl Future for Build {
type Output = Result<Window, Error>;

fn poll(self: Pin<&mut Self>, cx: &mut task::Context<'_>) -> Poll<Self::Output> {
self.get_mut().0.as_mut().poll(cx)
}
}

pub struct Window {
cx: Context,
el: Loop,
Expand Down
6 changes: 3 additions & 3 deletions examples/triangle/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ type Error = Box<dyn std::error::Error>;

fn main() {
env_logger::init();
if let Err(err) = run() {
if let Err(err) = helpers::block_on(run()) {
eprintln!("error: {err}");
}
}

fn run() -> Result<(), Error> {
async fn run() -> Result<(), Error> {
use {
dunge::{
color::Rgba,
Expand All @@ -32,7 +32,7 @@ fn run() -> Result<(), Error> {
}
};

let window = helpers::block_on(dunge::window().with_title("Triangle").make())?;
let window = dunge::window().with_title("Triangle").await?;
let cx = window.context();
let shader = cx.make_shader(triangle);
let layer = cx.make_layer(window.format(), &shader);
Expand Down

0 comments on commit 364ad31

Please sign in to comment.