Moderators: Sascha Willems, walaber
class dThread
{
public:
dThread(void);
virtual ~dThread(void);
virtual unsigned RunMyTask () { return 1;}
void ContinueExecution ();
void StopsExecution ();
void TerminateTask ();
void YieldTime();
void Lock(unsigned& lockVar);
void Unlock(unsigned& lockVar);
private:
void ExcuteTask();
#if defined (_MSC_VER)
static unsigned _stdcall TaskCallback(void *param);
#endif
#if (defined (_POSIX_VER) || defined (_MAC_VER))
static void* TaskCallback(void *Param);
#endif
unsigned m_taskSuspendedCount;
unsigned m_taskExecuting;
unsigned m_terminated;
#if defined (_MSC_VER)
uintptr_t m_threadhandle;
#endif
#if (defined (_POSIX_VER) || defined (_MAC_VER))
pthread_t m_threadhandle;
#endif
};
class dSimulationThread: public dThread
{
public:
dSimulationThread();
~dSimulationThread();
virtual unsigned RunMyTask ();
bool m_asycronousUpdate;
dFloat m_physicsTime;
};
unsigned dSimulationThread::RunMyTask ()
{
if (m_asycronousUpdate) {
DemoEntityManager& me = *((DemoEntityManager*)this);
me.UpdatePhysics();
}
return 1;
}
void DemoEntityManager::UpdatePhysics()
{
// read the controls
// update the physics
if (m_world) {
dFloat timestepInSecunds = 1.0f / MAX_PHYSICS_FPS;
unsigned64 timestepMicrosecunds = unsigned64 (timestepInSecunds * 1000000.0f);
unsigned64 currentTime = dGetTimeInMicrosenconds ();
unsigned64 nextTime = currentTime - m_microsecunds;
int loops = 0;
while ((nextTime >= timestepMicrosecunds) && (loops < MAX_PHYSICS_LOOPS)) {
loops ++;
// run the newton update function
if (!m_reEntrantUpdate) {
m_reEntrantUpdate = true;
if (m_physicsUpdate && m_world) {
// update the camera;
UpdateCamera (timestepInSecunds);
// update teh world
NewtonUpdate (m_world, timestepInSecunds);
dListNode* nextNode;
for (dListNode* node = GetFirst(); node; node = nextNode) {
nextNode = node->GetNext();
DemoEntity* const entity = node->GetInfo();
entity->AddRef();
entity->SimulationLister(this, node, timestepInSecunds);
entity->Release();
}
}
m_reEntrantUpdate = false;
}
nextTime -= timestepMicrosecunds;
m_microsecunds += timestepMicrosecunds;
}
if (loops) {
m_physicsTime = dFloat (dGetTimeInMicrosenconds () - currentTime) / 1000000.0f;
if (m_physicsTime >= MAX_PHYSICS_LOOPS * (1.0f / MAX_PHYSICS_FPS)) {
m_microsecunds = currentTime;
}
}
}
}
Users browsing this forum: No registered users and 0 guests