Update docs, fix doctests, and bump version number
This commit is contained in:
parent
cc033efe05
commit
3eb772cbd5
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "cat-box"
|
name = "cat-box"
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
description = "Work in progress game engine, inspired by arcade"
|
description = "Work in progress game engine, inspired by arcade"
|
||||||
|
|
84
README.md
84
README.md
|
@ -1,51 +1,93 @@
|
||||||
# cat-box
|
# 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/).
|
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
|
```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() {
|
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 i = 0u8;
|
||||||
let mut s = Sprite::new("duck.png", 500, 400).unwrap();
|
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;
|
i = (i + 1) % 255;
|
||||||
ctx.set_background_colour(i as u8, 64, 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 (start_x, start_y) = s.position();
|
||||||
let m = sdl2::mouse::MouseState::new(event_pump.as_ref());
|
let m = get_mouse_state(ctx);
|
||||||
let x_diff = m.x() - start_x;
|
let x_diff = m.x - start_x;
|
||||||
let y_diff = m.y() - start_y;
|
let y_diff = m.y - start_y;
|
||||||
|
|
||||||
let angle = (y_diff as f64).atan2(x_diff as f64);
|
let angle = (y_diff as f64).atan2(x_diff as f64);
|
||||||
s.set_angle(angle.to_degrees());
|
s.set_angle(angle.to_degrees());
|
||||||
|
|
||||||
for event in event_pump {
|
for spr in coll.iter() {
|
||||||
match event {
|
let (start_x, start_y) = spr.position();
|
||||||
Event::Quit { .. }
|
let m = get_mouse_state(ctx);
|
||||||
| Event::KeyDown {
|
let x_diff = m.x - start_x;
|
||||||
keycode: Some(Keycode::Escape),
|
let y_diff = m.y - start_y;
|
||||||
..
|
|
||||||
} => game.terminate(),
|
|
||||||
|
|
||||||
Event::KeyDown { keycode, .. } => {
|
let angle = (y_diff as f64).atan2(x_diff as f64);
|
||||||
let offset = match keycode.unwrap() {
|
spr.set_angle(angle.to_degrees());
|
||||||
Keycode::W | Keycode::Up => (0, 5),
|
}
|
||||||
Keycode::S | Keycode::Down => (0, -5),
|
|
||||||
Keycode::A | Keycode::Left => (-5, 0),
|
let keys = get_keyboard_state(ctx).keys;
|
||||||
Keycode::D | Keycode::Right => (5, 0),
|
|
||||||
|
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),
|
_ => (0, 0),
|
||||||
};
|
};
|
||||||
|
|
||||||
s.translate(offset);
|
s.translate(offset);
|
||||||
}
|
|
||||||
_ => {}
|
for spr in coll.iter() {
|
||||||
|
spr.translate(offset);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
s2.draw(ctx).unwrap();
|
||||||
s.draw(ctx).unwrap();
|
s.draw(ctx).unwrap();
|
||||||
|
coll.draw(ctx).unwrap();
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
107
src/lib.rs
107
src/lib.rs
|
@ -1,49 +1,83 @@
|
||||||
//! Work in progress game engine, inspired by [arcade](https://arcade.academy/).
|
//! Work in progress game engine, inspired by [arcade](https://arcade.academy/).
|
||||||
//!
|
//!
|
||||||
//! ```no_run
|
//! ```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() {
|
//! fn main() {
|
||||||
//! let game = Game::new("cat_box demo", 1000, 800);
|
//! let game = Game::new("catbox demo", 1000, 800);
|
||||||
//!
|
//!
|
||||||
//! let mut i = 0;
|
//! let mut i = 0u8;
|
||||||
//! let mut s = Sprite::new("duck.png", 500, 400).unwrap();
|
//! 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;
|
//! i = (i + 1) % 255;
|
||||||
//! ctx.set_background_colour(i as u8, 64, 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 (start_x, start_y) = s.position();
|
||||||
//! let m = sdl2::mouse::MouseState::new(event_pump.as_ref());
|
//! let m = get_mouse_state(ctx);
|
||||||
//! let x_diff = m.x() - start_x;
|
//! let x_diff = m.x - start_x;
|
||||||
//! let y_diff = m.y() - start_y;
|
//! let y_diff = m.y - start_y;
|
||||||
//!
|
//!
|
||||||
//! let angle = (y_diff as f64).atan2(x_diff as f64);
|
//! let angle = (y_diff as f64).atan2(x_diff as f64);
|
||||||
//! s.set_angle(angle.to_degrees());
|
//! s.set_angle(angle.to_degrees());
|
||||||
//!
|
//!
|
||||||
//! for event in event_pump {
|
//! for spr in coll.iter() {
|
||||||
//! match event {
|
//! let (start_x, start_y) = spr.position();
|
||||||
//! Event::Quit { .. }
|
//! let m = get_mouse_state(ctx);
|
||||||
//! | Event::KeyDown {
|
//! let x_diff = m.x - start_x;
|
||||||
//! keycode: Some(Keycode::Escape),
|
//! let y_diff = m.y - start_y;
|
||||||
//! ..
|
|
||||||
//! } => game.terminate(),
|
|
||||||
//!
|
//!
|
||||||
//! Event::KeyDown { keycode, .. } => {
|
//! let angle = (y_diff as f64).atan2(x_diff as f64);
|
||||||
//! let offset = match keycode.unwrap() {
|
//! spr.set_angle(angle.to_degrees());
|
||||||
//! Keycode::W | Keycode::Up => (0, 5),
|
//! }
|
||||||
//! Keycode::S | Keycode::Down => (0, -5),
|
//!
|
||||||
//! Keycode::A | Keycode::Left => (-5, 0),
|
//! let keys = get_keyboard_state(ctx).keys;
|
||||||
//! Keycode::D | Keycode::Right => (5, 0),
|
//!
|
||||||
|
//! 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),
|
//! _ => (0, 0),
|
||||||
//! };
|
//! };
|
||||||
//!
|
//!
|
||||||
//! s.translate(offset);
|
//! s.translate(offset);
|
||||||
//! }
|
//!
|
||||||
//! _ => {}
|
//! for spr in coll.iter() {
|
||||||
|
//! spr.translate(offset);
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
|
//! s2.draw(ctx).unwrap();
|
||||||
//! s.draw(ctx).unwrap();
|
//! s.draw(ctx).unwrap();
|
||||||
|
//! coll.draw(ctx).unwrap();
|
||||||
//! })
|
//! })
|
||||||
//! .unwrap();
|
//! .unwrap();
|
||||||
//! }
|
//! }
|
||||||
|
@ -179,7 +213,7 @@ impl Sprite {
|
||||||
/// # use cat_box::*;
|
/// # use cat_box::*;
|
||||||
/// # let mut s = Sprite::new("duck.png", 500, 400).unwrap();
|
/// # let mut s = Sprite::new("duck.png", 500, 400).unwrap();
|
||||||
/// # let game = Game::new("sprite demo", 1000, 1000);
|
/// # let game = Game::new("sprite demo", 1000, 1000);
|
||||||
/// # game.run(|ctx, _| {
|
/// # game.run(|ctx| {
|
||||||
/// s.draw(ctx);
|
/// s.draw(ctx);
|
||||||
/// # });
|
/// # });
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -282,7 +316,7 @@ impl SpriteCollection {
|
||||||
/// # use cat_box::*;
|
/// # use cat_box::*;
|
||||||
/// # let mut sprites = SpriteCollection::new();
|
/// # let mut sprites = SpriteCollection::new();
|
||||||
/// # let mut game = Game::new("asjdfhalksjdf", 1, 1);
|
/// # let mut game = Game::new("asjdfhalksjdf", 1, 1);
|
||||||
/// # game.run(|ctx, _| {
|
/// # game.run(|ctx| {
|
||||||
/// sprites.draw(ctx);
|
/// sprites.draw(ctx);
|
||||||
/// # });
|
/// # });
|
||||||
/// ```
|
/// ```
|
||||||
|
@ -484,7 +518,7 @@ pub enum TextMode {
|
||||||
/// ``` no_run
|
/// ``` no_run
|
||||||
/// # use cat_box::*;
|
/// # use cat_box::*;
|
||||||
/// # let game = Game::new("", 100, 100);
|
/// # let game = Game::new("", 100, 100);
|
||||||
/// # game.run(|ctx, _| {
|
/// # game.run(|ctx| {
|
||||||
/// let mode = TextMode::Shaded {
|
/// let mode = TextMode::Shaded {
|
||||||
/// foreground: (255, 255, 255),
|
/// foreground: (255, 255, 255),
|
||||||
/// background: (0, 0, 0)
|
/// background: (0, 0, 0)
|
||||||
|
@ -522,17 +556,26 @@ pub fn draw_text<S: AsRef<str>>(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
/// Representation of the mouse state.
|
||||||
pub struct MouseRepr {
|
pub struct MouseRepr {
|
||||||
pub buttons: Vec<MouseButton>,
|
pub buttons: Vec<MouseButton>,
|
||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32
|
pub y: i32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Representation of the keyboard state.
|
||||||
pub struct KeyboardRepr {
|
pub struct KeyboardRepr {
|
||||||
pub keys: Vec<Scancode>
|
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 {
|
pub fn get_mouse_state(ctx: &mut Context) -> MouseRepr {
|
||||||
let (_, _, pump) = ctx.inner();
|
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 {
|
pub fn get_keyboard_state(ctx: &mut Context) -> KeyboardRepr {
|
||||||
let (_, _, pump) = ctx.inner();
|
let (_, _, pump) = ctx.inner();
|
||||||
|
|
||||||
|
@ -590,7 +643,7 @@ impl Game {
|
||||||
/// ```no_run
|
/// ```no_run
|
||||||
/// # use cat_box::Game;
|
/// # use cat_box::Game;
|
||||||
/// # let game = Game::new("Cool game", 1000, 1000);
|
/// # let game = Game::new("Cool game", 1000, 1000);
|
||||||
/// game.run(|ctx, events| {
|
/// game.run(|ctx| {
|
||||||
/// // Game logic goes here
|
/// // Game logic goes here
|
||||||
/// });
|
/// });
|
||||||
/// ```
|
/// ```
|
||||||
|
|
Loading…
Reference in a new issue