Skip to content

Commit

Permalink
feat: addition to the rendering trait and banner
Browse files Browse the repository at this point in the history
  • Loading branch information
suspistew committed Mar 11, 2021
1 parent 2417b3e commit 0d2cf27
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 16 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# scion
Scion is a minimalist, **easy** to use, game engine built on top of legion and miniquad.
<img src="repo/banner.png" alt="Scion Engine" />

Scion is a minimalist, **easy** to use, modulable game engine built on top of legion and miniquad.




8 changes: 4 additions & 4 deletions examples/hello-world/main.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use scion::application::Scion;
use scion::legion::{system, Resources, World};
use scion::utils::time::Time;
use log::info;
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) {
fn draw(&mut self, _context: &mut Context, _world: &mut World, _resource: &mut Resources) {
unimplemented!()
}
}
Expand Down
24 changes: 16 additions & 8 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@ 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;
use crate::renderer::{RendererType, ScionRenderer};

use crate::renderer::bidimensional::triangle::Triangle;

/// `Scion` is the entry point of any application made with Scion engine.
pub struct Scion {
config: ScionConfig,
world: World,
resources: Resources,
schedule: Schedule,
context: Option<Context>
context: Option<Context>,
renderer: Box<dyn ScionRenderer>,
}

impl EventHandlerFree for Scion {
Expand All @@ -23,9 +26,11 @@ impl EventHandlerFree for Scion {
}

fn draw(&mut self) {
self.context.as_mut().expect("Miniquad context is mandatory to use the eventHandlerFree")
.clear(Some((0., 1., 1., 1.)), None, None);
self.renderer.draw(
self.context.as_mut().expect("Miniquad context is mandatory"),
&mut self.world, &mut self.resources)
}

fn resize_event(&mut self, w: f32, h: f32) {
self.resources
.get_mut::<WindowDimensions>().expect("Missing Screen Dimension Resource. Did something deleted it ?").set(w, h);
Expand All @@ -51,18 +56,19 @@ impl Scion {
ScionBuilder::new(app_config)
}

fn setup(mut self, context: Context ) -> Self{
fn setup(mut self, context: Context) -> Self {
let screen_size = context.screen_size();
self.context = Some(context);
self.resources.insert(Time::default());

self.resources.insert(WindowDimensions::new(screen_size));
self.world.push((Triangle,));
self
}

fn next_frame(&mut self) {
self.resources.get_mut::<Time>().expect("Time is an internal resource and can't be missing").frame();
self.schedule.execute(&mut self.world, &mut self.resources);
self.resources.get_mut::<Time>().expect("Time is an internal resource and can't be missing").frame();
self.schedule.execute(&mut self.world, &mut self.resources);
}
}

Expand Down Expand Up @@ -111,10 +117,12 @@ impl ScionBuilder {
world: Default::default(),
resources: Default::default(),
schedule: self.schedule_builder.build(),
context: None
context: None,
renderer: self.renderer.into_boxed_renderer()
};

let mut miniquad_conf = conf::Conf::default();
miniquad_conf.high_dpi=true;
if let Some(window_config) = scion.config.window_config.as_ref() {
miniquad_conf.fullscreen = window_config.fullscreen;
}
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 @@ -90,7 +90,7 @@ mod tests {
fn test_read_scion_toml() {
// Delete scion.toml before the test
let path = Path::new("Scion.toml");
std::fs::remove_file(path);
let _r = std::fs::remove_file(path);

let config = ScionConfigReader::read_or_create_scion_toml();
assert!(config.is_ok());
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ pub use legion;
pub mod application;
pub mod config;
pub mod utils;
pub mod renderer;
pub mod renderer;
27 changes: 27 additions & 0 deletions src/renderer/bidimensional/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
pub mod renderer;
pub mod triangle;

#[repr(C)]
pub struct Vec2 {
x: f32,
y: f32,
}

#[repr(C)]
pub struct Vec4 {
r: f32,
g: f32,
b: f32,
a: f32,
}

#[repr(C)]
pub struct Vertex {
pos: Vec2,
color: Vec4,
}

#[repr(C)]
pub struct Uniforms {
pub offset: (f32, f32),
}
16 changes: 16 additions & 0 deletions src/renderer/bidimensional/renderer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
use crate::renderer::{ScionRenderer, Renderable2D};
use miniquad::Context;
use legion::{Resources, World, Entity, IntoQuery};
use crate::renderer::bidimensional::triangle::Triangle;


pub struct Scion2D;

impl ScionRenderer for Scion2D {
fn draw(&mut self, context: &mut Context, world: &mut World, _resource: &mut Resources) {
let mut query_triangles = <(Entity, &Triangle)>::query();
query_triangles.for_each(world,|_triangle|{
Triangle::render(context)
});
}
}
87 changes: 87 additions & 0 deletions src/renderer/bidimensional/triangle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::renderer::Renderable2D;
use miniquad::{Context, Buffer, BufferType, Bindings, Shader, Pipeline, BufferLayout, VertexAttribute, VertexFormat };
use crate::renderer::bidimensional::{Vertex, Vec2, Uniforms, Vec4};

pub struct Triangle;

impl Renderable2D for Triangle {
fn render(context: &mut Context) {
let vertices: [Vertex; 3] = [
Vertex { pos: Vec2 { x: -0.5, y: -0.5 }, color: Vec4 { r: 1.0, g: 0., b: 0., a: 1.0, }, },
Vertex { pos: Vec2 { x: 0.5, y: -0.5 }, color: Vec4 { r: 0., g: 1., b: 0., a: 1.0 } },
Vertex { pos: Vec2 { x: 0., y: 0.5 }, color: Vec4 { r: 0., g: 0., b: 1., a: 1.0 } },
];
let vertex_buffer = Buffer::immutable(context, BufferType::VertexBuffer, &vertices);

let indices: [u16; 6] = [0, 1, 2, 0, 2, 3];
let index_buffer = Buffer::immutable(context, BufferType::IndexBuffer, &indices);

let bindings = Bindings {
vertex_buffers: vec![vertex_buffer],
index_buffer,
images: vec![],
};

let shader = Shader::new(context, shader::VERTEX, shader::FRAGMENT, shader::meta()).unwrap();

let pipeline = Pipeline::new(
context,
&[BufferLayout::default()],
&[
VertexAttribute::new("pos", VertexFormat::Float2),
VertexAttribute::new("color", VertexFormat::Float4),
],
shader,
);

context.begin_default_pass(Default::default());

context.apply_pipeline(&pipeline);
context.apply_bindings(&bindings);

context.apply_uniforms(&Uniforms {
offset: (0., 0.),
});

context.draw(0, 3, 1);
context.end_render_pass();

context.commit_frame();
}
}

mod shader {
use miniquad::*;

pub const VERTEX: &str =
r#"
#version 330 core
in vec2 pos;
in vec4 color;
uniform vec2 offset;
out lowp vec4 color_lowp;
void main() {
gl_Position = vec4(pos + offset, 0, 1);
color_lowp = color;
}
"#;

pub const FRAGMENT: &str =
r#"
#version 330 core
in lowp vec4 color_lowp;
out vec4 FragColor;
void main() {
FragColor = color_lowp;
}
"#;

pub fn meta() -> ShaderMeta {
ShaderMeta {
images: vec![],
uniforms: UniformBlockLayout {
uniforms: vec![UniformDesc::new("offset", UniformType::Float2)],
},
}
}
}
16 changes: 16 additions & 0 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
pub mod bidimensional;
use log::*;
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);
}

pub trait Renderable2D {
fn render(context: &mut Context);
}

/// Type of renderer to use to render the game.
pub enum RendererType {
Scion2D,
Expand All @@ -17,4 +24,13 @@ impl Default for RendererType{
fn default() -> Self {
RendererType::Scion2D
}
}

impl RendererType{
pub(crate) fn into_boxed_renderer(self) -> Box<dyn ScionRenderer>{
match self{
RendererType::Scion2D => { warn!("scion2S");Box::new(bidimensional::renderer::Scion2D) }
RendererType::Custom(boxed) => { warn!("Boxed"); boxed }
}
}
}

0 comments on commit 0d2cf27

Please # to comment.