Code Documentary
Where to find stuff
Simulator classes: classes.h contains the class definitions for all the Simulator classes (Simulator, TrackSegment, TrackRange, Train, TrainCar, TurnOut, Sensor). This was done to eliminate a large number of include/definition problems during compilation that were not being resolved by forward-declarations. However, each of the actual classes is contained in its own .cpp file.
Graphical classes: simwin.h contains class definitions for CMainWin and CApp. simwin.cpp contains the class code for these two classes, as well as the global logprint() function. tracksdisplay.h and .cpp contain the classes TrackDisplay, SensorDisplay, and TurnOutDisplay.
Miscellaneous: The ConfigReader class in in configreader.h/.cpp. dll_api.h/.cpp provides a rough implementation of the external API, which simply retrieves the simulator from the CMainWin instance and calls its API functions. ids.h and junk2.rc are for for the windows menus and messages.
How the system initializes
- CApp/CMainWin are created.
- CMainWin::OnLoadSystem is called (currently from the menu).
- Creates a new Simulator instance.
- Calls Simulator::loadTracks, loadTrains, etc.
- Calls CMainWin::loadSystem().
- Calls CMainWin::loadTracks, loadSensors, loadTurnOuts.
- Calls CMainWin::drawSystem().
- CMainWin::OnStartSimulation is called (currently from the menu).
- Starts Windows Timer for simulation (see below)
- System runs until CMainWin::OnStopSimulation is called, which kills the timer.
How the system runs
- CMainWin::OnTimer is called by the timer from above:
- Simulator::step()
- Calls Train::move() for each train in the simulation.
- CMainWin::drawSystem()
- Draws each track part
- Draws any trains on each part.
- Updates status of SensorDisplays, draws each one.
- Updates status of TurnOutDisplays, draws each one.
- End of Timer Event.
How a train moves
- Train::move() is called by Simulator::step().
- TrainCar::move() is called for each car in the Train.
- Actual distance travelled is calculated.
- TrackSegment::move(car, dist) is called for each track the car is on.
- TrackRange for the car's position is moved
- Calculates new position and returns whether the train has partially left this track
- If leaving:
- If connected to a track in that direction, call thatTrack::accept(car, from_this_track, dist_left)
- Otherwise, car::derail()
- TrackSegment::checkInteractions()
- Check for entering/leaving sensors.
- Check for overlapping cars, car::derail()
- TrackSegment::move done.
- TrackRange for the car's position is moved
- TrainCar::move done.
- Train::move() done