Flatland

Documentation

SourceForge.net Logo

simple.cpp

Go to the documentation of this file.
00001 // Summary: Very simple demo program for flatland.
00002 // Copyright: 2007  Philip Rideout.  All rights reserved.
00003 // License: see bsd-license.txt
00004 
00005 #include <ode/ode.h>
00006 #include "demo.hpp"
00007 #include "os.h"
00008 
00009 using namespace Demo;
00010 
00011 const int MaxObjects = 400;
00012 
00013 static void AddFallingObject(Space &space, Object *object);
00014 
00015 /// Entry point.
00016 int main(int argc, char** argv)
00017 {
00018     int width = 800;
00019     int height = 600;
00020 
00021     osInit(width, height, 0, 0);
00022 
00023     // Instantiate the app object.
00024     App app(width, height);
00025     app.InitGl();
00026     app.InitOde();
00027 
00028     // Insert some objects into the space.
00029     Space &space = app.GetSpace();
00030     space << new Demo::Line(vec2(0, 600), vec2(800, 600));
00031     space << new Demo::Line(vec2(800, 0), vec2(800, 600));
00032     space << new Demo::Line(vec2(0, 0), vec2(0, 600));
00033     {
00034         Wheel *wheel = new Wheel(vec2(390, 550), 50);
00035         wheel->GetFlatlandObject()->SetMass(5000);
00036         wheel->Property().outlineThickness = 1;
00037         wheel->Insert(space);
00038     }
00039     space.mark();
00040 
00041      // Upper limit on frames-per-second.
00042     const float fps = 60.0;
00043 
00044     // Simulation step size; proportional to speed and inversely proportional to precision.
00045     const float delta = 0.25;
00046 
00047     // Set up some time variables.  Time is measured in milliseconds.
00048     unsigned int currentTime = osGetMilliseconds();
00049     unsigned int previousSampleTime = currentTime;
00050     unsigned int previousDrawTime = currentTime;
00051     const unsigned int sampleDelay = 5000;
00052     const unsigned int drawDelay = (unsigned int) (1000.0 / fps);
00053 
00054     // Count the simulation "steps" (iterations) since the last performance sample.
00055     unsigned int stepCount = 0;
00056 
00057     // Main simulation loop.
00058     OS_Event event;
00059     for (;;)
00060     {
00061         // process all events
00062         while (osPollEvent(&event))
00063         {
00064             switch(event.type)
00065             {
00066                 case OS_QUIT: exit(0);
00067                 case OS_KEYUP:
00068                     switch (event.key.key)
00069                     {
00070                         case 'x': case 'X': case 'q': case 'Q':
00071                         case OSK_ESCAPE: exit(0);
00072                     }
00073             }
00074         }
00075 
00076         // update the clock
00077         currentTime = osGetMilliseconds();
00078 
00079         // take a performance sample and calculate the steps-per-millisecond
00080         if (space.size() == MaxObjects && currentTime - previousSampleTime > sampleDelay)
00081         {
00082             double spms = (double) stepCount / (double) (currentTime - previousSampleTime);
00083             previousSampleTime = currentTime;
00084             stepCount = 0;
00085             printf("%d\n", spms);
00086         }
00087 
00088         // draw the scene and do a page flip
00089         if (currentTime - previousDrawTime > drawDelay)
00090         {
00091             app.Draw();
00092             osSwapBuffers();
00093             previousDrawTime = currentTime;
00094         }
00095 
00096         // update the simulation
00097         app.Step(delta);
00098         ++stepCount;
00099 
00100         // add a new block or ball every now and then
00101         if (space.size() < MaxObjects)
00102         {
00103             if (!(stepCount % 100))
00104                 AddFallingObject(space, new Ball(vec2(400, 0), 15));
00105             if (!((stepCount + 50) % 100))
00106                 AddFallingObject(space, new Block(vec2(400, 0), 20, 20));
00107         }
00108     }
00109 
00110     return 0;
00111 }
00112 
00113 /// Assigns a color to an object and adds it to the space.
00114 void AddFallingObject(Space &space, Object *object)
00115 {
00116     if (space.size() >= MaxObjects)
00117         space.pop();
00118 
00119     Color pastel(60.0f + (rand() % 90) - 45);
00120     object->Property().outlineColor = pastel;
00121     pastel.IncreaseBrightness(0.5);
00122     object->Property().fillColor = pastel;
00123     object->Property().outlineThickness = 1;
00124     space.push_back(object);
00125 }

Generated on Sat Jan 13 17:20:21 2007 for Flatland by doxygen 1.5.1