Click here to Skip to main content
11,934,783 members (45,096 online)
Click here to Skip to main content
Add your own
alternative version


92 bookmarked

WaTor - An OpenGL based screensaver

, 14 Feb 2015 GPL3
Rate this:
Please Sign up or sign in to vote.
An article on implementing a simple predator prey simulation using OpenGL and MFC.

The latest version of the Wa-Tor article can be found at

Table of contents

  1. Table of contents
  2. Introduction
  3. Set of rules
  4. Screen saver basics
  5. Installation
  6. Configuration
  7. Limitations
  8. License
  9. References
  10. Release notes


This article describes the implementation of an OpenGL screen saver based on a discrete simulation of predator-prey interaction. The simulation is called Wa-Tor. It was originally described by Alexander K. Dewdney in the Scientific American magazine.

It simulates the hypothetical toroidal Planet Wa-Tor (Water Torus) whose surface is completely covered with water, occupied by two species: fish and sharks. The sharks are the predators. They eat the fish. The fish exist on a never ending supply of plankton. Both sharks and fish live according to a strict set of rules. This simulation of a simple ecology is highly dynamic as both species are walking a thin line between continuing life and extinction.

The screensaver is based on a dialog based MFC application combined with OpenGL for the 2D drawing. As usual, for OpenGL, using the binary without a hardware or drivers correctly supporting it will result in very low frame rates. The source code requires Microsoft Visual Studio V7.1 in order to compile, MSVC6 is not supported.

Set of rules

In this paragraph, I will briefly explain the basic rules for a Wator simulation and describe the changes necessary in order to implement the screensaver. In general, the simulation is based on discrete time steps. The simulation runs on a rectangular grid. To represent the toroidal world, opposing sides of the grid are connected. If an individual moves out on one side of the simulation domain, it reenters immediately on the opposing side. Fish and shark move every time step (if possible) and interact according to the following set of rules:

Rules for fish

