author | unc0rr |
Thu, 10 Jan 2019 21:24:32 +0100 | |
changeset 14547 | 90893b397933 |
parent 11046 | 47a8c19ecb60 |
permissions | -rw-r--r-- |
2012 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2009 Kristian Lehmann <email@thexception.net> |
|
11046 | 4 |
* Copyright (c) 2004-2015 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
9998
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
2012 | 18 |
*/ |
19 |
||
20 |
#include "bgwidget.h" |
|
6579 | 21 |
#include "hwconsts.h" |
2012 | 22 |
|
6242 | 23 |
SpritePosition::SpritePosition(QWidget * parent, int sw, int sh) |
2012 | 24 |
{ |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
25 |
wParent = parent; |
6242 | 26 |
iSpriteWidth = sw; |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
27 |
iSpriteHeight = sh; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
28 |
reset(); |
2012 | 29 |
} |
30 |
||
31 |
SpritePosition::~SpritePosition() |
|
32 |
{ |
|
33 |
} |
|
34 |
||
35 |
void SpritePosition::move() |
|
36 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
37 |
fX += fXMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
38 |
fY += fYMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
39 |
iAngle += 4; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
40 |
if (iAngle >= 360) iAngle = 0; |
6241 | 41 |
if ((fX - fXMov) > wParent->width()) reset(); |
42 |
else if ((fY - fYMov) > wParent->height()) reset(); |
|
2012 | 43 |
} |
44 |
||
45 |
void SpritePosition::reset() |
|
46 |
{ |
|
6242 | 47 |
// random movement values |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
48 |
fYMov = ((qrand() % 400)+300) / 100.0f; |
6242 | 49 |
fXMov = fYMov * 0.2f+((qrand()%100)/100.0f * 0.6f); //so between 0.2 and 0.8, or 0.5 +/- 0.3 |
50 |
||
51 |
// random respawn locations |
|
52 |
int tmp = fXMov * (wParent->height() / fYMov); |
|
53 |
fX = (qrand() % (wParent->width() + tmp)) - tmp; |
|
54 |
||
55 |
// adjust respawn location to be next to (but outside) the parent's limits |
|
56 |
if (fX > -iSpriteWidth) |
|
57 |
{ |
|
58 |
fY = -1 * iSpriteHeight; |
|
59 |
} |
|
60 |
else |
|
61 |
{ |
|
6243 | 62 |
fY = qrand() % wParent->height(); |
6242 | 63 |
fX = -iSpriteWidth; |
64 |
} |
|
65 |
||
66 |
// random initial angle |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
67 |
iAngle = qrand() % 360; |
2012 | 68 |
} |
69 |
||
70 |
QPoint SpritePosition::pos() |
|
71 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
72 |
return QPoint((int)fX,(int)fY); |
2012 | 73 |
} |
74 |
||
75 |
int SpritePosition::getAngle() |
|
76 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
77 |
return iAngle; |
2012 | 78 |
} |
79 |
||
80 |
void SpritePosition::init() |
|
81 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
82 |
fY = qrand() % (wParent->height() + 1); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
83 |
fX = qrand() % (wParent->width() + 1); |
2012 | 84 |
} |
85 |
||
6574 | 86 |
BGWidget::BGWidget(QWidget * parent) : QWidget(parent), enabled(false) |
2012 | 87 |
{ |
2071
0faa147c47df
Still trying to improve perf of stars - this seems to help a little bit
nemo
parents:
2069
diff
changeset
|
88 |
setAttribute(Qt::WA_NoSystemBackground, true); |
6579 | 89 |
|
90 |
QString fname; |
|
91 |
||
92 |
//For each season, there is a replacement for the star (Star.png) |
|
93 |
//Todo: change element for easter and birthday |
|
94 |
//Simply replace Confetti.png and Egg.png with an appropriate graphic) |
|
95 |
switch (season) |
|
96 |
{ |
|
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
97 |
case SEASON_CHRISTMAS : |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
98 |
fname = "Flake.png"; |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
99 |
break; |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
100 |
case SEASON_EASTER : |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
101 |
fname = "Egg.png"; |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
102 |
break; |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
103 |
case SEASON_HWBDAY : |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
104 |
fname = "Confetti.png"; |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
105 |
break; |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
106 |
default : |
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
107 |
fname = "Star.png"; |
6579 | 108 |
} |
109 |
||
110 |
sprite.load(":/res/" + fname); |
|
2377 | 111 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
112 |
setAutoFillBackground(false); |
2012 | 113 |
|
6242 | 114 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.width(), sprite.height()); |
2377 | 115 |
|
6241 | 116 |
for (int i = 0; i < 90; i++) |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
117 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
118 |
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
|
119 |
rotatedSprites[i]->fill(0); |
2012 | 120 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
121 |
QPoint translate(sprite.width()/2, sprite.height()/2); |
2012 | 122 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
123 |
QPainter p; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
124 |
p.begin(rotatedSprites[i]); |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6583
diff
changeset
|
125 |
// p.setRenderHint(QPainter::Antialiasing); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
126 |
p.setRenderHint(QPainter::SmoothPixmapTransform); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
127 |
p.translate(translate.x(), translate.y()); |
6241 | 128 |
p.rotate(4 * i); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
129 |
p.translate(-1*translate.x(), -1*translate.y()); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
130 |
p.drawImage(0, 0, sprite); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
131 |
} |
2012 | 132 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
133 |
timerAnimation = new QTimer(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
134 |
connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate())); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
135 |
timerAnimation->setInterval(ANIMATION_INTERVAL); |
2012 | 136 |
} |
137 |
||
138 |
BGWidget::~BGWidget() |
|
139 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
140 |
for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i]; |
6241 | 141 |
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
|
142 |
delete timerAnimation; |
2012 | 143 |
} |
144 |
||
145 |
void BGWidget::paintEvent(QPaintEvent *event) |
|
146 |
{ |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
2948
diff
changeset
|
147 |
Q_UNUSED(event); |
6574 | 148 |
if (!enabled) |
149 |
return; |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
2948
diff
changeset
|
150 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
151 |
QPainter p; |
6241 | 152 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
153 |
p.begin(this); |
6241 | 154 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
155 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
156 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
157 |
QPoint point = spritePositions[i]->pos(); |
6241 | 158 |
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
|
159 |
} |
6241 | 160 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
161 |
p.end(); |
2012 | 162 |
} |
163 |
||
164 |
void BGWidget::animate() |
|
165 |
{ |
|
6574 | 166 |
if (!enabled) |
167 |
return; |
|
168 |
||
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
169 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
170 |
{ |
6241 | 171 |
QPoint oldPos = spritePositions[i]->pos(); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
172 |
spritePositions[i]->move(); |
6241 | 173 |
QPoint newPos = spritePositions[i]->pos(); |
174 |
||
175 |
int xdiff = newPos.x() - oldPos.x(); |
|
176 |
int ydiff = newPos.y() - oldPos.y(); |
|
177 |
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
|
178 |
} |
8393
85bd6c7b2641
Can now change theme for static and mission maps.
dag10 <gottlieb.drew@gmail.com>
parents:
6952
diff
changeset
|
179 |
|
8420
98e3cc0418f9
Removed constant repainting of bgwidget on every frame.
dag10
parents:
8393
diff
changeset
|
180 |
//repaint(); // Repaint every frame. Prevents ghosting of widgets if widgets resize in runtime. |
2012 | 181 |
} |
182 |
||
183 |
void BGWidget::startAnimation() |
|
184 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
185 |
timerAnimation->start(); |
2012 | 186 |
} |
187 |
||
188 |
void BGWidget::stopAnimation() |
|
189 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
190 |
timerAnimation->stop(); |
6583 | 191 |
repaint(); |
2012 | 192 |
} |
193 |
||
194 |
void BGWidget::init() |
|
195 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
196 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i]->init(); |
2012 | 197 |
} |