Section 16 Home page Section 18

17. Sokoban: A More Complex Example

As a more complex example of a complete GEM program, my Sokoban implementation may be worth a look. The source code is at The program illustrates use of a menu, multiple windows, file selector and multiple event types.

If you review that source code, you will find many things similar to the code in this document, however, some things are different. This document was written, in part, to simplify and unify the code I have for GEM handling in my programs, and I haven’t put all the simplifications back. Also, in a larger application, there are simply more things to handle, which add a layer of complexity.

In particular, note how I manage different window types: there is one window showing a list of levels, and other windows showing the levels in the game, and a third window type is used to show statistics. I have a slot in win_data called window_type, and this is set to LEVELS, POSITION or STATISTICS when the window is created. Within draw_interior I now switch to the appropriate calling code, depending on the window type.

Additionally, in some situations, such as when a move is made, not all of the window needs to be redrawn. I have used a flag for the type of update: when a move is made, this flag is MOVE. The drawing code for the position will then only update the parts of the window related to that last move. Such refinements are necessary to prevent your application flickering too much, due to constant updates.

Sokoban also provides an example of evnt_multi, as the program responds to window events, as above, but also mouse and keyboard events. Double click mouse events are monitored and, when they occur on the table of levels, their position is compared with a list of positions of the level names, so the program can open the correct level as selected in the table.