Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Fix blood canvas blitting
Browse files Browse the repository at this point in the history
  • Loading branch information
darthdeus committed Jan 27, 2024
1 parent 092d434 commit e3e0564
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
with `get_sprite_culling`, e.g. if you wish to build an inspector window to control this.
- Added `enable_child_transforms` on `GameConfig` allowing the user to disable child/parent
transform update. If you have a lot of entities (10k+) but aren't using child transforms,
setting this to false may give you extra few percent of free performance. This is enabled by default.
setting this to false may give you extra few percent of free performance. This is enabled
by default.
- Added `flip_x/y` and `blood_canvas_blit_at_pro` which allows blitting sprites with arbitrary
flipping. Note that this also fixes a long standing bug when in some cases sprites would be
blitted flipped upside down.

# v0.3.0

Expand Down
18 changes: 12 additions & 6 deletions comfy-core/src/blood_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,18 @@ impl BloodCanvas {

for x in 0..rect.size.x {
for y in 0..rect.size.y {
let px = image.get_pixel(
(x + rect.offset.x) as u32,
(y + rect.offset.y) as u32,
);
let mut read_x = x + rect.offset.x;
let mut read_y = y + rect.offset.y;

if flip_x {
read_x = rect.offset.x + rect.size.x - x - 1;
}

if !flip_y {
read_y = rect.offset.y + rect.size.y - y - 1;
}

let px = image.get_pixel(read_x as u32, read_y as u32);

if px.0[3] > 0 {
let px_pos = position + vec2(x as f32, y as f32) / 16.0 -
Expand All @@ -214,8 +222,6 @@ impl BloodCanvas {
Into::<Color>::into(*px) * tint,
);
}

// self.set_pixel(px_pos, Into::<Color>::into(*px) * tint);
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions comfy-wgpu/src/blood_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ pub fn blood_canvas_blit_at(
);
}

pub fn blood_canvas_blit_at_pro(
texture: TextureHandle,
position: Vec2,
source_rect: Option<IRect>,
tint: Color,
flip_x: bool,
flip_y: bool,
) {
BLOOD_CANVAS.get().unwrap().borrow_mut().blit_at(
texture,
position,
source_rect,
tint,
flip_x,
flip_y,
);
}

pub fn blood_canvas_blit_at_sized(
texture: TextureHandle,
position: Vec2,
Expand Down
12 changes: 9 additions & 3 deletions comfy/examples/blood_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ fn update(_c: &mut EngineContext) {
}

if is_mouse_button_pressed(MouseButton::Right) {
blood_canvas_blit_at(
blood_canvas_blit_at_pro(
texture_id("error"),
mouse_world(),
None,
RED.alpha(0.5),
RED.alpha(0.7),
flip_coin(0.5),
flip_coin(0.5),
);
}

Expand Down Expand Up @@ -126,7 +128,11 @@ fn update(_c: &mut EngineContext) {
animated_sprite.play("idle");
}

if is_key_pressed(KeyCode::Space) {}
if is_key_pressed(KeyCode::Space) {
blood_canvas_blit_quad_draw(
animated_sprite.to_quad_draw(transform),
);
}

main_camera_mut().center = transform.position;
}
Expand Down

0 comments on commit e3e0564

Please sign in to comment.