Documentation Set for Swarm 2.2
Prev | Chapter 4. Mag 4x:Sketch of Code | Next

# 4.8. The main() function

The last main type of code needed for an application is the function `main()` the first function called in your program. All the real work has been done already - all that's left is to create the objects at the right time.

    int main(int argc, const char** argv)
    {
     id theTopLevelSwarm ;

     // Swarm initialization: all Swarm apps must call this first.
     initSwarm(argc, argv);

     // swarmGUIMode is set in initSwarm(). It's set to be 0 if you
     // typed `heatbugs --batchmode' or `heatbugs -b', Otherwise, it's set to
     // 1.

     if (swarmGUIMode == 1)
     // We've got graphics, so make a full ObserverSwarm to get GUI objects
       theTopLevelSwarm = [HeatbugObserverSwarm create: globalZone];
     else
     // No graphics - make a batchmode swarm and run it.
       theTopLevelSwarm = [HeatbugBatchSwarm create: globalZone];

     [theTopLevelSwarm buildObjects];
     [theTopLevelSwarm buildActions];
     [theTopLevelSwarm activateIn: nil];
     [theTopLevelSwarm go];

     // theTopLevelSwarm has finished processing, so it's time to quit.
     return 0;
    }

`main()` calls `initSwarm` (required in all Swarm applications). It then detects if it should do graphics or not, creates the appropriate top level Swarm to contain the model, and sets it to running. Simple as that!

Prev | Home | Next
Building a Data Graph | Up | Conclusion