3.2. Event-driven Programming

SGL provides a simple framework upon which programmers can build event-driven applications. All SGL programs are event driven, which means a program must be able to react to external events such as user input at any time during the program's execution. For example, in an event-driven application the programmer does not use a statement such as

  std::cin >> value;

that causes the program's execution to come to a halt and wait for the user to enter a value. In this situation, the timing of the user's input is predetermined; when the program's execution gets to the statement containing std::cin, the user must provide input. In a graphical, event-driven environment, the user may move the mouse pointer anywhere within an application's window and click the mouse's button at any time. A mouse click in a certain location within the window may be significant to the application. A right mouse button click may bring up a context-sensitive menu that gives the user a choice to save a document or quit the application.

Events other than user input are possible; for example, an animation may use a timer to notify the application every 200 milliseconds that the contents of the window need to be redrawn.

In an event-driven environment, the programmer starts the framework's event processing loop, thereby releasing control of the program's execution. In the SGL, the programmer calls the run method of the application's main window object. The SGL event manager (which is actually the GLUT event loop) provides checks for events. The programmer's responsibility is to write code that the SGL framework can call on when an event occurs. When an event does occur, the SGL event manager calls a corresponding method in the window object.

As an example, consider the user pressing the mouse button when the pointer is within an application's window. The physical window on the display corresponds to a C++ software object that is an instance of a class derived from sgl::Window. The Window class provides a method called mouse_pressed. When the user depresses the mouse button, the event manager reacts by invoking the mouse_pressed method of the affected window. The event manager passes to the method the pointer's location and information about which button was pressed (left or right). A custom class derived from Window can override the mouse_pressed method to implement behavior appropriate for the application. The mouse_pressed method in the base class Window does nothing, so classes derived from Window can ignore mouse pressed events by not overriding the mouse_pressed method. Failure to override the method results in the derived class inheriting the do nothing method of the Window class.

In the SGL, as in GLUT, when the event manager is activated, the client code cannot take back control over the program's execution. The SGL event manager controls the execution until the program terminates.


Copyright  ©2019 Richard L. HaltermanVersion 0.9.5February 17, 2019
Creative Commons License This work is licensed under a Creative Commons License.