Skip to content

Commit

Permalink
feat: Renderer Trait, enum and builder fn
Browse files Browse the repository at this point in the history
Fixes #12
  • Loading branch information
suspistew committed Mar 10, 2021
1 parent bfb9e42 commit c295e02
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 4 deletions.
13 changes: 11 additions & 2 deletions examples/hello-world/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
use scion::application::Scion;
use scion::legion::system;
use scion::legion::{system, Resources, World};
use scion::utils::time::Time;
use log::info;
use scion::utils::window::WindowDimensions;
use scion::config::scion_config::{ScionConfig, ScionConfigBuilder};
use scion::config::window_config::{WindowConfig, WindowConfigBuilder};
use scion::renderer::{RendererType, ScionRenderer};
use miniquad::Context;

struct T;
impl ScionRenderer for T{
fn draw(&mut self, context: &mut Context, world: &mut World, resource: &mut Resources) {
unimplemented!()
}
}

#[system]
fn time(#[resource] time: &Time) {
Expand All @@ -21,5 +29,6 @@ fn main() {
Scion::app()
.with_system(time_system())
.with_system(screen_system())
.with_renderer(RendererType::Custom(Box::new(T)))
.run();
}
}
10 changes: 9 additions & 1 deletion src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use miniquad::{conf, Context, EventHandlerFree, UserData};
use crate::config::scion_config::{ScionConfig, ScionConfigReader};
use crate::utils::time::Time;
use crate::utils::window::WindowDimensions;
use crate::renderer::RendererType;

/// `Scion` is the entry point of any application made with Scion engine.
pub struct Scion {
Expand Down Expand Up @@ -68,13 +69,15 @@ impl Scion {
pub struct ScionBuilder {
config: ScionConfig,
schedule_builder: Builder,
renderer: RendererType
}

impl ScionBuilder {
fn new(config: ScionConfig) -> Self {
Self {
config,
schedule_builder: Default::default(),
renderer: Default::default()
}
}

Expand All @@ -88,14 +91,19 @@ impl ScionBuilder {
self
}

fn with_thread_local_fn<F: FnMut(&mut World, &mut Resources) + 'static>(
pub fn with_thread_local_fn<F: FnMut(&mut World, &mut Resources) + 'static>(
mut self,
function: F,
) -> Self {
self.schedule_builder.add_thread_local_fn(function);
self
}

pub fn with_renderer(mut self, renderer_type: RendererType) -> Self{
self.renderer = renderer_type;
self
}

/// Builds, setups and runs the Scion application
pub fn run(mut self) {
let scion = Scion {
Expand Down
2 changes: 1 addition & 1 deletion src/config/scion_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Default for ScionConfig {
Self {
app_name: "Scion game".to_string(),
logger_config: Some(Default::default()),
window_config: Some(Default::default())
window_config: Some(Default::default()),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub use legion;
pub mod application;
pub mod config;
pub mod utils;
pub mod renderer;
20 changes: 20 additions & 0 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use miniquad::Context;
use legion::{World, Resources};

/// Trait to implement in order to create a renderer to use in a `Scion` application
pub trait ScionRenderer {
/// The draw method is called each frame
fn draw(&mut self, context: &mut Context, world: &mut World, resource: &mut Resources);
}

/// Type of renderer to use to render the game.
pub enum RendererType {
Scion2D,
Custom(Box<dyn ScionRenderer>)
}

impl Default for RendererType{
fn default() -> Self {
RendererType::Scion2D
}
}

0 comments on commit c295e02

Please # to comment.