From 32921cd14f3f6b60658d15851010ea642e9a516b Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Tue, 15 Mar 2022 11:36:33 -0500 Subject: [PATCH] Add ability to set drawing colour --- src/lib.rs | 7 ++++++- src/main.rs | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index ebaf0df..4e2b8e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -235,7 +235,7 @@ impl Sprite { /// Game context. /// -/// This should never actually be used; instead, just pass it around to the various cat-box functions such as [`Sprite::draw()`]. +/// In most cases, this should never actually be used; instead, just pass it around to the various cat-box functions such as [`Sprite::draw()`]. pub struct Context { canvas: Canvas, texture_creator: TextureCreator, @@ -264,6 +264,11 @@ impl Context { fn clear(&mut self) { self.canvas.clear(); } + + /// Set the background colour. See [`Canvas::set_draw_color()`](sdl2::render::Canvas::set_draw_color()) for more info. + pub fn set_background_colour(&mut self, r: u8, g: u8, b: u8) { + self.canvas.set_draw_color(Color::RGB(r, g, b)); + } } /// Representation of the game. diff --git a/src/main.rs b/src/main.rs index ee41e71..a83f14e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ fn main() { let mut s2 = Sprite::new("duck.png", 400, 500).unwrap(); game.run(|ctx, event_pump| { i = (i + 1.0) % 360.0; + ctx.set_background_colour(i as u8, 64, 255); let (start_x, start_y) = s.position(); let m = sdl2::mouse::MouseState::new(event_pump.as_ref());