-
-
Notifications
You must be signed in to change notification settings - Fork 877
/
PhysicsDoer.cpp
41 lines (32 loc) · 905 Bytes
/
PhysicsDoer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include "PhysicsDoer.h"
#include <QDebug>
const int PhysicsDoer::c_physics_fps = 20;
PhysicsDoer::PhysicsDoer(Game * game) :
m_game(game),
m_physics_timer(NULL)
{
// run in our own thread
m_thread = new QThread();
m_thread->start();
this->moveToThread(m_thread);
}
void PhysicsDoer::doPhysics()
{
float elapsed_time = m_physics_time.restart() / 1000.0f;
m_game->doPhysics(elapsed_time);
}
void PhysicsDoer::start()
{
if (QThread::currentThread() != m_thread) {
bool success;
success = QMetaObject::invokeMethod(this, "start", Qt::QueuedConnection);
Q_ASSERT(success);
return;
}
m_physics_timer = new QTimer(this);
bool success;
success = connect(m_physics_timer, SIGNAL(timeout()), this, SLOT(doPhysics()));
Q_ASSERT(success);
m_physics_time.start();
m_physics_timer->start(1000 / c_physics_fps);
}