What I'm trying to achieve is simply a game loop. I could use a QTimer set to the desired frame rate. But I chosed to try existing approach I already use with Win32 API. I will post my code here so it can be tested:
tbb::tick_count t0, t1;
MyGame2
::MyGame2(QWidget *parent, Qt
::WFlags flags
){
ui.setupUi(this);
ui.textEditMainConsole->appendPlainText("Hello!");
//ui.textEditMainConsole->setDisabled(true);
//ui.textEditMainConsole->clear();
//hiResTimerStart = boost::chrono::steady_clock::now();
t0 = tbb::tick_count::now();
timer.setInterval(0);
timer.start();
connect(&timer, SIGNAL(timeout()), this, SLOT(OnIdle()));
}
MyGame2::~MyGame2()
{
timer.stop();
}
void MyGame2::OnIdle()
{
tbb::tick_count t1 = tbb::tick_count::now();
float dt = (t1 - t0).seconds();
//boost::chrono::steady_clock::time_point hiResTimerEnd = boost::chrono::steady_clock::now();
//boost::chrono::microseconds msecs = boost::chrono::duration_cast<boost::chrono::microseconds>(hiResTimerEnd - hiResTimerStart);
//unsigned long p = 1000000/60;
//unsigned long p = 1000000/85;
//if (msecs.count() < p) {
if (dt < 1.0f/60) {
} else {
//============
std::stringstream ss;
//ss << "~~~ dCount: " << msecs.count();
ss << "~~~ dCount: " << dt;
std::string s = ss.str();
//ui.textEditMainConsole->clear();
//ui.textEditMainConsole->appendPlainText(s.c_str());
ui.textEditMainConsole->setPlainText(s.c_str());
//============
//hiResTimerStart = hiResTimerEnd;
t0 = t1;
}
pApp->processEvents();
}
tbb::tick_count t0, t1;
MyGame2::MyGame2(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
ui.setupUi(this);
ui.textEditMainConsole->appendPlainText("Hello!");
//ui.textEditMainConsole->setDisabled(true);
//ui.textEditMainConsole->clear();
//hiResTimerStart = boost::chrono::steady_clock::now();
t0 = tbb::tick_count::now();
timer.setInterval(0);
timer.start();
connect(&timer, SIGNAL(timeout()), this, SLOT(OnIdle()));
}
MyGame2::~MyGame2()
{
timer.stop();
}
extern QApplication *pApp;
void MyGame2::OnIdle()
{
tbb::tick_count t1 = tbb::tick_count::now();
float dt = (t1 - t0).seconds();
//boost::chrono::steady_clock::time_point hiResTimerEnd = boost::chrono::steady_clock::now();
//boost::chrono::microseconds msecs = boost::chrono::duration_cast<boost::chrono::microseconds>(hiResTimerEnd - hiResTimerStart);
//unsigned long p = 1000000/60;
//unsigned long p = 1000000/85;
//if (msecs.count() < p) {
if (dt < 1.0f/60) {
} else {
//============
std::stringstream ss;
//ss << "~~~ dCount: " << msecs.count();
ss << "~~~ dCount: " << dt;
std::string s = ss.str();
//ui.textEditMainConsole->clear();
//ui.textEditMainConsole->appendPlainText(s.c_str());
ui.textEditMainConsole->setPlainText(s.c_str());
//============
//hiResTimerStart = hiResTimerEnd;
t0 = t1;
}
pApp->processEvents();
}
To copy to clipboard, switch view to plain text mode
Bookmarks