Skip to content

Commit

Permalink
feat: configurable color to the clear color
Browse files Browse the repository at this point in the history
FIXES #59
  • Loading branch information
suspistew committed Jun 9, 2021
1 parent fffecb8 commit 5402b47
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 16 deletions.
19 changes: 19 additions & 0 deletions examples/bomberman/main.rs
Original file line number Diff line number Diff line change
@@ -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();
}
1 change: 0 additions & 1 deletion examples/taquin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use scion::{
config::{scion_config::ScionConfigBuilder, window_config::WindowConfigBuilder},
core::{
components::{
material::Material,
maths::{
camera::Camera,
transform::{Coordinates, Transform},
Expand Down
1 change: 0 additions & 1 deletion examples/tetris/layer.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
1 change: 1 addition & 0 deletions src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions src/config/window_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Color>,
}

impl Default for WindowConfig {
Expand All @@ -51,6 +54,7 @@ impl Default for WindowConfig {
maximized: false,
resizable: true,
transparent: false,
default_background_color: None
}
}
}
Expand All @@ -76,6 +80,11 @@ impl WindowConfigBuilder {
self
}

pub fn with_default_background_color(mut self, color: Option<Color>) -> Self {
self.config.default_background_color = color;
self
}

pub fn get(self) -> WindowConfig {
self.config
}
Expand Down
5 changes: 3 additions & 2 deletions src/core/components/color.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -61,4 +62,4 @@ impl Into<GlColor> for &Color {
a: self.a,
}
}
}
}
4 changes: 3 additions & 1 deletion src/core/event_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::{
},
rendering::renderer_state::RendererState,
};
use crate::config::scion_config::ScionConfig;

pub(crate) fn handle_event(
event: Event<()>,
Expand All @@ -23,6 +24,7 @@ pub(crate) fn handle_event(
renderer: &mut RendererState,
world: &mut World,
resources: &mut Resources,
config: &ScionConfig
) {
match event {
Event::WindowEvent {
Expand Down Expand Up @@ -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),
}
Expand Down
28 changes: 20 additions & 8 deletions src/rendering/bidimensional/scion2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
});
}
Expand Down Expand Up @@ -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,
},
}
Expand Down
3 changes: 2 additions & 1 deletion src/rendering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
Expand Down
5 changes: 3 additions & 2 deletions src/rendering/renderer_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -80,15 +81,15 @@ 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
.create_command_encoder(&wgpu::CommandEncoderDescriptor {
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(())
Expand Down

0 comments on commit 5402b47

Please # to comment.