Skip to content

Commit

Permalink
Revert change to clear API
Browse files Browse the repository at this point in the history
The new API ended up being awkward to use when you're doing stuff without a batcher (e.g. UI library integrations), so it needs a rethink. I'd like to get to a point where you can just call `clear` on the target itself, but that doesn't play nicely with `Window`...
  • Loading branch information
17cupsofcoffee committed Jan 20, 2024
1 parent ff7f3dc commit 3396c49
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 25 deletions.
5 changes: 3 additions & 2 deletions examples/bunnymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,13 @@ impl EventHandler for GameState {
}

fn draw(&mut self, app: &mut App) {
app.gfx.clear(&app.window, Color::rgb(0.392, 0.584, 0.929));

for bunny in &self.bunnies {
self.batch
.texture(&self.texture, bunny.position, DrawParams::new());
}

self.batch
.clear_and_draw(Color::rgb(0.392, 0.584, 0.929), &app.window);
self.batch.draw(&app.window);
}
}
22 changes: 12 additions & 10 deletions src/graphics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,25 @@ impl Graphics {
}
}

pub fn clear(&self, target: &impl Target, color: Color) {
unsafe {
target.bind(self);

self.state
.gl
.clear_color(color.r, color.g, color.b, color.a);

self.state.gl.clear(glow::COLOR_BUFFER_BIT);
}
}

pub fn draw<T>(&self, pass: RenderPass<'_, T>)
where
T: Target,
{
unsafe {
pass.target.bind(self);

if let Some(color) = pass.clear_color {
self.state
.gl
.clear_color(color.r, color.g, color.b, color.a);

self.state.gl.clear(glow::COLOR_BUFFER_BIT);
}

self.bind_vertex_buffer(Some(pass.mesh.raw.vertex_buffer));
self.bind_index_buffer(Some(pass.mesh.raw.index_buffer));
self.bind_shader(Some(pass.shader.raw.id));
Expand Down Expand Up @@ -275,6 +279,4 @@ pub struct RenderPass<'a, T> {

pub index_start: usize,
pub index_count: usize,

pub clear_color: Option<Color>,
}
12 changes: 1 addition & 11 deletions src/graphics/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Batcher {
}
}

fn draw_internal(&mut self, clear_color: Option<Color>, target: &impl Target) {
pub fn draw(&mut self, target: &impl Target) {
let mut index = 0;

for mut batch in self.batches.drain(..) {
Expand All @@ -174,8 +174,6 @@ impl Batcher {

index_start: 0,
index_count: num_sprites * 6,

clear_color: clear_color.filter(|_| index == 0),
});

index += num_sprites;
Expand All @@ -188,14 +186,6 @@ impl Batcher {
self.matrices.clear();
}

pub fn clear_and_draw(&mut self, clear_color: Color, target: &impl Target) {
self.draw_internal(Some(clear_color), target)
}

pub fn draw(&mut self, target: &impl Target) {
self.draw_internal(None, target)
}

pub fn push_matrix(&mut self, matrix: Mat3) {
// TODO: Support relative transforms
self.matrices.push(matrix);
Expand Down
4 changes: 2 additions & 2 deletions src/graphics/scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use glam::Vec2;
use crate::graphics::Canvas;
use crate::window::Window;

use super::{Batcher, Color, DrawParams, Graphics, Target};
use super::{Batcher, DrawParams, Graphics, Target};

pub struct Scaler {
canvas: Canvas,
Expand Down Expand Up @@ -31,7 +31,7 @@ impl Scaler {
DrawParams::new().scale(scale),
);

batch.clear_and_draw(Color::BLACK, target);
batch.draw(target);

self.offset = offset;
self.scale = scale;
Expand Down

0 comments on commit 3396c49

Please sign in to comment.