From 2fffa618dda72fae415b2d25faf9dfe461662246 Mon Sep 17 00:00:00 2001 From: Jonathan Johnson Date: Wed, 13 Dec 2023 14:02:14 -0800 Subject: [PATCH] Added more opacity parameters --- examples/batching.rs | 1 + examples/lazy-texture.rs | 2 +- src/app.rs | 2 +- src/render.rs | 37 ++++++++++++++++++++++++++----------- src/tilemap.rs | 4 ++-- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/examples/batching.rs b/examples/batching.rs index d6bfd8185..fc46288a0 100644 --- a/examples/batching.rs +++ b/examples/batching.rs @@ -37,6 +37,7 @@ fn main() -> Result<(), EventLoopError> { Point::new(texture_size.width * x, texture_size.height * y), texture_size, ), + 1., ); } } diff --git a/examples/lazy-texture.rs b/examples/lazy-texture.rs index ef634ff3b..19fc5634e 100644 --- a/examples/lazy-texture.rs +++ b/examples/lazy-texture.rs @@ -8,7 +8,7 @@ fn main() -> Result<(), EventLoopError> { wgpu::FilterMode::Linear, ); kludgine::app::run(move |mut renderer, _window| { - renderer.draw_texture_at(&texture, Point::inches(1, 1)); + renderer.draw_texture_at(&texture, Point::inches(1, 1), 1.); true }) } diff --git a/src/app.rs b/src/app.rs index 04e9c5ca4..3a00d5430 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1232,7 +1232,7 @@ where _window: Window<'_>, graphics: &mut RenderingGraphics<'_, 'pass>, ) -> bool { - self.rendering.render(graphics); + self.rendering.render(1., graphics); self.keep_running } } diff --git a/src/render.rs b/src/render.rs index e9ffc3f09..5923f4bff 100644 --- a/src/render.rs +++ b/src/render.rs @@ -13,8 +13,9 @@ use crate::pipeline::{ }; use crate::shapes::Shape; use crate::{ - sealed, Assert, ClipGuard, ClipRect, Clipped, Color, DefaultHasher, Drawable, Graphics, - RenderingGraphics, ShapeSource, Texture, TextureBlit, TextureSource, VertexCollection, + sealed, Assert, ClipGuard, ClipRect, Clipped, Color, DefaultHasher, Drawable, DrawableExt, + Graphics, RenderingGraphics, ShapeSource, Texture, TextureBlit, TextureSource, + VertexCollection, }; /// An easy-to-use graphics renderer that batches operations on the GPU @@ -29,6 +30,7 @@ pub struct Renderer<'render, 'gfx> { pub(crate) graphics: &'render mut Graphics<'gfx>, data: &'render mut Drawing, clip_index: u32, + opacity: f32, } impl<'gfx> Deref for Renderer<'_, 'gfx> { @@ -65,31 +67,40 @@ impl<'render, 'gfx> Renderer<'render, 'gfx> { } /// Draws `texture` at `destination`, scaling as necessary. - pub fn draw_texture(&mut self, texture: &impl TextureSource, destination: Rect) - where + pub fn draw_texture( + &mut self, + texture: &impl TextureSource, + destination: Rect, + opacity: f32, + ) where Unit: figures::Unit + ScreenUnit + ShaderScalable, i32: From<::Signed>, { self.draw_textured_shape( - &TextureBlit::new(texture.default_rect(), destination, Color::WHITE), + TextureBlit::new(texture.default_rect(), destination, Color::WHITE).opacity(opacity), texture, ); } /// Draws `texture` at `destination`. - pub fn draw_texture_at(&mut self, texture: &impl TextureSource, destination: Point) - where + pub fn draw_texture_at( + &mut self, + texture: &impl TextureSource, + destination: Point, + opacity: f32, + ) where Unit: figures::Unit + ScreenUnit + ShaderScalable, i32: From<::Signed>, { let texture_rect = texture.default_rect(); let scaled_size = Size::::from_upx(texture_rect.size, self.scale); self.draw_textured_shape( - &TextureBlit::new( + TextureBlit::new( texture_rect, Rect::new(destination, scaled_size), Color::WHITE, - ), + ) + .opacity(opacity), texture, ); } @@ -167,7 +178,9 @@ impl<'render, 'gfx> Renderer<'render, 'gfx> { flags, scale, rotation, - opacity: shape.opacity.unwrap_or(1.), + opacity: shape + .opacity + .map_or(self.opacity, |opacity| opacity * self.opacity), translation: shape .translation .into_px(self.graphics.scale()) @@ -636,6 +649,7 @@ impl Drawing { graphics, clip_index: 0, data: self, + opacity: 1., } } @@ -648,7 +662,7 @@ impl Drawing { } /// Renders the prepared graphics from the last frame. - pub fn render<'pass>(&'pass self, graphics: &mut RenderingGraphics<'_, 'pass>) { + pub fn render<'pass>(&'pass self, opacity: f32, graphics: &mut RenderingGraphics<'_, 'pass>) { if let Some(buffers) = &self.buffers { let mut current_texture_id = None; let mut needs_texture_binding = graphics.active_pipeline_if_needed(); @@ -698,6 +712,7 @@ impl Drawing { } let mut constants = command.constants; + constants.opacity *= opacity; constants.translation += current_clip.origin.into_signed().map(Px::into_unscaled); if !constants.translation.is_zero() { constants.flags |= FLAG_TRANSLATE; diff --git a/src/tilemap.rs b/src/tilemap.rs index 381ce7255..dea711f10 100644 --- a/src/tilemap.rs +++ b/src/tilemap.rs @@ -402,7 +402,7 @@ impl TileKind { TileKind::Texture(texture) => { // TODO support other scaling options like // aspect-fit rather than fill. - context.draw_texture(texture, tile_rect); + context.draw_texture(texture, tile_rect, 1.); None } TileKind::Color(color) => { @@ -411,7 +411,7 @@ impl TileKind { } TileKind::Sprite(sprite) => { if let Ok(frame) = sprite.get_frame(Some(context.elapsed())) { - context.draw_texture(&frame, tile_rect); + context.draw_texture(&frame, tile_rect, 1.); sprite.remaining_frame_duration().ok().flatten() } else { // TODO show a broken image?