Update documentation

This commit is contained in:
Yash Karandikar 2022-03-16 11:53:26 -05:00
parent 2392e2acce
commit 70cd3e2102
2 changed files with 38 additions and 15 deletions

View file

@ -10,7 +10,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();
game.run(|canvas, event_pump| { game.run(|ctx, event_pump| {
i = (i + 1.0) % 360.0; i = (i + 1.0) % 360.0;
let (start_x, start_y) = s.position(); let (start_x, start_y) = s.position();
@ -33,8 +33,8 @@ fn main() {
let offset = match keycode.unwrap() { let offset = match keycode.unwrap() {
Keycode::W | Keycode::Up => (0, 5), Keycode::W | Keycode::Up => (0, 5),
Keycode::S | Keycode::Down => (0, -5), Keycode::S | Keycode::Down => (0, -5),
Keycode::A | Keycode::Left => (5, 0), Keycode::A | Keycode::Left => (-5, 0),
Keycode::D | Keycode::Right => (-5, 0), Keycode::D | Keycode::Right => (5, 0),
_ => (0, 0), _ => (0, 0),
}; };
@ -44,7 +44,7 @@ fn main() {
} }
} }
s.draw(canvas).unwrap(); s.draw(ctx).unwrap();
}) })
.unwrap(); .unwrap();
} }

View file

@ -57,7 +57,7 @@ use sdl2::{
rwops::RWops, rwops::RWops,
surface::Surface, surface::Surface,
video::{Window, WindowBuildError, WindowContext}, video::{Window, WindowBuildError, WindowContext},
EventPump, IntegerOrSdlError, ttf::{Sdl2TtfContext, Font, FontError}, EventPump, IntegerOrSdlError, ttf::{Sdl2TtfContext, FontError},
}; };
#[doc(no_inline)] #[doc(no_inline)]
@ -239,16 +239,6 @@ impl Sprite {
} }
} }
pub enum TextMode {
Transparent {
colour: (u8, u8, u8)
},
Shaded {
foreground: (u8, u8, u8),
background: (u8, u8, u8)
}
}
/// Game context. /// Game context.
/// ///
/// In most cases, 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()`].
@ -289,6 +279,39 @@ impl Context {
} }
} }
/// Set the mode for drawing text.
pub enum TextMode {
/// Render the text transparently.
Transparent {
colour: (u8, u8, u8)
},
/// Render the text with a foreground and a background colour.
///
/// This creates a box around the text.
Shaded {
foreground: (u8, u8, u8),
background: (u8, u8, u8)
}
}
/// Draw text to the screen.
///
/// This loads a font from the current directory, case sensitive.
///
/// `pos` refers to the *center* of the rendered text.
///
/// Refer to [`TextMode`] for information about colouring.
///
/// ``` no_run
/// # use cat_box::*;
/// # let game = Game::new("", 100, 100);
/// # game.run(|ctx, _| {
/// let mode = TextMode::Shaded {
/// foreground: (255, 255, 255),
/// background: (0, 0, 0)
/// };
/// draw_text(ctx, "text to draw", "arial.ttf", 72, (300, 300), mode);
/// # });
pub fn draw_text<S: AsRef<str>>(ctx: &mut Context, text: S, font: &str, size: u16, pos: (i32, i32), mode: TextMode) -> Result<()> { pub fn draw_text<S: AsRef<str>>(ctx: &mut Context, text: S, font: &str, size: u16, pos: (i32, i32), mode: TextMode) -> Result<()> {
let font = ctx.ttf_subsystem.load_font(font, size)?; let font = ctx.ttf_subsystem.load_font(font, size)?;
let renderer = font.render(text.as_ref()); let renderer = font.render(text.as_ref());