aboutsummaryrefslogtreecommitdiffstats
path: root/src/declarative/particles/qsgparticlesystem_p.h
blob: abb7f52c31affa6cec2353face234e62c08c83e7 (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the Declarative module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#ifndef PARTICLESYSTEM_H
#define PARTICLESYSTEM_H

#include <QSGItem>
#include <QElapsedTimer>
#include <QVector>
#include <QHash>
#include <QPointer>
#include <QSignalMapper>
#include <QtDeclarative/private/qsgsprite_p.h>
#include <QAbstractAnimation>
#include <QtDeclarative/qdeclarative.h>
#include <private/qv8engine_p.h> //For QDeclarativeV8Handle

QT_BEGIN_HEADER

QT_BEGIN_NAMESPACE

QT_MODULE(Declarative)

class QSGParticleSystem;
class QSGParticleAffector;
class QSGParticleEmitter;
class QSGParticlePainter;
class QSGParticleData;
class QSGParticleSystemAnimation;
class QSGSpriteEngine;
class QSGSprite;
class QSGV8ParticleData;

struct QSGParticleDataHeapNode{
    int time;//in ms
    QSet<QSGParticleData*> data;//Set ptrs instead?
};

class QSGParticleDataHeap {
    //Idea is to do a binary heap, but which also stores a set of int,Node* so that if the int already exists, you can
    //add it to the data* list. Pops return the whole list at once.
public:
    QSGParticleDataHeap();
    void insert(QSGParticleData* data);

    int top();

    QSet<QSGParticleData*> pop();

    void clear();

    bool contains(QSGParticleData*);//O(n), for debugging purposes only
private:
    void grow();
    void swap(int, int);
    void bubbleUp(int);
    void bubbleDown(int);
    int m_size;
    int m_end;
    QSGParticleDataHeapNode m_tmp;
    QVector<QSGParticleDataHeapNode> m_data;
    QHash<int,int> m_lookups;
};

class QSGParticleGroupData{
public:
    QSGParticleGroupData(int id, QSGParticleSystem* sys);
    ~QSGParticleGroupData();

    int size();
    QString name();

    void setSize(int newSize);

    int index;
    QSet<QSGParticlePainter*> painters;

    //TODO: Refactor particle data list out into a separate class
    QVector<QSGParticleData*> data;
    QSGParticleDataHeap dataHeap;
    QSet<int> reusableIndexes;

    void initList();
    void kill(QSGParticleData* d);

    //After calling this, initialize, then call prepareRecycler(d)
    QSGParticleData* newDatum(bool respectsLimits);

    //TODO: Find and clean up those that don't get added to the recycler (currently they get lost)
    void prepareRecycler(QSGParticleData* d);

private:
    int m_size;
    QSGParticleSystem* m_system;
};

struct Color4ub {
    uchar r;
    uchar g;
    uchar b;
    uchar a;
};

class QSGParticleData{
public:
    //TODO: QObject like memory management (without the cost, just attached to system)
    QSGParticleData(QSGParticleSystem* sys);

    //Convenience functions for working backwards, because parameters are from the start of particle life
    //If setting multiple parameters at once, doing the conversion yourself will be faster.

    //sets the x accleration without affecting the instantaneous x velocity or position
    void setInstantaneousAX(qreal ax);
    //sets the x velocity without affecting the instantaneous x postion
    void setInstantaneousVX(qreal vx);
    //sets the instantaneous x postion
    void setInstantaneousX(qreal x);
    //sets the y accleration without affecting the instantaneous y velocity or position
    void setInstantaneousAY(qreal ay);
    //sets the y velocity without affecting the instantaneous y postion
    void setInstantaneousVY(qreal vy);
    //sets the instantaneous Y postion
    void setInstantaneousY(qreal y);

    //TODO: Slight caching?
    qreal curX() const;
    qreal curVX() const;
    qreal curAX() const { return ax; }
    qreal curY() const;
    qreal curVY() const;
    qreal curAY() const { return ay; }

    int group;
    QSGParticleEmitter* e;//### Needed?
    QSGParticleSystem* system;
    int index;
    int systemIndex;

    //General Position Stuff
    float x;
    float y;
    float t;
    float lifeSpan;
    float size;
    float endSize;
    float vx;
    float vy;
    float ax;
    float ay;

    //Other stuff, now universally shared
    Color4ub color;
    float xx;
    float xy;
    float yx;
    float yy;
    float rotation;
    float rotationSpeed;
    float autoRotate;//Assume that GPUs prefer floats to bools
    float animIdx;
    float frameDuration;
    float frameCount;
    float animT;
    float r;
    QSGItem* delegate;
    int modelIndex;
    float update;//Used by custom affectors

    void debugDump();
    bool stillAlive();
    float lifeLeft();
    float curSize();
    void clone(const QSGParticleData& other);//Not =, leaves meta-data like index
    QDeclarativeV8Handle v8Value();
private:
    QSGV8ParticleData* v8Datum;
};

class QSGParticleSystem : public QSGItem
{
    Q_OBJECT
    Q_PROPERTY(bool running READ isRunning WRITE setRunning NOTIFY runningChanged)
    Q_PROPERTY(int startTime READ startTime WRITE setStartTime NOTIFY startTimeChanged)
    Q_PROPERTY(QDeclarativeListProperty<QSGSprite> particleStates READ particleStates)

public:
    explicit QSGParticleSystem(QSGItem *parent = 0);
    ~QSGParticleSystem();
    QDeclarativeListProperty<QSGSprite> particleStates();

    //TODO: Hook up running and temporal manipulators to the animation
    bool isRunning() const
    {
        return m_running;
    }

    int startTime() const
    {
        return m_startTime;
    }

    int count(){ return m_particle_count; }

signals:

    void systemInitialized();
    void runningChanged(bool arg);

    void startTimeChanged(int arg);


public slots:
    void reset();
    void setRunning(bool arg);


    void setStartTime(int arg)
    {
        m_startTime = arg;
    }

    void fastForward(int ms)
    {
        m_startTime += ms;
    }

    virtual int duration() const { return -1; }

protected:
    //This one only once per frame (effectively)
    void componentComplete();

private slots:
    void emittersChanged();
    void loadPainter(QObject* p);
    void createEngine(); //### method invoked by sprite list changing (in engine.h) - pretty nasty
    void particleStateChange(int idx);

public://###but only really for related class usage. Perhaps we should all be friends?
    //These can be called multiple times per frame, performance critical
    void emitParticle(QSGParticleData* p);
    QSGParticleData* newDatum(int groupId, bool respectLimits = true, int sysIdx = -1);//TODO: implement respectLimits in emitters (which means interacting with maxCount?)
    void finishNewDatum(QSGParticleData*);
    void moveGroups(QSGParticleData *d, int newGIdx);
    int nextSystemIndex();

    //This one only once per painter per frame
    int systemSync(QSGParticlePainter* p);

    QSet<QSGParticleData*> m_needsReset;
    QVector<QSGParticleData*> m_bySysIdx; //Another reference to the data (data owned by group), but by sysIdx
    QHash<QString, int> m_groupIds;
    QHash<int, QSGParticleGroupData*> m_groupData;
    QSGSpriteEngine* m_spriteEngine;

    int m_timeInt;
    bool m_initialized;

    void registerParticlePainter(QSGParticlePainter* p);
    void registerParticleEmitter(QSGParticleEmitter* e);
    void registerParticleAffector(QSGParticleAffector* a);

    int m_particle_count;
    static void stateRedirect(QDeclarativeListProperty<QObject> *prop, QObject *value);//From QSGSprite
private:
    void initializeSystem();
    bool m_running;
    QList<QPointer<QSGParticleEmitter> > m_emitters;
    QList<QPointer<QSGParticleAffector> > m_affectors;
    QList<QPointer<QSGParticlePainter> > m_particlePainters;
    QList<QPointer<QSGParticlePainter> > m_syncList;
    qint64 m_startTime;
    int m_nextGroupId;
    int m_nextIndex;
    QSet<int> m_reusableIndexes;
    bool m_componentComplete;
    QList<QSGSprite*> m_states;

    QSignalMapper m_painterMapper;
    QSignalMapper m_emitterMapper;
    friend class QSGParticleSystemAnimation;
    void updateCurrentTime( int currentTime );
    QSGParticleSystemAnimation* m_animation;
};

// Internally, this animation drives all the timing. Painters sync up in their updatePaintNode
class QSGParticleSystemAnimation : public QAbstractAnimation
{
    Q_OBJECT
public:
    QSGParticleSystemAnimation(QSGParticleSystem* system)
        : QAbstractAnimation(static_cast<QObject*>(system)), m_system(system)
    { }
protected:
    virtual void updateCurrentTime( int t )
    {
        m_system->updateCurrentTime(t);
    }

    virtual int duration() const
    {
        return -1;
    }

private:
    QSGParticleSystem* m_system;
};


QT_END_NAMESPACE

QT_END_HEADER

#endif // PARTICLESYSTEM_H