Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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 frame's 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

Posted

:o! Don't use timers. Slow. Bad.

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)

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

Posted

Thanks for the code for calculating FPS, but if don't 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.

 

 

:o! Don't use timers. Slow. Bad.

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)

Posted

Hello PentiumGuy,

 

I'm 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 don't 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 didn't explain it very well) I will do it.

Posted

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).

 

I'm 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 tutor's corner:

http://www.xtremedotnettalk.com/showthread.php?t=87687.

 

-TPG

My VB.NET Game Programming Tutorial Site (GDI+, Direct3D, Tetris [coming soon], a full RPG.... you name it!)

vbprogramming.8k.com

My Project (Need VB.NET Programmers)

http://workspaces.gotdotnet.com/ResolutionRPG

Posted (edited)

>>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 can't draw correctly the fps).

But I've tried to create an idle function (without using timer) called by the PaintEventHandler of the panel, but it doesn't function well.

 

I looked at your example but it's completely different from mine. I don't have problems drawing the 3d secene. I just want to create a loop not controlled by the timer function, but I don't 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 frame's 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;

}

}

 

 

};

Edited by Iceplug

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...