1 /*---------------------------------------------------------------------------*\
5 * Copyright (C) 2000-2006 by the OpenSG Forum *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
41 //---------------------------------------------------------------------------
46 #include "OSGConfig.h"
48 #include "OSGFrameHandler.h"
52 // Documentation for this class is emitted in the
53 // OSGFrameHandlerBase.cpp file.
54 // To modify it, please change the .fcd file (OSGFrameHandler.fcd) and
55 // regenerate the base file.
57 /***************************************************************************\
59 \***************************************************************************/
61 bool FrameHandler::_bRegistersInstanceRelease
= false;
62 FrameHandlerUnrecPtr
FrameHandler::_pGlobalInstance
= NULL
;
64 /***************************************************************************\
66 \***************************************************************************/
68 void FrameHandler::initMethod(InitPhase ePhase
)
70 Inherited::initMethod(ePhase
);
72 if(ePhase
== TypeObject::SystemPost
)
77 FrameHandler
*FrameHandler::the(void)
79 if(GlobalSystemState
!= Running
)
82 if(_pGlobalInstance
== NULL
)
84 FrameHandlerUnrecPtr pHandler
= FrameHandler::create();
86 setGlobalInstance(pHandler
);
89 return _pGlobalInstance
;
92 void FrameHandler::setGlobalInstance(FrameHandler
*pHandler
)
94 if(_bRegistersInstanceRelease
== false)
96 _bRegistersInstanceRelease
= true;
98 addPreFactoryExitFunction(&FrameHandler::releaseGlobalInstance
);
101 _pGlobalInstance
= pHandler
;
104 bool FrameHandler::releaseGlobalInstance(void)
106 _pGlobalInstance
= NULL
;
111 /***************************************************************************\
113 \***************************************************************************/
115 /*-------------------------------------------------------------------------*\
117 \*-------------------------------------------------------------------------*/
119 /*----------------------- constructors & destructors ----------------------*/
121 FrameHandler::FrameHandler(void) :
124 _mfUninitializedFrameTasks(),
125 _mfInitializedFrameTasks ()
129 FrameHandler::FrameHandler(const FrameHandler
&source
) :
132 _mfUninitializedFrameTasks( ),
133 _mfInitializedFrameTasks ( )
137 FrameHandler::~FrameHandler(void)
141 /*----------------------------- class specific ----------------------------*/
143 void FrameHandler::changed(ConstFieldMaskArg whichField
,
147 Inherited::changed(whichField
, origin
, details
);
150 void FrameHandler::dump( UInt32
,
151 const BitVector
) const
153 SLOG
<< "Dump FrameHandler NI" << std::endl
;
156 void FrameHandler::addTask(FrameTaskInterface
*pTask
,
157 bool bNoFrameFunction
)
162 if(bNoFrameFunction
== false)
163 this->pushToFrameTasks (pTask
);
165 this->pushToUninitializedFrameTasks(pTask
);
168 void FrameHandler::removeTask(FrameTaskInterface
*pTask
)
172 bool rc
= this->removeObjFromFrameTasks (pTask
);
173 rc
|= this->removeObjFromInitializedFrameTasks (pTask
);
175 bool rcU
= this->removeObjFromUninitializedFrameTasks(pTask
);
177 if(rc
== true && rcU
== false)
184 bool FrameHandler::init(void)
186 bool returnValue
= true;
188 InterfaceStoreConstIt tIt
= this->getMFUninitializedFrameTasks()->begin();
189 InterfaceStoreConstIt tEnd
= this->getMFUninitializedFrameTasks()->end ();
193 returnValue
&= (*tIt
)->init();
195 this->pushToInitializedFrameTasks(*tIt
);
200 this->clearUninitializedFrameTasks();
205 void FrameHandler::frame(Time frameTime
)
207 if(_mfUninitializedFrameTasks
.size() != 0)
212 setCurrTime(frameTime
);
214 if(_sfStartTime
.getValue() < 0.0)
216 setStartTime(_sfCurrTime
.getValue());
220 _sfCurrTime
.getValue() -= _sfStartTime
.getValue();
222 if(_sfPaused
.getValue() == false)
224 SFTime
*pSFTimeStamp
= editSFTimeStamp();
226 if(_sfConstantTime
.getValue() == true)
228 pSFTimeStamp
->getValue() += _sfConstantTimeStep
.getValue();
230 if(pSFTimeStamp
->getValue() < 0.)
231 pSFTimeStamp
->setValue(0.0);
235 pSFTimeStamp
->getValue() +=
236 (_sfCurrTime
.getValue() - _sfLastTime
.getValue()) *
237 _sfTimeScale
.getValue();
239 if(pSFTimeStamp
->getValue() < 0.)
240 pSFTimeStamp
->setValue(0.0);
244 setLastTime(_sfCurrTime
.getValue());
246 ++(editSFFrameCount()->getValue());
251 void FrameHandler::frame(void)
253 frame(getSystemTime());
256 void FrameHandler::shutdown(void)
258 InterfaceStoreConstIt tIt
= getMFInitializedFrameTasks()->begin();
259 InterfaceStoreConstIt tEnd
= getMFInitializedFrameTasks()->end ();
269 void FrameHandler::callTasks(void)
271 Int32 currPrio
= TypeTraits
<Int32
>::getMin();
272 InterfaceStoreConstIt tIt
= getMFFrameTasks()->begin();
273 InterfaceStoreConstIt tEnd
= getMFFrameTasks()->end ();
277 if((*tIt
)->getPriority() > currPrio
)
279 currPrio
= (*tIt
)->getPriority();
283 (*tIt
)->frame(_sfTimeStamp
.getValue(), _sfFrameCount
.getValue());
289 //! Get the FrameHandler::_mfFrameTasks field.
290 const FrameHandler::InterfaceStore
*FrameHandler::getMFFrameTasks(void) const
292 return &_mfFrameTasks
;
295 //! Get the FrameHandler::_mfUninitializedFrameTasks field.
296 const FrameHandler::InterfaceStore
*
297 FrameHandler::getMFUninitializedFrameTasks(void) const
299 return &_mfUninitializedFrameTasks
;
302 //! Get the FrameHandler::_mfUninitializedFrameTasks field.
303 const FrameHandler::InterfaceStore
*
304 FrameHandler::getMFInitializedFrameTasks(void) const
306 return &_mfInitializedFrameTasks
;
309 void FrameHandler::pushToFrameTasks(FrameTaskInterface
* const value
)
311 InterfaceStoreIt tIt
= std::lower_bound(_mfFrameTasks
.begin(),
312 _mfFrameTasks
.end (), value
,
315 _mfFrameTasks
.insert(tIt
, value
);
318 void FrameHandler::pushToUninitializedFrameTasks(
319 FrameTaskInterface
* const value
)
321 _mfUninitializedFrameTasks
.push_back(value
);
324 void FrameHandler::pushToInitializedFrameTasks(
325 FrameTaskInterface
* const value
)
327 _mfInitializedFrameTasks
.push_back(value
);
331 bool FrameHandler::removeObjFromFrameTasks(FrameTaskInterface
* const value
)
333 bool returnValue
= false;
335 InterfaceStoreIt it
= std::find(_mfFrameTasks
.begin(),
336 _mfFrameTasks
.end (), value
);
338 if(it
!= _mfFrameTasks
.end())
340 _mfFrameTasks
.erase(it
);
348 bool FrameHandler::removeObjFromUninitializedFrameTasks(
349 FrameTaskInterface
* const value
)
351 bool returnValue
= false;
353 InterfaceStoreIt it
= std::find(_mfUninitializedFrameTasks
.begin(),
354 _mfUninitializedFrameTasks
.end (), value
);
356 if(it
!= _mfUninitializedFrameTasks
.end())
358 _mfUninitializedFrameTasks
.erase(it
);
366 bool FrameHandler::removeObjFromInitializedFrameTasks(
367 FrameTaskInterface
* const value
)
369 bool returnValue
= false;
371 InterfaceStoreIt it
= std::find(_mfInitializedFrameTasks
.begin(),
372 _mfInitializedFrameTasks
.end (), value
);
374 if(it
!= _mfInitializedFrameTasks
.end())
376 _mfInitializedFrameTasks
.erase(it
);
384 void FrameHandler::clearFrameTasks(void)
386 _mfFrameTasks
.clear();
389 void FrameHandler::clearUninitializedFrameTasks(void)
391 _mfUninitializedFrameTasks
.clear();
394 void FrameHandler::clearInitializedFrameTasks(void)
396 _mfInitializedFrameTasks
.clear();
399 void FrameHandler::resolveLinks(void)
401 Inherited::resolveLinks();
403 static_cast<FrameHandler
*>(this)->clearFrameTasks();
404 static_cast<FrameHandler
*>(this)->clearUninitializedFrameTasks();
405 static_cast<FrameHandler
*>(this)->clearInitializedFrameTasks ();