Lab: Fun with ncurses

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:

  • Placing characters a specific locations on the screen
  • Printing special characters (a diamond in this case)
  • Colors
  • Non-blocking input (which lets us check for input rather than waiting for it)

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:

  1. Check for user input (handle any we receive)
  2. Draw to the screen using the current state
  3. Update state for the next iteration
  4. Pause briefly

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.

References

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:

  • Section 6: Output functions (printing strings instead of just characters)
  • Section 8: Attributes (printing bold, underlined, blinking, or other styled text)
  • Section 10: Colors (more ways to add colored text and backgrounds to your output)
  • Section 11: Interfacing with the keyboard (read arrow keys, and other inputs from the keyboard)
  • Section 14.3: Special characters you can use to draw shapes

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.

Your Task

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:

  • Draw the board from a familiar game to the terminal. You don’t need a completed game, but drawing the full game state is a big step.
  • Create an animation of moving stripes, shapes, or something else.
  • Create a program that lets you move a character around the screen, perhaps in some kind of environment.