Skip to content

Commit

Permalink
feat: 增加fps显示
Browse files Browse the repository at this point in the history
  • Loading branch information
mhduiy committed Dec 13, 2024
1 parent e167c53 commit 811b8e2
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/cpp/components/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
set(COMPONENTS_SRC
${CMAKE_CURRENT_SOURCE_DIR}/deivicelistviewmodel.cpp
${CMAKE_CURRENT_SOURCE_DIR}/deivicelistviewmodel.h
${CMAKE_CURRENT_SOURCE_DIR}/fpsitem.cpp
${CMAKE_CURRENT_SOURCE_DIR}/fpsitem.h
)

target_sources(${EXE_NAME} PRIVATE ${COMPONENTS_SRC})
Expand Down
24 changes: 24 additions & 0 deletions src/cpp/components/fpsitem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include "fpsitem.h"

#include <QTimer>
#include <QQuickWindow>

FpsItem::FpsItem() {
auto *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, [this] {
setFps(_frameCount);
_frameCount = 0;
});
connect(this, &QQuickItem::windowChanged, this, [this] {
if (window()) {
connect(window(), &QQuickWindow::afterRendering, this, [this] { _frameCount++; }, Qt::DirectConnection);
}
});
timer->start(1000);
}

void FpsItem::setFps(int fps)
{
m_fps = fps;
Q_EMIT fpsChanged(fps);
}
19 changes: 19 additions & 0 deletions src/cpp/components/fpsitem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <QQuickItem>

class FpsItem : public QQuickItem {
Q_OBJECT
Q_PROPERTY(int fps WRITE setFps READ getFps NOTIFY fpsChanged)
public:
FpsItem();
void setFps(int);
[[nodiscard]] inline int getFps() const { return m_fps; };

signals:
void fpsChanged(int);

private:
int _frameCount = 0;
int m_fps = 0;
};
8 changes: 8 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "cpp/imagePageTool/imagePageTool.h"
#include "cpp/settingPageTools/settingPageTools.h"
#include "cpp/utils/notificationcontroller.h"
#include "cpp/components/fpsitem.h"

bool checkADB() {
QProcess process;
Expand Down Expand Up @@ -96,8 +97,15 @@ int main(int argc, char *argv[])
qmlRegisterSingletonInstance("NotificationController", 1, 0, "NotificationController", NotificationController::instance());
qInfo() << "load8" << loaderTimer.elapsed();

qmlRegisterType<FpsItem>( "FpsItem", 1, 0, "FpsItem");

QQuickWindow::setGraphicsApi(QSGRendererInterface::OpenGL);

QSurfaceFormat format;
format.setSwapBehavior(QSurfaceFormat::DoubleBuffer);
format.setSwapInterval(0);
QSurfaceFormat::setDefaultFormat(format);

QQmlApplicationEngine engine;
const QUrl url("qrc:/qml/Main.qml");
engine.load(url);
Expand Down
15 changes: 15 additions & 0 deletions src/qml/Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "./components/"
import "./pages/"
import ADBControl 1.0
import WallpaperHelper 1.0
import FpsItem 1.0

ApplicationWindow {
id: root
Expand All @@ -27,6 +28,20 @@ ApplicationWindow {
blurTarget: rootRect
}

FpsItem {
id: fpsItem
}

Text {
z: 1000
anchors.right: parent.right
anchors.bottom: parent.bottom
anchors.bottomMargin: 10
anchors.rightMargin: 10
text: "fps:" + fpsItem.fps
color: "#0495d5"
}

Rectangle {
id: rootRect
color: "transparent"
Expand Down

0 comments on commit 811b8e2

Please sign in to comment.