Clear in the event loop instead of in draw()

This commit is contained in:
Yash Karandikar 2022-03-15 11:04:18 -05:00
parent 0902130e7d
commit 58187f9703
2 changed files with 9 additions and 8 deletions

View file

@ -179,8 +179,6 @@ impl Sprite {
let (creator, canvas) = ctx.inner(); let (creator, canvas) = ctx.inner();
let text = creator.create_texture_from_surface(&self.surf)?; let text = creator.create_texture_from_surface(&self.surf)?;
canvas.fill_rect(None)?;
canvas.clear();
canvas.copy_ex(&text, None, self.rect, self.angle, None, false, false)?; canvas.copy_ex(&text, None, self.rect, self.angle, None, false, false)?;
Ok(()) Ok(())
@ -241,7 +239,7 @@ pub struct Context {
} }
impl Context { impl Context {
pub fn new(canvas: Canvas<Window>) -> Self { fn new(canvas: Canvas<Window>) -> Self {
let creator = canvas.texture_creator(); let creator = canvas.texture_creator();
Self { Self {
canvas, canvas,
@ -249,17 +247,17 @@ impl Context {
} }
} }
pub fn canvas(&mut self) -> &mut Canvas<Window> {
&mut self.canvas
}
pub fn inner(&mut self) -> (&TextureCreator<WindowContext>, &mut Canvas<Window>) { pub fn inner(&mut self) -> (&TextureCreator<WindowContext>, &mut Canvas<Window>) {
(&self.texture_creator, &mut self.canvas) (&self.texture_creator, &mut self.canvas)
} }
pub fn update(&mut self) { fn update(&mut self) {
self.canvas.present(); self.canvas.present();
} }
fn clear(&mut self) {
self.canvas.clear();
}
} }
/// Representation of the game. /// Representation of the game.
@ -324,6 +322,7 @@ impl Game {
if self.stopped.get() { if self.stopped.get() {
break; break;
} }
ctx.clear();
func(&mut ctx, &mut events); func(&mut ctx, &mut events);
ctx.update(); ctx.update();
} }

View file

@ -5,6 +5,7 @@ fn main() {
let mut i = 0.0; let mut i = 0.0;
let mut s = Sprite::new("duck.png", 500, 400).unwrap(); let mut s = Sprite::new("duck.png", 500, 400).unwrap();
let mut s2 = Sprite::new("duck.png", 400, 500).unwrap();
game.run(|ctx, event_pump| { game.run(|ctx, event_pump| {
i = (i + 1.0) % 360.0; i = (i + 1.0) % 360.0;
@ -39,6 +40,7 @@ fn main() {
} }
} }
s2.draw(ctx).unwrap();
s.draw(ctx).unwrap(); s.draw(ctx).unwrap();
}) })
.unwrap(); .unwrap();