Skip to content

Commit

Permalink
pass run basic usd
Browse files Browse the repository at this point in the history
  • Loading branch information
yangfengzzz committed Jan 15, 2024
1 parent f0bdebc commit 6df15b0
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 17 deletions.
30 changes: 13 additions & 17 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
#include <QPushButton>
#include <QMainWindow>
#include <QtGui/qevent.h>
#include <fmt/format.h>
#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 {

Expand All @@ -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);
Expand All @@ -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()});
}

Expand All @@ -44,34 +49,25 @@ 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);

Canvas canvas{&window};
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();
Expand Down
2 changes: 2 additions & 0 deletions viewport/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ set(COMMON_FILES
camera.cpp
viewport.h
viewport.mm
framerate.h
framerate.cpp
)

source_group("common\\" FILES ${COMMON_FILES})
Expand Down
52 changes: 52 additions & 0 deletions viewport/framerate.cpp
Original file line number Diff line number Diff line change
@@ -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<double>(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<size_t>(0u);
for (auto i = 0u; i < _durations.size(); i++) {
total_duration += _durations[i];
total_frame_count += _frames[i];
}
return static_cast<double>(total_frame_count) / total_duration;
}

}// namespace vox
35 changes: 35 additions & 0 deletions viewport/framerate.h
Original file line number Diff line number Diff line change
@@ -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 <chrono>

#include <vector>

namespace vox {

class Framerate {

public:
using Clock = std::chrono::steady_clock;
using Timepoint = Clock::time_point;

private:
std::vector<double> _durations;
std::vector<size_t> _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

0 comments on commit 6df15b0

Please sign in to comment.