-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.cpp
More file actions
286 lines (229 loc) · 7.75 KB
/
main.cpp
File metadata and controls
286 lines (229 loc) · 7.75 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (c) 2012 Samuel Rødal
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <QtGui>
#include <QtQuick>
#define GETTER(type, name) \
type name() const { return m_ ## name; }
#define GETTER_INDIRECT(type, name, accessor) \
type name() const { return accessor; }
#define SETTER(name) \
if (value == m_ ## name) return; m_ ## name = value; emit name ## Changed();
class Controller : public QObject
{
Q_OBJECT
Q_PROPERTY(bool frameSkipEnabled READ frameSkipEnabled WRITE setFrameSkipEnabled NOTIFY frameSkipEnabledChanged)
Q_PROPERTY(bool motionBlurEnabled READ motionBlurEnabled WRITE setMotionBlurEnabled NOTIFY motionBlurEnabledChanged)
Q_PROPERTY(bool followMouse READ followMouse WRITE setFollowMouse NOTIFY followMouseChanged)
Q_PROPERTY(bool paused READ paused WRITE setPaused NOTIFY pausedChanged)
Q_PROPERTY(qreal velocity READ velocity WRITE setVelocity NOTIFY velocityChanged)
Q_PROPERTY(int skippedFrames READ skippedFrames NOTIFY skippedFramesChanged)
Q_PROPERTY(QPointF posA READ posA NOTIFY positionsChanged)
Q_PROPERTY(QPointF posB READ posB NOTIFY positionsChanged)
Q_PROPERTY(QPointF posC READ posC NOTIFY positionsChanged)
Q_PROPERTY(QPointF posD READ posD NOTIFY positionsChanged)
Q_PROPERTY(QPointF posE READ posE NOTIFY positionsChanged)
Q_PROPERTY(QPointF posF READ posF NOTIFY positionsChanged)
Q_PROPERTY(QRectF bounds READ bounds NOTIFY boundsChanged)
public:
Controller(QWindow *view);
GETTER(bool, frameSkipEnabled)
GETTER(bool, motionBlurEnabled)
GETTER(bool, followMouse)
GETTER(bool, paused)
GETTER(int, skippedFrames)
GETTER_INDIRECT(QPointF, posA, m_pos[0])
GETTER_INDIRECT(QPointF, posB, m_pos[1])
GETTER_INDIRECT(QPointF, posC, m_pos[2])
GETTER_INDIRECT(QPointF, posD, m_pos[3])
GETTER_INDIRECT(QPointF, posE, m_pos[4])
GETTER_INDIRECT(QPointF, posF, m_pos[5])
GETTER(QRectF, bounds)
GETTER(qreal, velocity)
public slots:
void step();
void update();
void mouseMoved(const QPoint &pos);
void setFrameSkipEnabled(bool value)
{
SETTER(frameSkipEnabled)
}
void setMotionBlurEnabled(bool value)
{
SETTER(motionBlurEnabled)
}
void setVelocity(qreal value)
{
SETTER(velocity)
}
void setPaused(bool value)
{
SETTER(paused)
}
void setFollowMouse(bool value)
{
SETTER(followMouse)
}
signals:
void frameSkipEnabledChanged();
void motionBlurEnabledChanged();
void velocityChanged();
void positionsChanged();
void boundsChanged();
void followMouseChanged();
void pausedChanged();
void skippedFramesChanged();
private:
void initialize();
QWindow *m_view;
bool m_frameSkipEnabled;
bool m_motionBlurEnabled;
bool m_followMouse;
bool m_paused;
qreal m_velocity;
int m_skippedFrames;
QPointF m_pos[6];
QList<QPointF> m_positions;
QRectF m_bounds;
int m_frame;
qreal m_t;
bool m_hologram;
bool m_wobble;
bool m_shadow;
QPainterPath m_mouseTrail;
QPointF m_mousePos;
QTime m_time;
};
Controller::Controller(QWindow *view)
: m_view(view)
, m_frameSkipEnabled(false)
, m_motionBlurEnabled(true)
, m_followMouse(false)
, m_paused(false)
, m_velocity(0.02)
, m_skippedFrames(0)
, m_frame(0)
, m_t(0)
, m_hologram(false)
, m_wobble(false)
{
}
void Controller::mouseMoved(const QPoint &pos)
{
if (m_mouseTrail.elementCount() == 0)
m_mouseTrail.moveTo(pos);
else
m_mouseTrail.lineTo(pos);
}
const int tw = 256;
const int th = 256;
void Controller::step()
{
int width = m_view->width();
int height = m_view->height();
if (m_positions.size() > 5)
m_positions = m_positions.mid(5);
else
m_positions.clear();
int steps = m_motionBlurEnabled ? 5 : 1;
qreal timescale = (m_frameSkipEnabled ? 2 * m_velocity : m_velocity) * 120. / (steps * m_view->screen()->refreshRate());
for (int i = 0; i < steps; ++i) {
qreal x, y;
if (m_followMouse) {
qreal t = i * (1 / qreal(6));
if (!m_mouseTrail.isEmpty())
m_mousePos = m_mouseTrail.pointAtPercent(t);
x = m_mousePos.x();
y = m_mousePos.y();
} else {
m_t += timescale;
x = 0.5 * (tw + (width - tw) * (1 + qSin(m_t)));
y = 0.5 * (th + (height - th) * (1 + qSin(0.47 * m_t)));
}
m_positions << QPointF(x, y);
}
if (m_motionBlurEnabled) {
if (m_positions.size() < 6)
m_positions.prepend(m_positions.first());
m_bounds = QRectF(m_positions.at(0), m_positions.at(1));
for (int i = 2; i < m_positions.size(); i += 2)
m_bounds = m_bounds.united(QRectF(m_positions.at(i), m_positions.at(i+1)));
m_bounds = m_bounds.normalized();
for (int i = 0; i < m_positions.size(); ++i)
m_pos[i] = (m_positions.at(i) - m_bounds.center()) * (1 / 256.);
emit positionsChanged();
} else {
m_bounds = QRectF(m_positions.at(0), QSizeF());
}
emit boundsChanged();
m_mousePos = m_mouseTrail.currentPosition();
m_mouseTrail = QPainterPath();
m_mouseTrail.moveTo(m_mousePos);
}
void Controller::update()
{
if (!m_paused && (!m_frameSkipEnabled || (m_frame & 1)))
step();
if (m_time.isNull()) {
m_time.start();
} else {
qreal elapsed = m_time.elapsed();
qreal refresh = 1000. / m_view->screen()->refreshRate();
qreal hi = 1.35 * refresh;
if (elapsed > hi) {
++m_skippedFrames;
emit skippedFramesChanged();
}
m_time.restart();
}
m_frame++;
}
class View : public QQuickView
{
Q_OBJECT
protected:
virtual void mouseMoveEvent(QMouseEvent *event)
{
emit mouseMoved(event->pos());
QQuickView::mouseMoveEvent(event);
}
signals:
void mouseMoved(const QPoint &pos);
};
int main(int argc, char **argv)
{
QGuiApplication app(argc, argv);
View view;
QSurfaceFormat format = view.requestedFormat();
format.setSwapBehavior(QSurfaceFormat::TripleBuffer);
view.setFormat(format);
Controller controller(&view);
view.rootContext()->setContextProperty("controller", &controller);
view.rootContext()->setContextProperty("screen", view.screen());
view.setSource(QUrl("main.qml"));
view.setResizeMode(QQuickView::SizeRootObjectToView);
view.setGeometry(0, 0, 800, 600);
view.showFullScreen();
QObject::connect(&view, SIGNAL(afterRendering()), &controller, SLOT(update()));
QObject::connect(&view, SIGNAL(mouseMoved(const QPoint &)), &controller, SLOT(mouseMoved(const QPoint &)));
return app.exec();
}
#include "main.moc"