From 3eb772cbd57f2a7f8db0d40d2cf01a846e0d3892 Mon Sep 17 00:00:00 2001 From: Yash Karandikar Date: Wed, 30 Mar 2022 16:53:31 -0500 Subject: [PATCH] Update docs, fix doctests, and bump version number --- Cargo.toml | 2 +- README.md | 92 +++++++++++++++++++++++++++----------- src/lib.rs | 127 +++++++++++++++++++++++++++++++++++++---------------- 3 files changed, 158 insertions(+), 63 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3e195e9..b41221a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "cat-box" -version = "0.1.3" +version = "0.1.4" edition = "2018" license = "MIT" description = "Work in progress game engine, inspired by arcade" diff --git a/README.md b/README.md index 0123822..662e83b 100644 --- a/README.md +++ b/README.md @@ -1,52 +1,94 @@ # cat-box +[![crates.io](https://img.shields.io/crates/v/cat-box.svg)](https://crates.io/crates/cat-box) +[![Documentation](https://docs.rs/cat-box/badge.svg)](https://docs.rs/cat-box) +[![MIT License](https://img.shields.io/crates/l/cat-box.svg)](./LICENSE) + Work in progress game engine, inspired by [arcade](https://arcade.academy/). +## Getting started + +Add `cat-box` to your `Cargo.toml`, and then follow the example below. Read [the documentation](https://docs.rs/cat-box/latest/cat_box/) for more info. + ```rs -use cat_box::{Event, Game, Keycode, Sprite}; +use cat_box::{draw_text, Game, Sprite, SpriteCollection, get_mouse_state, get_keyboard_state}; +use sdl2::keyboard::Scancode; fn main() { - let game = Game::new("cat-box demo", 1000, 800); + let game = Game::new("catbox demo", 1000, 800); let mut i = 0u8; let mut s = Sprite::new("duck.png", 500, 400).unwrap(); - game.run(|ctx, event_pump| { + let mut s2 = Sprite::new("duck.png", 400, 500).unwrap(); + + let mut coll = SpriteCollection::new(); + for n in 0..10 { + for o in 0..8 { + let x = Sprite::new("duck.png", n * 100, o * 100).unwrap(); + coll.push(x); + } + } + game.run(|ctx| { i = (i + 1) % 255; ctx.set_background_colour(i as u8, 64, 255); + draw_text( + ctx, + format!("i is {}", i), + "MesloLGS NF Regular.ttf", + 72, + (300, 300), + cat_box::TextMode::Shaded { + foreground: (255, 255, 255), + background: (0, 0, 0), + }, + ) + .unwrap(); + let (start_x, start_y) = s.position(); - let m = sdl2::mouse::MouseState::new(event_pump.as_ref()); - let x_diff = m.x() - start_x; - let y_diff = m.y() - start_y; + let m = get_mouse_state(ctx); + let x_diff = m.x - start_x; + let y_diff = m.y - start_y; let angle = (y_diff as f64).atan2(x_diff as f64); s.set_angle(angle.to_degrees()); - for event in event_pump { - match event { - Event::Quit { .. } - | Event::KeyDown { - keycode: Some(Keycode::Escape), - .. - } => game.terminate(), + for spr in coll.iter() { + let (start_x, start_y) = spr.position(); + let m = get_mouse_state(ctx); + let x_diff = m.x - start_x; + let y_diff = m.y - start_y; - Event::KeyDown { keycode, .. } => { - let offset = match keycode.unwrap() { - Keycode::W | Keycode::Up => (0, 5), - Keycode::S | Keycode::Down => (0, -5), - Keycode::A | Keycode::Left => (-5, 0), - Keycode::D | Keycode::Right => (5, 0), - _ => (0, 0), - }; + let angle = (y_diff as f64).atan2(x_diff as f64); + spr.set_angle(angle.to_degrees()); + } - s.translate(offset); - } - _ => {} + let keys = get_keyboard_state(ctx).keys; + + for key in keys { + let offset = match key { + Scancode::Escape => { + game.terminate(); + (0, 0) + }, + Scancode::W | Scancode::Up => (0, 5), + Scancode::S | Scancode::Down => (0, -5), + Scancode::A | Scancode::Left => (-5, 0), + Scancode::D | Scancode::Right => (5, 0), + _ => (0, 0), + }; + + s.translate(offset); + + for spr in coll.iter() { + spr.translate(offset); } } + s2.draw(ctx).unwrap(); s.draw(ctx).unwrap(); + coll.draw(ctx).unwrap(); }) .unwrap(); } -``` +``` \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index a7237e3..9002ed2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,49 +1,83 @@ //! Work in progress game engine, inspired by [arcade](https://arcade.academy/). //! //! ```no_run -//! use cat_box::{Event, Game, Keycode, Sprite}; +//! use cat_box::{draw_text, Game, Sprite, SpriteCollection, get_mouse_state, get_keyboard_state}; +//! use sdl2::keyboard::Scancode; //! //! fn main() { -//! let game = Game::new("cat_box demo", 1000, 800); -//! -//! let mut i = 0; +//! let game = Game::new("catbox demo", 1000, 800); +//! +//! let mut i = 0u8; //! let mut s = Sprite::new("duck.png", 500, 400).unwrap(); -//! game.run(|ctx, event_pump| { +//! let mut s2 = Sprite::new("duck.png", 400, 500).unwrap(); +//! +//! let mut coll = SpriteCollection::new(); +//! for n in 0..10 { +//! for o in 0..8 { +//! let x = Sprite::new("duck.png", n * 100, o * 100).unwrap(); +//! coll.push(x); +//! } +//! } +//! game.run(|ctx| { //! i = (i + 1) % 255; //! ctx.set_background_colour(i as u8, 64, 255); -//! +//! +//! draw_text( +//! ctx, +//! format!("i is {}", i), +//! "MesloLGS NF Regular.ttf", +//! 72, +//! (300, 300), +//! cat_box::TextMode::Shaded { +//! foreground: (255, 255, 255), +//! background: (0, 0, 0), +//! }, +//! ) +//! .unwrap(); +//! //! let (start_x, start_y) = s.position(); -//! let m = sdl2::mouse::MouseState::new(event_pump.as_ref()); -//! let x_diff = m.x() - start_x; -//! let y_diff = m.y() - start_y; -//! +//! let m = get_mouse_state(ctx); +//! let x_diff = m.x - start_x; +//! let y_diff = m.y - start_y; +//! //! let angle = (y_diff as f64).atan2(x_diff as f64); //! s.set_angle(angle.to_degrees()); -//! -//! for event in event_pump { -//! match event { -//! Event::Quit { .. } -//! | Event::KeyDown { -//! keycode: Some(Keycode::Escape), -//! .. -//! } => game.terminate(), -//! -//! Event::KeyDown { keycode, .. } => { -//! let offset = match keycode.unwrap() { -//! Keycode::W | Keycode::Up => (0, 5), -//! Keycode::S | Keycode::Down => (0, -5), -//! Keycode::A | Keycode::Left => (-5, 0), -//! Keycode::D | Keycode::Right => (5, 0), -//! _ => (0, 0), -//! }; -//! -//! s.translate(offset); -//! } -//! _ => {} +//! +//! for spr in coll.iter() { +//! let (start_x, start_y) = spr.position(); +//! let m = get_mouse_state(ctx); +//! let x_diff = m.x - start_x; +//! let y_diff = m.y - start_y; +//! +//! let angle = (y_diff as f64).atan2(x_diff as f64); +//! spr.set_angle(angle.to_degrees()); +//! } +//! +//! let keys = get_keyboard_state(ctx).keys; +//! +//! for key in keys { +//! let offset = match key { +//! Scancode::Escape => { +//! game.terminate(); +//! (0, 0) +//! }, +//! Scancode::W | Scancode::Up => (0, 5), +//! Scancode::S | Scancode::Down => (0, -5), +//! Scancode::A | Scancode::Left => (-5, 0), +//! Scancode::D | Scancode::Right => (5, 0), +//! _ => (0, 0), +//! }; +//! +//! s.translate(offset); +//! +//! for spr in coll.iter() { +//! spr.translate(offset); //! } //! } -//! +//! +//! s2.draw(ctx).unwrap(); //! s.draw(ctx).unwrap(); +//! coll.draw(ctx).unwrap(); //! }) //! .unwrap(); //! } @@ -179,7 +213,7 @@ impl Sprite { /// # use cat_box::*; /// # let mut s = Sprite::new("duck.png", 500, 400).unwrap(); /// # let game = Game::new("sprite demo", 1000, 1000); - /// # game.run(|ctx, _| { + /// # game.run(|ctx| { /// s.draw(ctx); /// # }); /// ``` @@ -282,7 +316,7 @@ impl SpriteCollection { /// # use cat_box::*; /// # let mut sprites = SpriteCollection::new(); /// # let mut game = Game::new("asjdfhalksjdf", 1, 1); - /// # game.run(|ctx, _| { + /// # game.run(|ctx| { /// sprites.draw(ctx); /// # }); /// ``` @@ -484,7 +518,7 @@ pub enum TextMode { /// ``` no_run /// # use cat_box::*; /// # let game = Game::new("", 100, 100); -/// # game.run(|ctx, _| { +/// # game.run(|ctx| { /// let mode = TextMode::Shaded { /// foreground: (255, 255, 255), /// background: (0, 0, 0) @@ -522,17 +556,26 @@ pub fn draw_text>( Ok(()) } -#[derive(Debug)] +/// Representation of the mouse state. pub struct MouseRepr { pub buttons: Vec, pub x: i32, pub y: i32 } +/// Representation of the keyboard state. pub struct KeyboardRepr { pub keys: Vec } +/// Get the mouse state. +/// ```no_run +/// # use cat_box::*; +/// # let game = Game::new("catbox-demo", 10, 10); +/// # game.run(|ctx| { +/// let m = get_mouse_state(ctx); +/// println!("({}, {})", m.x, m.y); +/// # }); pub fn get_mouse_state(ctx: &mut Context) -> MouseRepr { let (_, _, pump) = ctx.inner(); @@ -545,6 +588,16 @@ pub fn get_mouse_state(ctx: &mut Context) -> MouseRepr { } } +/// Get the keyboard state. +/// ```no_run +/// # use cat_box::*; +/// # let game = Game::new("catbox-demo", 10, 10); +/// # game.run(|ctx| { +/// let k = get_keyboard_state(ctx); +/// for code in k.keys { +/// println!("{}", code); +/// } +/// # }); pub fn get_keyboard_state(ctx: &mut Context) -> KeyboardRepr { let (_, _, pump) = ctx.inner(); @@ -590,7 +643,7 @@ impl Game { /// ```no_run /// # use cat_box::Game; /// # let game = Game::new("Cool game", 1000, 1000); - /// game.run(|ctx, events| { + /// game.run(|ctx| { /// // Game logic goes here /// }); /// ```