forked from samdavydov/propertymap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.cpp
136 lines (104 loc) · 3.32 KB
/
Player.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
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
#include "Player.h"
#include <QElapsedTimer>
#include <QTimer>
#include <QtMath>
#include <QQmlContext>
#include <QDebug>
#include "QmlPropertyMap/QmlPropertyMap.h"
#include "QuickPropertyMap/QuickPropertyMap.h"
#include "StaticPropertyMap/StaticPropertyMap.h"
#define UNUSED_FUNCTION(x) void (*__##x)(x&, const PairData&) = &init; (void)__##x;
namespace
{
const int COUNT = 1000;
const double R = 6;
double posX(int i, double angle) { return (i / 3) * R * cos(angle * M_PI / 180) + 10; }
double posY(int i, double angle) { return (i / 3) * R * sin(angle * M_PI / 180) + 10; }
using PairData = QVector<QPair<QByteArray, QVariant>>;
void init(StaticPropertyMap& pm, const PairData& data)
{
for (const auto& i : data)
pm.insert(i.first, i.second);
}
void init(QmlPropertyMap& pm, const PairData& data)
{
for (const auto& i : data)
pm.insert(i.first, i.second);
}
void init(QuickPropertyMap& pm, const PairData& data)
{
for (const auto& i : data)
pm.addProperty(i.first, i.second);
pm.build();
}
}
Player::Player(QWindow* parent)
: QQuickView(parent)
, m_propertyMap(new PropertyMap(this))
, m_timer(new QTimer(this))
, m_step(0)
{
UNUSED_FUNCTION(StaticPropertyMap)
UNUSED_FUNCTION(QmlPropertyMap)
UNUSED_FUNCTION(QuickPropertyMap)
setResizeMode(QQuickView::SizeRootObjectToView);
m_data.resize(3 * COUNT);
m_speed.resize(COUNT);
for (int i = 0; i != COUNT; ++i)
{
m_data[3 * i + 0] = {QString("x_%1").arg(i).toLatin1(), 0.0};
m_data[3 * i + 1] = {QString("y_%1").arg(i).toLatin1(), 0.0};
m_data[3 * i + 2] = {QString("r_%1").arg(i).toLatin1(), 0.0};
m_speed[i] = 1.0 * qrand() / RAND_MAX + 0.1;
}
m_data.append({QByteArray("count"), COUNT});
m_data.append({QByteArray("fps") , 0 });
m_data.append({QByteArray("title"), m_propertyMap->metaObject()->className()});
init(*m_propertyMap, m_data);
rootContext()->setContextProperty("ngi", m_propertyMap);
test1();
test2();
test3();
}
void Player::test1()
{
connect(m_timer, &QTimer::timeout, this, &Player::advance);
m_timer->start(16);
}
void Player::test2()
{
connect(m_propertyMap, &PropertyMap::valueChanged, [](const QString& name, const QVariant& value)
{
qDebug() << name << value;
});
}
void Player::test3()
{
qInfo("feeding %s:", m_propertyMap->metaObject()->className());
for (int size : QVector<int>{1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000})
{
PairData data(size);
for (int i = 0; i != size; ++i)
data[i] = {QStringLiteral("x_%1").arg(i).toLatin1(), 0.0};
PropertyMap p;
QElapsedTimer t;
t.start();
init(p, data);
qInfo("%d %lld", size, t.elapsed());
}
}
void Player::advance()
{
QElapsedTimer t;
t.start();
++m_step;
for (int i = 0; i != COUNT; ++i)
{
double angle = m_step * m_speed[i];
m_propertyMap->insert(m_data[3 * i + 0].first, posX(i, angle));
m_propertyMap->insert(m_data[3 * i + 1].first, posY(i, angle));
m_propertyMap->insert(m_data[3 * i + 2].first, angle + 90);
}
if (m_step % 10 == 0)
m_propertyMap->insert("fps", int(1000.0 / (t.nsecsElapsed() / 1000000.0)));
}