Skip to content

Commit

Permalink
Added more opacity parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ecton committed Dec 13, 2023
1 parent 73a5936 commit 2fffa61
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/batching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn main() -> Result<(), EventLoopError> {
Point::new(texture_size.width * x, texture_size.height * y),
texture_size,
),
1.,
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/lazy-texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ where
_window: Window<'_>,
graphics: &mut RenderingGraphics<'_, 'pass>,
) -> bool {
self.rendering.render(graphics);
self.rendering.render(1., graphics);
self.keep_running
}
}
Expand Down
37 changes: 26 additions & 11 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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> {
Expand Down Expand Up @@ -65,31 +67,40 @@ impl<'render, 'gfx> Renderer<'render, 'gfx> {
}

/// Draws `texture` at `destination`, scaling as necessary.
pub fn draw_texture<Unit>(&mut self, texture: &impl TextureSource, destination: Rect<Unit>)
where
pub fn draw_texture<Unit>(
&mut self,
texture: &impl TextureSource,
destination: Rect<Unit>,
opacity: f32,
) where
Unit: figures::Unit + ScreenUnit + ShaderScalable,
i32: From<<Unit as IntoSigned>::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<Unit>(&mut self, texture: &impl TextureSource, destination: Point<Unit>)
where
pub fn draw_texture_at<Unit>(
&mut self,
texture: &impl TextureSource,
destination: Point<Unit>,
opacity: f32,
) where
Unit: figures::Unit + ScreenUnit + ShaderScalable,
i32: From<<Unit as IntoSigned>::Signed>,
{
let texture_rect = texture.default_rect();
let scaled_size = Size::<Unit>::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,
);
}
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -636,6 +649,7 @@ impl Drawing {
graphics,
clip_index: 0,
data: self,
opacity: 1.,
}
}

Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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?
Expand Down

0 comments on commit 2fffa61

Please sign in to comment.