author | nemo |
Sat, 30 Jul 2011 17:23:47 -0400 | |
changeset 5482 | c047e70c53f4 |
parent 5113 | d92b58d100ae |
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 |
||
22 |
SpritePosition::SpritePosition(QWidget * parent, int sh) |
|
23 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
24 |
wParent = parent; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
25 |
iSpriteHeight = sh; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
26 |
reset(); |
2012 | 27 |
} |
28 |
||
29 |
SpritePosition::~SpritePosition() |
|
30 |
{ |
|
31 |
} |
|
32 |
||
33 |
void SpritePosition::move() |
|
34 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
35 |
fX += fXMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
36 |
fY += fYMov; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
37 |
iAngle += 4; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
38 |
if (iAngle >= 360) iAngle = 0; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
39 |
if (fY > wParent->height()) reset(); |
2012 | 40 |
} |
41 |
||
42 |
void SpritePosition::reset() |
|
43 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
44 |
fY = -1 * iSpriteHeight; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
45 |
fX = (qrand() % ((int)(wParent->width() * 1.5))) - wParent->width()/2; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
46 |
fYMov = ((qrand() % 400)+300) / 100.0f; |
5113
d92b58d100ae
background stars won't go all in the same direction anymore
Jonathan@Jonathan-PC.fritz.box
parents:
4976
diff
changeset
|
47 |
fXMov = fYMov * 0.2f+((qrand()%100)/100.0f * 0.6f); //so between 0.2 and 0.6, or 0.5 +/- 0.3 |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
48 |
iAngle = qrand() % 360; |
2012 | 49 |
} |
50 |
||
51 |
QPoint SpritePosition::pos() |
|
52 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
53 |
return QPoint((int)fX,(int)fY); |
2012 | 54 |
} |
55 |
||
56 |
int SpritePosition::getAngle() |
|
57 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
58 |
return iAngle; |
2012 | 59 |
} |
60 |
||
61 |
void SpritePosition::init() |
|
62 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
63 |
fY = qrand() % (wParent->height() + 1); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
64 |
fX = qrand() % (wParent->width() + 1); |
2012 | 65 |
} |
66 |
||
67 |
BGWidget::BGWidget(QWidget * parent) : QWidget(parent) |
|
68 |
{ |
|
2071
0faa147c47df
Still trying to improve perf of stars - this seems to help a little bit
nemo
parents:
2069
diff
changeset
|
69 |
setAttribute(Qt::WA_NoSystemBackground, true); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
70 |
sprite.load(":/res/Star.png"); |
2377 | 71 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
72 |
setAutoFillBackground(false); |
2012 | 73 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
74 |
for (int i = 0; i < SPRITE_MAX; i++) spritePositions[i] = new SpritePosition(this, sprite.height()); |
2377 | 75 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
76 |
for (int i = 0; i < 360; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
77 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
78 |
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
|
79 |
rotatedSprites[i]->fill(0); |
2012 | 80 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
81 |
QPoint translate(sprite.width()/2, sprite.height()/2); |
2012 | 82 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
83 |
QPainter p; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
84 |
p.begin(rotatedSprites[i]); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
85 |
// p.setRenderHint(QPainter::Antialiasing); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
86 |
p.setRenderHint(QPainter::SmoothPixmapTransform); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
87 |
p.translate(translate.x(), translate.y()); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
88 |
p.rotate(i); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
89 |
p.translate(-1*translate.x(), -1*translate.y()); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
90 |
p.drawImage(0, 0, sprite); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
91 |
} |
2012 | 92 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
93 |
timerAnimation = new QTimer(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
94 |
connect(timerAnimation, SIGNAL(timeout()), this, SLOT(animate())); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
95 |
timerAnimation->setInterval(ANIMATION_INTERVAL); |
2012 | 96 |
} |
97 |
||
98 |
BGWidget::~BGWidget() |
|
99 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
100 |
for (int i = 0; i < SPRITE_MAX; i++) delete spritePositions[i]; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
101 |
for (int i = 0; i < 360; i++) delete rotatedSprites[i]; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
102 |
delete timerAnimation; |
2012 | 103 |
} |
104 |
||
105 |
void BGWidget::paintEvent(QPaintEvent *event) |
|
106 |
{ |
|
4560
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
2948
diff
changeset
|
107 |
Q_UNUSED(event); |
5d6c7f88db73
- Some work on drawMap widget and scene to allow undo, clear, save and load operations
unc0rr
parents:
2948
diff
changeset
|
108 |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
109 |
QPainter p; |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
110 |
p.begin(this); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
111 |
//p.setRenderHint(QPainter::Antialiasing); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
112 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
113 |
{ |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
114 |
QPoint point = spritePositions[i]->pos(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
115 |
p.drawImage(point.x(), point.y(), *rotatedSprites[spritePositions[i]->getAngle()]); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
116 |
} |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
117 |
p.end(); |
2012 | 118 |
} |
119 |
||
120 |
void BGWidget::animate() |
|
121 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
122 |
for (int i = 0; i < SPRITE_MAX; i++) |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
123 |
{ |
2069
f7fad4ad4455
update only the stars - cuts CPU useage in half on my machine
nemo
parents:
2012
diff
changeset
|
124 |
// bottom edge of star *seems* clipped, but in fact, if I switch to just plain old repaint()/update() it is still clipped - artifact of transform? As for 5, is arbitrary number. 4 was noticeably clipping, 5 seemed same as update() - I assume extra room is due to rotation and value really should be calculated proportional to width/height |
f7fad4ad4455
update only the stars - cuts CPU useage in half on my machine
nemo
parents:
2012
diff
changeset
|
125 |
update(spritePositions[i]->pos().x(),spritePositions[i]->pos().y(), sprite.width()+5, sprite.height()+5); |
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
126 |
spritePositions[i]->move(); |
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
127 |
} |
2012 | 128 |
} |
129 |
||
130 |
void BGWidget::startAnimation() |
|
131 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
132 |
timerAnimation->start(); |
2012 | 133 |
} |
134 |
||
135 |
void BGWidget::stopAnimation() |
|
136 |
{ |
|
2948
3f21a9dc93d0
Replace tabs with spaces using 'expand -t 4' command
unc0rr
parents:
2377
diff
changeset
|
137 |
timerAnimation->stop(); |
2012 | 138 |
} |
139 |
||
140 |
void BGWidget::init() |
|
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++) spritePositions[i]->init(); |
2012 | 143 |
} |