Render FPS in a static text.

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
alfabeta90
Posts: 52
Joined: Wed Dec 06, 2006 5:18 pm

Render FPS in a static text.

Post by alfabeta90 »

I wanted to show a fps in a static text.

Code: Select all

    while(device->run())
    { 
        driver->beginScene(true, true, SColor(0,192,192,192));
                smgr->drawAll();
                guienv->drawAll();
	    driver->endScene();

		int fps = driver->getFPS();
		core::stringw str = L"FPS: ";     
		str += fps;     
		guienv->addStaticText(str.c_str(), rect<s32>(35,35,140,50), false, false, 0);
	}
But if i run program i see: "FPS: " and then a black little somthing. I think that are fps number whos are rendered on previous fps number. Any help?
Dorth
Posts: 931
Joined: Sat May 26, 2007 11:03 pm

Post by Dorth »

you have to either use a string buffer then throw in it the numbers with the << operator or use something like itoa to convert them to text. Right now the value you give it is not something a string recognizes...
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

You only need to call addStaticText() once. After that you need to use setText(). Otherwise you're creating a new static text over the top of the old one every frame.

Code: Select all

IGUIStaticText* fpsTextElement = guienv->addStaticText(L"", rect<s32>(35, 35, 140, 50), false, false, 0);

int lastFPS = 0;

while (device->run())
{
  if (driver->beginScene(true, true, SColor(0,192,192,192)))
  {
    smgr->drawAll(); 
    guienv->drawAll(); 

    driver->endScene(); 
  }

  // only update fps display if fps changed
  int fps = driver->getFPS(); 
  if (lastFPS != fps)
  {
    core::stringw str = L"FPS: ";      
    str += fps;      
    fpsTextElement->setText(str.c_str());

    lastFPS = fps;
  }
}
Travis
alfabeta90
Posts: 52
Joined: Wed Dec 06, 2006 5:18 pm

Post by alfabeta90 »

alfabeta90 wrote:I do so:

Code: Select all

	IGUIFont* font = device->getGUIEnvironment()->getFont("images/courier.bmp");
	font->draw(buffer,rect<s32>(10,10,300,60),SColor(255,255,255,255));
What must i do to show fps because font has no methos lik setText?
Dances
Posts: 454
Joined: Sat Jul 02, 2005 1:45 am
Location: Canada
Contact:

Post by Dances »

alfabeta90 wrote:
alfabeta90 wrote:I do so:

Code: Select all

	IGUIFont* font = device->getGUIEnvironment()->getFont("images/courier.bmp");
	font->draw(buffer,rect<s32>(10,10,300,60),SColor(255,255,255,255));
What must i do to show fps because font has no methos lik setText?
When you use addStaticText the first time you'll get a pointer to an IStaticText... or something like that... you should probably check the API.

Anyway, when you've got that you use setText on that pointer.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

If you insist on using drawText() you need to change the text that you draw...

Code: Select all

IGUIFont* font = device->getGUIEnvironment()->getFont("images/courier.bmp"); 

int lastFPS = 0; 

while (device->run()) 
{ 
  if (driver->beginScene(true, true, SColor(0,192,192,192))) 
  { 
    smgr->drawAll(); 
    guienv->drawAll(); 

    driver->endScene(); 
  } 

  // only update fps display if fps changed 
  int fps = driver->getFPS(); 
  if (font && (lastFPS != fps)) 
  { 
    core::stringw str = L"FPS: ";      
    str += fps;      

    font->draw(str.c_str(), rect<s32>(10,10,300,60), video::SColor(255,255,255,255)); 

    lastFPS = fps; 
  } 
} 
I don't know why you'd want to do that though. The IGUIStaticText does exactly the same thing, the only diference is that it creates a GUI element to hold the state.

Travis
Post Reply