trying to eat the spaghetti (make it less)
This commit is contained in:
parent
3dd1a0caee
commit
4ad7d2ebd0
42 changed files with 189 additions and 668 deletions
7
crates/moonhare_event/Cargo.toml
Normal file
7
crates/moonhare_event/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "moonhare_event"
|
||||
edition = "2024"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
1
crates/moonhare_event/README.md
Normal file
1
crates/moonhare_event/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
# MoonhareEvent
|
||||
12
crates/moonhare_event/src/event.rs
Normal file
12
crates/moonhare_event/src/event.rs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
pub enum EventType {
|
||||
None,
|
||||
WindowClose, WindowResize,
|
||||
KeyPressed, KeyReleased,
|
||||
MouseButtonPressed, MouseButtonReleased, MouseMoved, MouseScrolled
|
||||
}
|
||||
|
||||
/// Base Class for Engine Events
|
||||
pub trait Event {
|
||||
fn get_event_name() -> &'static str;
|
||||
fn get_event_type() -> EventType;
|
||||
}
|
||||
5
crates/moonhare_event/src/event_listener.rs
Normal file
5
crates/moonhare_event/src/event_listener.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use crate::event::Event;
|
||||
|
||||
pub trait EventListener {
|
||||
fn on_event<T: Event>(event: T);
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
use crate::{event::Event,};
|
||||
|
||||
struct KeyPressedEvent{}
|
||||
|
||||
const KEY_PRESSED_EVENT_NAME: &str = "KeyPressedEvent";
|
||||
|
||||
impl Event for KeyPressedEvent {
|
||||
fn get_event_name() -> &'static str {
|
||||
KEY_PRESSED_EVENT_NAME
|
||||
}
|
||||
|
||||
fn get_event_type() -> crate::event::EventType {
|
||||
crate::event::EventType::KeyPressed
|
||||
}
|
||||
}
|
||||
|
||||
impl super::KeyEvent for KeyPressedEvent {
|
||||
|
||||
}
|
||||
9
crates/moonhare_event/src/events/key_events/mod.rs
Normal file
9
crates/moonhare_event/src/events/key_events/mod.rs
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
use crate::event::Event;
|
||||
|
||||
pub mod key_pressed_event;
|
||||
|
||||
pub trait KeyEvent: Event {
|
||||
fn get_key_code() {
|
||||
|
||||
}
|
||||
}
|
||||
3
crates/moonhare_event/src/events/mod.rs
Normal file
3
crates/moonhare_event/src/events/mod.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod key_events;
|
||||
pub mod mouse_events;
|
||||
pub mod window_events;
|
||||
5
crates/moonhare_event/src/events/mouse_events/mod.rs
Normal file
5
crates/moonhare_event/src/events/mouse_events/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use crate::event::Event;
|
||||
|
||||
pub trait MouseEvent: Event {
|
||||
|
||||
}
|
||||
5
crates/moonhare_event/src/events/window_events/mod.rs
Normal file
5
crates/moonhare_event/src/events/window_events/mod.rs
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
use crate::event::Event;
|
||||
|
||||
pub trait WindowEvent: Event {
|
||||
|
||||
}
|
||||
3
crates/moonhare_event/src/lib.rs
Normal file
3
crates/moonhare_event/src/lib.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
pub mod event;
|
||||
pub mod event_listener;
|
||||
pub mod events;
|
||||
7
crates/moonhare_graphics/Cargo.toml
Normal file
7
crates/moonhare_graphics/Cargo.toml
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "moonhare_graphics"
|
||||
edition = "2024"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
1
crates/moonhare_graphics/README.md
Normal file
1
crates/moonhare_graphics/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
# MoonhareGraphics
|
||||
0
crates/moonhare_graphics/src/gpu_buffer.rs
Normal file
0
crates/moonhare_graphics/src/gpu_buffer.rs
Normal file
0
crates/moonhare_graphics/src/graphics_server.rs
Normal file
0
crates/moonhare_graphics/src/graphics_server.rs
Normal file
4
crates/moonhare_graphics/src/lib.rs
Normal file
4
crates/moonhare_graphics/src/lib.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
/// Crate for providing an abstraction layer over different graphics APIs
|
||||
|
||||
pub mod graphics_server;
|
||||
pub mod shader;
|
||||
4
crates/moonhare_graphics/src/shader.rs
Normal file
4
crates/moonhare_graphics/src/shader.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub enum ShaderType {
|
||||
Vertex,
|
||||
Fragment,
|
||||
}
|
||||
11
crates/moonhare_internal/Cargo.toml
Normal file
11
crates/moonhare_internal/Cargo.toml
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
[package]
|
||||
name = "moonhare_internal"
|
||||
edition = "2024"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
moonhare_event = { path = "../moonhare_event" }
|
||||
moonhare_graphics = { path = "../moonhare_graphics" }
|
||||
moonhare_log = { path = "../moonhare_log" }
|
||||
moonhare_window = { path = "../moonhare_window" }
|
||||
4
crates/moonhare_internal/src/lib.rs
Normal file
4
crates/moonhare_internal/src/lib.rs
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
pub use moonhare_event as event;
|
||||
pub use moonhare_graphics as graphics;
|
||||
pub use moonhare_log as log;
|
||||
pub use moonhare_window as window;
|
||||
10
crates/moonhare_log/Cargo.toml
Normal file
10
crates/moonhare_log/Cargo.toml
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "moonhare_log"
|
||||
edition = "2024"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
log = "*"
|
||||
fern = { version = "0.7", features = ["colored"] }
|
||||
humantime = "*"
|
||||
2
crates/moonhare_log/README.md
Normal file
2
crates/moonhare_log/README.md
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
# MoonhareLog
|
||||
This crate provides a Wrapper around the log and fern crates for logging in the Moonhare Game Engine.
|
||||
86
crates/moonhare_log/src/lib.rs
Normal file
86
crates/moonhare_log/src/lib.rs
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
/// Wrapper around `log` and `fern` crates
|
||||
///
|
||||
use std::{fmt::Display, io, time::SystemTime};
|
||||
|
||||
/// Configures the Log Output Settings
|
||||
pub fn configere_logger() -> Result<(), fern::InitError>{
|
||||
let base_config = fern::Dispatch::new();
|
||||
|
||||
// configure colors for the whole line
|
||||
let colors_line = fern::colors::ColoredLevelConfig::new()
|
||||
.error(fern::colors::Color::Red)
|
||||
.warn(fern::colors::Color::Yellow)
|
||||
// we actually don't need to specify the color for debug and info, they are white by default
|
||||
.info(fern::colors::Color::TrueColor { r: 85, g: 85, b: 85 })
|
||||
.debug(fern::colors::Color::White)
|
||||
// depending on the terminals color scheme, this is the same as the background color
|
||||
.trace(fern::colors::Color::Black);
|
||||
|
||||
// Separate file config so we can include year, month and day in file logs
|
||||
let file_config = fern::Dispatch::new()
|
||||
.format(|out, message, record| {
|
||||
out.finish(format_args!(
|
||||
"[{} {} {}] {}",
|
||||
humantime::format_rfc3339_seconds(SystemTime::now()),
|
||||
record.level(),
|
||||
record.target(),
|
||||
message
|
||||
))
|
||||
})
|
||||
.chain(fern::log_file("moonhare_engine.log")?);
|
||||
|
||||
|
||||
let stdout_config = fern::Dispatch::new()
|
||||
.format(move |out, message, record| {
|
||||
// special format for debug messages coming from our own crate.
|
||||
if record.level() > log::LevelFilter::Info && record.target() == "cmd_program" {
|
||||
out.finish(format_args!(
|
||||
"DEBUG @ {}: {}",
|
||||
humantime::format_rfc3339_seconds(SystemTime::now()),
|
||||
message
|
||||
))
|
||||
} else {
|
||||
out.finish(format_args!(
|
||||
"{color_line}[{date} {level} {target}] {message}\x1B[0m",
|
||||
color_line = format_args!(
|
||||
"\x1B[{}m",
|
||||
colors_line.get_color(&record.level()).to_fg_str()
|
||||
),
|
||||
date = humantime::format_rfc3339_seconds(SystemTime::now()),
|
||||
level = record.level(),
|
||||
target = record.target(),
|
||||
message = message
|
||||
))
|
||||
}
|
||||
})
|
||||
.chain(io::stdout());
|
||||
|
||||
base_config
|
||||
.chain(file_config)
|
||||
.chain(stdout_config)
|
||||
.apply()?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn info<T: Display>(arg: T) {
|
||||
log::info!("{}", arg);
|
||||
}
|
||||
|
||||
pub fn warn<T: Display>(arg: T) {
|
||||
log::warn!("{}", arg);
|
||||
}
|
||||
|
||||
pub fn debug<T: Display>(arg: T) {
|
||||
log::debug!("{}", arg);
|
||||
}
|
||||
|
||||
pub fn trace<T: Display>(arg: T) {
|
||||
log::trace!("{}", arg);
|
||||
}
|
||||
|
||||
pub fn error<T: Display>(arg: T) {
|
||||
log::error!("{}", arg);
|
||||
}
|
||||
|
||||
|
||||
8
crates/moonhare_window/Cargo.toml
Normal file
8
crates/moonhare_window/Cargo.toml
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "moonhare_window"
|
||||
edition = "2024"
|
||||
version.workspace = true
|
||||
authors.workspace = true
|
||||
|
||||
[dependencies]
|
||||
winit = "0.30.12"
|
||||
1
crates/moonhare_window/README.md
Normal file
1
crates/moonhare_window/README.md
Normal file
|
|
@ -0,0 +1 @@
|
|||
# MoonhareWindow
|
||||
2
crates/moonhare_window/src/lib.rs
Normal file
2
crates/moonhare_window/src/lib.rs
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
pub mod window_config;
|
||||
pub mod winit_window;
|
||||
55
crates/moonhare_window/src/window_config.rs
Normal file
55
crates/moonhare_window/src/window_config.rs
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
use std::{any::Any, string};
|
||||
|
||||
/// General Config for [`WinitWindow`](crate::winit::winit_window::WinitWindow)
|
||||
pub struct WindowConfig {
|
||||
pub title: String,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub visble: bool,
|
||||
pub decorations: bool,
|
||||
}
|
||||
|
||||
impl Default for WindowConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
title: "Moonhare Engine".to_owned(),
|
||||
width: 1280,
|
||||
height: 720,
|
||||
visble: default_visibility(),
|
||||
decorations: default_decorations(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Todo: Set Functions should be inside WinitWindow i guess?
|
||||
impl WindowConfig {
|
||||
fn set_window_name(mut self, name: String) {
|
||||
self.title = name;
|
||||
}
|
||||
|
||||
fn set_window_height(mut self, new_height: u32) {
|
||||
self.height = new_height;
|
||||
}
|
||||
|
||||
fn set_window_width(mut self, new_width: u32) {
|
||||
self.width = new_width;
|
||||
}
|
||||
|
||||
fn set_window_visible(mut self, visible: bool) {
|
||||
self.visble = visible;
|
||||
}
|
||||
|
||||
fn set_window_decoration(mut self, decoration: bool) {
|
||||
self.decorations = decoration;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
fn default_visibility() -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn default_decorations() -> bool {
|
||||
true
|
||||
}
|
||||
54
crates/moonhare_window/src/winit_window.rs
Normal file
54
crates/moonhare_window/src/winit_window.rs
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
use std::error::Error;
|
||||
|
||||
use winit::{application::ApplicationHandler, dpi::LogicalSize, event::WindowEvent, event_loop::{ActiveEventLoop, EventLoop}, window::{Window, WindowAttributes, WindowId}};
|
||||
|
||||
use crate::window_config::WindowConfig;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct WinitWindow {
|
||||
pub window: Option<Window>,
|
||||
}
|
||||
|
||||
impl WinitWindow {
|
||||
pub fn create_window(&mut self, event_loop: &ActiveEventLoop, config: WindowConfig) -> WinitWindow {
|
||||
let mut window_attributes = WindowAttributes::default()
|
||||
.with_title(config.title);
|
||||
|
||||
let logical_size = LogicalSize::new(config.width, config.height);
|
||||
window_attributes = window_attributes.with_inner_size(logical_size);
|
||||
window_attributes = window_attributes.with_max_inner_size(logical_size);
|
||||
|
||||
// Set Visible
|
||||
window_attributes = window_attributes.with_visible(config.visble);
|
||||
window_attributes = window_attributes.with_decorations(config.decorations);
|
||||
|
||||
let window = event_loop.create_window(window_attributes).unwrap();
|
||||
|
||||
Self { window: Some(window) }
|
||||
}
|
||||
}
|
||||
|
||||
impl ApplicationHandler for WinitWindow {
|
||||
fn resumed(&mut self, event_loop: &ActiveEventLoop) {
|
||||
}
|
||||
|
||||
fn window_event(
|
||||
&mut self,
|
||||
event_loop: &ActiveEventLoop,
|
||||
window_id: winit::window::WindowId,
|
||||
event: winit::event::WindowEvent,
|
||||
) {
|
||||
match event {
|
||||
WindowEvent::CloseRequested => {
|
||||
event_loop.exit();
|
||||
},
|
||||
WindowEvent::RedrawRequested => {
|
||||
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue