Today’s lab will be more of an open-ended exploration of the ncurses library, which lets you do some pretty interesting things with “graphics” in the terminal.
These aren’t true graphics because you can’t draw perfect lines or shapes (you’re still limited to using characters), but ncurses lets you place characters at any point on the screen.
There are also colors and special characters available that let you create interesting images and animations.
We’ll start the lab with a fairly short ncurses demo program, which you can download in the archive ncurses.tar.gz.
Run these commands to get started:
$ cd ~/csc161/labs
$ tar xvzf ~/Downloads/ncurses.tar.gz
$ cd ncurses
Run make to build the program, and then run ./fireworks to run it.
Press space to create a firework burst at a random point on the screen, and q to quit.
This program creates a point_t structure to track each point from a firework burst, and it updates those points using simple update rules applied in a loop.
Points are stored in a dynamic array, and they expire after a certain number of updates.
Since points are always added to the back of the array, this dynamic array behaves like a queue.
Even though this program is very short, it uses quite a few ncurses features:
The program is also structured around what its called a game loop. A game loop runs as long as the program is running, and does the following things on each iteration:
As you probably guessed, game loops are common in games, but they make sense for lots of interactive applications. You aren’t obligated to use a game loop in your lab, but it may help you if you plan to let users interact with your program.
The fireworks demo is a good example of some ncurses functionality, and the comments should help you make sense of what’s going on.
But there are many more useful things you can do with the library, which you can find in the NCURSES Programming HOWTO.
In particular, you might find interesting things under these sections:
You’re welcome to explore this manual and use any techniques you find.
While ncurses supports mouse input, it can be tricky to get it working correctly.
If you want to try it you can, but don’t spend too much time if it isn’t working for you.
You can also find man pages for ncurses functions, but they’re not currently installed on MathLAN.
Online manual pages like https://man7.org/linux/man-pages/ can help you here.
You and your partner should think of something you want to create using ncurses and try to implement it.
You don’t have to make your idea especially complicated, but you should either deal with user input, do something interesting with colors, or animate the output.
You’re welcome to borrow code and ideas from the fireworks demo, but your project should be substantially different from the demo.
Here are a few rough ideas in case you’re having trouble thinking of something to work on: