Click Here if you would like to see a complete code listing to this point.
In the last tutorial, Get More User-provided Options, we added the options to select different images. Here, we'll be adding the C51 model of train. At this point, we could just duplicate the "d51.rs" file and rename it to "c51.rs", and rename all the "d51" bits to "c51", and make some minor modifications elsewhere, and we'd be done with it. But as I think about that, I realize that's going to duplicate a lot of code for each different image we want to use, and I wonder if there might be a better way to do this.
I also think about how the author of a new image, of a boat, or a plane, or a rocket, etc, has to fit his/her ASCII-art into the confines of a vector of vector of Strings, and has to deal with other complexities, and wonder if that can be simplified.
I have already gone quite a ways with this tutorial using my first Plan B for the C51 train (and other images), which is the path you'll follow if you continue following this page, but I'm now thinking I'll chase after a second Plan B (ooh, I know, we can call it "Plan C"!) for the C51 train (and other images). You're free to follow either path (be aware that Plan B eventually stops before total completion, but has a lot of informative Rust programming involved), but I think you'll find Plan C to be simpler, and perhaps, better. Continue reading on this page for the original Plan B, or Click Here for Plan C.
Create a C51 Module
The first thing to do is to create a new "src/c51.rs" file, containing the following:
Then edit "src/main()" to look like this:
When you compile/run this program, it should function as it did previously, with the exception that if you tell it to run the "C51" train (e.g., $ cargo run -- --kind c51), you'll just get a message that says, "This is the 'c51' module."
$ cargo run -- --kind c51
Compiling sl v0.1.0 (/home/kent/projects/RUST/sl)
warning: unused variable: `oops`
--> src/d51.rs:31:40
|
31 | pub fn draw_d51(delay: u64, fly: bool, oops: bool) {
| ^^^^ help: if this is intentional, prefix it with an underscore: `_oops`
|
= note: `#[warn(unused_variables)]` on by default
warning: `sl` (bin "sl") generated 1 warning
Finished dev [unoptimized + debuginfo] target(s) in 0.73s
Running `target/debug/sl --kind c51`
This is the 'c51' module.
$
Add Other Modules
While we're here, we could add similar modules for "little", "jack", "boat", and "plane", but that might be getting ahead of ourselves, especially if we wind up discovering we don't like the method we're using for the C51.
Get the C51 Image
Find where you have the C-version of "sl", and copy the "sl.h" file to "src/c51.txt" (or you can just copy the text below). Then edit "src/c51.txt" and remove everything that is not the drawings in quotes, and put they main body of the train over each set of wheels, and do whatever other massaging of the date that needs to be done to produce the following:
We're going to have our program read this file, and build our vector of vectors of Strings from it. This format, without all the vector-code overhead, will make it easier for ASCII-artists to produce their ASCII-art.
Make the D51 Drawing Routines more Generic, To Be Used With All the Images
As we think about this, most everything in the "src/d51.rs" file will need to be duplicated for the C51 train, and for every other image we might want to draw. Rather than duplicate this code, what if we make it available to all the images to be drawn?
This drawing code seems integral to the main program, so it could go into "src/main.rs", but we didn't really want to clutter up that file, so we created "src/extras.rs" to "hold the overflow". That seems like a good place to put this code.
Let's move ("edit/cut") the entire "draw_d51()" function out of "src/d51.rs" into ("edit/paste") "src/extras.rs". It doesn't really matter where it goes, but I pasted the "draw_d51() function at the bottom of the "src/extras.rs" file.
We can also move the "my_mvaddstr()" function. Again, it can go anywhere in the "src/extras.rs" file, but I pasted it at the bottom.
Now that all the ncurses code and sleep code has moved, we no longer need those "use" lines in "src/d51.rs", but we do need them in "src/extras.rs", so let's move those lines also. Just put them near the top of the file.
If we did our moving correctly, there are only two changes we need to make to our code for it to still do what it did before. The first is to make the "get_d51()" function public, so that the code that is now in another file ("extras.rs") can find it: