Menu

[c54480]: / winter.cpp  Maximize  Restore  History

Download this file

230 lines (194 with data), 5.4 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
#include "winter.h"
#include "misc.h"
#include <QDebug>
#include <QGraphicsScene>
#include <QGraphicsView>
#include <QPen>
SnowFlake::SnowFlake(QGraphicsScene *scene)
{
size=qrand()%10+3;
int s4=size/4;
int s3=size/3;
int s6=size/6;
for (int a=0; a<6; a++)
{
lines.append(scene->addLine(0, -s6, 0, -size));
lines.last()->setRotation(a*60);
lines.append(scene->addLine(-s4, -size + s6, 0, -size + s3));
lines.last()->setRotation(a*60);
lines.append(scene->addLine( s4, -size + s6, 0, -size + s3));
lines.last()->setRotation(a*60);
}
QPen p(Qt::white);
foreach (QGraphicsLineItem *l, lines)
{
l->setZValue(1000);
l->setPen(p);
l->setParentItem(this);
}
dv=QPointF(qrand()%10/10.0-0.5, qrand()%10/10.0 +1);
da=qrand()%20 / 10.0 - 1;
}
SnowFlake::~SnowFlake()
{
//qDebug()<<"Destr. SnowFlake";
while(lines.isEmpty())
delete lines.takeFirst();
}
QRectF SnowFlake::boundingRect() const
{
return QRectF (-size, -size, size*2, size*2);
}
void SnowFlake::paint(QPainter*, const QStyleOptionGraphicsItem*, QWidget*)
{
}
void SnowFlake::animate()
{
moveBy(dv.x() + dblow.x(), dv.y() + dblow.y());
setRotation(rotation() + da);
dblow = dblow *0.9;
}
void SnowFlake::blow(const QPointF &v)
{
dblow=v;
}
Winter::Winter(QGraphicsView *v)
{
view=v;
updateView();
/*
test=view->scene()->addLine(
QLineF(viewRect.topLeft(), viewRect.bottomRight()),
QPen(Qt::blue) );
*/
maxFlakes=1500;
maxFalling=140;
maxUnfreeze=50;
makeSnow();
animTimer = new QTimer;
connect (animTimer, SIGNAL (timeout()), this, SLOT (animate() ));
animTimer->start(50);
snowTimer = new QTimer;
connect (snowTimer, SIGNAL (timeout()), this, SLOT (makeSnow() ));
snowTimer->start(1000);
}
Winter::~Winter()
{
delete animTimer;
delete snowTimer;
while (!fallingSnow.isEmpty())
delete fallingSnow.takeFirst();
while (!frozenSnow.isEmpty())
delete frozenSnow.takeFirst();
}
void Winter::updateView()
{
QPointF p0=view->mapToScene( QPoint(0,0));
QPointF p1=view->mapToScene( view->rect().width(), view->rect().height() );
viewRect=QRectF(p0,p1);
}
void Winter::setObstacles(QList <QRectF> obslist)
{
obstacles=obslist;
QList <SnowFlake*> unfreeze;
// Find frozen snowflakes, which are free again
QPointF p;
int i=0;
bool frozen;
while (i < frozenSnow.count())
{
p=frozenSnow.at(i)->pos();
frozen=false;
int j=0;
while (j<obstacles.count() && !frozen)
{
if (obstacles.at(j).contains(p) )
frozen=true;
j++;
}
if (!frozen)
{
unfreeze.append(frozenSnow.at(i));
frozenSnow.removeAt(i);
} else
i++;
}
// Remove some flakes, if too many
while (fallingSnow.count() + unfreeze.count() > maxFalling + maxUnfreeze)
delete unfreeze.takeFirst();
while (!unfreeze.isEmpty())
{
// Blow a bit up
unfreeze.first()->blow( QPointF(qrand()%10/10.0-0.5, qrand()%10/10.0 -5));
fallingSnow.append(unfreeze.takeFirst());
}
}
void Winter::animate()
{
updateView();
//test->setLine(QLineF(viewRect.topLeft(), viewRect.bottomRight()));
QPointF p;
int i=0;
bool cont;
while (i<fallingSnow.count())
{
p=fallingSnow.at(i)->pos();
cont=true;
int j=0;
while (j<obstacles.count() && cont)
{
if (obstacles.at(j).contains(p) && qrand()%(obstacles.count()+1) > obstacles.count()-1)
{
// Freeze snowflake on obstacle
// Probality is equale for obstacles or falling through
frozenSnow.append(fallingSnow.at(i));
fallingSnow.removeAt(i);
cont=false;
}
j++;
}
if (cont && p.y() > viewRect.y() + viewRect.height() + 20)
{
delete fallingSnow.takeAt(i);
cont=false;
}
// Let snowflake fall further
if (cont) fallingSnow.at(i)->animate();
i++;
}
}
void Winter::makeSnow()
{
//qDebug()<<"falling: "<<fallingSnow.count()<<" frozen: "<<frozenSnow.count();
if (fallingSnow.count() + frozenSnow.count() <maxFlakes)
{
if (fallingSnow.count() < maxFalling)
{
// Create more snowflakes
SnowFlake *snowflake;
for (int i=0; i<10; i++)
{
snowflake=new SnowFlake(view->scene());
snowflake->setPos( 0,0);
snowflake->setRotation(qrand()%60);
view->scene()->addItem(snowflake);
snowflake->setPos(
rand()%round_int(viewRect.width()) + viewRect.x(),
viewRect.y() -20
);
fallingSnow.append(snowflake);
}
}
} else
{
// Remove some of the existing frozen flakes
for (int i=0; i<10; i++)
{
if (frozenSnow.count()>0)
{
int j=qrand()%frozenSnow.count();
delete frozenSnow.takeAt(j);
}
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.