-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
194 lines (135 loc) · 3.95 KB
/
Copy pathmain.cpp
File metadata and controls
194 lines (135 loc) · 3.95 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "mainwindow.h"
#include <QApplication>
#include <QDir>
#include <QThread>
#include <QElapsedTimer>
#include <fenv.h>
#if 0
#include <QDir>
#include "path.h"
#include <QFileSystemWatcher>
#include <QProcess>
class FileShader
{
QString filepath_source;
QString filepath_spv;
public:
FileShader(const QString &filepath_source, const QString &filepath_spv)
{
this->filepath_source = filepath_source;
this->filepath_spv = filepath_spv;
}
QString source()
{
return filepath_source;
}
QString spv()
{
return filepath_spv;
}
};
class DynamicChangeShaderCompiller : public QObject
{
QFileSystemWatcher fileWatcher;
QHash<QString, FileShader*> hash;
void fileChanged(const QString &path)
{
//qDebug() << "fileChanged" << path;
FileShader *fileShader = hash[path];
if (fileShader == nullptr)
return;
QStringList arguments;
arguments << "-V";
arguments << fileShader->source();
arguments << "-o";
arguments << fileShader->spv();
QProcess::execute("glslangValidator", arguments);
fileWatcher.addPath(path);
}
void add(const QString &source, const QString &spriv)
{
if (spriv.endsWith(".vert") || spriv.endsWith(".frag"))
{
qDebug("error DynamicChangeShaderCompiller::add");
return;
}
QDir dir(path::getShaders() + "../" + source);
QString path_source = dir.absolutePath();
hash[path_source] = new FileShader(path_source, path::getShaders() + spriv);
fileWatcher.addPath(path_source);
}
public:
DynamicChangeShaderCompiller()
{
add("PaintVertex.vert", "PaintVertex.spv");
add("AnimatedPaintFragment.frag", "AnimatedPaintFragment.spv");
QObject::connect(&fileWatcher, &QFileSystemWatcher::fileChanged, this, &DynamicChangeShaderCompiller::fileChanged);
}
};
#endif
#if 0
#include <QAbstractAnimation>
class GameLoop : public QAbstractAnimation {
public:
GameLoop(QObject *parent = nullptr) : QAbstractAnimation(parent) {
// Запускаем таймер для подсчета времени между кадрами (Delta Time)
frameTimer.start();
}
// Обязательный метод: возвращает длительность анимации.
// Возвращаем -1, чтобы цикл шел бесконечно.
int duration() const override {
return -1;
}
void setGameWidget(MainWindow *widget)
{
m_gameWidget = widget;
}
protected:
// Этот метод вызывается автоматически на каждом кадре главного цикла
void updateCurrentTime(int currentTime) override {
// 1. Считаем Delta Time (время в секундах, прошедшее с прошлого кадра)
double deltaTime = frameTimer.restart() / 1000.0;
// Предотвращаем скачки физики при сильных лагах
if (deltaTime > 0.1) deltaTime = 0.1;
m_gameWidget->onEnterFrame();
}
private:
QElapsedTimer frameTimer;
MainWindow *m_gameWidget = nullptr;
};
#endif
int main(int argc, char *argv[])
{
//feenableexcept(FE_INVALID);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
qputenv("QT_ENABLE_HIGHDPI_SCALING", "0");
#endif
QApplication a(argc, argv);
QDir::setCurrent(a.applicationDirPath());
MainWindow w;
w.show();
//DynamicChangeShaderCompiller dynamicChangeShaderCompiller;
//qint64 lastTime = QDateTime::currentMSecsSinceEpoch();
//int fps = 0;
QElapsedTimer timer;
timer.start();
while (w.isVisible())
{
timer.restart();
QCoreApplication::processEvents(QEventLoop::AllEvents);
w.onEnterFrame();
//fps++;
//qint64 current = QDateTime::currentMSecsSinceEpoch();
//if (current > lastTime + 1000)
//{
// lastTime = current;
// qDebug() << fps;
// fps = 0;
//}
qint64 elapsed = timer.elapsed();
if (elapsed < 16) {
QThread::msleep(16 - elapsed);
}
}
return 0;
}