1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//! # Artimate
//!
//! Artimate is a simple, pixel-based graphics framework for Rust, perfect for creative coding,
//! generative art, and interactive applications. It provides a minimal API for creating
//! applications that render directly to a pixel buffer, making it ideal for:
//!
//! - Generative art and creative coding
//! - Small pixel-based games and simulations
//! - Interactive animations
//! - Algorithm visualizations
//!
//! ## Features
//!
//! - **Simple API**: Just define a draw function that returns RGBA pixel data
//! - **Two modes**: Sketch mode for simple graphics, App mode for stateful applications
//! - **Built-in utilities**: Mouse input, time tracking, window management
//! - **Frame saving**: Automatically save frames as PNG files
//! - **Input handling**: Keyboard and mouse event handling
//! - **GPU-accelerated**: Uses `pixels` crate for efficient rendering
//!
//! ## Quick Start
//!
//! ### Simple Sketch
//!
//! ```rust,no_run
//! use artimate::app::{App, Config, Error};
//!
//! fn main() -> Result<(), Error> {
//! let config = Config::with_dims(800, 600);
//! let mut app = App::sketch(config, draw);
//! app.run()
//! }
//!
//! fn draw(app: &App, _model: &()) -> Vec<u8> {
//! // Create a simple gradient
//! let mut pixels = vec![0u8; (app.config.width * app.config.height * 4) as usize];
//!
//! for y in 0..app.config.height {
//! for x in 0..app.config.width {
//! let i = ((y * app.config.width + x) * 4) as usize;
//! pixels[i] = (x * 255 / app.config.width) as u8; // Red
//! pixels[i + 1] = (y * 255 / app.config.height) as u8; // Green
//! pixels[i + 2] = 128; // Blue
//! pixels[i + 3] = 255; // Alpha
//! }
//! }
//!
//! pixels
//! }
//! ```
//!
//! ### Stateful Application
//!
//! ```rust,no_run
//! use artimate::app::{App, AppMode, Config, Error};
//!
//! #[derive(Clone)]
//! struct Model {
//! position: f32,
//! velocity: f32,
//! }
//!
//! fn main() -> Result<(), Error> {
//! let config = Config::with_dims(800, 600);
//! let model = Model { position: 0.0, velocity: 100.0 };
//! let mut app = App::app(model, config, update, draw);
//! app.run()
//! }
//!
//! fn update(app: &App<AppMode, Model>, mut model: Model) -> Model {
//! // Update position based on time
//! model.position += model.velocity * (1.0 / 60.0); // Assuming 60 FPS
//!
//! // Bounce at edges
//! if model.position > app.config.width as f32 || model.position < 0.0 {
//! model.velocity = -model.velocity;
//! }
//!
//! model
//! }
//!
//! fn draw(app: &App<AppMode, Model>, model: &Model) -> Vec<u8> {
//! // Draw based on model state
//! let mut pixels = vec![0u8; (app.config.width * app.config.height * 4) as usize];
//!
//! // Draw a moving circle
//! let circle_x = model.position as u32;
//! let circle_y = app.config.height / 2;
//! let radius = 50;
//!
//! for y in 0..app.config.height {
//! for x in 0..app.config.width {
//! let dx = (x as i32 - circle_x as i32).abs();
//! let dy = (y as i32 - circle_y as i32).abs();
//!
//! if dx * dx + dy * dy <= radius * radius {
//! let i = ((y * app.config.width + x) * 4) as usize;
//! pixels[i] = 255; // Red
//! pixels[i + 1] = 100; // Green
//! pixels[i + 2] = 100; // Blue
//! pixels[i + 3] = 255; // Alpha
//! }
//! }
//! }
//!
//! pixels
//! }
//! ```
//!
//! ## Configuration
//!
//! The `Config` struct allows you to customize window and rendering behavior:
//!
//! ```rust
//! use artimate::app::Config;
//!
//! let config = Config::with_dims(1200, 800)
//! .set_title("My Artimate App")
//! .set_frames_to_save(60) // Save first 60 frames as PNG
//! .set_cursor_visibility(false) // Hide cursor
//! .no_loop(); // Render only one frame
//! ```
//!
//! ## Input Handling
//!
//! Access mouse position and handle keyboard events:
//!
//! ```rust,no_run
//! use artimate::app::{App, Config, Error};
//! use winit::keyboard::Key;
//!
//! fn main() -> Result<(), Error> {
//! let config = Config::with_dims(800, 600);
//! let mut app = App::sketch(config, draw);
//!
//! // Handle keyboard input
//! app.on_key_press(Key::Character("r".into()), |app| {
//! println!("R key pressed!");
//! });
//!
//! app.run()
//! }
//!
//! fn draw(app: &App, _model: &()) -> Vec<u8> {
//! // Use mouse position in drawing
//! let mouse_x = app.mouse_x();
//! let mouse_y = app.mouse_y();
//!
//! // ... drawing logic using mouse position
//! vec![0; (app.config.width * app.config.height * 4) as usize]
//! }
//! ```
//!
//! ## Integration with Graphics Libraries
//!
//! Artimate works well with other graphics libraries that can render to pixel buffers.
//! For example, you can use `tiny-skia` for 2D vector graphics or `wassily` for
//! creative coding utilities.
//!
//! ## Performance
//!
//! Artimate is designed to be efficient for real-time graphics:
//! - GPU-accelerated rendering via the `pixels` crate
//! - Minimal overhead pixel buffer management
//! - Supports high frame rates for smooth animations
//!
//! When the application exits, performance statistics are printed including
//! average FPS, total frame count, and elapsed time.