PDA

View Full Version : QTimer Question



ComaWhite
5th June 2008, 23:45
I have an application in Qt-4 with OpenGL. And I want to have in the title bar the FPS. But I can't find any examples on source of how to implement it.:crying:

jacek
6th June 2008, 00:31
Then stop looking for examples and think about the solution. How to calculate FPS?

anthibug
3rd July 2008, 10:53
QTime current_time;
QTime last_time;
int elapsed_time;
float fps; // current fps in the loop, depending on the "work time" required during the paintGL call

void gldrawer::paintGL()
{
last_time = current_time;
current_time = QTime::currentTime();
elapsed_time = (current_time.second()*1000 + current_time.msec()) - (last_time.second()*1000 + last_time.msec()); // /!\ max = 59 seconds 999 ms
this->fps = 1000 / elapsed_time;



/*
Si on souhaite limiter l'affichage à X fps pour éviter des cycles processeurs inutiles, il faut :
[code non testé, mais l'idée est là ]

static FPS = 30; // je veux limiter à 30FPS
static int time_per_frame = 1000/FPS;

if(elapsed_time < time_per_frame)
QTest::qWait(time_per_frame - elapsed_time);
*/



.... painting stuff ....
}


n'joy
this work only if you redraw every time the scene.
For example in the constructor :


QTimer qTimerRedraw; // not QTime but QTimer
connect(&qTimerRedraw, SIGNAL(timeout()), this, SLOT(paintGL()));
qTimerRedraw.start(0);



But i'm currently having problems with this (resize events, moving the window, ...).
See : http://www.qtcentre.org/forum/f-qt-programming-2/t-qtimer-start0-opengl-resizemove-window-crash-14595-post75079.html#post75079

ComaWhite
1st August 2008, 06:03
Thank you so much. I didn't think anyone would ever respond to it. I'll give it a try later on today and let you know. =)

ComaWhite
1st August 2008, 18:07
I noticed that it works great. But only two problems. The FPS clock when putting in the titlebar is goes too quickly to even see it and when I disabled vsync in the nVidia control panel it only went to 1000 and thats it.



#ifndef COMATIMER_HPP
#define COMATIMER_HPP

#include "comaglobal.hpp"
#include <QTime>

COMA_BEGIN_NAMESPACE

class COMA_DLL Timer
{
public:
Timer()
{
reset();
}

~Timer()
{
}

void reset()
{
m_zeroClock = QTime::currentTime();
m_startClock = QTime(0, 0, 0);
}

ULong milliseconds()
{
QTime now = QTime::currentTime();
return (now.second() - m_startClock.second()) * 1000 + (now.msec() - m_startClock.msec()) / 1000;
}
private:
QTime m_zeroClock;
QTime m_startClock;

};

COMA_END_NAMESPACE

#endif /*COMATIMER_HPP*/




void
ComaGL::paintGL()
{
++g_fps;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

Vector3 eye(0.0, 0.0, -15.0);
Vector3 centre(0.0, 0.0, 0.0);
Vector3 up(0.0, 1.0, 0.0);

Matrix4 modelViewMatrix(Matrix4::M4_Identity);
modelViewMatrix.lookAt(eye, centre, up);

glMultMatrixf(modelViewMatrix.data());

Matrix4 rot(Matrix4::M4_Identity);
rot.rotateX(g_angle);
glMultMatrixf(rot.data());
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);

glVertexPointer(3, GL_FLOAT, 0, &m_tri[0]);
glColorPointer(3, GL_FLOAT, 0, &m_colour[0]);
glDrawArrays(GL_QUADS, 0, 4);

glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);

g_angle += 2.0;

g_currentTime = g_timer.milliseconds();
ULong time = g_currentTime - g_lastTime;

if(g_currentTime - g_lastTime > 1000)
{
setWindowTitle(QString("Fps: %1")
.arg((UInt32)(g_fps / (Real)(g_currentTime - g_lastTime) * 1000.0)));
g_lastTime = g_currentTime;
g_fps = 0;
}
}