@@ -99,6 +99,7 @@ pub struct Screensaver {
99
99
darken_min : SrgbaTuple ,
100
100
bg_color : Option < SrgbaTuple > ,
101
101
stats_canv : Canvas ,
102
+ delay : Duration ,
102
103
cfg : Config ,
103
104
}
104
105
@@ -123,7 +124,7 @@ impl Screensaver {
123
124
} ,
124
125
bg_color : {
125
126
if let Some ( c) = & cfg. bg_color {
126
- let hc = HexColor :: parse_rgb ( & c) ?;
127
+ let hc = HexColor :: parse_rgb ( c) ?;
127
128
128
129
Some ( SrgbaTuple (
129
130
hc. r as f32 / 255.0 ,
@@ -142,6 +143,7 @@ impl Screensaver {
142
143
} ,
143
144
( scr_size. 0 , 3 ) ,
144
145
) ,
146
+ delay : Screensaver :: calculate_delay ( cfg. fps ) ,
145
147
cfg,
146
148
} ) ;
147
149
@@ -279,6 +281,7 @@ impl Screensaver {
279
281
self . draw_bg ( ) ;
280
282
}
281
283
284
+ /// Fill the screen with background color.
282
285
fn draw_bg ( & mut self ) {
283
286
if let Some ( c) = self . bg_color {
284
287
self . canv
@@ -314,10 +317,8 @@ impl Screensaver {
314
317
/// Run the main loop in the current thread until an external event is received (a key press or
315
318
/// signal) or some internal error is occurred.
316
319
pub fn run ( & mut self ) -> Result < ( ) > {
317
- let delay = Duration :: from_millis ( 1000 / self . cfg . fps as u64 ) ;
318
-
319
320
while !self . state . quit {
320
- self . handle_events ( delay) ?;
321
+ self . handle_events ( self . delay ) ?;
321
322
322
323
if !self . state . pause {
323
324
self . gen_next_piece ( ) ;
@@ -334,6 +335,10 @@ impl Screensaver {
334
335
Ok ( ( ) )
335
336
}
336
337
338
+ fn calculate_delay ( fps : i64 ) -> Duration {
339
+ Duration :: from_millis ( 1000 / fps as u64 )
340
+ }
341
+
337
342
/// Handle input and incoming events.
338
343
fn handle_events ( & mut self , delay : Duration ) -> Result < ( ) > {
339
344
// The poll_input function blocks the thread if the argument is nonzero, so we can use it
@@ -361,6 +366,28 @@ impl Screensaver {
361
366
KeyCode :: Char ( 'c' ) => self . clear ( ) ,
362
367
KeyCode :: Char ( 'l' ) => self . redraw ( ) ?,
363
368
KeyCode :: Char ( 's' ) => self . cfg . show_stats = !self . cfg . show_stats ,
369
+ KeyCode :: Char ( ',' ) => {
370
+ self . cfg . fps -= 1 ;
371
+ self . cfg . fps = self . cfg . fps . clamp ( 1 , i64:: MAX ) ;
372
+
373
+ self . delay = Self :: calculate_delay ( self . cfg . fps )
374
+ }
375
+ KeyCode :: Char ( '.' ) => {
376
+ self . cfg . fps = self . cfg . fps . saturating_add ( 1 ) ;
377
+
378
+ self . delay = Self :: calculate_delay ( self . cfg . fps )
379
+ }
380
+ KeyCode :: Char ( '<' ) => {
381
+ self . cfg . fps -= 10 ;
382
+ self . cfg . fps = self . cfg . fps . clamp ( 1 , i64:: MAX ) ;
383
+
384
+ self . delay = Self :: calculate_delay ( self . cfg . fps )
385
+ }
386
+ KeyCode :: Char ( '>' ) => {
387
+ self . cfg . fps = self . cfg . fps . saturating_add ( 10 ) ;
388
+
389
+ self . delay = Self :: calculate_delay ( self . cfg . fps )
390
+ }
364
391
_ => { }
365
392
} ,
366
393
InputEvent :: Key ( KeyEvent {
@@ -433,15 +460,16 @@ impl Screensaver {
433
460
} ) ;
434
461
435
462
let s = format ! (
436
- "pcs. drawn: {}, lpcs. drawn: {}, c. pcs. drawn: {}, pps. drawn: {}, pcs. rem: {}, l. drawn: {}, pps. len: {}, pipe color: {}" ,
463
+ "pcs. drawn: {}, lpcs. drawn: {}, c. pcs. drawn: {}, pps. drawn: {}, pcs. rem: {}, l. drawn: {}, pps. len: {}, pipe color: {}, fps: {} " ,
437
464
self . state. pieces_total,
438
465
self . state. layer_pieces_total,
439
466
self . state. currently_drawn_pieces,
440
467
self . state. pipes_total,
441
468
self . state. pieces_remaining,
442
469
self . state. layers_drawn,
443
470
pipe_len,
444
- color
471
+ color,
472
+ self . cfg. fps,
445
473
) ;
446
474
447
475
self . stats_canv . put_str ( s) ;
0 commit comments