@@ -10,11 +10,17 @@ 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 ;
17- let y = unsafe { ncurses:: LINES } / 2 ;
23+ let y = ncurses:: LINES ( ) / 2 ;
1824 let body_iter = self . body ( ) . iter ( ) ;
1925 let wheelset_iter = self . wheelset ( x as usize ) . iter ( ) ;
2026 let iter = body_iter. chain ( wheelset_iter) ;
@@ -55,7 +61,7 @@ trait Render: Train + Copy {
5561 }
5662
5763 fn render_line ( & self , y : i32 , x : i32 , line : & str ) {
58- let paint_len = ( unsafe { ncurses:: COLS } - x) as usize ;
64+ let paint_len = ( ncurses:: COLS ( ) - x) as usize ;
5965 if paint_len < line. len ( ) {
6066 ncurses:: mvaddstr ( y, x, & line[ 0 ..paint_len] ) ;
6167 } else if x < 0 {
@@ -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
@@ -98,23 +107,37 @@ fn main() {
98107
99108 ncurses:: noecho ( ) ;
100109 ncurses:: curs_set ( ncurses:: CURSOR_VISIBILITY :: CURSOR_INVISIBLE ) ;
101- ncurses:: nodelay ( unsafe { ncurses:: stdscr } , true ) ;
102- ncurses:: leaveok ( unsafe { ncurses:: stdscr } , true ) ;
103- ncurses:: scrollok ( unsafe { ncurses:: stdscr } , false ) ;
110+ ncurses:: nodelay ( ncurses:: stdscr ( ) , true ) ;
111+ ncurses:: leaveok ( ncurses:: stdscr ( ) , true ) ;
112+ ncurses:: scrollok ( ncurses:: stdscr ( ) , false ) ;
104113
105- for x in ( -85 .. unsafe { 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