author | Xeli |
Mon, 14 Nov 2011 18:03:51 +0100 | |
branch | hedgeroid |
changeset 6346 | db58a42bb5e7 |
parent 6243 | 9777d802be1a |
child 6574 | ec059b55aa72 |
permissions | -rw-r--r-- |
2012 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2009 Kristian Lehmann <email@thexception.net> |
|
4976 | 4 |
* Copyright (c) 2009-2011 Andrey Korotaev <unC0Rr@gmail.com> |
2012 | 5 |
* |
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; version 2 of the License |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 |
*/ |
|
19 |
||
20 |
#include "bgwidget.h" |
|
21 |
||
6242 | 22 |
SpritePosition::SpritePosition(QWidget * parent, int sw, int sh) |
2012 | 23 |
{ |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
24 |
wParent = parent; |
6242 | 25 |
iSpriteWidth = sw; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
26 |
iSpriteHeight = sh; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
27 |
reset(); |
2012 | 28 |
} |
29 |
||
30 |
SpritePosition::~SpritePosition() |
|
31 |
{ |
|
32 |
} |
|
33 |
||
34 |
void SpritePosition::move() |
|
35 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
36 |
fX += fXMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
37 |
fY += fYMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
38 |
iAngle += 4; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
39 |
if (iAngle >= 360) iAngle = 0; |
6241 | 40 |
if ((fX - fXMov) > wParent->width()) reset(); |
41 |
else if ((fY - fYMov) > wParent->height()) reset(); |
|
2012 | 42 |
} |
43 |
||
44 |
void SpritePosition::reset() |
|
45 |
{ |
|
6242 | 46 |
// random movement values |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
47 |
fYMov = ((qrand() % 400)+300) / 100.0f; |
6242 | 48 |
fXMov = fYMov * 0.2f+((qrand()%100)/100.0f * 0.6f); //so between 0.2 and 0.8, or 0.5 +/- 0.3 |
49 |
||
50 |
// random respawn locations |
|
51 |
int tmp = fXMov * (wParent->height() / fYMov); |
|
52 |
fX = (qrand() % (wParent->width() + tmp)) - tmp; |
|
53 |
||
54 |
// adjust respawn location to be next to (but outside) the parent's limits |
|
55 |
if (fX > -iSpriteWidth) |
|
56 |
{ |
|
57 |
fY = -1 * iSpriteHeight; |
|
58 |
} |
|
59 |
else |
|
60 |
{ |
|
6243 | 61 |
fY = qrand() % wParent->height(); |
6242 | 62 |
fX = -iSpriteWidth; |
63 |
} |
|
64 |
||
65 |
// random initial angle |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
66 |
iAngle = qrand() % 360; |
2012 | 67 |
} |
68 |
||
69 |
QPoint SpritePosition::pos() |
|
70 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
71 |
return QPoint((int)fX,(int)fY); |
2012 | 72 |
} |
73 |
||
74 |
int SpritePosition::getAngle() |
|
75 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
76 |
return iAngle; |
2012 | 77 |
} |
78 |
||
79 |
void SpritePosition::init() |
|
80 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
81 |
fY = qrand() % (wParent->height() + 1); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
82 |
fX = qrand() % (wParent->width() + 1); |
2012 | 83 |
} |
84 |
||
85 |
BGWidget::BGWidget(QWidget * parent) : QWidget(parent) |
|
86 |
{ |
|
2071
0faa147c47df
Still trying to improve perf of stars - this seems to help a little bit
nemo
parents:
2069
diff
changeset
|
87 |
setAttribute(Qt::WA_NoSystemBackground, true); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
88 |
sprite.load(":/res/Star.png"); |
2377 | 89 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
90 |
setAutoFillBackground(false); |
2012 | 91 |
|
6242 | 92 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.width(), sprite.height()); |
2377 | 93 |
|
6241 | 94 |
for (int i = 0; i < 90; i++) |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
95 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
96 |
rotatedSprites[i] = new QImage(sprite.width(), sprite.height(), QImage::Format_ARGB32); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
97 |
rotatedSprites[i]->fill(0); |
2012 | 98 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
99 |
QPoint translate(sprite.width()/2, sprite.height()/2); |
2012 | 100 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
101 |
QPainter p; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
102 |
p.begin(rotatedSprites[i]); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
103 |
// p.setRenderHint(QPainter::Antialiasing); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
104 |
p.setRenderHint(QPainter::SmoothPixmapTransform); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
105 |
p.translate(translate.x(), translate.y()); |
6241 | 106 |
p.rotate(4 * i); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
107 |
p.translate(-1*translate.x(), -1*translate.y()); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
108 |
p.drawImage(0, 0, sprite); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
109 |
} |
2012 | 110 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
111 |
timerAnimation = new QTimer(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
112 |
connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate())); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
113 |
timerAnimation->setInterval(ANIMATION_INTERVAL); |
2012 | 114 |
} |
115 |
||
116 |
BGWidget::~BGWidget() |
|
117 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
118 |
for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i]; |
6241 | 119 |
for (int i = 0; i < 90; i++) delete rotatedSprites[i]; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
120 |
delete timerAnimation; |
2012 | 121 |
} |
122 |
||
123 |
void BGWidget::paintEvent(QPaintEvent *event) |
|
124 |
{ |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
2948
diff
changeset
|
125 |
Q_UNUSED(event); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
2948
diff
changeset
|
126 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
127 |
QPainter p; |
6241 | 128 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
129 |
p.begin(this); |
6241 | 130 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
131 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
132 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
133 |
QPoint point = spritePositions[i]->pos(); |
6241 | 134 |
p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()/4]); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
135 |
} |
6241 | 136 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
137 |
p.end(); |
2012 | 138 |
} |
139 |
||
140 |
void BGWidget::animate() |
|
141 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
142 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
143 |
{ |
6241 | 144 |
QPoint oldPos = spritePositions[i]->pos(); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
145 |
spritePositions[i]->move(); |
6241 | 146 |
QPoint newPos = spritePositions[i]->pos(); |
147 |
||
148 |
int xdiff = newPos.x() - oldPos.x(); |
|
149 |
int ydiff = newPos.y() - oldPos.y(); |
|
150 |
update(oldPos.x(), oldPos.y(), xdiff+sprite.width(), ydiff+sprite.height()); |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
151 |
} |
2012 | 152 |
} |
153 |
||
154 |
void BGWidget::startAnimation() |
|
155 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
156 |
timerAnimation->start(); |
2012 | 157 |
} |
158 |
||
159 |
void BGWidget::stopAnimation() |
|
160 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
161 |
timerAnimation->stop(); |
2012 | 162 |
} |
163 |
||
164 |
void BGWidget::init() |
|
165 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
166 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init(); |
2012 | 167 |
} |