Skip to content

Commit b3b0f64

Browse files
committed
simple: Add more set row message filtering
1 parent b50c458 commit b3b0f64

5 files changed

Lines changed: 45 additions & 61 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rust-rocket"
3-
version = "0.13.0"
3+
version = "0.13.1"
44
authors = [
55
"Tim Peters <mail@darksecond.nl>",
66
"Lauri Gustafsson <lauri.gustafsson@iki.fi>",

examples/simple.rs

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,44 +46,36 @@ impl TimeSource {
4646
fn main() {
4747
let mut rocket = Rocket::new("tracks.bin", 60.).unwrap();
4848
let mut time_source = TimeSource::new();
49-
let mut previous_time = Duration::ZERO;
49+
let mut previous_print_time = Duration::ZERO;
5050

5151
'main: loop {
52-
// <Handle other event sources such as SDL here>
52+
// <Handle other event sources such as SDL or winit here>
5353

54-
// Get current frame's time
55-
let time = time_source.get_time();
56-
57-
// Keep the rocket tracker in sync.
58-
// It's recommended to combine consecutive seek events to a single seek.
59-
let mut seek = None;
54+
// Handle events from the rocket tracker
6055
while let Some(event) = rocket.poll_events().ok().flatten() {
6156
match event {
62-
Event::Seek(to) => seek = Some(to),
57+
Event::Seek(to) => time_source.seek(to),
6358
Event::Pause(state) => time_source.pause(state),
64-
Event::NotConnected => /* Alternatively: break the loop here and keep rendering frames */ {
59+
Event::NotConnected =>
60+
/* Alternatively: break the loop here and keep rendering frames */
61+
{
6562
std::thread::sleep(Duration::from_millis(10));
6663
continue 'main;
6764
}
6865
}
6966
}
70-
// It's recommended to call set_time only when necessary.
71-
// This ensures the smoothest scrolling in the editor.
72-
match seek {
73-
Some(to) => {
74-
time_source.seek(to);
75-
continue;
76-
}
77-
None => rocket.set_time(&time),
78-
}
67+
68+
// Get current frame's time and keep the tracker updated
69+
let time = time_source.get_time();
70+
rocket.set_time(&time);
7971

8072
// <In a full demo you would render a frame here>
8173

8274
// Filter redundant output
83-
if time != previous_time {
75+
if time != previous_print_time {
8476
println!("{:?}: test = {}", time, rocket.get_value("test"));
8577
}
86-
previous_time = time;
78+
previous_print_time = time;
8779
thread::sleep(Duration::from_millis(10));
8880
}
8981
}

src/client.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,23 @@
6363
//! let time = music.get_time();
6464
//! let row = time_to_row(time);
6565
//!
66-
//! // Keep the rocket tracker in sync
67-
//! // It's recommended to combine consecutive seek events to a single seek.
66+
//! // Keep the rocket tracker in sync.
67+
//! // When using the low level API, it's recommended to combine consecutive seek events to a single seek.
6868
//! // This ensures the smoothest scrolling in editor.
6969
//! let mut seek = None;
7070
//! while let Some(event) = rocket.poll_events()? {
7171
//! match event {
7272
//! Event::SetRow(to) => seek = Some(to),
7373
//! Event::Pause(state) => music.pause(state),
74-
//! Event::SaveTracks => {/* Call save_tracks and serialize to a file */}
74+
//! Event::SaveTracks => {/* Call save_tracks and write to a file */}
7575
//! }
7676
//! }
77-
//! // It's recommended to call set_time only when the not seeking.
78-
//! match seek {
79-
//! Some(to_row) => {
80-
//! music.seek(row_to_time(to_row));
81-
//! continue;
82-
//! }
83-
//! None => rocket.set_row(row as u32)?,
77+
//! // When using the low level API, it's recommended to call set_time only when the not seeking.
78+
//! if let Some(seek) = seek {
79+
//! music.seek(row_to_time(seek));
80+
//! continue;
8481
//! }
82+
//! rocket.set_row(row as u32)?;
8583
//!
8684
//! // Render frame and read values with Track's get_value function
8785
//! let _ = get(&mut rocket, "track0", row);

src/simple.rs

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -50,28 +50,18 @@
5050
//! // Create window, render resources etc...
5151
//!
5252
//! loop {
53-
//! // Get current frame's time
54-
//! let time = music.get_time();
55-
//!
56-
//! // Keep the rocket tracker in sync.
57-
//! // It's recommended to combine consecutive seek events to a single seek.
58-
//! let mut seek = None;
53+
//! // Handle events from the rocket tracker
5954
//! while let Some(event) = rocket.poll_events().ok().flatten() {
6055
//! match dbg!(event) {
61-
//! Event::Seek(to) => seek = Some(to),
56+
//! Event::Seek(to) => music.seek(to),
6257
//! Event::Pause(state) => music.pause(state),
6358
//! Event::NotConnected => break,
6459
//! }
6560
//! }
66-
//! // It's recommended to call set_time only when the not seeking.
67-
//! // This ensures the smoothest scrolling in editor.
68-
//! match seek {
69-
//! Some(to) => {
70-
//! music.seek(to);
71-
//! continue;
72-
//! }
73-
//! None => rocket.set_time(&time),
74-
//! }
61+
//!
62+
//! // Get current frame's time and keep the tracker updated
63+
//! let time = music.get_time();
64+
//! rocket.set_time(&time);
7565
//!
7666
//! // Read values with Rocket's get_value function while rendering the frame
7767
//! let _ = rocket.get_value("track0");
@@ -132,7 +122,8 @@ pub enum Event {
132122
Seek(Duration),
133123
/// The tracker pauses or unpauses.
134124
Pause(bool),
135-
/// The client is not connected. Next call to `poll_events` will attempt a reconnection.
125+
/// The client is not connected. Next calls to [`poll_events`](Rocket::poll_events) will eventually attempt to
126+
/// reconnect.
136127
///
137128
/// There are three equally sensible ways to handle this variant:
138129
///
@@ -144,22 +135,22 @@ pub enum Event {
144135
///
145136
/// Options 2 and 3 result is a busy wait, e.g. waste a lot of CPU time.
146137
/// It's better to combine them with `std::thread::sleep` for at least a few milliseconds in order to mitigate that.
147-
///
148-
/// See [module documentation](crate::simple#Examples) and [`poll_events`](Rocket::poll_events).
138+
///
139+
/// See `simple.rs` in the `examples`-directory.
149140
NotConnected,
150141
}
151142

152143
/// Provides sync values.
153144
///
154145
/// # Usage
155146
///
156-
/// See [module-level documentation](crate::simple#Usage).
147+
/// See [module documentation](crate::simple#Usage).
157148
pub struct Rocket<P: AsRef<Path>> {
158149
path: P,
159150
bps: f32,
160151
row: f32,
161152
#[cfg(not(feature = "player"))]
162-
sent_row: u32,
153+
tracker_row: u32,
163154
#[cfg(not(feature = "player"))]
164155
connected: bool,
165156
#[cfg(not(feature = "player"))]
@@ -175,7 +166,7 @@ impl<P: AsRef<Path>> Rocket<P> {
175166
///
176167
/// # Without `player` feature
177168
///
178-
/// Attemps to connect to a rocket tracker, and retries indefinitely every 1s if connection can't be established,
169+
/// Attempts to connect to a rocket tracker, and retries indefinitely every 1s if connection can't be established,
179170
/// during which the function doesn't return and the caller is **blocked**.
180171
///
181172
/// # With `player` feature
@@ -235,7 +226,7 @@ impl<P: AsRef<Path>> Rocket<P> {
235226
bps: bpm / SECS_PER_MINUTE,
236227
row: 0.,
237228
#[cfg(not(feature = "player"))]
238-
sent_row: 0,
229+
tracker_row: 0,
239230
#[cfg(not(feature = "player"))]
240231
connected: true,
241232
#[cfg(not(feature = "player"))]
@@ -284,12 +275,14 @@ impl<P: AsRef<Path>> Rocket<P> {
284275
#[cfg(not(feature = "player"))]
285276
{
286277
let row = self.row as u32;
287-
if self.connected && row != self.sent_row {
288-
while let Err(ref e) = self.rocket.set_row(row) {
289-
print_errors(PREFIX, e);
290-
self.connected = false;
278+
if self.connected && row != self.tracker_row {
279+
match self.rocket.set_row(row) {
280+
Ok(()) => self.tracker_row = row,
281+
Err(ref e) => {
282+
print_errors(PREFIX, e);
283+
self.connected = false;
284+
}
291285
}
292-
self.sent_row = row;
293286
}
294287
}
295288
}
@@ -371,6 +364,7 @@ impl<P: AsRef<Path>> Rocket<P> {
371364
Ok(Some(event)) => {
372365
let handled = match event {
373366
crate::client::Event::SetRow(row) => {
367+
self.tracker_row = row;
374368
let beat = row as f32 / ROWS_PER_BEAT;
375369
Event::Seek(Duration::from_secs_f32(beat / self.bps))
376370
}

0 commit comments

Comments
 (0)