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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
//! # pxl
//!
//! A simple framework for making graphical programs in Rust. `pxl` is intended to avoid
//! Rust's most challenging concepts, while still providing a compelling platform upon
//! which to develop graphical games and programs.
//!
//! ## Features
//!
//! - Pixel-based rendering
//! - Sample-based audio synthesis
//! - Custom vertex and fragment shaders
//! - `pxl-build`, a compile-time resource loading crate
//! - Action and text input
use ;
/// The number of audio samples in a second. Synthesizer
/// implementations will need this to calculate the current
/// time from the number of samples played so far.
pub const SAMPLES_PER_SECOND: u32 = 48_000;
/// An RGBA pixel. Components should normally be between
/// `0.0` and `1.0` inclusive.
/// An image made of pixels. Used by the `pxl-build` crate
/// for image resources
/// A single stereo audio sample, representing `1/SAMPLES_PER_SECOND`
/// of an audio signal
/// Enum representing input buttons
///
/// In the current runtime, the arrow keys produce `Left`, `Right`, `Up` and
/// `Down` events, and the spacebar produces `Action` events.
///
/// Buttons are intended to be abstract, and in the future gamepad,
/// touch, and/or keyboard input may produce Button events
/// Enum representing the state of an input button
/// Input events
///
/// Note that a single keyboard press may generate both
/// a `Button` and `Key` event. For example, the spacebar
/// will generate both a `Button::Action` event and a
/// `Key{character: ' '}` event.
/// Trait for things that can generate sound
///
/// When a computer program plays audio, it typically generates audio samples that
/// are fed to a DAC, a digital-to-analog converter. This is highly latency-sensitive:
/// audible pops and crackles may be heard if the program does not generate samples
/// quickly enough.
///
/// A `pxl::Program` may not be available to generate samples at all times, for example
/// if it is busy rendering. To make sure that samples for the underlying audio hardware
/// can be generated at all times, even during rendering, audio-sample synthesis is delegated
/// to a dedicated `Synthesizer` object.
///
/// A `pxl::Program` that wishes to play audio should return an `Arc<Mutex<Synthesizer>>`
/// from `Program::synthesizer`. `Synthesizer::synthesize` will be called as needed on the
/// returned `Sythesizer` object to generate samples to feed the underlying audio hardware.
///
/// To communicate with and update the `Synthesizer`, the `pxl::Program` implementation
/// should keep it's own `Arc<Mutex<Synthesizer>>` with a reference to the synthesizer
/// object, locking it and updating it as needed.
///
/// In order to avoid starving the audio hardware of samples, the lock holding the
/// synthesizer should only be kept locked briefly.
/// Trait representing a `pxl::Program`
///
/// To run a program, see the `run` function below.
/// Run a `pxl::Program`. `run` takes care of instantiating your
/// program, so your program is passed as a type parameter, not
/// as a value.
///
/// For example, to get a 256 by 256 black window that does nothing,
/// aside from being quietly awesome, do:
///
/// ```no_run
/// // Import the pxl create
/// extern crate pxl;
///
/// // Bring public members of pxl into scope
/// use pxl::*;
///
/// // Create an empty stuct to hold your program state
/// struct AwesomeProgram {
/// }
///
/// // Implement `pxl::Program` for your program. Only
/// // a few methods are required.
/// impl Program for AwesomeProgram {
/// // Create a new `AwesomeProgram`
/// fn new() -> AwesomeProgram {
/// AwesomeProgram{}
/// }
///
/// // Set the dimensions of the window
/// fn dimensions(&self) -> (usize, usize) {
/// (256, 256)
/// }
/// }
///
/// // The crate's main function
/// fn main() {
/// // Run the program, using the beloved turbofish syntax to
/// // indicate to the runtime which type of program to be run
/// run::<AwesomeProgram>();
/// }
/// ```
///
/// Once you have this working, you can start extending it
/// by implementing the various methods in `pxl::Program`.
///
/// Good luck and have fun!
///
/// <3,
/// Casey
!