Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 6 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ members = [
"crates/unen_net",
"crates/unen_event",
"crates/unen_event_derive",
"crates/unen_core",
"crates/unen_logging",
"crates/unen_runner",
"crates/unen_window",
"crates/unen_winit",
"crates/unen_render",
"crates/unen",
"crates/unen_app",
]
resolver = "2"

Expand All @@ -25,16 +24,16 @@ strip = true

[workspace.dependencies]
# Logging
log = { version = "0.4.28"}
log = { version = "0.4.28" }
tracing = { version = "0.1.41", features = ["log"] }
tracing-subscriber ={ version = "0.3.20" }
tracing-subscriber = { version = "0.3.20" }

# Error Handling
thiserror ={ version = "2.0.17" }
thiserror = { version = "2.0.17" }

# Derive Macros
syn = { version = "2.0.106", features = ["full"] }
quote = { version = "1.0.41"}
quote = { version = "1.0.41" }
proc-macro2 = { version = "1.0.101" }

# Algorithm
Expand All @@ -48,7 +47,7 @@ pollster = { version = "0.4.0" }
# Rendering
winit = { version = "0.30.12", features = ["android-native-activity"] }
wgpu = { version = "27.0.1", features = ["webgl"] }
raw-window-handle = { version = "0.6.2"}
raw-window-handle = { version = "0.6.2" }

# Web
console_error_panic_hook = { version = "0.1.7" }
Expand Down
15 changes: 7 additions & 8 deletions crates/unen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,16 @@ repository.workspace = true
keywords.workspace = true

[dependencies]
unen_core = { path = "../unen_core"}
unen_app = { path = "../unen_app" }
unen_event = { path = "../unen_event", features = ["derive"] }
unen_logging = { path = "../unen_logging" }
unen_runner = { path = "../unen_runner" }
unen_logging = { path = "../unen_logging", features = ["tracing"] }
unen_winit = { path = "../unen_winit" }
unen_render = { path = "../unen_render"}
unen_render = { path = "../unen_render" }

[[example]]
name = "basic_setup"
path = "examples/basic_setup.rs"
name = "basic_app"
path = "examples/basic_app.rs"

[[example]]
name = "window_setup"
path = "examples/window_setup.rs"
name = "window_app"
path = "examples/window_app.rs"
9 changes: 9 additions & 0 deletions crates/unen/examples/basic_app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use unen::prelude::*;

fn main() {
let mut app = create_app();

app.system(START, TracingLogger);

app.run();
}
10 changes: 0 additions & 10 deletions crates/unen/examples/basic_setup.rs

This file was deleted.

28 changes: 28 additions & 0 deletions crates/unen/examples/window_app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use unen::prelude::*;

pub struct WindowApp;

impl System for WindowApp {
fn execute(&mut self, state: AppState, commands: &mut CommandRegistry) -> AppState {
commands.add(commands::SetClearColor {
r: 0.3,
g: 0.2,
b: 0.1,
a: 1.0,
});

state
}
}

fn main() {
let mut app = create_app();

app.runner(WinitRunner::default())
.runner(WinitRunner::default())
.system(START, TracingLogger)
.system(STEP, Renderer::default())
.system(STEP, WindowApp);

app.run();
}
12 changes: 0 additions & 12 deletions crates/unen/examples/window_setup.rs

This file was deleted.

4 changes: 3 additions & 1 deletion crates/unen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod prelude {
pub use unen_core::prelude::*;
pub use unen_app::prelude::*;
pub use unen_event::prelude::*;
pub use unen_logging::prelude::*;
pub use unen_render::prelude::*;
pub use unen_winit::prelude::*;
}
5 changes: 3 additions & 2 deletions crates/unen_runner/Cargo.toml → crates/unen_app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
[package]
name = "unen_runner"
name = "unen_app"
description = "Application crate for UnnamedEngine"
version.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
keywords.workspace = true

[dependencies]
unen_event = { path = "../unen_event", features = ["derive"] }
tracing = { workspace = true }

signal-hook = { workspace = true }
82 changes: 82 additions & 0 deletions crates/unen_app/src/app.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use std::sync::{atomic::AtomicBool, Arc};

use crate::{
command::CommandRegistry,
runner::{MininalRunner, Runner, RunnerData},
stage::{Stage, StageContainer},
system::System,
};

pub fn create_app() -> App {
App::default()
}

#[derive(Debug, Default)]
pub struct AppState;

pub struct App {
state: AppState,
runner: Box<dyn Runner>,
stages: StageContainer,
commands: CommandRegistry,
}

impl Default for App {
fn default() -> Self {
Self {
runner: MininalRunner::new_boxed(),
state: AppState,
stages: StageContainer::default(),
commands: CommandRegistry::default(),
}
}
}

impl App {
/// Starts the application, consumes `self`.
pub fn run(self) {
let App {
state,
mut runner,
stages,
commands,
} = self;

let term = Arc::new(AtomicBool::new(false));

let data = RunnerData {
stages,
state,
term,
commands,
};

runner.as_mut().run(data);

// Prints a newline to not mix logs with ctl echo
println!();

tracing::info!("Successfully terminated engine.");
tracing::info!("See you later :D");
}

pub fn system<S: Stage, M: System>(&mut self, stage: S, system: M) -> &mut Self {
let system_name = system.name();
let stage_name = stage.name();
self.stages.get(stage).push(system);
tracing::info!("Attached {} system to {} stage.", system_name, stage_name);
self
}

pub fn stage<S: Stage>(&mut self, stage: S) -> &mut Self {
let stage_name = stage.name();
self.stages.insert(stage);
tracing::info!("Inserted '{}' stage.", stage_name);
self
}

pub fn runner<R: Runner>(&mut self, runner: R) -> &mut Self {
self.runner = Box::new(runner);
self
}
}
40 changes: 40 additions & 0 deletions crates/unen_app/src/command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{
any::{Any, TypeId},
collections::HashMap,
};

pub trait Command: Send + Sync + 'static {
type Target: Send + Sync + 'static;
fn apply(self: Box<Self>, target: &mut Self::Target);
}

#[derive(Default)]
pub struct CommandRegistry {
queues: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
}

impl CommandRegistry {
pub fn add<C: Command>(&mut self, command: C) {
let queue = self
.queues
.entry(TypeId::of::<C::Target>())
.or_insert_with(|| Box::new(Vec::<Box<dyn Command<Target = C::Target>>>::new()));

let queue = queue
.downcast_mut::<Vec<Box<dyn Command<Target = C::Target>>>>()
.unwrap();

queue.push(Box::new(command));
}

pub fn apply_all<T: 'static + Send + Sync>(&mut self, target: &mut T) {
if let Some(queue) = self.queues.remove(&TypeId::of::<T>()) {
let queue = *queue
.downcast::<Vec<Box<dyn Command<Target = T>>>>()
.unwrap();
for command in queue {
command.apply(target);
}
}
}
}
13 changes: 13 additions & 0 deletions crates/unen_app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod app;
mod command;
mod runner;
mod stage;
mod system;

pub mod prelude {
pub use crate::app::{create_app, App, AppState};
pub use crate::command::{Command, CommandRegistry};
pub use crate::runner::{MininalRunner, Runner, RunnerData};
pub use crate::stage::{Stage, StageContainer, START, STEP, STOP};
pub use crate::system::{System, SystemContainer};
}
Loading