2012
|
1 |
/*
|
|
2 |
* Hedgewars, a free turn based strategy game
|
|
3 |
* Copyright (c) 2009 Kristian Lehmann <email@thexception.net>
|
|
4 |
*
|
|
5 |
* This program is free software; you can redistribute it and/or modify
|
|
6 |
* it under the terms of the GNU General Public License as published by
|
|
7 |
* the Free Software Foundation; version 2 of the License
|
|
8 |
*
|
|
9 |
* This program is distributed in the hope that it will be useful,
|
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
12 |
* GNU General Public License for more details.
|
|
13 |
*
|
|
14 |
* You should have received a copy of the GNU General Public License
|
|
15 |
* along with this program; if not, write to the Free Software
|
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
17 |
*/
|
|
18 |
|
|
19 |
#include "bgwidget.h"
|
|
20 |
|
|
21 |
SpritePosition::SpritePosition(QWidget * parent, int sh)
|
|
22 |
{
|
|
23 |
wParent = parent;
|
|
24 |
iSpriteHeight = sh;
|
|
25 |
reset();
|
|
26 |
}
|
|
27 |
|
|
28 |
SpritePosition::~SpritePosition()
|
|
29 |
{
|
|
30 |
}
|
|
31 |
|
|
32 |
void SpritePosition::move()
|
|
33 |
{
|
|
34 |
fX += fXMov;
|
|
35 |
fY += fYMov;
|
|
36 |
iAngle += 4;
|
|
37 |
if (iAngle >= 360) iAngle = 0;
|
|
38 |
if (fY > wParent->height()) reset();
|
|
39 |
}
|
|
40 |
|
|
41 |
void SpritePosition::reset()
|
|
42 |
{
|
|
43 |
fY = -1 * iSpriteHeight;
|
|
44 |
fX = (qrand() % ((int)(wParent->width() * 1.5))) - wParent->width()/2;
|
|
45 |
fYMov = ((qrand() % 400)+300) / 100.0f;
|
|
46 |
fXMov = fYMov * 0.5f;
|
|
47 |
iAngle = qrand() % 360;
|
|
48 |
}
|
|
49 |
|
|
50 |
QPoint SpritePosition::pos()
|
|
51 |
{
|
|
52 |
return QPoint((int)fX,(int)fY);
|
|
53 |
}
|
|
54 |
|
|
55 |
int SpritePosition::getAngle()
|
|
56 |
{
|
|
57 |
return iAngle;
|
|
58 |
}
|
|
59 |
|
|
60 |
void SpritePosition::init()
|
|
61 |
{
|
|
62 |
fY = qrand() % (wParent->height() + 1);
|
|
63 |
fX = qrand() % (wParent->width() + 1);
|
|
64 |
}
|
|
65 |
|
|
66 |
BGWidget::BGWidget(QWidget * parent) : QWidget(parent)
|
|
67 |
{
|
|
68 |
sprite.load(":/res/Star.png");
|
|
69 |
|
|
70 |
setAutoFillBackground(false);
|
|
71 |
|
|
72 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.height());
|
|
73 |
|
|
74 |
for (int i = 0; i < 360; i++)
|
|
75 |
{
|
|
76 |
rotatedSprites[i] = new QImage(sprite.width(), sprite.height(), QImage::Format_ARGB32);
|
|
77 |
rotatedSprites[i]->fill(0);
|
|
78 |
|
|
79 |
QPoint translate(sprite.width()/2, sprite.height()/2);
|
|
80 |
|
|
81 |
QPainter p;
|
|
82 |
p.begin(rotatedSprites[i]);
|
|
83 |
p.setRenderHint(QPainter::Antialiasing);
|
|
84 |
p.setRenderHint(QPainter::SmoothPixmapTransform);
|
|
85 |
p.translate(translate.x(), translate.y());
|
|
86 |
p.rotate(i);
|
|
87 |
p.translate(-1*translate.x(), -1*translate.y());
|
|
88 |
p.drawImage(0, 0, sprite);
|
|
89 |
|
|
90 |
}
|
|
91 |
|
|
92 |
timerAnimation = new QTimer();
|
|
93 |
connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate()));
|
|
94 |
timerAnimation->setInterval(ANIMATION_INTERVAL);
|
|
95 |
}
|
|
96 |
|
|
97 |
BGWidget::~BGWidget()
|
|
98 |
{
|
|
99 |
for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i];
|
|
100 |
for (int i = 0; i < 360; i++) delete rotatedSprites[i];
|
|
101 |
delete timerAnimation;
|
|
102 |
}
|
|
103 |
|
|
104 |
void BGWidget::paintEvent(QPaintEvent *event)
|
|
105 |
{
|
|
106 |
QPainter p;
|
|
107 |
p.begin(this);
|
|
108 |
//p.setRenderHint(QPainter::Antialiasing);
|
|
109 |
for (int i = 0; i < SPRITE_MAX; i++)
|
|
110 |
{
|
|
111 |
QPoint point = spritePositions[i]->pos();
|
|
112 |
p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()]);
|
|
113 |
}
|
|
114 |
p.end();
|
|
115 |
}
|
|
116 |
|
|
117 |
void BGWidget::animate()
|
|
118 |
{
|
|
119 |
repaint();
|
|
120 |
for (int i = 0; i < SPRITE_MAX; i++)
|
|
121 |
{
|
|
122 |
spritePositions[i]->move();
|
|
123 |
}
|
|
124 |
}
|
|
125 |
|
|
126 |
void BGWidget::startAnimation()
|
|
127 |
{
|
|
128 |
timerAnimation->start();
|
|
129 |
}
|
|
130 |
|
|
131 |
void BGWidget::stopAnimation()
|
|
132 |
{
|
|
133 |
timerAnimation->stop();
|
|
134 |
}
|
|
135 |
|
|
136 |
void BGWidget::init()
|
|
137 |
{
|
|
138 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init();
|
|
139 |
}
|