add fps counter widget

This commit is contained in:
Eduardo Bart 2011-11-11 22:33:03 -02:00
parent fc65f99ead
commit 56f7ed3dd1
9 changed files with 130 additions and 19 deletions

View File

@ -50,7 +50,7 @@ IF(CMAKE_BUILD_TYPE STREQUAL "Debug")
ADD_DEFINITIONS(-D_DEBUG)
ENDIF(CMAKE_BUILD_TYPE STREQUAL "Debug")
SET(SOURCES
SET(SOURCES src/framework/ui/uiframecounter.cpp
# main
src/main.cpp

View File

@ -4,3 +4,7 @@ Label < UILabel
LargerLabel < Label
FrameCounter < UIFrameCounter
size: 68 16
align: right

View File

@ -70,3 +70,9 @@ TopPanel
image: /core_styles/icons/about.png
anchors.centerIn: parent
phantom: true
FrameCounter
anchors.top: parent.top
anchors.right: prev.left
margin.top: 8
margin.right: 12

View File

@ -98,7 +98,6 @@ void LuaInterface::registerFunctions()
g_lua.bindClassMemberFunction<UIWidget>("updateParentLayout", &UIWidget::updateParentLayout);
g_lua.bindClassMemberFunction<UIWidget>("destroy", &UIWidget::destroy);
// UILabel
g_lua.registerClass<UILabel, UIWidget>();
g_lua.bindClassStaticFunction<UILabel>("create", &UIWidget::create<UILabel>);
@ -126,6 +125,11 @@ void LuaInterface::registerFunctions()
g_lua.bindClassMemberFunction<UIWindow>("getTitle", &UIWindow::getTitle);
g_lua.bindClassMemberFunction<UIWindow>("setTitle", &UIWindow::setTitle);
// UIFrameCounter
g_lua.registerClass<UIFrameCounter, UIWidget>();
g_lua.bindClassStaticFunction<UIFrameCounter>("create", &UIWidget::create<UIFrameCounter>);
g_lua.bindClassMemberFunction("getFrameCount", &UIFrameCounter::getFrameCount);
// Protocol
g_lua.registerClass<Protocol>();

View File

@ -32,6 +32,7 @@ class UILabel;
class UIButton;
class UILineEdit;
class UIWindow;
class UIFrameCounter;
class UILayout;
class UIVerticalLayout;
class UIAnchorLayout;
@ -43,6 +44,7 @@ typedef std::shared_ptr<UILabel> UILabelPtr;
typedef std::shared_ptr<UIButton> UIButtonPtr;
typedef std::shared_ptr<UILineEdit> UILineEditPtr;
typedef std::shared_ptr<UIWindow> UIWindowPtr;
typedef std::shared_ptr<UIWindow> UIFrameCounterPtr;
typedef std::shared_ptr<UILayout> UILayoutPtr;
typedef std::shared_ptr<UIVerticalLayout> UIVerticalLayoutPtr;
typedef std::shared_ptr<UIAnchorLayout> UIAnchorLayoutPtr;

View File

@ -29,5 +29,6 @@
#include "uilabel.h"
#include "uilineedit.h"
#include "uiwindow.h"
#include "uiframecounter.h"
#endif

View File

@ -0,0 +1,62 @@
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "uiframecounter.h"
#include <framework/graphics/font.h>
#include <framework/otml/otmlnode.h>
#include <framework/platform/platform.h>
#include <framework/graphics/graphics.h>
void UIFrameCounter::setup()
{
UIWidget::setup();
setFocusable(false);
setPhantom(true);
setAlign(Fw::AlignLeft);
m_lastFrameTicks = g_platform.getTicks();
m_frameCount = 0;
}
void UIFrameCounter::render()
{
UIWidget::render();
int now = g_platform.getTicks();
if(now - m_lastFrameTicks >= 1000) {
m_fpsText = Fw::mkstr("FPS: ", m_frameCount);
m_lastFrameTicks = now;
m_frameCount = 0;
} else
m_frameCount++;
m_font->renderText(m_fpsText, m_rect, m_align, Fw::white);
}
void UIFrameCounter::onStyleApply(const OTMLNodePtr& styleNode)
{
UIWidget::onStyleApply(styleNode);
for(const OTMLNodePtr& node : styleNode->children()) {
if(node->tag() == "align")
setAlign(Fw::translateAlignment(node->value()));
}
}

View File

@ -0,0 +1,48 @@
/*
* Copyright (c) 2010-2011 OTClient <https://github.com/edubart/otclient>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef UIFRAMECOUNTER_H
#define UIFRAMECOUNTER_H
#include "uiwidget.h"
class UIFrameCounter : public UIWidget
{
public:
virtual void setup();
virtual void render();
void setAlign(Fw::AlignmentFlag align) { m_align = align; }
Fw::AlignmentFlag getAlign() { return m_align; }
int getFrameCount() { return m_frameCount; }
protected:
virtual void onStyleApply(const OTMLNodePtr& styleNode);
private:
Fw::AlignmentFlag m_align;
int m_frameCount;
int m_lastFrameTicks;
std::string m_fpsText;
};
#endif

View File

@ -101,11 +101,7 @@ void OTClient::init(std::vector<std::string> args)
void OTClient::run()
{
std::string fpsText;
Size fpsTextSize;
FontPtr defaultFont = g_fonts.getDefaultFont();
int frameTicks = g_platform.getTicks();
int lastFpsTicks = frameTicks;
int lastPollTicks = frameTicks;
int frameCount = 0;
@ -125,15 +121,6 @@ void OTClient::run()
g_platform.updateTicks();
frameTicks = g_platform.getTicks();
// calculate fps
frameCount++;
if(frameTicks - lastFpsTicks >= 1000) {
fpsText = Fw::mkstr("FPS: ", frameCount);
fpsTextSize = defaultFont->calculateTextRectSize(fpsText);
frameCount = 0;
lastFpsTicks = frameTicks;
}
// poll events every POLL_CYCLE_DELAY
// this delay exists to avoid massive polling thus increasing framerate
if(frameTicks - lastPollTicks >= POLL_CYCLE_DELAY) {
@ -149,9 +136,6 @@ void OTClient::run()
// render everything
render();
// render fps
defaultFont->renderText(fpsText, Point(g_graphics.getScreenSize().width() - fpsTextSize.width() - 70, 10));
// render end
g_graphics.endRender();