CSE 271 Lab 12: Games -- Managing the screen and handling events

0. Announcements

1. Overview

On Friday we introduced the curses library of functions to manage terminal screens. Today we will briefly revisit some of that, and consider how to time events at better than one second granularity.

2. Animation Using Curses

First, compile and run the hello5.c program that we saw near the end of class. Note that programs using curses need to link to the curses library (see comments at the beginning of the code). This version calls sleep(1) to wait one second between movements. While we could replace sleep() with the functionality of sleep utilizing signal(), alarm(), and pause(), we still have the same one second resolution.

3. Reconsidering Time

We really need much higher resolution timers for games. If you search the manual pages for "timer", you'll quickly come across setitimer(2). It provides timings in microseconds, and provides for repeating rather than one-shot timers. For convenience, it may be helpful to use a function that sets up the timing structure for you (using milliseconds, rather than microsecond delays). Here is set_ticker.c.

Exercise 1. Revise the hello5.c program. Replace the call to sleep() with calls to signal() and pause() as described above, but instead of alarm(), use set_ticker() to pause only for 250ms.

4. Handling Input

In the previous hello?.c programs, we had the word hello moving around the screen. How can we incorporate user input to control the program? One possibility would be to make the stdin file descriptor non-blocking, and then perform one read each time we go through the loop: However, that lets us process only one input (typically one keystroke) per loop, and our screen updates will not be perfectly synchronized (as the delay for processing the read, when input is present, will vary).

An alternative is to use timer ticks to handle movement, and the main program to handle user input to change settings. In bounce1d.c we add the ability to reverse direction of the moving word, and to make it faster or slower. Compile and run this program. Demonstrate to yourself that you can make it move faster (shorter timer delay) and slower (longer timer delay) by pressing the 'f' or 's' keys.

5. Two Dimensional Animation

How can we make the object move in multiple dimensions over time, rather than just in a line? Consider the problem of an object that needs to move three blocks to the right, and one block upward. How can we use a timer to handle these needs?

The answer is that the timer can run at a higher speed than the movement actually needs. We just need to keep track of the number of ticks needed before movement along one dimension. See bounce2d.c (along with bounce.h) for an example of a ball moving in two dimensions (same keys to make faster or slower). Notice also that this program ignores SIGINT, requiring you to press Q to exit.

Exercise 2. Revise the bounce2d.c program so that the ball does not get erased at every movement, but instead is replaced by a different character. Thus, the ball will visually leave a trail everywhere it moves.

This lab is intentionally short so that you can take the time to finish project 8 if needed, and otherwise to work on project 9.


Last revised: 9 April 2013, Prof. Davison.