diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f947a82..8e860212 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/modules/core_styles/styles/labels.otui b/modules/core_styles/styles/labels.otui index 76f1c379..36fa65a8 100644 --- a/modules/core_styles/styles/labels.otui +++ b/modules/core_styles/styles/labels.otui @@ -4,3 +4,7 @@ Label < UILabel LargerLabel < Label + +FrameCounter < UIFrameCounter + size: 68 16 + align: right \ No newline at end of file diff --git a/modules/topmenu/topmenu.otui b/modules/topmenu/topmenu.otui index 540204ae..9e8206c2 100644 --- a/modules/topmenu/topmenu.otui +++ b/modules/topmenu/topmenu.otui @@ -69,4 +69,10 @@ TopPanel size: 16 16 image: /core_styles/icons/about.png anchors.centerIn: parent - phantom: true \ No newline at end of file + phantom: true + + FrameCounter + anchors.top: parent.top + anchors.right: prev.left + margin.top: 8 + margin.right: 12 \ No newline at end of file diff --git a/src/framework/luascript/luafunctions.cpp b/src/framework/luascript/luafunctions.cpp index b834324c..62122bf4 100644 --- a/src/framework/luascript/luafunctions.cpp +++ b/src/framework/luascript/luafunctions.cpp @@ -98,7 +98,6 @@ void LuaInterface::registerFunctions() g_lua.bindClassMemberFunction("updateParentLayout", &UIWidget::updateParentLayout); g_lua.bindClassMemberFunction("destroy", &UIWidget::destroy); - // UILabel g_lua.registerClass(); g_lua.bindClassStaticFunction("create", &UIWidget::create); @@ -126,6 +125,11 @@ void LuaInterface::registerFunctions() g_lua.bindClassMemberFunction("getTitle", &UIWindow::getTitle); g_lua.bindClassMemberFunction("setTitle", &UIWindow::setTitle); + // UIFrameCounter + g_lua.registerClass(); + g_lua.bindClassStaticFunction("create", &UIWidget::create); + g_lua.bindClassMemberFunction("getFrameCount", &UIFrameCounter::getFrameCount); + // Protocol g_lua.registerClass(); diff --git a/src/framework/ui/declarations.h b/src/framework/ui/declarations.h index dd0dd413..3c68ab28 100644 --- a/src/framework/ui/declarations.h +++ b/src/framework/ui/declarations.h @@ -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 UILabelPtr; typedef std::shared_ptr UIButtonPtr; typedef std::shared_ptr UILineEditPtr; typedef std::shared_ptr UIWindowPtr; +typedef std::shared_ptr UIFrameCounterPtr; typedef std::shared_ptr UILayoutPtr; typedef std::shared_ptr UIVerticalLayoutPtr; typedef std::shared_ptr UIAnchorLayoutPtr; diff --git a/src/framework/ui/ui.h b/src/framework/ui/ui.h index 47b732a7..b6d81460 100644 --- a/src/framework/ui/ui.h +++ b/src/framework/ui/ui.h @@ -29,5 +29,6 @@ #include "uilabel.h" #include "uilineedit.h" #include "uiwindow.h" +#include "uiframecounter.h" #endif diff --git a/src/framework/ui/uiframecounter.cpp b/src/framework/ui/uiframecounter.cpp new file mode 100644 index 00000000..def2cb50 --- /dev/null +++ b/src/framework/ui/uiframecounter.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2010-2011 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 +#include +#include +#include + +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())); + } +} \ No newline at end of file diff --git a/src/framework/ui/uiframecounter.h b/src/framework/ui/uiframecounter.h new file mode 100644 index 00000000..e119ae99 --- /dev/null +++ b/src/framework/ui/uiframecounter.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2010-2011 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 diff --git a/src/otclient/otclient.cpp b/src/otclient/otclient.cpp index 3e87be27..a965f429 100644 --- a/src/otclient/otclient.cpp +++ b/src/otclient/otclient.cpp @@ -101,11 +101,7 @@ void OTClient::init(std::vector 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();