Skip to content

Commit

Permalink
feat: Window config & replace the loop with winit loop
Browse files Browse the repository at this point in the history
  • Loading branch information
grzi committed Mar 6, 2021
1 parent c76126c commit 105de9a
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[dependencies]
legion = "0.4.0"
winit = "0.24.0"

serde = "1.0.124"
toml = "0.5.8"
32 changes: 27 additions & 5 deletions src/application.rs
Expand Up @@ -3,13 +3,17 @@ use crate::legion::systems::{ParallelRunnable, Runnable, Builder};
use std::thread;
use std::time::Duration;
use crate::config::scion_config::{ScionConfig, ScionConfigReader};
use winit::window::{Window, WindowBuilder};
use winit::event_loop::{EventLoop, ControlFlow};
use winit::event::{Event, WindowEvent};

/// `Scion` is the entry point of any application made with Scion engine.
pub struct Scion{
config: ScionConfig,
world: World,
resources: Resources,
schedule: Schedule
schedule: Schedule,
window: Option<Window>
}

impl Scion {
Expand All @@ -27,10 +31,27 @@ impl Scion {
}

fn run (mut self) {
loop {
let event_loop = EventLoop::new();
let window_builder: WindowBuilder = self.config.window_config.clone()
.expect("The window configuration has not been found").into();
self.window = Some(window_builder.build(&event_loop).expect(""));

event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll;
match event {
Event::WindowEvent {
event: WindowEvent::CloseRequested,
window_id,
} if window_id == self.window.as_ref().unwrap().id() => {
*control_flow = ControlFlow::Exit;
},
Event::MainEventsCleared => {
self.window.as_ref().unwrap().request_redraw();
}
_ => (),
}
self.schedule.execute(&mut self.world, &mut self.resources);
thread::sleep(Duration::from_secs(1));
}
});
}
}
pub struct ScionBuilder{
Expand Down Expand Up @@ -67,7 +88,8 @@ impl ScionBuilder{
config: self.config,
world: Default::default(),
resources: Default::default(),
schedule: self.schedule_builder.build()
schedule: self.schedule_builder.build(),
window: None
};
scion.setup();
scion.run();
Expand Down
3 changes: 2 additions & 1 deletion src/config/mod.rs
@@ -1 +1,2 @@
pub mod scion_config;
pub mod scion_config;
pub mod window_config;
18 changes: 15 additions & 3 deletions src/config/scion_config.rs
Expand Up @@ -2,12 +2,24 @@ use std::io::{Error, Read, Write, ErrorKind};
use std::path::Path;
use std::fs::File;
use serde::{Serialize, Deserialize};
use crate::config::window_config::WindowConfig;

/// Main configuration used by `crate::Scion` to configure the game.
#[derive(Default, Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct ScionConfig{
/// Name of the application, used for the window name in Windowed mode
pub(crate) app_name: String
/// Name of the application
pub(crate) app_name: String,
/// Configuration for the game window
pub(crate) window_config: Option<WindowConfig>
}

impl Default for ScionConfig{
fn default() -> Self {
Self{
app_name: "Scion game".to_string(),
window_config: Some(Default::default())
}
}
}

pub struct ScionConfigReader;
Expand Down
82 changes: 82 additions & 0 deletions src/config/window_config.rs
@@ -0,0 +1,82 @@
use std::path::PathBuf;
use winit::window::{WindowBuilder, WindowAttributes};
use winit::dpi::Size;
use serde::{Deserialize, Serialize};

/// Main configuration for the game window
#[derive(Debug, Serialize, Deserialize, Clone)]
pub(crate) struct WindowConfig {
/// Window title
pub title: String,
/// Enables fullscreen mode
#[serde(default)]
pub fullscreen: bool,
/// Default window width and height in pixels.
#[serde(default)]
pub dimensions: Option<(u32, u32)>,
/// Minimum window width and height in pixels.
#[serde(default)]
pub min_dimensions: Option<(u32, u32)>,
/// Maximum window width and height in pixels.
#[serde(default)]
pub max_dimensions: Option<(u32, u32)>,
/// Whether to display the window, Use full for loading
pub visibility: bool,
/// The path relative to the game executable of the window icon.
#[serde(default)]
pub icon: Option<PathBuf>,
/// Whether the window should always be on top of other windows.
#[serde(default)]
pub always_on_top: bool,
/// Whether the window should have borders and bars.
pub decorations: bool,
/// Whether the window should be maximized upon creation.
#[serde(default)]
pub maximized: bool,
/// If the user can resize the window
pub resizable: bool,
/// If the window should be able to be transparent.
#[serde(default)]
pub transparent: bool,
}

impl Default for WindowConfig {
fn default() -> Self {
Self {
title: "Default Scion game".to_string(),
fullscreen: false,
dimensions: Some((1024, 768)),
min_dimensions: Some((640, 480)),
max_dimensions: None,
visibility: true,
icon: None,
always_on_top: false,
decorations: true,
maximized: false,
resizable: true,
transparent: false
}
}
}

impl Into<WindowBuilder> for WindowConfig {
fn into(self) -> WindowBuilder {
let mut builder = WindowBuilder::new();

builder.window = WindowAttributes {
title: self.title.clone(),
fullscreen: None,
inner_size: self.dimensions.map(|d| d.into()).map(Size::Logical),
min_inner_size: self.min_dimensions.map(|d| d.into()).map(Size::Logical),
max_inner_size: self.max_dimensions.map(|d| d.into()).map(Size::Logical),
visible: self.visibility,
window_icon: None,
always_on_top: self.always_on_top,
decorations: self.decorations,
maximized: self.maximized,
resizable: self.resizable,
transparent: self.transparent,
};
builder
}
}

0 comments on commit 105de9a

Please sign in to comment.