@@ -10,8 +10,14 @@ use sl::Train;
1010use sl:: d51:: SL ;
1111use sl:: c51:: C51 ;
1212use sl:: logo:: Logo ;
13+ use sl:: tgv:: TGV ;
1314
14- trait Render : Train + Copy {
15+ fn speed2delay ( speed : u32 ) -> u32 {
16+ // if 4_000_000: 100 km/h -> 40 ms
17+ 4_000_000 /speed
18+ }
19+
20+ trait Render : Train {
1521 fn render ( & self , x : i32 ) {
1622 let mut len = 0 as i32 ;
1723 let y = ncurses:: LINES ( ) / 2 ;
@@ -68,9 +74,11 @@ trait Render: Train + Copy {
6874 }
6975}
7076
77+ impl Render for dyn Train { }
7178impl Render for SL { }
7279impl Render for C51 { }
7380impl Render for Logo { }
81+ impl Render for TGV { }
7482
7583fn main ( ) {
7684 use libc:: signal;
@@ -83,6 +91,7 @@ fn main() {
8391 let mut opts = Options :: new ( ) ;
8492 opts. optflag ( "l" , "" , "logo" ) ;
8593 opts. optflag ( "c" , "" , "C51" ) ;
94+ opts. optflag ( "G" , "" , "TGV" ) ;
8695 opts. optflag ( "a" , "" , "reserved for future use" ) ;
8796 opts. optflag ( "f" , "" , "reserved for future use" ) ;
8897
@@ -102,19 +111,33 @@ fn main() {
102111 ncurses:: leaveok ( ncurses:: stdscr ( ) , true ) ;
103112 ncurses:: scrollok ( ncurses:: stdscr ( ) , false ) ;
104113
105- for x in ( -85 ..ncurses:: COLS ( ) ) . rev ( ) {
106- ncurses:: clear ( ) ;
114+ let train: Box < dyn Train > = {
107115 if matches. opt_present ( "l" ) {
108- Logo . render ( x )
116+ Box :: new ( Logo )
109117 } else if matches. opt_present ( "c" ) {
110- C51 . render ( x)
118+ Box :: new ( C51 )
119+ } else if matches. opt_present ( "G" ) {
120+ // First Non prototype TGV was orange.
121+ // Note: must be called after initscr().
122+ if ncurses:: has_colors ( ) {
123+ ncurses:: start_color ( ) ;
124+ ncurses:: init_pair ( 1 , ncurses:: COLOR_YELLOW , ncurses:: COLOR_BLACK ) ;
125+ ncurses:: attron ( ncurses:: COLOR_PAIR ( 1 ) ) ;
126+ }
127+
128+ Box :: new ( TGV )
111129 } else {
112- SL . render ( x)
113- } ;
130+ Box :: new ( SL )
131+ }
132+ } ;
133+
134+ for x in ( -85 ..ncurses:: COLS ( ) ) . rev ( ) {
135+ ncurses:: clear ( ) ;
136+ train. render ( x) ;
114137 ncurses:: getch ( ) ;
115138 ncurses:: refresh ( ) ;
116139 unsafe {
117- usleep ( 40000 ) ;
140+ usleep ( speed2delay ( train . speed ( ) ) ) ;
118141 }
119142 }
120143 ncurses:: endwin ( ) ;
0 commit comments