Update docs, fix doctests, and bump version number

This commit is contained in:
Yash Karandikar 2022-03-30 16:53:31 -05:00
parent cc033efe05
commit 3eb772cbd5
3 changed files with 158 additions and 63 deletions

View File

@ -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"

View File

@ -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();
}
```
```

View File

@ -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<S: AsRef<str>>(
Ok(())
}
#[derive(Debug)]
/// Representation of the mouse state.
pub struct MouseRepr {
pub buttons: Vec<MouseButton>,
pub x: i32,
pub y: i32
}
/// Representation of the keyboard state.
pub struct KeyboardRepr {
pub keys: Vec<Scancode>
}
/// 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
/// });
/// ```