qdeclarativesparticles.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtDeclarative module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qdeclarativesparticles_p.h"
  42. #include "qdeclarativesparticles.h"
  43. #include <qdeclarativeinfo.h>
  44. #include <private/qdeclarativepixmapcache_p.h>
  45. #include <QtCore/QAbstractAnimation>
  46. #include <QPainter>
  47. #include <QtGui/qdrawutil.h>
  48. #include <QVarLengthArray>
  49. #include <stdlib.h>
  50. #include <math.h>
  51. #ifndef M_PI
  52. #define M_PI 3.14159265358979323846
  53. #define M_PI_2 (M_PI / 2.)
  54. #endif
  55. #ifndef INT_MAX
  56. #define INT_MAX 2147483647
  57. #endif
  58. QT_BEGIN_NAMESPACE
  59. #define PI_SQR 9.8696044
  60. // parabolic approximation
  61. inline qreal fastSin(qreal theta)
  62. {
  63. const qreal b = 4 / M_PI;
  64. const qreal c = -4 / PI_SQR;
  65. qreal y = b * theta + c * theta * qAbs(theta);
  66. return y;
  67. }
  68. inline qreal fastCos(qreal theta)
  69. {
  70. theta += M_PI_2;
  71. if (theta > M_PI)
  72. theta -= 2 * M_PI;
  73. return fastSin(theta);
  74. }
  75. void QDeclarativeSParticlesPrivate::tick(int time)
  76. {
  77. Q_Q(QDeclarativeSParticles);
  78. // if (!motion)
  79. // motion = new QDeclarativeParticleMotionLinear(q);
  80. // int oldCount = particles.count();
  81. // int removed = 0;
  82. int interval = time - lastAdvTime;
  83. // for (int i = 0; i < particles.count(); ) {
  84. // QDeclarativeParticle &particle = particles[i];
  85. // int age = time - particle.birthTime;
  86. // if (age >= particle.lifeSpan) {
  87. // QDeclarativeParticle part = particles.takeAt(i);
  88. // motion->destroy(part);
  89. // ++removed;
  90. // } else {
  91. // updateOpacity(particle, age);
  92. // motion->advance(particle, interval);
  93. // ++i;
  94. // }
  95. // }
  96. // if(emissionRate == -1)//Otherwise leave emission to the emission rate
  97. // while(removed-- && ((count == -1) || particles.count() < count))
  98. // createParticle(time);
  99. // if (!addParticleTime)
  100. // addParticleTime = time;
  101. // //Possibly emit new particles
  102. // if (((count == -1) || particles.count() < count) && emissionRate
  103. // && !(count==-1 && emissionRate==-1)) {
  104. // int emissionCount = -1;
  105. // if (emissionRate != -1){
  106. // qreal variance = 1.;
  107. // if (emissionVariance > 0.){
  108. // variance += (qreal(qrand())/RAND_MAX) * emissionVariance * (qrand()%2?-1.:1.);
  109. // }
  110. // qreal emission = emissionRate * (qreal(interval)/1000.);
  111. // emission = emission * variance + emissionCarry;
  112. // double tmpDbl;
  113. // emissionCarry = modf(emission, &tmpDbl);
  114. // emissionCount = (int)tmpDbl;
  115. // emissionCount = qMax(0,emissionCount);
  116. // }
  117. // while(((count == -1) || particles.count() < count) &&
  118. // (emissionRate==-1 || emissionCount--))
  119. // createParticle(time);
  120. // }
  121. // //Deal with emissions from requested bursts
  122. // for(int i=0; i<bursts.size(); i++){
  123. // int emission = 0;
  124. // if(bursts[i].second == -1){
  125. // emission = bursts[i].first;
  126. // }else{
  127. // qreal variance = 1.;
  128. // if (emissionVariance > 0.){
  129. // variance += (qreal(qrand())/RAND_MAX) * emissionVariance * (qrand()%2?-1.:1.);
  130. // }
  131. // qreal workingEmission = bursts[i].second * (qreal(interval)/1000.);
  132. // workingEmission *= variance;
  133. // emission = (int)workingEmission;
  134. // emission = qMax(emission, 0);
  135. // }
  136. // emission = qMin(emission, bursts[i].first);
  137. // bursts[i].first -= emission;
  138. // while(emission--)
  139. // createParticle(time);
  140. // }
  141. // for(int i=bursts.size()-1; i>=0; i--)
  142. // if(bursts[i].first <= 0)
  143. // bursts.removeAt(i);
  144. lastAdvTime = time;
  145. paintItem->updateSize();
  146. paintItem->update();
  147. // if (!(oldCount || particles.count()) && (!count || !emissionRate) && bursts.isEmpty()) {
  148. lastAdvTime = 0;
  149. clock.stop();
  150. // }
  151. }
  152. QDeclarativeSParticles::QDeclarativeSParticles(QDeclarativeItem *parent)
  153. : QDeclarativeItem(*(new QDeclarativeSParticlesPrivate), parent)
  154. {
  155. Q_D(QDeclarativeSParticles);
  156. d->init();
  157. }
  158. QDeclarativeSParticles::~QDeclarativeSParticles()
  159. {
  160. }
  161. void QDeclarativeSParticles::paint(QPainter *p, const QStyleOptionGraphicsItem *, QWidget *)
  162. {
  163. Q_UNUSED(p);
  164. //painting is done by the ParticlesPainter, so it can have the right size
  165. }
  166. void QDeclarativeSParticles::componentComplete()
  167. {
  168. Q_D(QDeclarativeSParticles);
  169. QDeclarativeItem::componentComplete();
  170. if (d->count && d->emissionRate) {
  171. d->paintItem->updateSize();
  172. d->clock.start();
  173. }
  174. if (d->lifeSpanDev > d->lifeSpan)
  175. d->lifeSpanDev = d->lifeSpan;
  176. }
  177. QT_END_NAMESPACE