UNIX Lecture Notes – Chapter 06

Categories:

Recommended

6.1 Introduction

An event-driven program is a program in which the ow of control of the program depends upon the occurrence of external events. The typical event-driven program remains in a state in which it listens for or awaits events, selects which events to respond to next, responds to them, and then returns to its listening state. Event driven programs must have some type of event recognition mechanism and event handling mechanism. Unlike sequential programs, event-driven programs must work correctly in an environment in which unexpected, dynamic, external stimuli come from sources such as users, hardware, or other processes.

6.2 Common Features of Event-Driven Programs

Event-driven programs include programs with graphical user interfaces, operating systems, device drivers, control system software, and video games, to name a few. Writing video games is a good means to master event-driven programming, because their requirements include those commonly encountered in other event-driven programs (EDPs), and because it is generally fun to write them. Typical video games need to handle the following:

Spatial control Like many other EDPs, video games have to manage the two-dimensional screen image, maintaining information about where all of its objects are located.

Timing Video games, like many EDPs, usually have moving images whose velocities are monitored and controlled by the game. Games may also time the user’s inputs. They often have to keep track of clock time and cause certain events to happen at specific times or at specific intervals of time.

Asynchronous inputs and signals Video games, like all EDPs, have to respond to unpredictable user inputs such as mouse clicks, mouse motion, and keystrokes, as well as inputs from other sensors. These events are asynchronous with respect to the execution of the program.

Process synchronization Video games usually have multiple threads of control. One or more objects might be moving independently across the screen while the user independently types or uses a tracking device. The program has to keep track of and synchronize these independent processes and objects.

6.3 Terminal-based Games

Early UNIX systems often came bundled with a large variety of terminal-based games, i.e., games that ran in a pseudo-terminal window rather than a console window. The distinction between these is that a terminal window is a character I/O device that treats its display area as a two- dimensional array whose cells can contain characters, whereas a console window is a bit-mapped display device each of whose pixels can be accessed individually. Historically, these games were located in /usr/games. These days, system administrators do not install the games, one reason being because they know that users tend to use up system resources having fun instead of working1. Another reason for not installing the terminal-based games is that there are now many free games that run on top of the X Windows system, using bit-mapped displays, making the older games seem less fun to those accustomed to the advanced technology. Perhaps those who appreciate the old terminal-based games are the same people who still appreciate black-and-white movies. The great advantage of writing a terminal-based game over one that uses a GUI, is that it is easier to concentrate on the principles rather than the details of the windowing system. Although it is more exciting to create a game that runs in a graphics window, that requires an entirely different set of topics to learn and it would be a distraction from the objective of learning how to control and use signals, how to use time and synchronization, and how to control what the user is able to do with the keyboard. If you also had to learn about video cards, X Windows, and widgets and windows, your time would be consumed with that instead. We will write a game similar to the game of pong, which runs in a terminal window. The game of pong is a simplification of an arcade game. In pong, there are two controlled objects: a ball and a paddle. The ball is a small circle or square that moves across the screen at some xed speed. The paddle is a vertical line segment that the user can move up and down with keystrokes. The edges of the terminal window are walls o of which the ball bounces.

Attribution

Stewart Weiss (2019), UNIX Application and System Programming, URL: http://www.compsci.hunter.cuny.edu/~sweiss/course_materials/unix_lecture_notes.php

This work is licensed under Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) :  (https://creativecommons.org/licenses/by-sa/4.0/).

VP Flipbook Maker

Get ready to create awesome flipbooks with Visual Paradigm Online Flipbook Maker! Easily convert your content into interactive flipbooks with just a few clicks and have fun customizing them with intuitive design tools!