Skip to content

Commit

Permalink
GRAPHICS: Add a simple FPS display
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Jan 21, 2014
1 parent 909c5c9 commit a0bc6cf
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/graphics/aurora/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ noinst_HEADERS = \
abcfont.h \
fontman.h \
text.h \
fps.h \
$(EMPTY)

libaurora_la_SOURCES = \
Expand All @@ -33,4 +34,5 @@ libaurora_la_SOURCES = \
abcfont.cpp \
fontman.cpp \
text.cpp \
fps.cpp \
$(EMPTY)
128 changes: 128 additions & 0 deletions src/graphics/aurora/fps.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/aurora/fps.cpp
* A simple text display showing the current FPS value.
*/

#include <OgreRoot.h>
#include <OgreFrameListener.h>

#include "common/threads.h"

#include "graphics/graphics.h"
#include "graphics/guiman.h"

#include "graphics/aurora/fontman.h"
#include "graphics/aurora/fps.h"
#include "graphics/aurora/text.h"

#include "events/requests.h"

namespace Graphics {

namespace Aurora {

class FPSListener : public Ogre::FrameListener {
private:
FPS *_fps;

public:
FPSListener(FPS &fps) : _fps(&fps) {
}

bool frameRenderingQueued(const Ogre::FrameEvent &event) {
_fps->set(GfxMan.getFPS());
return true;
}
};

FPS::FPS(uint size, float r, float g, float b, float a) : _fps(0.0), _text(0), _listener(0) {
create(size, r, g, b, a);
}

FPS::~FPS() {
destroy();
}

void FPS::create(uint size, float r, float g, float b, float a) {
if (!Common::isMainThread()) {
Events::MainThreadFunctor<void> functor(boost::bind(&FPS::create, this, size, r, g, b, a));

return RequestMan.callInMainThread(functor);
}

LOCK_FRAME();

_text = new Text(FontMan.get(kSystemFontMono, size), "", r, g, b, a);
_listener = new FPSListener(*this);

GUIMan.addRenderable(_text, kHorizontalAlignLeft, kVerticalAlignTop);

_text->setPosition(5.0, -5.0, -10.0);
_text->setVisible(true);

GUIMan.update();

Ogre::Root::getSingleton().addFrameListener(_listener);
}

void FPS::destroy() {
if (!Common::isMainThread()) {
Events::MainThreadFunctor<void> functor(boost::bind(&FPS::destroy, this));

return RequestMan.callInMainThread(functor);
}

LOCK_FRAME();

if (_listener)
Ogre::Root::getSingleton().removeFrameListener(_listener);

if (_text)
GUIMan.removeRenderable(_text);

delete _listener;
delete _text;

_listener = 0;
_text = 0;

GUIMan.update();
}

void FPS::set(double fps) {
if (_fps == fps)
return;

_text->set(Common::UString::sprintf("%.2f", fps));
GUIMan.update();

_fps = fps;
}

} // End of namespace Aurora

} // End of namespace Graphics
64 changes: 64 additions & 0 deletions src/graphics/aurora/fps.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in the AUTHORS file distributed with this source
* distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/aurora/fps.h
* A simple text display showing the current FPS value.
*/

#ifndef GRAPHICS_AURORA_FPS_H
#define GRAPHICS_AURORA_FPS_H

#include "common/types.h"

namespace Graphics {

namespace Aurora {

class Text;
class FPSListener;

class FPS {
public:
FPS(uint size = 13, float r = 1.0, float g = 1.0, float b = 1.0, float a = 1.0);
~FPS();
private:
double _fps;

Text *_text;
FPSListener *_listener;


void create(uint size, float r, float g, float b, float a);
void destroy();
void set(double fps);

friend class FPSListener;
};

} // End of namespace Aurora

} // End of namespace Graphics

#endif // GRAPHICS_AURORA_FPS_H

0 comments on commit a0bc6cf

Please sign in to comment.