baba/
lib.rs

1//! Baba is an extremely simple engine for game development, inspired by love2d.
2//!
3//! Its main goal is to provide a robust base for games of any complexity. It is currently built
4//! on top of [SDL2], which already has widespread usage and supports a huge variety of systems.
5//!
6//! Like SDL, it's entirely free. Baba uses the Apache license, which means usage for any purpose,
7//! commercial or not, is allowed.
8//!
9//! [SDL2]: https://libsdl.org/
10//!
11//! ## Getting started
12//!
13//! All of the magic happens when you call [`baba::run`](run), and you can use the library right
14//! away!
15//!
16//! ```no_run
17//! # use baba::prelude::*;
18//! fn main() -> baba::Result {
19//!     baba::run("My game", MyGame::update)
20//! }
21//!
22//! # #[derive(Default)]
23//! # struct MyGame;
24//! impl MyGame {
25//!     fn update(&mut self) {
26//!         // Update your game logic and draw onto the screen!
27//!         gfx::clear(Color::WHITE);
28//!     }
29//! }
30//! ```
31//!
32//! Refer to the [modules] to see what the engine can do. Baba is still pretty early in
33//! development, so loads more documentation are still coming.
34//!
35//! [modules]: #modules
36
37#![warn(
38    clippy::pedantic,
39    clippy::missing_const_for_fn,
40    clippy::use_self,
41    unsafe_op_in_unsafe_fn,
42    missing_docs
43)]
44#![allow(
45    clippy::cast_possible_wrap,
46    clippy::cast_possible_truncation,
47    clippy::cast_precision_loss,
48    clippy::module_name_repetitions,
49    clippy::semicolon_if_nothing_returned,
50    clippy::wildcard_imports,
51    clippy::missing_errors_doc,
52    clippy::missing_panics_doc
53)]
54
55mod error;
56mod game;
57pub mod gfx;
58pub mod input;
59pub mod math;
60pub use error::{Error, SdlError};
61pub use game::{Framerate, Game, Settings, WindowSettings};
62
63/// A [`Result`][std::result] type for baba programs.
64pub type Result<T = (), E = Error> = std::result::Result<T, E>;
65
66/// Simple entrypoint function for baba games.
67///
68/// If you don't currently need to set any engine options, this is simply a shorthand for that:
69///
70/// ```no_run
71/// # #[derive(Default)]
72/// # struct MyGame;
73/// # impl MyGame { fn update(&mut self) {} }
74/// # fn main() -> baba::Result {
75/// baba::game("My game", MyGame::update)
76///     .run()
77/// # }
78/// ```
79///
80/// See [`game`][game()] for more!
81pub fn run<S>(name: impl Into<String>, update: impl Fn(&mut S)) -> Result
82where
83    S: Default,
84{
85    game(name, update).run()
86}
87
88/// Entrypoint function for baba games.
89///
90/// This function creates a [`Game`] object, which you can set many options in. To start your game,
91/// simply call [`.run()`][Game::run] or [`.run_with(MyGame::new)`][Game::run_with] on it. Check
92/// out [`Game`] for the options you can set!
93///
94/// ```no_run
95/// fn main() -> baba::Result {
96///     baba::game("My game", MyGame::update)
97///         .window_title("Hello!")
98///         .run()
99/// }
100///
101/// #[derive(Default)]
102/// struct MyGame {
103///     // ...
104/// }
105/// # impl MyGame { fn update(&mut self) {} }
106/// ```
107///
108/// If you don't want to implement [`Default`], you can instead use [`run_with`][Game::run_with]:
109///
110/// ```no_run
111/// fn main() -> baba::Result {
112///     baba::game("My game", MyGame::update)
113///         .run_with(MyGame::new)
114/// }
115///
116/// # struct MyGame;
117/// # impl MyGame { fn update(&mut self) {} }
118/// impl MyGame {
119///     fn new() -> Self {
120///         Self {
121///             // ...
122///         }
123///     }
124/// }
125/// ```
126pub fn game<S>(name: impl Into<String>, update: impl Fn(&mut S)) -> Game<S, impl Fn(&mut S)> {
127    Game::new(name.into(), update)
128}
129
130/// Common functions and objects used by baba programs.
131///
132/// To avoid importing many things, it's recommended to glob import this on your files:
133///
134/// ```
135/// use baba::prelude::*;
136/// ```
137pub mod prelude {
138    #[doc(inline)]
139    pub use crate::game::{Framerate, Settings, WindowSettings};
140    #[doc(inline)]
141    pub use crate::gfx::{
142        self, Color, Drawable, Origin, ScaleMode, Texture, TextureOptions, TextureSlice, Transform,
143        Vertex, Viewport, ViewportScaling,
144    };
145    #[doc(inline)]
146    pub use crate::input::{self, is_key_down, is_key_pressed, KeyCode};
147    #[doc(inline)]
148    pub use crate::math::*;
149
150    #[doc(inline)]
151    pub use log::{debug, info, trace, warn};
152}