Skip to main content

nannou/
lib.rs

1#![doc(
2    html_logo_url = "https://raw.githubusercontent.com/nannou-org/nannou/master/assets/images/logo.png"
3)]
4
5//! An open-source creative-coding toolkit for Rust.
6//!
7//! [**Nannou**](http://nannou.cc) is a collection of code aimed at making it easy for artists to
8//! express themselves with simple, fast, reliable, portable code. Whether working on a 12-month
9//! laser installation or a 5 minute sketch, this framework aims to give artists easy access to the
10//! tools they need.
11//!
12//! If you're new to nannou, we recommend checking out [the
13//! examples](https://github.com/nannou-org/nannou/tree/master/examples) to get an idea of how
14//! nannou applications are structured and how the API works.
15//!
16//! Nannou is built on the [Bevy](https://bevyengine.org) game engine. The [`nannou::app`](app())
17//! and [`nannou::sketch`](sketch()) builders provide the familiar nannou entry points, while
18//! [`NannouPlugin`] bundles nannou's functionality as a Bevy [`Plugin`] so it can also be added to
19//! an existing Bevy `App`.
20//!
21//! When using nannou as a Bevy plugin, the [`context::App`] system param gives your own Bevy
22//! systems access to nannou's conveniences (time, input, the focused window and the `Draw` API)
23//! with no `unsafe` and no builder machinery. See the [`context`] module and the `system_param`
24//! example for details.
25use bevy::diagnostic::FrameTimeDiagnosticsPlugin;
26use bevy::prelude::{App as BevyApp, IntoScheduleConfigs, Plugin, PostUpdate};
27use bevy::winit::WinitSettings;
28
29pub use find_folder;
30pub use lyon;
31
32#[doc(inline)]
33pub use nannou_core::{glam, math, rand};
34
35pub use self::context::App;
36
37pub mod app;
38mod camera;
39pub mod context;
40mod frame;
41pub mod geom;
42pub mod image;
43#[cfg(feature = "serde")]
44pub mod io;
45mod light;
46pub mod noise;
47pub mod prelude;
48mod render;
49pub mod time;
50mod window;
51
52pub use nannou_wgpu as wgpu;
53
54pub struct NannouPlugin;
55
56impl Plugin for NannouPlugin {
57    fn build(&self, app: &mut BevyApp) {
58        app.add_plugins(nannou_draw::NannouDrawPlugin);
59        // Keep each default window camera's orthographic z range sized to its window.
60        app.add_systems(
61            PostUpdate,
62            window::update_default_camera_z_range.before(bevy::camera::CameraUpdateSystems),
63        );
64        // `FramePlugin` extracts per-window scale factors so a `Frame` can be constructed from a
65        // (custom or classic) render-world system.
66        app.add_plugins(crate::frame::FramePlugin);
67        // Ensure the resources the `bevy::App` system param relies on are present regardless of
68        // whether `NannouPlugin` is used standalone or via the `nannou::app`/`sketch` builders.
69        // `FrameTimeDiagnosticsPlugin` backs `App::fps`; guard against a double-add in case the
70        // user has already registered it.
71        if !app.is_plugin_added::<FrameTimeDiagnosticsPlugin>() {
72            app.add_plugins(FrameTimeDiagnosticsPlugin::default());
73        }
74        app.init_resource::<WinitSettings>();
75        #[cfg(feature = "video")]
76        {
77            app.add_plugins(nannou_video::NannouVideoPlugin);
78        }
79    }
80}
81
82/// Begin building the `App`.
83///
84/// The `model` argument is the function that the App will call to initialise your Model.
85///
86/// The Model can be thought of as the state that you would like to track throughout the
87/// lifetime of your nannou program from start to exit.
88///
89/// The given function is called before any event processing begins within the application.
90///
91/// The Model that is returned by the function is the same model that will be passed to the
92/// given event and view functions.
93pub fn app<M: 'static + Send>(model: app::ModelFn<M>) -> app::Builder<M> {
94    app::Builder::new(model)
95}
96
97/// Shorthand for building a simple app that has no model, handles no events and simply draws
98/// to a single window.
99///
100/// This is useful for late night hack sessions where you just don't care about all that other
101/// stuff, you just want to play around with some ideas or make something pretty.
102pub fn sketch(view: app::SketchViewFn) -> app::SketchBuilder {
103    app::Builder::sketch(view)
104}