-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgwindow.cpp
96 lines (79 loc) · 1.86 KB
/
mgwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "mgwindow.h"
/**
* Creates the main window of the application.
*/
MgWindow::MgWindow(QWidget *parent) :
QMainWindow(parent) ,
ui(new Ui::MgWindow)
{
// Install event filter.
installEventFilter(this);
// Setup main window.
ui->setupUi(this);
// Set resolution.
resize(1600, 900);
ui->vulkanWidget->setGeometry(0, 0, width(), height());
// Initialize Vulkan.
vkcInstance = new VkcInstance(ui->vulkanWidget);
#ifdef QT_DEBUG
vkcInstance->printDevices(new QFile("devices.txt"));
#endif
// Initialize fps timer.
fpsTimer = new QTimer(this);
connect(fpsTimer, SIGNAL(timeout()), this, SLOT(showFps()));
fpsTimer->start(1000);
frameCount = 0;
// Store original title.
title = this->windowTitle();
// Include loop() in eventLoop() with lower priority().
QCoreApplication::postEvent(this, new QEvent(QEvent::User), -2);
}
/**
* Destroys the main window and its components.
*/
MgWindow::~MgWindow()
{
removeEventFilter(this);
delete fpsTimer;
delete vkcInstance;
delete ui;
}
/**
* Filters the events.
*/
bool MgWindow::eventFilter(QObject *obj, QEvent *event)
{
switch (event->type())
{
/**
* Handle other events.
*/
default:
return QObject::eventFilter(obj, event);
/**
* Handle loop reentry event.
*/
case QEvent::User:
loop();
return true;
}
}
/**
* Begin the render loop.
*/
void MgWindow::loop()
{
// Main render.
vkcInstance->render();
frameCount++;
// Reenter loop() after processing all other events.
QCoreApplication::postEvent(this, new QEvent(QEvent::User), -2);
}
/**
* Display the number of frames rendered the last second.
*/
void MgWindow::showFps()
{
this->setWindowTitle(title + QString(" (FPS:%1)").arg(frameCount));
frameCount = 0;
}