FPS showing problem

Macaco

Active member
Joined
Jul 5, 2004
Messages
39
Hi guys,

I have a form which contains an opengl scene into a panel and I want to show the frames per second.

The code for showing frames per second is correct (copy paste from nehe).
The problem is that even inicializing my timer at 1 miliseconds, the framerate is never higher than 100 fps.
If I put the timer interval to 1 millisecond or 10 milliseconds the frame rate is 100 (and with 1 millisecond it should be higher.)

this->timer1->Enabled = true;
this->timer1->Interval = 1; //in milisegons
this->timer1->Tick += new System::EventHandler(this, timer1_Tick);

The code for fps is this one:
static float framesPerSecond = 0.0f; // This will store our fps
static float lastTime = 0.0f; // This will hold the time from the last frame
static char strFrameRate[50] = {0}; // We will store the string here for the window title

static float frameTime = 0.0f; // This stores the last frames time

float currentTime = timeGetTime() * 0.001f;

// Here we store the elapsed time between the current and last frame,
// then keep the current frame in our static variable for the next frame.
g_FrameInterval = currentTime - frameTime;
frameTime = currentTime;

Show->Render(t);

// Increase the frame counter
++framesPerSecond;

// Now we want to subtract the current time by the last time that was stored
// to see if the time elapsed has been over a second, which means we found our FPS.
if( currentTime - lastTime > 1.0f )
{
// Here we set the lastTime to the currentTime
lastTime = currentTime;

sprintf(strFrameRate, "Current Frames Per Second: %f", framesPerSecond);
this->Text = strFrameRate;

// Reset the frames per second
framesPerSecond = 0;
}


thank you for your time
 
:o! Dont use timers. Slow. Bad.
Code:
Namespace Game.Util
Public Class clsFPSCounter
Public count, curr, start As Integer
Public gFPS As Integer
Public Sub CalculateFPS()
curr = Environment.TickCount
If start + 1000 <= curr Then
start = Environment.TickCount
gFPS = count
count = 0
Else
count += 1
End If
End Sub
Public ReadOnly Property FPS()
Get
Return gFPS
End Get
End Property
End Class
End Namespace
 
....
 
Private fpsctr As New clsFPSCtr+
FPSCounter.start = Environment.TickCount
 
Do while Not GameOver
 
FPSCounter.CalculateFPS()
Other stuff
End While
 
messagebox.show(fpscounter.fps)
 
Thanks for the code for calculating FPS, but if dont use a timer then what function do I use to act as an idle?

In windows forms, the only function I know that acts as and idle function is the timer. If I call a normal function and I make infinite iterations the application makes an error.


ThePentiumGuy said:
:o! Dont use timers. Slow. Bad.
Code:
Namespace Game.Util
Public Class clsFPSCounter
Public count, curr, start As Integer
Public gFPS As Integer
Public Sub CalculateFPS()
curr = Environment.TickCount
If start + 1000 <= curr Then
start = Environment.TickCount
gFPS = count
count = 0
Else
count += 1
End If
End Sub
Public ReadOnly Property FPS()
Get
Return gFPS
End Get
End Property
End Class
End Namespace
 
....
 
Private fpsctr As New clsFPSCtr+
FPSCounter.start = Environment.TickCount
 
Do while Not GameOver
 
FPSCounter.CalculateFPS()
Other stuff
End While
 
messagebox.show(fpscounter.fps)
 
Hello PentiumGuy,

Im specially interested in solving this idle form problem which affects the fps value.
I would be very pleased if anyone helped me. The problem is that I dont find anyone who knows it or have put opengl into a panel which is in a form.

thanks!

P.D. If do you want me to re-write the problem (maybe i didnt explain it very well) I will do it.
 
Well, I would really not recommend you draw on a label (for now at least, try to get it to draw on the form first).

Im specially interested in solving this idle form problem which affects the fps value.
Do you have a game loop? That should refresh the form and the drawing automatically. An example of one can be found in the tutors corner:
http://www.computerhelp.forum/showthread.php?t=87687.

-TPG
 
>>Well, I would really not recommend you draw on a label (for now at least, try to get it to draw on the form first).

In fact I draw it in a panel. I cannot draw it directly to the form due to application requirements.

>>Do you have a game loop? That should refresh the form and the drawing automatically.

Now my loop is the timer function, because it calls each millisecond (but I cant draw correctly the fps).
But Ive tried to create an idle function (without using timer) called by the PaintEventHandler of the panel, but it doesnt function well.

I looked at your example but its completely different from mine. I dont have problems drawing the 3d secene. I just want to create a loop not controlled by the timer function, but I dont know.

Here is my central application code. As you can see, in timer1_Tick I have the loop.


namespace simpleapp
{

public __gc class testWindow : public System::Windows::Forms::Form
{
public:
testWindow(void)
{

InitializeComponent();
Show->Init(this->viewport,"finestra");

}

protected:
void Dispose(Boolean disposing)
{
if(rTarget) delete rTarget;
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::Timer * timer1;
private: System::Windows::Forms::Panel * viewport;
Scene * Show;
// Time

float t;

float g_FrameInterval;
//public: static __event EventHandler* Idle;

private:
CWinFormTarget *rTarget;
private: System::ComponentModel::IContainer * components;

void InitializeComponent(void)
{
//Time
g_FrameInterval = 0.0f;


this->components = new System::ComponentModel::Container();
this->viewport = new System::Windows::Forms::Panel();
this->timer1 = new System::Windows::Forms::Timer(this->components);
this->SuspendLayout();

// viewport
this->viewport->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->viewport->Location = System::Drawing::Point(10, 10);
this->viewport->Name = S"viewport";
this->viewport->Size = System::Drawing::Size(800, 600);
this->viewport->TabIndex = 0;

// timer1
this->timer1->Enabled = true;
this->timer1->Interval = 1; //en milisegons
this->timer1->Tick += new System::EventHandler(this, timer1_Tick);


// testWindow
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(820, 620);
this->Controls->Add(this->viewport);
this->Name = S"testWindow";
this->Text = S"testWindow";


// Add our OnIdle event to the Applications Idle
//Application::add_Idle(new EventHandler(this,OnIdle));

this->ResumeLayout(false);

Show = new Scene();
}



private: System::Void timer1_Tick(System::Object * sender, System::EventArgs * e)
{

static float framesPerSecond = 0.0f; // This will store our fps
static float lastTime = 0.0f; // This will hold the time from the last frame
static char strFrameRate[50] = {0}; // We will store the string here for the window title

static float frameTime = 0.0f; // This stores the last frames time

float currentTime = timeGetTime() * 0.001f;

// Here we store the elapsed time between the current and last frame,
// then keep the current frame in our static variable for the next frame.
g_FrameInterval = currentTime - frameTime;
frameTime = currentTime;

Show->Render(g_FrameInterval);

// Increase the frame counter
++framesPerSecond;

// Now we want to subtract the current time by the last time that was stored
// to see if the time elapsed has been over a second, which means we found our FPS.
if( currentTime - lastTime > 1.0f )
{
// Here we set the lastTime to the currentTime
lastTime = currentTime;

sprintf(strFrameRate, "Current Frames Per Second: %f", framesPerSecond);
this->Text = strFrameRate;

// Reset the frames per second
framesPerSecond = 0;
}
}


};
 
Last edited by a moderator:
Back
Top