In every time step, a fish moves randomly to one of the four neighboring fields, provided it is empty. Every fish has a predefined "breed time". On exceeding this time, it gives birth to a new fish in one of the neighboring cells, provided this randomly selected cell is free. (If not nothing happens.) Breed time counter of both the original and the descendant fish will be reset to zero. Technically fish never die. They live until they reach the breed time, then they clone and both parent as well as offspring restart their life cycle. (To clarify: according to the simulation the parent dies and creates two offspring individuals, but I don't delete the parent, I just reset it.)

The following picture shows options for prey movement. Arrows indicate possible movements. Fish are not allowed to move to cells occupied by sharks. If there are no free neighboring cells no movement occurs.

Rules for sharks

Sharks move randomly to fields that are either free or occupied by fish. Every round they lose one point of energy. If they enter a field occupied by a fish they eat the fish and gain a defined amount of energy. If the energy level drops below zero the shark dies. If the energy exceeds a predefined value sharks create an offspring in a free neighboring field. The energy is split evenly between the parent and the child.

Modifications made to the original algorithm

In order to turn this simulation into a screensaver, I made some changes to the original set of rules. According to the original algorithm, sharks do not move randomly. Instead they move specifically to cells in their neighborhood containing fish. Testing this is slow, thus I allow only random movement. Surprisingly there is no visible difference. Alexander K. Dewdney suggested an implementation very similar to a cellular automata. If you implement Wa-Tor that way and by looping over all cells in the simulation domain, you are forced to include cells that do not contain any individual in the calculation. Since this is too slow I chose to implement it as a linked list, thus instead of looping over all cells, my algorithm loops over existing individuals only.

Theoretical background

The information provided here is taken from the Wikipedia article on Lotka-Volterra differential equations. For a more detailed explanation I refer you to this article.

Drawing the number of individuals per species versus time yields curves characteristic for the so called Lotka-Volterra equations (also known as predator-prey equations).

Diagram: Population size vs. time for a predator prey model..

This behaviour can be described by two differential equations. There usual form is:


  • y is the number of some predator (here: sharks)
  • x is the number of its prey (here: fish)
  • t represents the development of the two populations against time
  • alpha, beta, gamma and delta are parameters representing the interaction of the two species.

Although the solution to these differential equations show a periodic behaviour, they can not be expressed in terms of normal trigonometric functions. However, an approximate linearised solution yields a simple harmonic motion with the population of predators leading that of prey by 90%.

Screen saver basics

Technically a Windows screensaver is a normal executable with the extension scr. Additionally it has to have a defined command line interface in order to display itself in configuration mode, in preview mode or in screen saver mode.

Command line optionn Saver action
/P:### Start the screensaver in preview mode. Treat ### as the decimal representation of a HWND. Popup a child window of this HWND and run in preview mode inside that window.
/C:### Open the screen saver configuration dialog. If ### is not present GetForegroundWindow() should be the parent of this dialog. Otherwise ### should be treated as the decimal representation of a HWND, which should be used as a parent.
/S Run as screen saver in full screen mode. When there is some kind of keyboard or mouse activity in the screen saver mode, the screen saver is ended.

Table 1: Command line options.


Download the archive with the binary and copy the file WatorSaver.scr to your Windows/System322 subdirectory. I do not provide a setup program due to the simplicity of the installation process.


After moving the screen saver to your windows/system32 directory, you can edit its settings like any other screen saver settings. Go to the Windows desktop, right click, and in the context menu, select the display options entry.

The configuration dialog allows setting up the simulation parameters described above as well as additional settings for the display options.

Screensaver parameters

All changes made to the parameters will be reflected instantly in the preview window. The following table lists the parameters and their meaning for the simulation process:

Prey breed time The number of time steps necessary before a prey can create an offspring.
Energy per hunted prey The energy a predator gets for each prey individual it eats. In each time step, the predator loses one energy point. It dies when the energy falls below zero.
Energy to create offspring The minimum amount of energy a predator must have in order to create an offspring. When creating an offspring, the energy is evenly split between the newly created offspring and the parent.
Pixelsize The size of an individual in pixels. Reducing this number will require very high performance!


Like most screensavers, this is a program featuring a controlled amusing misuse of system resources. It is currently a beta version and you use it without warranty of any kind!

Currently the screen saver does not support multiple monitors. Moreover it will only save your screen if you disable all static text displays in its configuration dialog. This won't change in future versions but I don't consider this a problem since today's TFT Displays don't need a screensaver anyway.

You use it at your own risk, no warranties of any kind!


Wator Screensaver
Copyright 2007 Ingo Berg

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA



A more detailed introduction on screen saver programming and issues related to this article can be found at the following places:

Release Notes

Rev 0.90 (Beta): 31/07/2005

First version of the screensaver released. This is a beta version released for testing purposes only. There is much room for improvement like adding multiple monitor support.

Rev 1.0: 18/08/2005

This release contains bug fixes and slight modifications that make the screensaver look prettier (nothing spectacular).

  • Fixed bug that prevented the screensaver from running on ATI graphics cards.
  • Added a background pattern.
  • Changed title bitmap.
  • Statistic is now scaled between its minimum and maximum.

Rev 1.01: 21/01/2006

  • Support for multiple monitors added.
  • The screensaver can now be deactivated by mouse moves.

Rev 1.02: 03/03/2007

  • License changed to GPL-II.


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Software Developer
Germany Germany
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Mahdi Nejadsahebi19-May-13 5:38
memberMahdi Nejadsahebi19-May-13 5:38 
GeneralThird Leg, Plankton Pin
RDABC11-Apr-07 11:39
memberRDABC11-Apr-07 11:39 
GeneralRe: Third Leg, Plankton Pin
iberg13-Apr-07 8:52
memberiberg13-Apr-07 8:52 
GeneralEnhancement: Filing populaton in and out Pin
tbrammer4-Dec-06 4:18
membertbrammer4-Dec-06 4:18 
GeneralRe: Enhancement: Filing populaton in and out Pin
iberg4-Dec-06 12:21
memberiberg4-Dec-06 12:21 
AnswerRe: Enhancement: Filing populaton in and out Pin
tbrammer4-Dec-06 22:55
membertbrammer4-Dec-06 22:55 
GeneralRe: Enhancement: Filing populaton in and out Pin
iberg5-Dec-06 12:56
memberiberg5-Dec-06 12:56 
QuestionBugBug Pin
Highcooley13-Nov-06 13:45
memberHighcooley13-Nov-06 13:45 
AnswerRe: BugBug Pin
iberg13-Nov-06 22:01
memberiberg13-Nov-06 22:01 
GeneralRe: BugBug Pin
Highcooley13-Nov-06 23:56
memberHighcooley13-Nov-06 23:56 
QuestionWhat Dewdney meant, maybe. Pin
Tadeusz Westawic3-Feb-06 17:28
memberTadeusz Westawic3-Feb-06 17:28 
AnswerRe: What Dewdney meant, maybe. Pin
iberg4-Feb-06 4:22
memberiberg4-Feb-06 4:22 
GeneralRe: What Dewdney meant, maybe. Pin
Tadeusz Westawic5-Feb-06 7:32
memberTadeusz Westawic5-Feb-06 7:32 
GeneralRe: What Dewdney meant, maybe. Pin
iberg11-Feb-06 14:24
memberiberg11-Feb-06 14:24 
GeneralRe: What Dewdney meant, maybe. Pin
Jasmine250127-Nov-06 19:52
memberJasmine250127-Nov-06 19:52 
GeneralLike it! Pin
Arinir24-Aug-05 23:47
memberArinir24-Aug-05 23:47 
GeneralRe: Like it! Pin
iberg25-Aug-05 0:07
memberiberg25-Aug-05 0:07 
GeneralRe: Like it! Pin
Jasmine250129-Nov-06 9:51
memberJasmine250129-Nov-06 9:51 
GeneralRe: Like it! Pin
iberg1-Dec-06 16:16
memberiberg1-Dec-06 16:16 
GeneralGreat presentation Pin
Martin.Holzherr24-Aug-05 22:10
memberMartin.Holzherr24-Aug-05 22:10 
GeneralRe: Great presentation Pin
iberg24-Aug-05 22:32
memberiberg24-Aug-05 22:32 
GeneralATI card problems Pin
iberg16-Aug-05 11:59
memberiberg16-Aug-05 11:59 
GeneralRe: ATI card problems Pin
Ryan Beesley23-Aug-05 13:32
memberRyan Beesley23-Aug-05 13:32 
GeneralRe: ATI card problems Pin
iberg24-Aug-05 2:42
memberiberg24-Aug-05 2:42 
General"Thus you have an infinite world on a finite grid."?! Pin
PedroMC11-Aug-05 4:43
memberPedroMC11-Aug-05 4:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151126.1 | Last Updated 14 Feb 2015
Article Copyright 2005 by iberg
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid