C

Qt Cluster: Rendering and Recovery from Main UI Failure

// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause

// This file is part of the Qt Safe Renderer module

import QtQuick

Item {
    id: fpscounter
    property real fpsNow: 0
    property bool running: false
    property alias fpsVisible: fpsLabel.visible
    property int fpsInterval: 1000
    property alias color: fpsLabel.color

    Item {
        id: swapTest
        property real t
        NumberAnimation on t {
            running: fpscounter.running
            from: 0
            to: 1
            duration: fpsInterval
            loops: Animation.Infinite
        }
        onTChanged: {
            ++fpsTimer.tick
        }
    }

    Timer {
        id: fpsTimer
        running: fpscounter.running
        repeat: true
        interval: fpsInterval
        property var lastFrameTime: new Date()
        property int tick

        onTriggered: {
            var now = new Date()
            var dt = now.getTime() - lastFrameTime.getTime()
            lastFrameTime = now
            var fps = (tick * fpsInterval) / dt
            fpsNow = Math.round(fps * 10) / 10
            tick = 0

            if (fpsVisible)
                fpsLabel.updateYerself()
        }
    }

    Text {
        id: fpsLabel
        visible: false

        anchors.centerIn: parent
        font.pixelSize: 10
        color: "white"

        function updateYerself() {
            text = Math.round(fpsNow)
        }
    }
}