diff --git a/examples/bomberman/main.rs b/examples/bomberman/main.rs new file mode 100644 index 0000000..5c0211d --- /dev/null +++ b/examples/bomberman/main.rs @@ -0,0 +1,19 @@ +use scion::Scion; +use scion::config::scion_config::ScionConfigBuilder; +use scion::config::window_config::WindowConfigBuilder; +use scion::core::components::color::Color; + +fn main() { + Scion::app_with_config( + ScionConfigBuilder::new() + .with_window_config( + WindowConfigBuilder::new() + .with_resizable(false) + .with_dimensions((768, 768)) + .with_default_background_color(Some(Color::new_rgb(150, 100, 0))) + .get(), + ) + .get(), + ) + .run(); +} \ No newline at end of file diff --git a/examples/taquin/main.rs b/examples/taquin/main.rs index 7e2af2f..512660c 100644 --- a/examples/taquin/main.rs +++ b/examples/taquin/main.rs @@ -2,7 +2,6 @@ use scion::{ config::{scion_config::ScionConfigBuilder, window_config::WindowConfigBuilder}, core::{ components::{ - material::Material, maths::{ camera::Camera, transform::{Coordinates, Transform}, diff --git a/examples/tetris/layer.rs b/examples/tetris/layer.rs index 64fb5ac..d2d9d85 100644 --- a/examples/tetris/layer.rs +++ b/examples/tetris/layer.rs @@ -1,7 +1,6 @@ use scion::{ core::{ components::{ - material::Material, maths::{camera::Camera, transform::Transform}, tiles::tileset::Tileset, ui::{font::Font, ui_image::UiImage, ui_text::UiText}, diff --git a/src/application.rs b/src/application.rs index 41c3b8a..8e6ab99 100644 --- a/src/application.rs +++ b/src/application.rs @@ -132,6 +132,7 @@ impl Scion { .expect("A renderer is mandatory to run this game !"), &mut self.world, &mut self.resources, + &self.config ); self.next_frame(); self.layer_machine.apply_layers_action( diff --git a/src/config/window_config.rs b/src/config/window_config.rs index 5c304f9..b7d15ea 100644 --- a/src/config/window_config.rs +++ b/src/config/window_config.rs @@ -5,6 +5,7 @@ use winit::{ dpi::Size, window::{WindowAttributes, WindowBuilder}, }; +use crate::core::components::color::Color; /// Main configuration for the game window /// Please use [`WindowConfigBuilder`] if you want to build if from code. @@ -34,6 +35,8 @@ pub struct WindowConfig { pub(crate) resizable: bool, /// If the window should be able to be transparent. pub(crate) transparent: bool, + /// Default background color of each frame in the window + pub(crate) default_background_color: Option, } impl Default for WindowConfig { @@ -51,6 +54,7 @@ impl Default for WindowConfig { maximized: false, resizable: true, transparent: false, + default_background_color: None } } } @@ -76,6 +80,11 @@ impl WindowConfigBuilder { self } + pub fn with_default_background_color(mut self, color: Option) -> Self { + self.config.default_background_color = color; + self + } + pub fn get(self) -> WindowConfig { self.config } diff --git a/src/core/components/color.rs b/src/core/components/color.rs index d4a6090..bd16ede 100644 --- a/src/core/components/color.rs +++ b/src/core/components/color.rs @@ -1,7 +1,8 @@ use crate::rendering::bidimensional::gl_representations::GlColor; +use serde::{Deserialize, Serialize}; /// A struct that represents colors for rendering. -#[derive(Clone)] +#[derive(Clone, Debug, Serialize, Deserialize)] pub struct Color { /// Red value of the color r: u8, @@ -61,4 +62,4 @@ impl Into for &Color { a: self.a, } } -} +} \ No newline at end of file diff --git a/src/core/event_handler.rs b/src/core/event_handler.rs index 2aff1ca..e45d44f 100644 --- a/src/core/event_handler.rs +++ b/src/core/event_handler.rs @@ -15,6 +15,7 @@ use crate::{ }, rendering::renderer_state::RendererState, }; +use crate::config::scion_config::ScionConfig; pub(crate) fn handle_event( event: Event<()>, @@ -23,6 +24,7 @@ pub(crate) fn handle_event( renderer: &mut RendererState, world: &mut World, resources: &mut Resources, + config: &ScionConfig ) { match event { Event::WindowEvent { @@ -65,7 +67,7 @@ pub(crate) fn handle_event( } Event::RedrawRequested(_) => { renderer.update(world, resources); - match renderer.render(world) { + match renderer.render(world, config) { Ok(_) => {} Err(e) => log::error!("{:?}", e), } diff --git a/src/rendering/bidimensional/scion2d.rs b/src/rendering/bidimensional/scion2d.rs index 23819b3..05ac1c5 100644 --- a/src/rendering/bidimensional/scion2d.rs +++ b/src/rendering/bidimensional/scion2d.rs @@ -22,6 +22,7 @@ use crate::{ ScionRenderer, }, }; +use crate::config::scion_config::ScionConfig; pub(crate) trait Renderable2D { fn vertex_buffer_descriptor(&mut self, material: Option<&Material>) -> BufferInitDescriptor; @@ -81,13 +82,14 @@ impl ScionRenderer for Scion2D { fn render( &mut self, world: &mut World, + config: &ScionConfig, frame: &SwapChainTexture, encoder: &mut CommandEncoder, ) { { encoder.begin_render_pass(&wgpu::RenderPassDescriptor { label: Some("Scion 2D Render Pass"), - color_attachments: &[get_default_color_attachment(frame)], + color_attachments: &[get_default_color_attachment(frame, config)], depth_stencil_attachment: None, }); } @@ -245,17 +247,27 @@ fn create_transform_uniform_bind_group( ) } -fn get_default_color_attachment(frame: &SwapChainTexture) -> RenderPassColorAttachment { +fn get_default_color_attachment<'a>(frame: &'a SwapChainTexture, config: &'a ScionConfig,) -> RenderPassColorAttachment<'a> { RenderPassColorAttachment { view: &frame.view, resolve_target: None, ops: wgpu::Operations { - load: wgpu::LoadOp::Clear(wgpu::Color { - r: 1., - g: 0., - b: 0., - a: 1.0, - }), + load: wgpu::LoadOp::Clear( + if let Some(color) = &config.window_config.as_ref().expect("Window config is missing").default_background_color { + wgpu::Color { + r: (color.red() as f32 / 255.) as f64, + g: (color.green() as f32 / 255.) as f64, + b: (color.blue() as f32 / 255.) as f64, + a: color.alpha() as f64, + } + }else{ + wgpu::Color { + r: 1., + g: 0., + b: 0., + a: 1.0, + } + }), store: true, }, } diff --git a/src/rendering/mod.rs b/src/rendering/mod.rs index ffc3af6..e5e72df 100644 --- a/src/rendering/mod.rs +++ b/src/rendering/mod.rs @@ -3,6 +3,7 @@ use legion::{Resources, World}; use wgpu::{CommandEncoder, Device, Queue, SwapChainDescriptor, SwapChainTexture}; use crate::rendering::bidimensional::scion2d::Scion2D; +use crate::config::scion_config::ScionConfig; pub mod bidimensional; pub(crate) mod renderer_state; @@ -21,7 +22,7 @@ pub trait ScionRenderer { ); /// Will be called after render, each time the window request redraw. - fn render(&mut self, world: &mut World, frame: &SwapChainTexture, encoder: &mut CommandEncoder); + fn render(&mut self, world: &mut World, config: &ScionConfig, frame: &SwapChainTexture, encoder: &mut CommandEncoder); } /// Type of renderer to use to render the game. diff --git a/src/rendering/renderer_state.rs b/src/rendering/renderer_state.rs index e518baa..0b117e9 100644 --- a/src/rendering/renderer_state.rs +++ b/src/rendering/renderer_state.rs @@ -2,6 +2,7 @@ use legion::{Resources, World}; use winit::{event::WindowEvent, window::Window}; use crate::rendering::ScionRenderer; +use crate::config::scion_config::ScionConfig; pub(crate) struct RendererState { surface: wgpu::Surface, @@ -80,7 +81,7 @@ impl RendererState { ); } - pub(crate) fn render(&mut self, world: &mut World) -> Result<(), wgpu::SwapChainError> { + pub(crate) fn render(&mut self, world: &mut World, config: &ScionConfig,) -> Result<(), wgpu::SwapChainError> { let frame = self.swap_chain.get_current_frame()?.output; let mut encoder = self .device @@ -88,7 +89,7 @@ impl RendererState { label: Some("Render Encoder"), }); - self.scion_renderer.render(world, &frame, &mut encoder); + self.scion_renderer.render(world, config, &frame, &mut encoder); self.queue.submit(std::iter::once(encoder.finish())); Ok(())