From 6df15b0e4ab2ab98712caa57c0a1d1357c7fd87d Mon Sep 17 00:00:00 2001 From: yangfengzzz Date: Mon, 15 Jan 2024 14:22:18 +0800 Subject: [PATCH] pass run basic usd --- main.cpp | 30 +++++++++++------------- viewport/CMakeLists.txt | 2 ++ viewport/framerate.cpp | 52 +++++++++++++++++++++++++++++++++++++++++ viewport/framerate.h | 35 +++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 17 deletions(-) create mode 100644 viewport/framerate.cpp create mode 100644 viewport/framerate.h diff --git a/main.cpp b/main.cpp index 86e5e2c..05189dc 100644 --- a/main.cpp +++ b/main.cpp @@ -8,8 +8,13 @@ #include #include #include +#include #include "viewport/viewport.h" #include "viewport/camera.h" +#include "viewport/framerate.h" + +static constexpr auto win_width = 1280u; +static constexpr auto win_height = 720u; class Canvas : public QWidget { @@ -19,7 +24,7 @@ class Canvas : public QWidget { public: explicit Canvas(QWidget *parent) noexcept : QWidget{parent}, - viewport{winId(), (uint)width(), (uint)height()}{ + viewport{winId(), win_width, win_height} { setAttribute(Qt::WA_NativeWindow); setAttribute(Qt::WA_PaintOnScreen); setAttribute(Qt::WA_OpaquePaintEvent); @@ -32,7 +37,7 @@ class Canvas : public QWidget { } void wheelEvent(QWheelEvent *event) override { - auto delta = event->angleDelta(); + auto delta = event->pixelDelta(); viewport.viewCamera()->panByDelta({(float)delta.x(), (float)delta.y()}); } @@ -44,12 +49,9 @@ class Canvas : public QWidget { }; int main(int argc, char *argv[]) { - static constexpr auto width = 1280u; - static constexpr auto height = 720u; - QApplication app{argc, argv}; QMainWindow window; - window.setFixedSize(width, height); + window.setFixedSize(win_width, win_height); window.setWindowTitle("Display"); window.setAutoFillBackground(true); @@ -57,21 +59,15 @@ int main(int argc, char *argv[]) { canvas.setFixedSize(window.contentsRect().size()); canvas.move(window.contentsRect().topLeft()); - QWidget overlay{&window}; - overlay.setFixedSize(window.contentsRect().size() / 2); - overlay.move(window.contentsRect().center() - overlay.rect().center()); - overlay.setAutoFillBackground(true); - - QPushButton button{"Quit", &overlay}; - button.move(overlay.contentsRect().center() - button.rect().center()); - QObject::connect(&button, &QPushButton::clicked, [&] { - window.setVisible(false); - }); - window.show(); + vox::Framerate framerate; while (window.isVisible()) { canvas.draw(); QApplication::processEvents(); + + framerate.record(); + auto title = fmt::format("Display - {:.2f} fps", framerate.report()); + window.setWindowTitle(title.c_str()); } QApplication::quit(); diff --git a/viewport/CMakeLists.txt b/viewport/CMakeLists.txt index e77ac33..150d87a 100644 --- a/viewport/CMakeLists.txt +++ b/viewport/CMakeLists.txt @@ -16,6 +16,8 @@ set(COMMON_FILES camera.cpp viewport.h viewport.mm + framerate.h + framerate.cpp ) source_group("common\\" FILES ${COMMON_FILES}) diff --git a/viewport/framerate.cpp b/viewport/framerate.cpp new file mode 100644 index 0000000..653b6bd --- /dev/null +++ b/viewport/framerate.cpp @@ -0,0 +1,52 @@ +// Copyright (c) 2024 Feng Yang +// +// I am making my contributions/submissions to this project solely in my +// personal capacity and am not conveying any rights to any intellectual +// property of any third parties. + +#include "framerate.h" + +namespace vox { + +Framerate::Framerate(size_t n) noexcept + : _history_size{n} { + _durations.reserve(n); + _frames.reserve(n); + _last = Clock::now(); +} + +void Framerate::clear() noexcept { + _durations.clear(); + _frames.clear(); + _last = Clock::now(); +} + +double Framerate::duration() const noexcept { + auto dt = Clock::now() - _last; + using namespace std::chrono_literals; + return static_cast(dt / 1ns) * 1e-9; +} + +void Framerate::record(size_t frame_count) noexcept { + if (_durations.size() == _history_size) { + _durations.erase(_durations.begin()); + _frames.erase(_frames.begin()); + } + using namespace std::chrono_literals; + _durations.emplace_back(duration()); + _frames.emplace_back(frame_count); + _last = Clock::now(); +} + +double Framerate::report() const noexcept { + if (_durations.empty()) { return 0.0; } + auto total_duration = 0.0; + auto total_frame_count = static_cast(0u); + for (auto i = 0u; i < _durations.size(); i++) { + total_duration += _durations[i]; + total_frame_count += _frames[i]; + } + return static_cast(total_frame_count) / total_duration; +} + +}// namespace vox diff --git a/viewport/framerate.h b/viewport/framerate.h new file mode 100644 index 0000000..79c92ce --- /dev/null +++ b/viewport/framerate.h @@ -0,0 +1,35 @@ +// Copyright (c) 2024 Feng Yang +// +// I am making my contributions/submissions to this project solely in my +// personal capacity and am not conveying any rights to any intellectual +// property of any third parties. + +#pragma once + +#include + +#include + +namespace vox { + +class Framerate { + +public: + using Clock = std::chrono::steady_clock; + using Timepoint = Clock::time_point; + +private: + std::vector _durations; + std::vector _frames; + Timepoint _last; + size_t _history_size; + +public: + explicit Framerate(size_t n = 5) noexcept; + void clear() noexcept; + void record(size_t frame_count = 1u) noexcept; + [[nodiscard]] double duration() const noexcept; + [[nodiscard]] double report() const noexcept; +}; + +}// namespace vox