fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / System / Statistics / Foregrounds / testPerfMonitorForeground.cpp
blob61c7bd639a7b4856c6d5a7d3a31f1729de99cdd3
1 #include "OSGGLUT.h"
2 #include "OSGConfig.h"
3 #include "OSGSimpleGeometry.h"
4 #include "OSGPassiveWindow.h"
5 #include "OSGSimpleSceneManager.h"
6 #include "OSGSceneFileHandler.h"
8 #include "OSGDrawable.h"
9 #include "OSGPerfMonitorForeground.h"
10 #include "OSGPerfMonitor.h"
11 #include "OSGPerfMonitorGuard.h"
12 #include "OSGBaseFunctions.h"
14 #include "OSGTextureBaseChunk.h"
15 #include "OSGMaterialChunk.h"
16 #include <boost/assign/list_of.hpp>
19 OSG::SimpleSceneManagerRefPtr mgr(NULL);
20 OSG::RenderActionRefPtr act(NULL);
22 OSG::PassiveWindowRecPtr pwin;
23 OSG::PerfMonitorForegroundRecPtr perfmon_fg;
25 bool show = true;
27 void doStuff(); // Function to just do some things to analyze
29 // redraw the window
30 void display(void)
32 doStuff();
33 OSG::PerfMonitor::the()->updateFrame(); // Have to update the stats each "frame"
35 mgr->redraw();
37 // all done, swap
38 glutSwapBuffers();
41 // react to size changes
42 void reshape(int w, int h)
44 mgr->resize(w,h);
45 glutPostRedisplay();
48 // react to mouse button presses
49 void mouse(int button, int state, int x, int y)
51 if (state)
52 mgr->mouseButtonRelease(button, x, y);
53 else
54 mgr->mouseButtonPress(button, x, y);
56 glutPostRedisplay();
59 // react to mouse motions with pressed buttons
60 void motion(int x, int y)
62 mgr->mouseMove(x, y);
63 glutPostRedisplay();
67 // react to keys
68 void keyboard(unsigned char k, int, int)
70 switch(k)
72 case 27:
74 OSG::osgExit();
75 exit(0);
77 case 'm':
78 perfmon_fg->cycleMode(-1);
79 break;
80 case 'M':
81 perfmon_fg->cycleMode();
82 break;
83 case 'S':
84 case 's':
85 mgr->setStatistics(!mgr->getStatistics());
86 break;
88 // Output help about the controls
89 // - If you add an option, please add it here too.
90 case '?':
91 case '/':
92 case 'h':
94 std::cerr << "\nControls:"
95 << "ESC: Exit application\n"
96 << "m/M: Switch stat mode.\n"
97 << "s: Cycle stats display.\n"
98 << std::endl;
100 break;
105 int main(int argc, char **argv)
107 OSG::osgInit(argc,argv);
108 OSG::PerfMonitor::the()->enable(true); // Enable performance monitoring
110 // GLUT init
111 glutInit(&argc, argv);
113 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
115 glutInitWindowSize(500, 500);
116 glutCreateWindow("PerfMonitor FG Test");
118 glutReshapeFunc(reshape);
119 glutDisplayFunc(display);
120 glutIdleFunc(display);
121 glutMouseFunc(mouse);
122 glutMotionFunc(motion);
123 glutKeyboardFunc(keyboard);
125 pwin=OSG::PassiveWindow::create();
126 pwin->init();
128 // create the scene
129 OSG::NodeUnrecPtr scene;
131 if(argc > 1)
133 scene = OSG::Node::create();
134 OSG::GroupUnrecPtr g = OSG::Group::create();
136 scene->setCore(g);
138 for(OSG::UInt16 i = 1; i < argc; ++i)
139 scene->addChild(OSG::SceneFileHandler::the()->read(argv[i]));
141 else
143 scene = OSG::makeTorus(.5, 3, 16, 16);
146 // create the SimpleSceneManager helper
147 mgr = OSG::SimpleSceneManager::create();
149 // create the window and initial camera/viewport
150 mgr->setWindow(pwin );
151 // tell the manager what to manage
152 mgr->setRoot (scene);
154 OSG::Thread::getCurrentChangeList()->commitChanges();
156 // show the whole scene
157 mgr->showAll();
159 // add the statistics forground
161 perfmon_fg = OSG::PerfMonitorForeground::create();
162 pwin->getPort(0)->addForeground(perfmon_fg);
164 //statfg->setMaxSize(25);
165 //statfg->setColor(Color4f(0,1,0,0.7));
167 act = OSG::RenderAction::create();
168 mgr->setAction(act);
170 // GLUT main loop
171 glutMainLoop();
173 return 0;
177 void randWork(unsigned baseTime, unsigned randMax, std::string name)
179 OSG::PerfMonitorGuard g(name);
180 unsigned rand_time = unsigned(OSG::osgRand() * float(randMax));
181 OSG::osgSleep(baseTime + rand_time);
184 void recursiveFunc(unsigned depth=3)
186 OSG::PerfMonitorGuard g("recursiveFunc");
188 //osgSleep(10);
189 //std::cout << "depth: " << depth << std::endl;
191 if (depth == 0)
192 { return; }
193 else
194 { recursiveFunc(depth-1); }
197 void doStuff()
199 static float x =0; // Value to influence the work
200 static float x_inc =0.01f;
201 x += x_inc;
202 if ((x<=0.0) || (x>=1.0))
204 x_inc = -x_inc;
205 x += x_inc;
208 OSG::PerfMonitorGuard gG("doStuff");
211 OSG::PerfMonitorGuard g("Work1");
212 recursiveFunc();
213 randWork(unsigned(10.0*x), 1, "10*x");
214 randWork(unsigned(10.0*(1.0-x)), 1, "10*(1-x)");
218 OSG::PerfMonitorGuard g("Work2");
219 recursiveFunc();
220 randWork(unsigned(5.0*x), 1, "5x-1");
221 randWork(2, 10, "2-10");
225 OSG::PerfMonitorGuard g("Work3");
226 recursiveFunc();
227 randWork( 1, unsigned(5.0*x), "1-5x");
228 randWork(2, 0, "2-0");
232 OSG::PerfMonitorGuard g("Work4");
233 recursiveFunc(3);
237 OSG::PerfMonitorGuard g("Work5");
238 recursiveFunc(3);
242 OSG::PerfMonitorGuard g("Work6");
243 recursiveFunc(3);
247 OSG::PerfMonitorGuard g("Work7");
248 recursiveFunc(3);